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