Adding proxy support to the core
[quassel.git] / src / common / network.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include "network.h"
21
22 #include <QDebug>
23 #include <QTextCodec>
24
25 QTextCodec *Network::_defaultCodecForServer = 0;
26 QTextCodec *Network::_defaultCodecForEncoding = 0;
27 QTextCodec *Network::_defaultCodecForDecoding = 0;
28
29 // ====================
30 //  Public:
31 // ====================
32 Network::Network(const NetworkId &networkid, QObject *parent)
33   : SyncableObject(parent),
34     _proxy(0),
35     _networkId(networkid),
36     _identity(0),
37     _myNick(QString()),
38     _latency(0),
39     _networkName(QString("<not initialized>")),
40     _currentServer(QString()),
41     _connected(false),
42     _connectionState(Disconnected),
43     _prefixes(QString()),
44     _prefixModes(QString()),
45     _useRandomServer(false),
46     _useAutoIdentify(false),
47     _useAutoReconnect(false),
48     _autoReconnectInterval(60),
49     _autoReconnectRetries(10),
50     _unlimitedReconnectRetries(false),
51     _codecForServer(0),
52     _codecForEncoding(0),
53     _codecForDecoding(0),
54     _autoAwayActive(false)
55 {
56   setObjectName(QString::number(networkid.toInt()));
57 }
58
59 Network::~Network() {
60   emit aboutToBeDestroyed();
61 }
62
63 bool Network::isChannelName(const QString &channelname) const {
64   if(channelname.isEmpty())
65     return false;
66
67   if(supports("CHANTYPES"))
68     return support("CHANTYPES").contains(channelname[0]);
69   else
70     return QString("#&!+").contains(channelname[0]);
71 }
72
73 NetworkInfo Network::networkInfo() const {
74   NetworkInfo info;
75   info.networkName = networkName();
76   info.networkId = networkId();
77   info.identity = identity();
78   info.codecForServer = codecForServer();
79   info.codecForEncoding = codecForEncoding();
80   info.codecForDecoding = codecForDecoding();
81   info.serverList = serverList();
82   info.useRandomServer = useRandomServer();
83   info.perform = perform();
84   info.useAutoIdentify = useAutoIdentify();
85   info.autoIdentifyService = autoIdentifyService();
86   info.autoIdentifyPassword = autoIdentifyPassword();
87   info.useAutoReconnect = useAutoReconnect();
88   info.autoReconnectInterval = autoReconnectInterval();
89   info.autoReconnectRetries = autoReconnectRetries();
90   info.unlimitedReconnectRetries = unlimitedReconnectRetries();
91   info.rejoinChannels = rejoinChannels();
92   return info;
93 }
94
95 void Network::setNetworkInfo(const NetworkInfo &info) {
96   // we don't set our ID!
97   if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
98   if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
99   if(info.codecForServer != codecForServer()) setCodecForServer(QTextCodec::codecForName(info.codecForServer));
100   if(info.codecForEncoding != codecForEncoding()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
101   if(info.codecForDecoding != codecForDecoding()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
102   if(info.serverList.count()) setServerList(toVariantList(info.serverList)); // FIXME compare components
103   if(info.useRandomServer != useRandomServer()) setUseRandomServer(info.useRandomServer);
104   if(info.perform != perform()) setPerform(info.perform);
105   if(info.useAutoIdentify != useAutoIdentify()) setUseAutoIdentify(info.useAutoIdentify);
106   if(info.autoIdentifyService != autoIdentifyService()) setAutoIdentifyService(info.autoIdentifyService);
107   if(info.autoIdentifyPassword != autoIdentifyPassword()) setAutoIdentifyPassword(info.autoIdentifyPassword);
108   if(info.useAutoReconnect != useAutoReconnect()) setUseAutoReconnect(info.useAutoReconnect);
109   if(info.autoReconnectInterval != autoReconnectInterval()) setAutoReconnectInterval(info.autoReconnectInterval);
110   if(info.autoReconnectRetries != autoReconnectRetries()) setAutoReconnectRetries(info.autoReconnectRetries);
111   if(info.unlimitedReconnectRetries != unlimitedReconnectRetries()) setUnlimitedReconnectRetries(info.unlimitedReconnectRetries);
112   if(info.rejoinChannels != rejoinChannels()) setRejoinChannels(info.rejoinChannels);
113 }
114
115 QString Network::prefixToMode(const QString &prefix) {
116   if(prefixes().contains(prefix))
117     return QString(prefixModes()[prefixes().indexOf(prefix)]);
118   else
119     return QString();
120 }
121
122 QString Network::modeToPrefix(const QString &mode) {
123   if(prefixModes().contains(mode))
124     return QString(prefixes()[prefixModes().indexOf(mode)]);
125   else
126     return QString();
127 }
128
129 QStringList Network::nicks() const {
130   // we don't use _ircUsers.keys() since the keys may be
131   // not up to date after a nick change
132   QStringList nicks;
133   foreach(IrcUser *ircuser, _ircUsers.values()) {
134     nicks << ircuser->nick();
135   }
136   return nicks;
137 }
138
139 QString Network::prefixes() {
140   if(_prefixes.isNull())
141     determinePrefixes();
142
143   return _prefixes;
144 }
145
146 QString Network::prefixModes() {
147   if(_prefixModes.isNull())
148     determinePrefixes();
149
150   return _prefixModes;
151 }
152
153 // example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG
154 Network::ChannelModeType Network::channelModeType(const QString &mode) {
155   if(mode.isEmpty())
156     return NOT_A_CHANMODE;
157
158   QString chanmodes = support("CHANMODES");
159   if(chanmodes.isEmpty())
160     return NOT_A_CHANMODE;
161
162   ChannelModeType modeType = A_CHANMODE;
163   for(int i = 0; i < chanmodes.count(); i++) {
164     if(chanmodes[i] == mode[0])
165       break;
166     else if(chanmodes[i] == ',')
167       modeType = (ChannelModeType)(modeType << 1);
168   }
169   if(modeType > D_CHANMODE) {
170     qWarning() << "Network" << networkId() << "supplied invalid CHANMODES:" << chanmodes;
171     modeType = NOT_A_CHANMODE;
172   }
173   return modeType;
174 }
175
176 QString Network::support(const QString &param) const {
177   QString support_ = param.toUpper();
178   if(_supports.contains(support_))
179     return _supports[support_];
180   else
181     return QString();
182 }
183
184 IrcUser *Network::newIrcUser(const QString &hostmask, const QVariantMap &initData) {
185   QString nick(nickFromMask(hostmask).toLower());
186   if(!_ircUsers.contains(nick)) {
187     IrcUser *ircuser = new IrcUser(hostmask, this);
188     if(!initData.isEmpty()) {
189       ircuser->fromVariantMap(initData);
190       ircuser->setInitialized();
191     }
192
193     if(proxy())
194       proxy()->synchronize(ircuser);
195     else
196       qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
197
198     connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
199
200     _ircUsers[nick] = ircuser;
201
202     emit ircUserAdded(hostmask);
203     emit ircUserAdded(ircuser);
204   }
205
206   return _ircUsers[nick];
207 }
208
209 IrcUser *Network::ircUser(QString nickname) const {
210   nickname = nickname.toLower();
211   if(_ircUsers.contains(nickname))
212     return _ircUsers[nickname];
213   else
214     return 0;
215 }
216
217 void Network::removeIrcUser(IrcUser *ircuser) {
218   QString nick = _ircUsers.key(ircuser);
219   if(nick.isNull())
220     return;
221
222   _ircUsers.remove(nick);
223   disconnect(ircuser, 0, this, 0);
224   ircuser->deleteLater();
225 }
226
227 void Network::removeIrcChannel(IrcChannel *channel) {
228   QString chanName = _ircChannels.key(channel);
229   if(chanName.isNull())
230     return;
231
232   _ircChannels.remove(chanName);
233   disconnect(channel, 0, this, 0);
234   channel->deleteLater();
235 }
236
237 void Network::removeChansAndUsers() {
238   QList<IrcUser *> users = ircUsers();
239   _ircUsers.clear();
240   QList<IrcChannel *> channels = ircChannels();
241   _ircChannels.clear();
242
243   foreach(IrcChannel *channel, channels) {
244     proxy()->detachObject(channel);
245     disconnect(channel, 0, this, 0);
246   }
247   foreach(IrcUser *user, users) {
248     proxy()->detachObject(user);
249     disconnect(user, 0, this, 0);
250   }
251
252   // the second loop is needed because quit can have sideffects
253   foreach(IrcUser *user, users) {
254     user->quit();
255   }
256
257   qDeleteAll(users);
258   qDeleteAll(channels);
259 }
260
261 IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap &initData) {
262   if(!_ircChannels.contains(channelname.toLower())) {
263     IrcChannel *channel = ircChannelFactory(channelname);
264     if(!initData.isEmpty()) {
265       channel->fromVariantMap(initData);
266       channel->setInitialized();
267     }
268
269     if(proxy())
270       proxy()->synchronize(channel);
271     else
272       qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
273
274     _ircChannels[channelname.toLower()] = channel;
275
276     emit ircChannelAdded(channelname);
277     emit ircChannelAdded(channel);
278   }
279   return _ircChannels[channelname.toLower()];
280 }
281
282 IrcChannel *Network::ircChannel(QString channelname) const {
283   channelname = channelname.toLower();
284   if(_ircChannels.contains(channelname))
285     return _ircChannels[channelname];
286   else
287     return 0;
288 }
289
290 QByteArray Network::defaultCodecForServer() {
291   if(_defaultCodecForServer)
292     return _defaultCodecForServer->name();
293   return QByteArray();
294 }
295
296 void Network::setDefaultCodecForServer(const QByteArray &name) {
297   _defaultCodecForServer = QTextCodec::codecForName(name);
298 }
299
300 QByteArray Network::defaultCodecForEncoding() {
301   if(_defaultCodecForEncoding)
302     return _defaultCodecForEncoding->name();
303   return QByteArray();
304 }
305
306 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
307   _defaultCodecForEncoding = QTextCodec::codecForName(name);
308 }
309
310 QByteArray Network::defaultCodecForDecoding() {
311   if(_defaultCodecForDecoding)
312     return _defaultCodecForDecoding->name();
313   return QByteArray();
314 }
315
316 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
317   _defaultCodecForDecoding = QTextCodec::codecForName(name);
318 }
319
320 QByteArray Network::codecForServer() const {
321   if(_codecForServer)
322     return _codecForServer->name();
323   return QByteArray();
324 }
325
326 void Network::setCodecForServer(const QByteArray &name) {
327   setCodecForServer(QTextCodec::codecForName(name));
328 }
329
330 void Network::setCodecForServer(QTextCodec *codec) {
331   _codecForServer = codec;
332   emit codecForServerSet(codecForServer());
333 }
334
335 QByteArray Network::codecForEncoding() const {
336   if(_codecForEncoding)
337     return _codecForEncoding->name();
338   return QByteArray();
339 }
340
341 void Network::setCodecForEncoding(const QByteArray &name) {
342   setCodecForEncoding(QTextCodec::codecForName(name));
343 }
344
345 void Network::setCodecForEncoding(QTextCodec *codec) {
346   _codecForEncoding = codec;
347   emit codecForEncodingSet(codecForEncoding());
348 }
349
350 QByteArray Network::codecForDecoding() const {
351   if(_codecForDecoding)
352     return _codecForDecoding->name();
353   else return QByteArray();
354 }
355
356 void Network::setCodecForDecoding(const QByteArray &name) {
357   setCodecForDecoding(QTextCodec::codecForName(name));
358 }
359
360 void Network::setCodecForDecoding(QTextCodec *codec) {
361   _codecForDecoding = codec;
362   emit codecForDecodingSet(codecForDecoding());
363 }
364
365 // FIXME use server encoding if appropriate
366 QString Network::decodeString(const QByteArray &text) const {
367   if(_codecForDecoding)
368     return ::decodeString(text, _codecForDecoding);
369   else return ::decodeString(text, _defaultCodecForDecoding);
370 }
371
372 QByteArray Network::encodeString(const QString &string) const {
373   if(_codecForEncoding) {
374     return _codecForEncoding->fromUnicode(string);
375   }
376   if(_defaultCodecForEncoding) {
377     return _defaultCodecForEncoding->fromUnicode(string);
378   }
379   return string.toAscii();
380 }
381
382 QString Network::decodeServerString(const QByteArray &text) const {
383   if(_codecForServer)
384     return ::decodeString(text, _codecForServer);
385   else
386     return ::decodeString(text, _defaultCodecForServer);
387 }
388
389 QByteArray Network::encodeServerString(const QString &string) const {
390   if(_codecForServer) {
391     return _codecForServer->fromUnicode(string);
392   }
393   if(_defaultCodecForServer) {
394     return _defaultCodecForServer->fromUnicode(string);
395   }
396   return string.toAscii();
397 }
398
399 // ====================
400 //  Public Slots:
401 // ====================
402 void Network::setNetworkName(const QString &networkName) {
403   _networkName = networkName;
404   emit networkNameSet(networkName);
405 }
406
407 void Network::setCurrentServer(const QString &currentServer) {
408   _currentServer = currentServer;
409   emit currentServerSet(currentServer);
410 }
411
412 void Network::setConnected(bool connected) {
413   if(_connected == connected)
414     return;
415
416   _connected = connected;
417   if(!connected) {
418     setMyNick(QString());
419     setCurrentServer(QString());
420     removeChansAndUsers();
421   }
422   emit connectedSet(connected);
423 }
424
425 //void Network::setConnectionState(ConnectionState state) {
426 void Network::setConnectionState(int state) {
427   _connectionState = (ConnectionState)state;
428   //qDebug() << "netstate" << networkId() << networkName() << state;
429   emit connectionStateSet(state);
430   emit connectionStateSet(_connectionState);
431 }
432
433 void Network::setMyNick(const QString &nickname) {
434   _myNick = nickname;
435   if(!_myNick.isEmpty() && !ircUser(myNick())) {
436     newIrcUser(myNick());
437   }
438   emit myNickSet(nickname);
439 }
440
441 void Network::setLatency(int latency) {
442   if(_latency == latency)
443     return;
444   _latency = latency;
445   emit latencySet(latency);
446 }
447
448 void Network::setIdentity(IdentityId id) {
449   _identity = id;
450   emit identitySet(id);
451 }
452
453 void Network::setServerList(const QVariantList &serverList) {
454   _serverList = fromVariantList<Server>(serverList);
455   emit serverListSet(serverList);
456 }
457
458 void Network::setUseRandomServer(bool use) {
459   _useRandomServer = use;
460   emit useRandomServerSet(use);
461 }
462
463 void Network::setPerform(const QStringList &perform) {
464   _perform = perform;
465   emit performSet(perform);
466 }
467
468 void Network::setUseAutoIdentify(bool use) {
469   _useAutoIdentify = use;
470   emit useAutoIdentifySet(use);
471 }
472
473 void Network::setAutoIdentifyService(const QString &service) {
474   _autoIdentifyService = service;
475   emit autoIdentifyServiceSet(service);
476 }
477
478 void Network::setAutoIdentifyPassword(const QString &password) {
479   _autoIdentifyPassword = password;
480   emit autoIdentifyPasswordSet(password);
481 }
482
483 void Network::setUseAutoReconnect(bool use) {
484   _useAutoReconnect = use;
485   emit useAutoReconnectSet(use);
486 }
487
488 void Network::setAutoReconnectInterval(quint32 interval) {
489   _autoReconnectInterval = interval;
490   emit autoReconnectIntervalSet(interval);
491 }
492
493 void Network::setAutoReconnectRetries(quint16 retries) {
494   _autoReconnectRetries = retries;
495   emit autoReconnectRetriesSet(retries);
496 }
497
498 void Network::setUnlimitedReconnectRetries(bool unlimited) {
499   _unlimitedReconnectRetries = unlimited;
500   emit unlimitedReconnectRetriesSet(unlimited);
501 }
502
503 void Network::setRejoinChannels(bool rejoin) {
504   _rejoinChannels = rejoin;
505   emit rejoinChannelsSet(rejoin);
506 }
507
508 void Network::addSupport(const QString &param, const QString &value) {
509   if(!_supports.contains(param)) {
510     _supports[param] = value;
511     emit supportAdded(param, value);
512   }
513 }
514
515 void Network::removeSupport(const QString &param) {
516   if(_supports.contains(param)) {
517     _supports.remove(param);
518     emit supportRemoved(param);
519   }
520 }
521
522 QVariantMap Network::initSupports() const {
523   QVariantMap supports;
524   QHashIterator<QString, QString> iter(_supports);
525   while(iter.hasNext()) {
526     iter.next();
527     supports[iter.key()] = iter.value();
528   }
529   return supports;
530 }
531
532 QVariantMap Network::initIrcUsersAndChannels() const {
533   QVariantMap usersAndChannels;
534   QVariantMap users;
535   QVariantMap channels;
536
537   QHash<QString, IrcUser *>::const_iterator userIter = _ircUsers.constBegin();
538   QHash<QString, IrcUser *>::const_iterator userIterEnd = _ircUsers.constEnd();
539   while(userIter != userIterEnd) {
540     users[userIter.value()->hostmask()] = userIter.value()->toVariantMap();
541     userIter++;
542   }
543   usersAndChannels["users"] = users;
544
545   QHash<QString, IrcChannel *>::const_iterator channelIter = _ircChannels.constBegin();
546   QHash<QString, IrcChannel *>::const_iterator channelIterEnd = _ircChannels.constEnd();
547   while(channelIter != channelIterEnd) {
548     channels[channelIter.value()->name()] = channelIter.value()->toVariantMap();
549     channelIter++;
550   }
551   usersAndChannels["channels"] = channels;
552
553   return usersAndChannels;
554 }
555
556 void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels) {
557   Q_ASSERT(proxy());
558   if(isInitialized()) {
559     qWarning() << "Network" << networkId() << "received init data for users and channels allthough there allready are known users or channels!";
560     return;
561   }
562
563   QVariantMap users = usersAndChannels.value("users").toMap();
564   QVariantMap::const_iterator userIter = users.constBegin();
565   QVariantMap::const_iterator userIterEnd = users.constEnd();
566   while(userIter != userIterEnd) {
567     newIrcUser(userIter.key(), userIter.value().toMap());
568     userIter++;
569   }
570
571   QVariantMap channels = usersAndChannels.value("channels").toMap();
572   QVariantMap::const_iterator channelIter = channels.constBegin();
573   QVariantMap::const_iterator channelIterEnd = channels.constEnd();
574   while(channelIter != channelIterEnd) {
575     newIrcChannel(channelIter.key(), channelIter.value().toMap());
576     channelIter++;
577   }
578 }
579
580 void Network::initSetSupports(const QVariantMap &supports) {
581   QMapIterator<QString, QVariant> iter(supports);
582   while(iter.hasNext()) {
583     iter.next();
584     addSupport(iter.key(), iter.value().toString());
585   }
586 }
587
588 IrcUser *Network::updateNickFromMask(const QString &mask) {
589   QString nick(nickFromMask(mask).toLower());
590   IrcUser *ircuser;
591
592   if(_ircUsers.contains(nick)) {
593     ircuser = _ircUsers[nick];
594     ircuser->updateHostmask(mask);
595   } else {
596     ircuser = newIrcUser(mask);
597   }
598   return ircuser;
599 }
600
601 void Network::ircUserNickChanged(QString newnick) {
602   QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
603
604   if(oldnick.isNull())
605     return;
606
607   if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
608
609   if(myNick().toLower() == oldnick)
610     setMyNick(newnick);
611 }
612
613 void Network::emitConnectionError(const QString &errorMsg) {
614   emit connectionError(errorMsg);
615 }
616
617 // ====================
618 //  Private:
619 // ====================
620 void Network::determinePrefixes() {
621   // seems like we have to construct them first
622   QString prefix = support("PREFIX");
623
624   if(prefix.startsWith("(") && prefix.contains(")")) {
625     _prefixes = prefix.section(")", 1);
626     _prefixModes = prefix.mid(1).section(")", 0, 0);
627   } else {
628     QString defaultPrefixes("~&@%+");
629     QString defaultPrefixModes("qaohv");
630
631     if(prefix.isEmpty()) {
632       _prefixes = defaultPrefixes;
633       _prefixModes = defaultPrefixModes;
634       return;
635     }
636
637     // we just assume that in PREFIX are only prefix chars stored
638     for(int i = 0; i < defaultPrefixes.size(); i++) {
639       if(prefix.contains(defaultPrefixes[i])) {
640         _prefixes += defaultPrefixes[i];
641         _prefixModes += defaultPrefixModes[i];
642       }
643     }
644     // check for success
645     if(!_prefixes.isNull())
646       return;
647
648     // well... our assumption was obviously wrong...
649     // check if it's only prefix modes
650     for(int i = 0; i < defaultPrefixes.size(); i++) {
651       if(prefix.contains(defaultPrefixModes[i])) {
652         _prefixes += defaultPrefixes[i];
653         _prefixModes += defaultPrefixModes[i];
654       }
655     }
656     // now we've done all we've could...
657   }
658 }
659
660 /************************************************************************
661  * NetworkInfo
662  ************************************************************************/
663
664 bool NetworkInfo::operator==(const NetworkInfo &other) const {
665   if(networkId != other.networkId) return false;
666   if(networkName != other.networkName) return false;
667   if(identity != other.identity) return false;
668   if(codecForServer != other.codecForServer) return false;
669   if(codecForEncoding != other.codecForEncoding) return false;
670   if(codecForDecoding != other.codecForDecoding) return false;
671   if(serverList != other.serverList) return false;
672   if(useRandomServer != other.useRandomServer) return false;
673   if(perform != other.perform) return false;
674   if(useAutoIdentify != other.useAutoIdentify) return false;
675   if(autoIdentifyService != other.autoIdentifyService) return false;
676   if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
677   if(useAutoReconnect != other.useAutoReconnect) return false;
678   if(autoReconnectInterval != other.autoReconnectInterval) return false;
679   if(autoReconnectRetries != other.autoReconnectRetries) return false;
680   if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
681   if(rejoinChannels != other.rejoinChannels) return false;
682   return true;
683 }
684
685 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
686   return !(*this == other);
687 }
688
689 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
690   QVariantMap i;
691   i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
692   i["NetworkName"] = info.networkName;
693   i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
694   i["CodecForServer"] = info.codecForServer;
695   i["CodecForEncoding"] = info.codecForEncoding;
696   i["CodecForDecoding"] = info.codecForDecoding;
697   i["ServerList"] = toVariantList(info.serverList);
698   i["UseRandomServer"] = info.useRandomServer;
699   i["Perform"] = info.perform;
700   i["UseAutoIdentify"] = info.useAutoIdentify;
701   i["AutoIdentifyService"] = info.autoIdentifyService;
702   i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
703   i["UseAutoReconnect"] = info.useAutoReconnect;
704   i["AutoReconnectInterval"] = info.autoReconnectInterval;
705   i["AutoReconnectRetries"] = info.autoReconnectRetries;
706   i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
707   i["RejoinChannels"] = info.rejoinChannels;
708   out << i;
709   return out;
710 }
711
712 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
713   QVariantMap i;
714   in >> i;
715   info.networkId = i["NetworkId"].value<NetworkId>();
716   info.networkName = i["NetworkName"].toString();
717   info.identity = i["Identity"].value<IdentityId>();
718   info.codecForServer = i["CodecForServer"].toByteArray();
719   info.codecForEncoding = i["CodecForEncoding"].toByteArray();
720   info.codecForDecoding = i["CodecForDecoding"].toByteArray();
721   info.serverList = fromVariantList<Network::Server>(i["ServerList"].toList());
722   info.useRandomServer = i["UseRandomServer"].toBool();
723   info.perform = i["Perform"].toStringList();
724   info.useAutoIdentify = i["UseAutoIdentify"].toBool();
725   info.autoIdentifyService = i["AutoIdentifyService"].toString();
726   info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
727   info.useAutoReconnect = i["UseAutoReconnect"].toBool();
728   info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
729   info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
730   info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
731   info.rejoinChannels = i["RejoinChannels"].toBool();
732   return in;
733 }
734
735 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
736   dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
737                 << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
738                 << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
739                 << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
740                 << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
741                 << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
742                 << " rejoinChannels = " << i.rejoinChannels << ")";
743   return dbg.space();
744 }
745
746 QDataStream &operator<<(QDataStream &out, const Network::Server &server) {
747   QVariantMap serverMap;
748   serverMap["Host"] = server.host;
749   serverMap["Port"] = server.port;
750   serverMap["Password"] = server.password;
751   serverMap["UseSSL"] = server.useSsl;
752   serverMap["sslVersion"] = server.sslVersion;
753   serverMap["UseProxy"] = server.useProxy;
754   serverMap["ProxyType"] = server.proxyType;
755   serverMap["ProxyHost"] = server.proxyHost;
756   serverMap["ProxyPort"] = server.proxyPort;
757   serverMap["ProxyUser"] = server.proxyUser;
758   serverMap["ProxyPass"] = server.proxyPass;
759   out << serverMap;
760   return out;
761 }
762
763 QDataStream &operator>>(QDataStream &in, Network::Server &server) {
764   QVariantMap serverMap;
765   in >> serverMap;
766   server.host = serverMap["Host"].toString();
767   server.port = serverMap["Port"].toUInt();
768   server.password = serverMap["Password"].toString();
769   server.useSsl = serverMap["UseSSL"].toBool();
770   server.sslVersion = serverMap["sslVersion"].toInt();
771   server.useProxy = serverMap["UseProxy"].toBool();
772   server.proxyType = serverMap["ProxyType"].toInt();
773   server.proxyHost = serverMap["ProxyHost"].toString();
774   server.proxyPort = serverMap["ProxyPort"].toUInt();
775   server.proxyUser = serverMap["ProxyUser"].toString();
776   server.proxyPass = serverMap["ProxyPass"].toString();
777   return in;
778 }
779
780
781 bool Network::Server::operator==(const Server &other) const {
782   if(host != other.host) return false;
783   if(port != other.port) return false;
784   if(password != other.password) return false;
785   if(useSsl != other.useSsl) return false;
786   if(sslVersion != other.sslVersion) return false;
787   if(useProxy != other.useProxy) return false;
788   if(proxyType != other.proxyType) return false;
789   if(proxyHost != other.proxyHost) return false;
790   if(proxyPort != other.proxyPort) return false;
791   if(proxyUser != other.proxyUser) return false;
792   if(proxyPass != other.proxyPass) return false;
793   return true;
794 }
795
796 bool Network::Server::operator!=(const Server &other) const {
797   return !(*this == other);
798 }
799
800 QDebug operator<<(QDebug dbg, const Network::Server &server) {
801   dbg.nospace() << "Server(host = " << server.host << ":" << server.port << ", useSsl = " << server.useSsl << ")";
802   return dbg.space();
803 }