23ce589f9a6551f8007acff50fdee38c7994c5c6
[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     connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
201     if(!ircuser->isInitialized())
202       connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
203
204     _ircUsers[nick] = ircuser;
205
206     emit ircUserAdded(hostmask);
207     emit ircUserAdded(ircuser);
208     if(ircuser->isInitialized())
209       emit ircUserInitDone(ircuser);
210   }
211
212   return _ircUsers[nick];
213 }
214
215 void Network::ircUserDestroyed() {
216   IrcUser *ircUser = static_cast<IrcUser *>(sender());
217   if(!ircUser)
218     return;
219
220   QHash<QString, IrcUser *>::iterator ircUserIter = _ircUsers.begin();
221   while(ircUserIter != _ircUsers.end()) {
222     if(ircUser == *ircUserIter) {
223       ircUserIter = _ircUsers.erase(ircUserIter);
224       break;
225     }
226     ircUserIter++;
227   }
228 }
229
230 void Network::removeIrcUser(IrcUser *ircuser) {
231   QString nick = _ircUsers.key(ircuser);
232   if(nick.isNull())
233     return;
234
235   _ircUsers.remove(nick);
236   disconnect(ircuser, 0, this, 0);
237   emit ircUserRemoved(nick);
238   emit ircUserRemoved(ircuser);
239   ircuser->deleteLater();
240 }
241
242 void Network::removeIrcUser(const QString &nick) {
243   IrcUser *ircuser;
244   if((ircuser = ircUser(nick)) != 0)
245     removeIrcUser(ircuser);
246 }
247
248 void Network::removeChansAndUsers() {
249   QList<IrcUser *> users = ircUsers();
250   foreach(IrcUser *user, users) {
251     removeIrcUser(user);
252   }
253   QList<IrcChannel *> channels = ircChannels();
254   foreach(IrcChannel *channel, channels) {
255     removeIrcChannel(channel);
256   }
257 }
258
259 IrcUser *Network::ircUser(QString nickname) const {
260   nickname = nickname.toLower();
261   if(_ircUsers.contains(nickname))
262     return _ircUsers[nickname];
263   else
264     return 0;
265 }
266
267 IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap &initData) {
268   if(!_ircChannels.contains(channelname.toLower())) {
269     IrcChannel *channel = ircChannelFactory(channelname);
270     if(!initData.isEmpty()) {
271       channel->fromVariantMap(initData);
272       channel->setInitialized();
273     }
274
275     if(proxy())
276       proxy()->synchronize(channel);
277     else
278       qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
279
280     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
281     if(!channel->isInitialized())
282       connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
283
284     _ircChannels[channelname.toLower()] = channel;
285
286     emit ircChannelAdded(channelname);
287     emit ircChannelAdded(channel);
288     if(channel->isInitialized())
289       emit ircChannelInitDone(channel);
290   }
291   return _ircChannels[channelname.toLower()];
292 }
293
294 IrcChannel *Network::ircChannel(QString channelname) const {
295   channelname = channelname.toLower();
296   if(_ircChannels.contains(channelname))
297     return _ircChannels[channelname];
298   else
299     return 0;
300 }
301
302 QByteArray Network::defaultCodecForServer() {
303   if(_defaultCodecForServer)
304     return _defaultCodecForServer->name();
305   return QByteArray();
306 }
307
308 void Network::setDefaultCodecForServer(const QByteArray &name) {
309   _defaultCodecForServer = QTextCodec::codecForName(name);
310 }
311
312 QByteArray Network::defaultCodecForEncoding() {
313   if(_defaultCodecForEncoding)
314     return _defaultCodecForEncoding->name();
315   return QByteArray();
316 }
317
318 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
319   _defaultCodecForEncoding = QTextCodec::codecForName(name);
320 }
321
322 QByteArray Network::defaultCodecForDecoding() {
323   if(_defaultCodecForDecoding)
324     return _defaultCodecForDecoding->name();
325   return QByteArray();
326 }
327
328 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
329   _defaultCodecForDecoding = QTextCodec::codecForName(name);
330 }
331
332 QByteArray Network::codecForServer() const {
333   if(_codecForServer)
334     return _codecForServer->name();
335   return QByteArray();
336 }
337
338 void Network::setCodecForServer(const QByteArray &name) {
339   setCodecForServer(QTextCodec::codecForName(name));
340 }
341
342 void Network::setCodecForServer(QTextCodec *codec) {
343   _codecForServer = codec;
344   emit codecForServerSet(codecForServer());
345 }
346
347 QByteArray Network::codecForEncoding() const {
348   if(_codecForEncoding)
349     return _codecForEncoding->name();
350   return QByteArray();
351 }
352
353 void Network::setCodecForEncoding(const QByteArray &name) {
354   setCodecForEncoding(QTextCodec::codecForName(name));
355 }
356
357 void Network::setCodecForEncoding(QTextCodec *codec) {
358   _codecForEncoding = codec;
359   emit codecForEncodingSet(codecForEncoding());
360 }
361
362 QByteArray Network::codecForDecoding() const {
363   if(_codecForDecoding)
364     return _codecForDecoding->name();
365   else return QByteArray();
366 }
367
368 void Network::setCodecForDecoding(const QByteArray &name) {
369   setCodecForDecoding(QTextCodec::codecForName(name));
370 }
371
372 void Network::setCodecForDecoding(QTextCodec *codec) {
373   _codecForDecoding = codec;
374   emit codecForDecodingSet(codecForDecoding());
375 }
376
377 // FIXME use server encoding if appropriate
378 QString Network::decodeString(const QByteArray &text) const {
379   if(_codecForDecoding)
380     return ::decodeString(text, _codecForDecoding);
381   else return ::decodeString(text, _defaultCodecForDecoding);
382 }
383
384 QByteArray Network::encodeString(const QString &string) const {
385   if(_codecForEncoding) {
386     return _codecForEncoding->fromUnicode(string);
387   }
388   if(_defaultCodecForEncoding) {
389     return _defaultCodecForEncoding->fromUnicode(string);
390   }
391   return string.toAscii();
392 }
393
394 QString Network::decodeServerString(const QByteArray &text) const {
395   if(_codecForServer)
396     return ::decodeString(text, _codecForServer);
397   else
398     return ::decodeString(text, _defaultCodecForServer);
399 }
400
401 QByteArray Network::encodeServerString(const QString &string) const {
402   if(_codecForServer) {
403     return _codecForServer->fromUnicode(string);
404   }
405   if(_defaultCodecForServer) {
406     return _defaultCodecForServer->fromUnicode(string);
407   }
408   return string.toAscii();
409 }
410
411 // ====================
412 //  Public Slots:
413 // ====================
414 void Network::setNetworkName(const QString &networkName) {
415   _networkName = networkName;
416   emit networkNameSet(networkName);
417 }
418
419 void Network::setCurrentServer(const QString &currentServer) {
420   _currentServer = currentServer;
421   emit currentServerSet(currentServer);
422 }
423
424 void Network::setConnected(bool connected) {
425   if(_connected == connected)
426     return;
427
428   _connected = connected;
429   if(!connected) {
430     setMyNick(QString());
431     setCurrentServer(QString());
432     removeChansAndUsers();
433   }
434   emit connectedSet(connected);
435 }
436
437 //void Network::setConnectionState(ConnectionState state) {
438 void Network::setConnectionState(int state) {
439   _connectionState = (ConnectionState)state;
440   //qDebug() << "netstate" << networkId() << networkName() << state;
441   emit connectionStateSet(state);
442   emit connectionStateSet(_connectionState);
443 }
444
445 void Network::setMyNick(const QString &nickname) {
446   _myNick = nickname;
447   if(!_myNick.isEmpty() && !ircUser(myNick())) {
448     newIrcUser(myNick());
449   }
450   emit myNickSet(nickname);
451 }
452
453 void Network::setLatency(int latency) {
454   if(_latency == latency)
455     return;
456   _latency = latency;
457   emit latencySet(latency);
458 }
459
460 void Network::setIdentity(IdentityId id) {
461   _identity = id;
462   emit identitySet(id);
463 }
464
465 void Network::setServerList(const QVariantList &serverList) {
466   _serverList = serverList;
467   emit serverListSet(serverList);
468 }
469
470 void Network::setUseRandomServer(bool use) {
471   _useRandomServer = use;
472   emit useRandomServerSet(use);
473 }
474
475 void Network::setPerform(const QStringList &perform) {
476   _perform = perform;
477   emit performSet(perform);
478 }
479
480 void Network::setUseAutoIdentify(bool use) {
481   _useAutoIdentify = use;
482   emit useAutoIdentifySet(use);
483 }
484
485 void Network::setAutoIdentifyService(const QString &service) {
486   _autoIdentifyService = service;
487   emit autoIdentifyServiceSet(service);
488 }
489
490 void Network::setAutoIdentifyPassword(const QString &password) {
491   _autoIdentifyPassword = password;
492   emit autoIdentifyPasswordSet(password);
493 }
494
495 void Network::setUseAutoReconnect(bool use) {
496   _useAutoReconnect = use;
497   emit useAutoReconnectSet(use);
498 }
499
500 void Network::setAutoReconnectInterval(quint32 interval) {
501   _autoReconnectInterval = interval;
502   emit autoReconnectIntervalSet(interval);
503 }
504
505 void Network::setAutoReconnectRetries(quint16 retries) {
506   _autoReconnectRetries = retries;
507   emit autoReconnectRetriesSet(retries);
508 }
509
510 void Network::setUnlimitedReconnectRetries(bool unlimited) {
511   _unlimitedReconnectRetries = unlimited;
512   emit unlimitedReconnectRetriesSet(unlimited);
513 }
514
515 void Network::setRejoinChannels(bool rejoin) {
516   _rejoinChannels = rejoin;
517   emit rejoinChannelsSet(rejoin);
518 }
519
520 void Network::addSupport(const QString &param, const QString &value) {
521   if(!_supports.contains(param)) {
522     _supports[param] = value;
523     emit supportAdded(param, value);
524   }
525 }
526
527 void Network::removeSupport(const QString &param) {
528   if(_supports.contains(param)) {
529     _supports.remove(param);
530     emit supportRemoved(param);
531   }
532 }
533
534 QVariantMap Network::initSupports() const {
535   QVariantMap supports;
536   QHashIterator<QString, QString> iter(_supports);
537   while(iter.hasNext()) {
538     iter.next();
539     supports[iter.key()] = iter.value();
540   }
541   return supports;
542 }
543
544 QVariantMap Network::initIrcUsersAndChannels() const {
545   QVariantMap usersAndChannels;
546   QVariantMap users;
547   QVariantMap channels;
548
549   QHash<QString, IrcUser *>::const_iterator userIter = _ircUsers.constBegin();
550   QHash<QString, IrcUser *>::const_iterator userIterEnd = _ircUsers.constEnd();
551   while(userIter != userIterEnd) {
552     users[userIter.value()->hostmask()] = userIter.value()->toVariantMap();
553     userIter++;
554   }
555   usersAndChannels["users"] = users;
556
557   QHash<QString, IrcChannel *>::const_iterator channelIter = _ircChannels.constBegin();
558   QHash<QString, IrcChannel *>::const_iterator channelIterEnd = _ircChannels.constEnd();
559   while(channelIter != channelIterEnd) {
560     channels[channelIter.value()->name()] = channelIter.value()->toVariantMap();
561     channelIter++;
562   }
563   usersAndChannels["channels"] = channels;
564
565   return usersAndChannels;
566 }
567
568 void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels) {
569   Q_ASSERT(proxy());
570   if(isInitialized()) {
571     qWarning() << "Network" << networkId() << "received init data for users and channels allthough there allready are known users or channels!";
572     return;
573   }
574
575   QVariantMap users = usersAndChannels.value("users").toMap();
576   QVariantMap::const_iterator userIter = users.constBegin();
577   QVariantMap::const_iterator userIterEnd = users.constEnd();
578   while(userIter != userIterEnd) {
579     newIrcUser(userIter.key(), userIter.value().toMap());
580     userIter++;
581   }
582
583   QVariantMap channels = usersAndChannels.value("channels").toMap();
584   QVariantMap::const_iterator channelIter = channels.constBegin();
585   QVariantMap::const_iterator channelIterEnd = channels.constEnd();
586   while(channelIter != channelIterEnd) {
587     newIrcChannel(channelIter.key(), channelIter.value().toMap());
588     channelIter++;
589   }
590
591 }
592
593 void Network::initSetSupports(const QVariantMap &supports) {
594   QMapIterator<QString, QVariant> iter(supports);
595   while(iter.hasNext()) {
596     iter.next();
597     addSupport(iter.key(), iter.value().toString());
598   }
599 }
600
601 IrcUser *Network::updateNickFromMask(const QString &mask) {
602   QString nick(nickFromMask(mask).toLower());
603   IrcUser *ircuser;
604
605   if(_ircUsers.contains(nick)) {
606     ircuser = _ircUsers[nick];
607     ircuser->updateHostmask(mask);
608   } else {
609     ircuser = newIrcUser(mask);
610   }
611   return ircuser;
612 }
613
614 void Network::ircUserNickChanged(QString newnick) {
615   QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
616
617   if(oldnick.isNull())
618     return;
619
620   if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
621
622   if(myNick().toLower() == oldnick)
623     setMyNick(newnick);
624 }
625
626 void Network::ircUserInitDone() {
627   IrcUser *ircuser = static_cast<IrcUser *>(sender());
628   Q_ASSERT(ircuser);
629   connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
630   emit ircUserInitDone(ircuser);
631 }
632
633 void Network::ircChannelInitDone() {
634   IrcChannel *ircChannel = static_cast<IrcChannel *>(sender());
635   Q_ASSERT(ircChannel);
636   disconnect(ircChannel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
637   emit ircChannelInitDone(ircChannel);
638 }
639
640 void Network::removeIrcChannel(IrcChannel *channel) {
641   QString chanName = _ircChannels.key(channel);
642   if(chanName.isNull())
643     return;
644
645   _ircChannels.remove(chanName);
646   disconnect(channel, 0, this, 0);
647   emit ircChannelRemoved(chanName);
648   emit ircChannelRemoved(channel);
649   channel->deleteLater();
650 }
651
652 void Network::removeIrcChannel(const QString &channel) {
653   IrcChannel *chan;
654   if((chan = ircChannel(channel)) != 0)
655     removeIrcChannel(chan);
656 }
657
658 void Network::channelDestroyed() {
659   IrcChannel *channel = static_cast<IrcChannel *>(sender());
660   Q_ASSERT(channel);
661   _ircChannels.remove(_ircChannels.key(channel));
662   emit ircChannelRemoved(channel);
663 }
664
665 void Network::emitConnectionError(const QString &errorMsg) {
666   emit connectionError(errorMsg);
667 }
668
669 // ====================
670 //  Private:
671 // ====================
672 void Network::determinePrefixes() {
673   // seems like we have to construct them first
674   QString prefix = support("PREFIX");
675
676   if(prefix.startsWith("(") && prefix.contains(")")) {
677     _prefixes = prefix.section(")", 1);
678     _prefixModes = prefix.mid(1).section(")", 0, 0);
679   } else {
680     QString defaultPrefixes("~&@%+");
681     QString defaultPrefixModes("qaohv");
682
683     if(prefix.isEmpty()) {
684       _prefixes = defaultPrefixes;
685       _prefixModes = defaultPrefixModes;
686       return;
687     }
688
689     // we just assume that in PREFIX are only prefix chars stored
690     for(int i = 0; i < defaultPrefixes.size(); i++) {
691       if(prefix.contains(defaultPrefixes[i])) {
692         _prefixes += defaultPrefixes[i];
693         _prefixModes += defaultPrefixModes[i];
694       }
695     }
696     // check for success
697     if(!_prefixes.isNull())
698       return;
699
700     // well... our assumption was obviously wrong...
701     // check if it's only prefix modes
702     for(int i = 0; i < defaultPrefixes.size(); i++) {
703       if(prefix.contains(defaultPrefixModes[i])) {
704         _prefixes += defaultPrefixes[i];
705         _prefixModes += defaultPrefixModes[i];
706       }
707     }
708     // now we've done all we've could...
709   }
710 }
711
712 /************************************************************************
713  * NetworkInfo
714  ************************************************************************/
715
716 bool NetworkInfo::operator==(const NetworkInfo &other) const {
717   if(networkId != other.networkId) return false;
718   if(networkName != other.networkName) return false;
719   if(identity != other.identity) return false;
720   if(codecForServer != other.codecForServer) return false;
721   if(codecForEncoding != other.codecForEncoding) return false;
722   if(codecForDecoding != other.codecForDecoding) return false;
723   if(serverList != other.serverList) return false;
724   if(useRandomServer != other.useRandomServer) return false;
725   if(perform != other.perform) return false;
726   if(useAutoIdentify != other.useAutoIdentify) return false;
727   if(autoIdentifyService != other.autoIdentifyService) return false;
728   if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
729   if(useAutoReconnect != other.useAutoReconnect) return false;
730   if(autoReconnectInterval != other.autoReconnectInterval) return false;
731   if(autoReconnectRetries != other.autoReconnectRetries) return false;
732   if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
733   if(rejoinChannels != other.rejoinChannels) return false;
734   return true;
735 }
736
737 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
738   return !(*this == other);
739 }
740
741 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
742   QVariantMap i;
743   i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
744   i["NetworkName"] = info.networkName;
745   i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
746   i["CodecForServer"] = info.codecForServer;
747   i["CodecForEncoding"] = info.codecForEncoding;
748   i["CodecForDecoding"] = info.codecForDecoding;
749   i["ServerList"] = info.serverList;
750   i["UseRandomServer"] = info.useRandomServer;
751   i["Perform"] = info.perform;
752   i["UseAutoIdentify"] = info.useAutoIdentify;
753   i["AutoIdentifyService"] = info.autoIdentifyService;
754   i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
755   i["UseAutoReconnect"] = info.useAutoReconnect;
756   i["AutoReconnectInterval"] = info.autoReconnectInterval;
757   i["AutoReconnectRetries"] = info.autoReconnectRetries;
758   i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
759   i["RejoinChannels"] = info.rejoinChannels;
760   out << i;
761   return out;
762 }
763
764 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
765   QVariantMap i;
766   in >> i;
767   info.networkId = i["NetworkId"].value<NetworkId>();
768   info.networkName = i["NetworkName"].toString();
769   info.identity = i["Identity"].value<IdentityId>();
770   info.codecForServer = i["CodecForServer"].toByteArray();
771   info.codecForEncoding = i["CodecForEncoding"].toByteArray();
772   info.codecForDecoding = i["CodecForDecoding"].toByteArray();
773   info.serverList = i["ServerList"].toList();
774   info.useRandomServer = i["UseRandomServer"].toBool();
775   info.perform = i["Perform"].toStringList();
776   info.useAutoIdentify = i["UseAutoIdentify"].toBool();
777   info.autoIdentifyService = i["AutoIdentifyService"].toString();
778   info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
779   info.useAutoReconnect = i["UseAutoReconnect"].toBool();
780   info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
781   info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
782   info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
783   info.rejoinChannels = i["RejoinChannels"].toBool();
784   return in;
785 }
786
787 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
788   dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
789       << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
790       << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
791       << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
792       << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
793       << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
794       << " rejoinChannels = " << i.rejoinChannels << ")";
795   return dbg.space();
796 }