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