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