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