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