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