fix upgradeSchema.sh
[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   emit configChanged();
340 }
341
342 QByteArray Network::codecForEncoding() const {
343   if(_codecForEncoding)
344     return _codecForEncoding->name();
345   return QByteArray();
346 }
347
348 void Network::setCodecForEncoding(const QByteArray &name) {
349   setCodecForEncoding(QTextCodec::codecForName(name));
350 }
351
352 void Network::setCodecForEncoding(QTextCodec *codec) {
353   _codecForEncoding = codec;
354   QByteArray codecName = codecForEncoding();
355   SYNC_OTHER(setCodecForEncoding, ARG(codecName))
356   emit configChanged();
357 }
358
359 QByteArray Network::codecForDecoding() const {
360   if(_codecForDecoding)
361     return _codecForDecoding->name();
362   else return QByteArray();
363 }
364
365 void Network::setCodecForDecoding(const QByteArray &name) {
366   setCodecForDecoding(QTextCodec::codecForName(name));
367 }
368
369 void Network::setCodecForDecoding(QTextCodec *codec) {
370   _codecForDecoding = codec;
371   QByteArray codecName = codecForDecoding();
372   SYNC_OTHER(setCodecForDecoding, ARG(codecName))
373   emit configChanged();
374 }
375
376 // FIXME use server encoding if appropriate
377 QString Network::decodeString(const QByteArray &text) const {
378   if(_codecForDecoding)
379     return ::decodeString(text, _codecForDecoding);
380   else return ::decodeString(text, _defaultCodecForDecoding);
381 }
382
383 QByteArray Network::encodeString(const QString &string) const {
384   if(_codecForEncoding) {
385     return _codecForEncoding->fromUnicode(string);
386   }
387   if(_defaultCodecForEncoding) {
388     return _defaultCodecForEncoding->fromUnicode(string);
389   }
390   return string.toAscii();
391 }
392
393 QString Network::decodeServerString(const QByteArray &text) const {
394   if(_codecForServer)
395     return ::decodeString(text, _codecForServer);
396   else
397     return ::decodeString(text, _defaultCodecForServer);
398 }
399
400 QByteArray Network::encodeServerString(const QString &string) const {
401   if(_codecForServer) {
402     return _codecForServer->fromUnicode(string);
403   }
404   if(_defaultCodecForServer) {
405     return _defaultCodecForServer->fromUnicode(string);
406   }
407   return string.toAscii();
408 }
409
410 /*** Handle networks.ini ***/
411
412 QStringList Network::presetNetworks(bool onlyDefault) {
413   // lazily find the file, make sure to not call one of the other preset functions first (they'll fail else)
414   if(_networksIniPath.isNull()) {
415     _networksIniPath = Quassel::findDataFilePath("networks.ini");
416     if(_networksIniPath.isNull()) {
417       _networksIniPath = ""; // now we won't check again, as it's not null anymore
418       return QStringList();
419     }
420   }
421   if(!_networksIniPath.isEmpty()) {
422     QSettings s(_networksIniPath, QSettings::IniFormat);
423     QStringList networks = s.childGroups();
424     if(!networks.isEmpty()) {
425       // we sort the list case-insensitive
426       QMap<QString, QString> sorted;
427       foreach(QString net, networks) {
428         if(onlyDefault && !s.value(QString("%1/Default").arg(net)).toBool())
429           continue;
430         sorted[net.toLower()] = net;
431       }
432       return sorted.values();
433     }
434   }
435   return QStringList();
436 }
437
438 QStringList Network::presetDefaultChannels(const QString &networkName) {
439   if(_networksIniPath.isEmpty())  // be sure to have called presetNetworks() first, else this always fails
440     return QStringList();
441   QSettings s(_networksIniPath, QSettings::IniFormat);
442   return s.value(QString("%1/DefaultChannels").arg(networkName)).toStringList();
443 }
444
445 NetworkInfo Network::networkInfoFromPreset(const QString &networkName) {
446   NetworkInfo info;
447   if(!_networksIniPath.isEmpty()) {
448     info.networkName = networkName;
449     QSettings s(_networksIniPath, QSettings::IniFormat);
450     s.beginGroup(info.networkName);
451     foreach(QString server, s.value("Servers").toStringList()) {
452       bool ssl = false;
453       QStringList splitserver = server.split(':', QString::SkipEmptyParts);
454       if(splitserver.count() != 2) {
455         qWarning() << "Invalid server entry in networks.conf:" << server;
456         continue;
457       }
458       if(splitserver[1].at(0) == '+')
459         ssl = true;
460       uint port = splitserver[1].toUInt();
461       if(!port) {
462         qWarning() << "Invalid port entry in networks.conf:" << server;
463         continue;
464       }
465       info.serverList << Network::Server(splitserver[0].trimmed(), port, QString(), ssl);
466     }
467   }
468   return info;
469 }
470
471
472 // ====================
473 //  Public Slots:
474 // ====================
475 void Network::setNetworkName(const QString &networkName) {
476   _networkName = networkName;
477   SYNC(ARG(networkName))
478   emit networkNameSet(networkName);
479   emit configChanged();
480 }
481
482 void Network::setCurrentServer(const QString &currentServer) {
483   _currentServer = currentServer;
484   SYNC(ARG(currentServer))
485   emit currentServerSet(currentServer);
486 }
487
488 void Network::setConnected(bool connected) {
489   if(_connected == connected)
490     return;
491
492   _connected = connected;
493   if(!connected) {
494     setMyNick(QString());
495     setCurrentServer(QString());
496     removeChansAndUsers();
497   }
498   SYNC(ARG(connected))
499   emit connectedSet(connected);
500 }
501
502 //void Network::setConnectionState(ConnectionState state) {
503 void Network::setConnectionState(int state) {
504   _connectionState = (ConnectionState)state;
505   //qDebug() << "netstate" << networkId() << networkName() << state;
506   SYNC(ARG(state))
507   emit connectionStateSet(_connectionState);
508 }
509
510 void Network::setMyNick(const QString &nickname) {
511   _myNick = nickname;
512   if(!_myNick.isEmpty() && !ircUser(myNick())) {
513     newIrcUser(myNick());
514   }
515   SYNC(ARG(nickname))
516   emit myNickSet(nickname);
517 }
518
519 void Network::setLatency(int latency) {
520   if(_latency == latency)
521     return;
522   _latency = latency;
523   SYNC(ARG(latency))
524 }
525
526 void Network::setIdentity(IdentityId id) {
527   _identity = id;
528   SYNC(ARG(id))
529   emit identitySet(id);
530   emit configChanged();
531 }
532
533 void Network::setServerList(const QVariantList &serverList) {
534   _serverList = fromVariantList<Server>(serverList);
535   SYNC(ARG(serverList))
536   emit configChanged();
537 }
538
539 void Network::setUseRandomServer(bool use) {
540   _useRandomServer = use;
541   SYNC(ARG(use))
542   emit configChanged();
543 }
544
545 void Network::setPerform(const QStringList &perform) {
546   _perform = perform;
547   SYNC(ARG(perform))
548   emit configChanged();
549 }
550
551 void Network::setUseAutoIdentify(bool use) {
552   _useAutoIdentify = use;
553   SYNC(ARG(use))
554   emit configChanged();
555 }
556
557 void Network::setAutoIdentifyService(const QString &service) {
558   _autoIdentifyService = service;
559   SYNC(ARG(service))
560   emit configChanged();
561 }
562
563 void Network::setAutoIdentifyPassword(const QString &password) {
564   _autoIdentifyPassword = password;
565   SYNC(ARG(password))
566   emit configChanged();
567 }
568
569 void Network::setUseAutoReconnect(bool use) {
570   _useAutoReconnect = use;
571   SYNC(ARG(use))
572   emit configChanged();
573 }
574
575 void Network::setAutoReconnectInterval(quint32 interval) {
576   _autoReconnectInterval = interval;
577   SYNC(ARG(interval))
578   emit configChanged();
579 }
580
581 void Network::setAutoReconnectRetries(quint16 retries) {
582   _autoReconnectRetries = retries;
583   SYNC(ARG(retries))
584   emit configChanged();
585 }
586
587 void Network::setUnlimitedReconnectRetries(bool unlimited) {
588   _unlimitedReconnectRetries = unlimited;
589   SYNC(ARG(unlimited))
590   emit configChanged();
591 }
592
593 void Network::setRejoinChannels(bool rejoin) {
594   _rejoinChannels = rejoin;
595   SYNC(ARG(rejoin))
596   emit configChanged();
597 }
598
599 void Network::addSupport(const QString &param, const QString &value) {
600   if(!_supports.contains(param)) {
601     _supports[param] = value;
602     SYNC(ARG(param), ARG(value))
603   }
604 }
605
606 void Network::removeSupport(const QString &param) {
607   if(_supports.contains(param)) {
608     _supports.remove(param);
609     SYNC(ARG(param))
610   }
611 }
612
613 QVariantMap Network::initSupports() const {
614   QVariantMap supports;
615   QHashIterator<QString, QString> iter(_supports);
616   while(iter.hasNext()) {
617     iter.next();
618     supports[iter.key()] = iter.value();
619   }
620   return supports;
621 }
622
623 QVariantMap Network::initIrcUsersAndChannels() const {
624   QVariantMap usersAndChannels;
625   QVariantMap users;
626   QVariantMap channels;
627
628   QHash<QString, IrcUser *>::const_iterator userIter = _ircUsers.constBegin();
629   QHash<QString, IrcUser *>::const_iterator userIterEnd = _ircUsers.constEnd();
630   while(userIter != userIterEnd) {
631     users[userIter.value()->hostmask()] = userIter.value()->toVariantMap();
632     userIter++;
633   }
634   usersAndChannels["users"] = users;
635
636   QHash<QString, IrcChannel *>::const_iterator channelIter = _ircChannels.constBegin();
637   QHash<QString, IrcChannel *>::const_iterator channelIterEnd = _ircChannels.constEnd();
638   while(channelIter != channelIterEnd) {
639     channels[channelIter.value()->name()] = channelIter.value()->toVariantMap();
640     channelIter++;
641   }
642   usersAndChannels["channels"] = channels;
643
644   return usersAndChannels;
645 }
646
647 void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels) {
648   Q_ASSERT(proxy());
649   if(isInitialized()) {
650     qWarning() << "Network" << networkId() << "received init data for users and channels allthough there allready are known users or channels!";
651     return;
652   }
653
654   QVariantMap users = usersAndChannels.value("users").toMap();
655   QVariantMap::const_iterator userIter = users.constBegin();
656   QVariantMap::const_iterator userIterEnd = users.constEnd();
657   while(userIter != userIterEnd) {
658     newIrcUser(userIter.key(), userIter.value().toMap());
659     userIter++;
660   }
661
662   QVariantMap channels = usersAndChannels.value("channels").toMap();
663   QVariantMap::const_iterator channelIter = channels.constBegin();
664   QVariantMap::const_iterator channelIterEnd = channels.constEnd();
665   while(channelIter != channelIterEnd) {
666     newIrcChannel(channelIter.key(), channelIter.value().toMap());
667     channelIter++;
668   }
669 }
670
671 void Network::initSetSupports(const QVariantMap &supports) {
672   QMapIterator<QString, QVariant> iter(supports);
673   while(iter.hasNext()) {
674     iter.next();
675     addSupport(iter.key(), iter.value().toString());
676   }
677 }
678
679 IrcUser *Network::updateNickFromMask(const QString &mask) {
680   QString nick(nickFromMask(mask).toLower());
681   IrcUser *ircuser;
682
683   if(_ircUsers.contains(nick)) {
684     ircuser = _ircUsers[nick];
685     ircuser->updateHostmask(mask);
686   } else {
687     ircuser = newIrcUser(mask);
688   }
689   return ircuser;
690 }
691
692 void Network::ircUserNickChanged(QString newnick) {
693   QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
694
695   if(oldnick.isNull())
696     return;
697
698   if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
699
700   if(myNick().toLower() == oldnick)
701     setMyNick(newnick);
702 }
703
704 void Network::emitConnectionError(const QString &errorMsg) {
705   emit connectionError(errorMsg);
706 }
707
708 // ====================
709 //  Private:
710 // ====================
711 void Network::determinePrefixes() {
712   // seems like we have to construct them first
713   QString prefix = support("PREFIX");
714
715   if(prefix.startsWith("(") && prefix.contains(")")) {
716     _prefixes = prefix.section(")", 1);
717     _prefixModes = prefix.mid(1).section(")", 0, 0);
718   } else {
719     QString defaultPrefixes("~&@%+");
720     QString defaultPrefixModes("qaohv");
721
722     if(prefix.isEmpty()) {
723       _prefixes = defaultPrefixes;
724       _prefixModes = defaultPrefixModes;
725       return;
726     }
727
728     // we just assume that in PREFIX are only prefix chars stored
729     for(int i = 0; i < defaultPrefixes.size(); i++) {
730       if(prefix.contains(defaultPrefixes[i])) {
731         _prefixes += defaultPrefixes[i];
732         _prefixModes += defaultPrefixModes[i];
733       }
734     }
735     // check for success
736     if(!_prefixes.isNull())
737       return;
738
739     // well... our assumption was obviously wrong...
740     // check if it's only prefix modes
741     for(int i = 0; i < defaultPrefixes.size(); i++) {
742       if(prefix.contains(defaultPrefixModes[i])) {
743         _prefixes += defaultPrefixes[i];
744         _prefixModes += defaultPrefixModes[i];
745       }
746     }
747     // now we've done all we've could...
748   }
749 }
750
751 /************************************************************************
752  * NetworkInfo
753  ************************************************************************/
754
755 NetworkInfo::NetworkInfo()
756 : networkId(0),
757   identity(1),
758   useRandomServer(false),
759   useAutoIdentify(false),
760   autoIdentifyService("NickServ"),
761   useAutoReconnect(true),
762   autoReconnectInterval(60),
763   autoReconnectRetries(20),
764   unlimitedReconnectRetries(false),
765   rejoinChannels(true)
766 {
767
768 }
769
770 bool NetworkInfo::operator==(const NetworkInfo &other) const {
771   if(networkId != other.networkId) return false;
772   if(networkName != other.networkName) return false;
773   if(identity != other.identity) return false;
774   if(codecForServer != other.codecForServer) return false;
775   if(codecForEncoding != other.codecForEncoding) return false;
776   if(codecForDecoding != other.codecForDecoding) return false;
777   if(serverList != other.serverList) return false;
778   if(useRandomServer != other.useRandomServer) return false;
779   if(perform != other.perform) return false;
780   if(useAutoIdentify != other.useAutoIdentify) return false;
781   if(autoIdentifyService != other.autoIdentifyService) return false;
782   if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
783   if(useAutoReconnect != other.useAutoReconnect) return false;
784   if(autoReconnectInterval != other.autoReconnectInterval) return false;
785   if(autoReconnectRetries != other.autoReconnectRetries) return false;
786   if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
787   if(rejoinChannels != other.rejoinChannels) return false;
788   return true;
789 }
790
791 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
792   return !(*this == other);
793 }
794
795 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
796   QVariantMap i;
797   i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
798   i["NetworkName"] = info.networkName;
799   i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
800   i["CodecForServer"] = info.codecForServer;
801   i["CodecForEncoding"] = info.codecForEncoding;
802   i["CodecForDecoding"] = info.codecForDecoding;
803   i["ServerList"] = toVariantList(info.serverList);
804   i["UseRandomServer"] = info.useRandomServer;
805   i["Perform"] = info.perform;
806   i["UseAutoIdentify"] = info.useAutoIdentify;
807   i["AutoIdentifyService"] = info.autoIdentifyService;
808   i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
809   i["UseAutoReconnect"] = info.useAutoReconnect;
810   i["AutoReconnectInterval"] = info.autoReconnectInterval;
811   i["AutoReconnectRetries"] = info.autoReconnectRetries;
812   i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
813   i["RejoinChannels"] = info.rejoinChannels;
814   out << i;
815   return out;
816 }
817
818 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
819   QVariantMap i;
820   in >> i;
821   info.networkId = i["NetworkId"].value<NetworkId>();
822   info.networkName = i["NetworkName"].toString();
823   info.identity = i["Identity"].value<IdentityId>();
824   info.codecForServer = i["CodecForServer"].toByteArray();
825   info.codecForEncoding = i["CodecForEncoding"].toByteArray();
826   info.codecForDecoding = i["CodecForDecoding"].toByteArray();
827   info.serverList = fromVariantList<Network::Server>(i["ServerList"].toList());
828   info.useRandomServer = i["UseRandomServer"].toBool();
829   info.perform = i["Perform"].toStringList();
830   info.useAutoIdentify = i["UseAutoIdentify"].toBool();
831   info.autoIdentifyService = i["AutoIdentifyService"].toString();
832   info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
833   info.useAutoReconnect = i["UseAutoReconnect"].toBool();
834   info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
835   info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
836   info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
837   info.rejoinChannels = i["RejoinChannels"].toBool();
838   return in;
839 }
840
841 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
842   dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
843                 << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
844                 << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
845                 << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
846                 << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
847                 << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
848                 << " rejoinChannels = " << i.rejoinChannels << ")";
849   return dbg.space();
850 }
851
852 QDataStream &operator<<(QDataStream &out, const Network::Server &server) {
853   QVariantMap serverMap;
854   serverMap["Host"] = server.host;
855   serverMap["Port"] = server.port;
856   serverMap["Password"] = server.password;
857   serverMap["UseSSL"] = server.useSsl;
858   serverMap["sslVersion"] = server.sslVersion;
859   serverMap["UseProxy"] = server.useProxy;
860   serverMap["ProxyType"] = server.proxyType;
861   serverMap["ProxyHost"] = server.proxyHost;
862   serverMap["ProxyPort"] = server.proxyPort;
863   serverMap["ProxyUser"] = server.proxyUser;
864   serverMap["ProxyPass"] = server.proxyPass;
865   out << serverMap;
866   return out;
867 }
868
869 QDataStream &operator>>(QDataStream &in, Network::Server &server) {
870   QVariantMap serverMap;
871   in >> serverMap;
872   server.host = serverMap["Host"].toString();
873   server.port = serverMap["Port"].toUInt();
874   server.password = serverMap["Password"].toString();
875   server.useSsl = serverMap["UseSSL"].toBool();
876   server.sslVersion = serverMap["sslVersion"].toInt();
877   server.useProxy = serverMap["UseProxy"].toBool();
878   server.proxyType = serverMap["ProxyType"].toInt();
879   server.proxyHost = serverMap["ProxyHost"].toString();
880   server.proxyPort = serverMap["ProxyPort"].toUInt();
881   server.proxyUser = serverMap["ProxyUser"].toString();
882   server.proxyPass = serverMap["ProxyPass"].toString();
883   return in;
884 }
885
886
887 bool Network::Server::operator==(const Server &other) const {
888   if(host != other.host) return false;
889   if(port != other.port) return false;
890   if(password != other.password) return false;
891   if(useSsl != other.useSsl) return false;
892   if(sslVersion != other.sslVersion) return false;
893   if(useProxy != other.useProxy) return false;
894   if(proxyType != other.proxyType) return false;
895   if(proxyHost != other.proxyHost) return false;
896   if(proxyPort != other.proxyPort) return false;
897   if(proxyUser != other.proxyUser) return false;
898   if(proxyPass != other.proxyPass) return false;
899   return true;
900 }
901
902 bool Network::Server::operator!=(const Server &other) const {
903   return !(*this == other);
904 }
905
906 QDebug operator<<(QDebug dbg, const Network::Server &server) {
907   dbg.nospace() << "Server(host = " << server.host << ":" << server.port << ", useSsl = " << server.useSsl << ")";
908   return dbg.space();
909 }