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