1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
25 QTextCodec *Network::_defaultCodecForServer = 0;
26 QTextCodec *Network::_defaultCodecForEncoding = 0;
27 QTextCodec *Network::_defaultCodecForDecoding = 0;
29 // ====================
31 // ====================
32 INIT_SYNCABLE_OBJECT(Network)
33 Network::Network(const NetworkId &networkid, QObject *parent)
34 : SyncableObject(parent),
36 _networkId(networkid),
40 _networkName(QString("<not initialized>")),
41 _currentServer(QString()),
43 _connectionState(Disconnected),
45 _prefixModes(QString()),
46 _useRandomServer(false),
47 _useAutoIdentify(false),
49 _useAutoReconnect(false),
50 _autoReconnectInterval(60),
51 _autoReconnectRetries(10),
52 _unlimitedReconnectRetries(false),
53 _useCustomMessageRate(false),
54 _messageRateBurstSize(5),
55 _messageRateDelay(2200),
56 _unlimitedMessageRate(false),
60 _autoAwayActive(false)
62 setObjectName(QString::number(networkid.toInt()));
68 emit aboutToBeDestroyed();
72 bool Network::isChannelName(const QString &channelname) const
74 if (channelname.isEmpty())
77 if (supports("CHANTYPES"))
78 return support("CHANTYPES").contains(channelname[0]);
80 return QString("#&!+").contains(channelname[0]);
84 bool Network::isStatusMsg(const QString &target) const
89 if (supports("STATUSMSG"))
90 return support("STATUSMSG").contains(target[0]);
92 return QString("@+").contains(target[0]);
96 NetworkInfo Network::networkInfo() const
99 info.networkName = networkName();
100 info.networkId = networkId();
101 info.identity = identity();
102 info.codecForServer = codecForServer();
103 info.codecForEncoding = codecForEncoding();
104 info.codecForDecoding = codecForDecoding();
105 info.serverList = serverList();
106 info.useRandomServer = useRandomServer();
107 info.perform = perform();
108 info.useAutoIdentify = useAutoIdentify();
109 info.autoIdentifyService = autoIdentifyService();
110 info.autoIdentifyPassword = autoIdentifyPassword();
111 info.useSasl = useSasl();
112 info.saslAccount = saslAccount();
113 info.saslPassword = saslPassword();
114 info.useAutoReconnect = useAutoReconnect();
115 info.autoReconnectInterval = autoReconnectInterval();
116 info.autoReconnectRetries = autoReconnectRetries();
117 info.unlimitedReconnectRetries = unlimitedReconnectRetries();
118 info.rejoinChannels = rejoinChannels();
119 info.useCustomMessageRate = useCustomMessageRate();
120 info.messageRateBurstSize = messageRateBurstSize();
121 info.messageRateDelay = messageRateDelay();
122 info.unlimitedMessageRate = unlimitedMessageRate();
127 void Network::setNetworkInfo(const NetworkInfo &info)
129 // we don't set our ID!
130 if (!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
131 if (info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
132 if (info.codecForServer != codecForServer()) setCodecForServer(QTextCodec::codecForName(info.codecForServer));
133 if (info.codecForEncoding != codecForEncoding()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
134 if (info.codecForDecoding != codecForDecoding()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
135 if (info.serverList.count()) setServerList(toVariantList(info.serverList)); // FIXME compare components
136 if (info.useRandomServer != useRandomServer()) setUseRandomServer(info.useRandomServer);
137 if (info.perform != perform()) setPerform(info.perform);
138 if (info.useAutoIdentify != useAutoIdentify()) setUseAutoIdentify(info.useAutoIdentify);
139 if (info.autoIdentifyService != autoIdentifyService()) setAutoIdentifyService(info.autoIdentifyService);
140 if (info.autoIdentifyPassword != autoIdentifyPassword()) setAutoIdentifyPassword(info.autoIdentifyPassword);
141 if (info.useSasl != useSasl()) setUseSasl(info.useSasl);
142 if (info.saslAccount != saslAccount()) setSaslAccount(info.saslAccount);
143 if (info.saslPassword != saslPassword()) setSaslPassword(info.saslPassword);
144 if (info.useAutoReconnect != useAutoReconnect()) setUseAutoReconnect(info.useAutoReconnect);
145 if (info.autoReconnectInterval != autoReconnectInterval()) setAutoReconnectInterval(info.autoReconnectInterval);
146 if (info.autoReconnectRetries != autoReconnectRetries()) setAutoReconnectRetries(info.autoReconnectRetries);
147 if (info.unlimitedReconnectRetries != unlimitedReconnectRetries()) setUnlimitedReconnectRetries(info.unlimitedReconnectRetries);
148 if (info.rejoinChannels != rejoinChannels()) setRejoinChannels(info.rejoinChannels);
149 // Custom rate limiting
150 if (info.useCustomMessageRate != useCustomMessageRate())
151 setUseCustomMessageRate(info.useCustomMessageRate);
152 if (info.messageRateBurstSize != messageRateBurstSize())
153 setMessageRateBurstSize(info.messageRateBurstSize);
154 if (info.messageRateDelay != messageRateDelay())
155 setMessageRateDelay(info.messageRateDelay);
156 if (info.unlimitedMessageRate != unlimitedMessageRate())
157 setUnlimitedMessageRate(info.unlimitedMessageRate);
161 QString Network::prefixToMode(const QString &prefix) const
163 if (prefixes().contains(prefix))
164 return QString(prefixModes()[prefixes().indexOf(prefix)]);
170 QString Network::modeToPrefix(const QString &mode) const
172 if (prefixModes().contains(mode))
173 return QString(prefixes()[prefixModes().indexOf(mode)]);
179 QStringList Network::nicks() const
181 // we don't use _ircUsers.keys() since the keys may be
182 // not up to date after a nick change
184 foreach(IrcUser *ircuser, _ircUsers.values()) {
185 nicks << ircuser->nick();
191 QString Network::prefixes() const
193 if (_prefixes.isNull())
200 QString Network::prefixModes() const
202 if (_prefixModes.isNull())
209 // example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG
210 Network::ChannelModeType Network::channelModeType(const QString &mode)
213 return NOT_A_CHANMODE;
215 QString chanmodes = support("CHANMODES");
216 if (chanmodes.isEmpty())
217 return NOT_A_CHANMODE;
219 ChannelModeType modeType = A_CHANMODE;
220 for (int i = 0; i < chanmodes.count(); i++) {
221 if (chanmodes[i] == mode[0])
223 else if (chanmodes[i] == ',')
224 modeType = (ChannelModeType)(modeType << 1);
226 if (modeType > D_CHANMODE) {
227 qWarning() << "Network" << networkId() << "supplied invalid CHANMODES:" << chanmodes;
228 modeType = NOT_A_CHANMODE;
234 QString Network::support(const QString ¶m) const
236 QString support_ = param.toUpper();
237 if (_supports.contains(support_))
238 return _supports[support_];
244 bool Network::saslMaybeSupports(const QString &saslMechanism) const
246 // Get the SASL capability value
247 QString saslCapValue = capValue(IrcCap::SASL);
248 // SASL mechanisms are only specified in capability values as part of SASL 3.2. In SASL 3.1,
249 // it's handled differently. If we don't know via capability value, assume it's supported to
250 // reduce the risk of breaking existing setups.
251 // See: http://ircv3.net/specs/extensions/sasl-3.1.html
252 // And: http://ircv3.net/specs/extensions/sasl-3.2.html
253 return (saslCapValue.length() == 0)
254 || (saslCapValue.contains(saslMechanism, Qt::CaseInsensitive));
258 IrcUser *Network::newIrcUser(const QString &hostmask, const QVariantMap &initData)
260 QString nick(nickFromMask(hostmask).toLower());
261 if (!_ircUsers.contains(nick)) {
262 IrcUser *ircuser = ircUserFactory(hostmask);
263 if (!initData.isEmpty()) {
264 ircuser->fromVariantMap(initData);
265 ircuser->setInitialized();
269 proxy()->synchronize(ircuser);
271 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
273 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
275 _ircUsers[nick] = ircuser;
277 // This method will be called with a nick instead of hostmask by setInitIrcUsersAndChannels().
278 // Not a problem because initData contains all we need; however, making sure here to get the real
279 // hostmask out of the IrcUser afterwards.
280 QString mask = ircuser->hostmask();
281 SYNC_OTHER(addIrcUser, ARG(mask));
282 // emit ircUserAdded(mask);
283 emit ircUserAdded(ircuser);
286 return _ircUsers[nick];
290 IrcUser *Network::ircUser(QString nickname) const
292 nickname = nickname.toLower();
293 if (_ircUsers.contains(nickname))
294 return _ircUsers[nickname];
300 void Network::removeIrcUser(IrcUser *ircuser)
302 QString nick = _ircUsers.key(ircuser);
306 _ircUsers.remove(nick);
307 disconnect(ircuser, 0, this, 0);
308 ircuser->deleteLater();
312 void Network::removeIrcChannel(IrcChannel *channel)
314 QString chanName = _ircChannels.key(channel);
315 if (chanName.isNull())
318 _ircChannels.remove(chanName);
319 disconnect(channel, 0, this, 0);
320 channel->deleteLater();
324 void Network::removeChansAndUsers()
326 QList<IrcUser *> users = ircUsers();
328 QList<IrcChannel *> channels = ircChannels();
329 _ircChannels.clear();
332 qDeleteAll(channels);
336 IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap &initData)
338 if (!_ircChannels.contains(channelname.toLower())) {
339 IrcChannel *channel = ircChannelFactory(channelname);
340 if (!initData.isEmpty()) {
341 channel->fromVariantMap(initData);
342 channel->setInitialized();
346 proxy()->synchronize(channel);
348 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
350 _ircChannels[channelname.toLower()] = channel;
352 SYNC_OTHER(addIrcChannel, ARG(channelname))
353 // emit ircChannelAdded(channelname);
354 emit ircChannelAdded(channel);
356 return _ircChannels[channelname.toLower()];
360 IrcChannel *Network::ircChannel(QString channelname) const
362 channelname = channelname.toLower();
363 if (_ircChannels.contains(channelname))
364 return _ircChannels[channelname];
370 QByteArray Network::defaultCodecForServer()
372 if (_defaultCodecForServer)
373 return _defaultCodecForServer->name();
378 void Network::setDefaultCodecForServer(const QByteArray &name)
380 _defaultCodecForServer = QTextCodec::codecForName(name);
384 QByteArray Network::defaultCodecForEncoding()
386 if (_defaultCodecForEncoding)
387 return _defaultCodecForEncoding->name();
392 void Network::setDefaultCodecForEncoding(const QByteArray &name)
394 _defaultCodecForEncoding = QTextCodec::codecForName(name);
398 QByteArray Network::defaultCodecForDecoding()
400 if (_defaultCodecForDecoding)
401 return _defaultCodecForDecoding->name();
406 void Network::setDefaultCodecForDecoding(const QByteArray &name)
408 _defaultCodecForDecoding = QTextCodec::codecForName(name);
412 QByteArray Network::codecForServer() const
415 return _codecForServer->name();
420 void Network::setCodecForServer(const QByteArray &name)
422 setCodecForServer(QTextCodec::codecForName(name));
426 void Network::setCodecForServer(QTextCodec *codec)
428 _codecForServer = codec;
429 QByteArray codecName = codecForServer();
430 SYNC_OTHER(setCodecForServer, ARG(codecName))
431 emit configChanged();
435 QByteArray Network::codecForEncoding() const
437 if (_codecForEncoding)
438 return _codecForEncoding->name();
443 void Network::setCodecForEncoding(const QByteArray &name)
445 setCodecForEncoding(QTextCodec::codecForName(name));
449 void Network::setCodecForEncoding(QTextCodec *codec)
451 _codecForEncoding = codec;
452 QByteArray codecName = codecForEncoding();
453 SYNC_OTHER(setCodecForEncoding, ARG(codecName))
454 emit configChanged();
458 QByteArray Network::codecForDecoding() const
460 if (_codecForDecoding)
461 return _codecForDecoding->name();
462 else return QByteArray();
466 void Network::setCodecForDecoding(const QByteArray &name)
468 setCodecForDecoding(QTextCodec::codecForName(name));
472 void Network::setCodecForDecoding(QTextCodec *codec)
474 _codecForDecoding = codec;
475 QByteArray codecName = codecForDecoding();
476 SYNC_OTHER(setCodecForDecoding, ARG(codecName))
477 emit configChanged();
481 // FIXME use server encoding if appropriate
482 QString Network::decodeString(const QByteArray &text) const
484 if (_codecForDecoding)
485 return ::decodeString(text, _codecForDecoding);
486 else return ::decodeString(text, _defaultCodecForDecoding);
490 QByteArray Network::encodeString(const QString &string) const
492 if (_codecForEncoding) {
493 return _codecForEncoding->fromUnicode(string);
495 if (_defaultCodecForEncoding) {
496 return _defaultCodecForEncoding->fromUnicode(string);
498 return string.toLatin1();
502 QString Network::decodeServerString(const QByteArray &text) const
505 return ::decodeString(text, _codecForServer);
507 return ::decodeString(text, _defaultCodecForServer);
511 QByteArray Network::encodeServerString(const QString &string) const
513 if (_codecForServer) {
514 return _codecForServer->fromUnicode(string);
516 if (_defaultCodecForServer) {
517 return _defaultCodecForServer->fromUnicode(string);
519 return string.toLatin1();
523 // ====================
525 // ====================
526 void Network::setNetworkName(const QString &networkName)
528 _networkName = networkName;
529 SYNC(ARG(networkName))
530 emit networkNameSet(networkName);
531 emit configChanged();
535 void Network::setCurrentServer(const QString ¤tServer)
537 _currentServer = currentServer;
538 SYNC(ARG(currentServer))
539 emit currentServerSet(currentServer);
543 void Network::setConnected(bool connected)
545 if (_connected == connected)
548 _connected = connected;
550 setMyNick(QString());
551 setCurrentServer(QString());
552 removeChansAndUsers();
555 emit connectedSet(connected);
559 //void Network::setConnectionState(ConnectionState state) {
560 void Network::setConnectionState(int state)
562 _connectionState = (ConnectionState)state;
563 //qDebug() << "netstate" << networkId() << networkName() << state;
565 emit connectionStateSet(_connectionState);
569 void Network::setMyNick(const QString &nickname)
572 if (!_myNick.isEmpty() && !ircUser(myNick())) {
573 newIrcUser(myNick());
576 emit myNickSet(nickname);
580 void Network::setLatency(int latency)
582 if (_latency == latency)
589 void Network::setIdentity(IdentityId id)
593 emit identitySet(id);
594 emit configChanged();
598 void Network::setServerList(const QVariantList &serverList)
600 _serverList = fromVariantList<Server>(serverList);
601 SYNC(ARG(serverList))
602 emit configChanged();
606 void Network::setUseRandomServer(bool use)
608 _useRandomServer = use;
610 emit configChanged();
614 void Network::setPerform(const QStringList &perform)
618 emit configChanged();
622 void Network::setUseAutoIdentify(bool use)
624 _useAutoIdentify = use;
626 emit configChanged();
630 void Network::setAutoIdentifyService(const QString &service)
632 _autoIdentifyService = service;
634 emit configChanged();
638 void Network::setAutoIdentifyPassword(const QString &password)
640 _autoIdentifyPassword = password;
642 emit configChanged();
646 void Network::setUseSasl(bool use)
650 emit configChanged();
654 void Network::setSaslAccount(const QString &account)
656 _saslAccount = account;
658 emit configChanged();
662 void Network::setSaslPassword(const QString &password)
664 _saslPassword = password;
666 emit configChanged();
670 void Network::setUseAutoReconnect(bool use)
672 _useAutoReconnect = use;
674 emit configChanged();
678 void Network::setAutoReconnectInterval(quint32 interval)
680 _autoReconnectInterval = interval;
682 emit configChanged();
686 void Network::setAutoReconnectRetries(quint16 retries)
688 _autoReconnectRetries = retries;
690 emit configChanged();
694 void Network::setUnlimitedReconnectRetries(bool unlimited)
696 _unlimitedReconnectRetries = unlimited;
698 emit configChanged();
702 void Network::setRejoinChannels(bool rejoin)
704 _rejoinChannels = rejoin;
706 emit configChanged();
710 void Network::setUseCustomMessageRate(bool useCustomRate)
712 if (_useCustomMessageRate != useCustomRate) {
713 _useCustomMessageRate = useCustomRate;
714 SYNC(ARG(useCustomRate))
715 emit configChanged();
716 emit useCustomMessageRateSet(_useCustomMessageRate);
721 void Network::setMessageRateBurstSize(quint32 burstSize)
724 // Can't go slower than one message at a time. Also blocks old clients from trying to set
726 qWarning() << "Received invalid setMessageRateBurstSize data - message burst size must be "
727 "non-zero positive, given" << burstSize;
730 if (_messageRateBurstSize != burstSize) {
731 _messageRateBurstSize = burstSize;
733 emit configChanged();
734 emit messageRateBurstSizeSet(_messageRateBurstSize);
739 void Network::setMessageRateDelay(quint32 messageDelay)
741 if (messageDelay == 0) {
742 // Nonsensical to have no delay - just check the Unlimited box instead. Also blocks old
743 // clients from trying to set this to 0.
744 qWarning() << "Received invalid setMessageRateDelay data - message delay must be non-zero "
745 "positive, given" << messageDelay;
748 if (_messageRateDelay != messageDelay) {
749 _messageRateDelay = messageDelay;
750 SYNC(ARG(messageDelay))
751 emit configChanged();
752 emit messageRateDelaySet(_messageRateDelay);
757 void Network::setUnlimitedMessageRate(bool unlimitedRate)
759 if (_unlimitedMessageRate != unlimitedRate) {
760 _unlimitedMessageRate = unlimitedRate;
761 SYNC(ARG(unlimitedRate))
762 emit configChanged();
763 emit unlimitedMessageRateSet(_unlimitedMessageRate);
768 void Network::addSupport(const QString ¶m, const QString &value)
770 if (!_supports.contains(param)) {
771 _supports[param] = value;
772 SYNC(ARG(param), ARG(value))
777 void Network::removeSupport(const QString ¶m)
779 if (_supports.contains(param)) {
780 _supports.remove(param);
786 QVariantMap Network::initSupports() const
788 QVariantMap supports;
789 QHashIterator<QString, QString> iter(_supports);
790 while (iter.hasNext()) {
792 supports[iter.key()] = iter.value();
797 void Network::addCap(const QString &capability, const QString &value)
799 // IRCv3 specs all use lowercase capability names
800 QString _capLowercase = capability.toLower();
801 if (!_caps.contains(_capLowercase)) {
802 _caps[_capLowercase] = value;
803 SYNC(ARG(capability), ARG(value))
804 emit capAdded(_capLowercase);
808 void Network::acknowledgeCap(const QString &capability)
810 // IRCv3 specs all use lowercase capability names
811 QString _capLowercase = capability.toLower();
812 if (!_capsEnabled.contains(_capLowercase)) {
813 _capsEnabled.append(_capLowercase);
814 SYNC(ARG(capability))
815 emit capAcknowledged(_capLowercase);
819 void Network::removeCap(const QString &capability)
821 // IRCv3 specs all use lowercase capability names
822 QString _capLowercase = capability.toLower();
823 if (_caps.contains(_capLowercase)) {
824 // Remove from the list of available capabilities.
825 _caps.remove(_capLowercase);
826 // Remove it from the acknowledged list if it was previously acknowledged. The SYNC call
827 // ensures this propogates to the other side.
828 // Use removeOne() for speed; no more than one due to contains() check in acknowledgeCap().
829 _capsEnabled.removeOne(_capLowercase);
830 SYNC(ARG(capability))
831 emit capRemoved(_capLowercase);
835 void Network::clearCaps()
837 // IRCv3 specs all use lowercase capability names
838 if (_caps.empty() && _capsEnabled.empty()) {
839 // Avoid the sync call if there's nothing to clear (e.g. failed reconnects)
842 // To ease core-side configuration, loop through the list and emit capRemoved for each entry.
843 // If performance issues arise, this can be converted to a more-efficient setup without breaking
844 // protocol (in theory).
845 QString _capLowercase;
846 foreach (const QString &capability, _caps) {
847 _capLowercase = capability.toLower();
848 emit capRemoved(_capLowercase);
850 // Clear capabilities from the stored list
852 _capsEnabled.clear();
857 QVariantMap Network::initCaps() const
860 QHashIterator<QString, QString> iter(_caps);
861 while (iter.hasNext()) {
863 caps[iter.key()] = iter.value();
869 // There's potentially a lot of users and channels, so it makes sense to optimize the format of this.
870 // Rather than sending a thousand maps with identical keys, we convert this into one map containing lists
871 // where each list index corresponds to a particular IrcUser. This saves sending the key names a thousand times.
872 // Benchmarks have shown space savings of around 56%, resulting in saving several MBs worth of data on sync
873 // (without compression) with a decent amount of IrcUsers.
874 QVariantMap Network::initIrcUsersAndChannels() const
876 QVariantMap usersAndChannels;
878 if (_ircUsers.count()) {
879 QHash<QString, QVariantList> users;
880 QHash<QString, IrcUser *>::const_iterator it = _ircUsers.begin();
881 QHash<QString, IrcUser *>::const_iterator end = _ircUsers.end();
883 const QVariantMap &map = it.value()->toVariantMap();
884 QVariantMap::const_iterator mapiter = map.begin();
885 while (mapiter != map.end()) {
886 users[mapiter.key()] << mapiter.value();
891 // Can't have a container with a value type != QVariant in a QVariant :(
892 // However, working directly on a QVariantMap is awkward for appending, thus the detour via the hash above.
894 foreach(const QString &key, users.keys())
895 userMap[key] = users[key];
896 usersAndChannels["Users"] = userMap;
899 if (_ircChannels.count()) {
900 QHash<QString, QVariantList> channels;
901 QHash<QString, IrcChannel *>::const_iterator it = _ircChannels.begin();
902 QHash<QString, IrcChannel *>::const_iterator end = _ircChannels.end();
904 const QVariantMap &map = it.value()->toVariantMap();
905 QVariantMap::const_iterator mapiter = map.begin();
906 while (mapiter != map.end()) {
907 channels[mapiter.key()] << mapiter.value();
912 QVariantMap channelMap;
913 foreach(const QString &key, channels.keys())
914 channelMap[key] = channels[key];
915 usersAndChannels["Channels"] = channelMap;
918 return usersAndChannels;
922 void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels)
925 if (isInitialized()) {
926 qWarning() << "Network" << networkId() << "received init data for users and channels although there already are known users or channels!";
930 // toMap() and toList() are cheap, so we can avoid copying to lists...
931 // However, we really have to make sure to never accidentally detach from the shared data!
933 const QVariantMap &users = usersAndChannels["Users"].toMap();
936 int count = users["nick"].toList().count();
937 foreach(const QString &key, users.keys()) {
938 if (users[key].toList().count() != count) {
939 qWarning() << "Received invalid usersAndChannels init data, sizes of attribute lists don't match!";
944 // now create the individual IrcUsers
945 for(int i = 0; i < count; i++) {
947 foreach(const QString &key, users.keys())
948 map[key] = users[key].toList().at(i);
949 newIrcUser(map["nick"].toString(), map); // newIrcUser() properly handles the hostmask being just the nick
952 // same thing for IrcChannels
953 const QVariantMap &channels = usersAndChannels["Channels"].toMap();
956 count = channels["name"].toList().count();
957 foreach(const QString &key, channels.keys()) {
958 if (channels[key].toList().count() != count) {
959 qWarning() << "Received invalid usersAndChannels init data, sizes of attribute lists don't match!";
963 // now create the individual IrcChannels
964 for(int i = 0; i < count; i++) {
966 foreach(const QString &key, channels.keys())
967 map[key] = channels[key].toList().at(i);
968 newIrcChannel(map["name"].toString(), map);
973 void Network::initSetSupports(const QVariantMap &supports)
975 QMapIterator<QString, QVariant> iter(supports);
976 while (iter.hasNext()) {
978 addSupport(iter.key(), iter.value().toString());
983 void Network::initSetCaps(const QVariantMap &caps)
985 QMapIterator<QString, QVariant> iter(caps);
986 while (iter.hasNext()) {
988 addCap(iter.key(), iter.value().toString());
993 IrcUser *Network::updateNickFromMask(const QString &mask)
995 QString nick(nickFromMask(mask).toLower());
998 if (_ircUsers.contains(nick)) {
999 ircuser = _ircUsers[nick];
1000 ircuser->updateHostmask(mask);
1003 ircuser = newIrcUser(mask);
1009 void Network::ircUserNickChanged(QString newnick)
1011 QString oldnick = _ircUsers.key(qobject_cast<IrcUser *>(sender()));
1013 if (oldnick.isNull())
1016 if (newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
1018 if (myNick().toLower() == oldnick)
1023 void Network::emitConnectionError(const QString &errorMsg)
1025 emit connectionError(errorMsg);
1029 // ====================
1031 // ====================
1032 void Network::determinePrefixes() const
1034 // seems like we have to construct them first
1035 QString prefix = support("PREFIX");
1037 if (prefix.startsWith("(") && prefix.contains(")")) {
1038 _prefixes = prefix.section(")", 1);
1039 _prefixModes = prefix.mid(1).section(")", 0, 0);
1042 QString defaultPrefixes("~&@%+");
1043 QString defaultPrefixModes("qaohv");
1045 if (prefix.isEmpty()) {
1046 _prefixes = defaultPrefixes;
1047 _prefixModes = defaultPrefixModes;
1050 // clear the existing modes, just in case we're run multiple times
1051 _prefixes = QString();
1052 _prefixModes = QString();
1054 // we just assume that in PREFIX are only prefix chars stored
1055 for (int i = 0; i < defaultPrefixes.size(); i++) {
1056 if (prefix.contains(defaultPrefixes[i])) {
1057 _prefixes += defaultPrefixes[i];
1058 _prefixModes += defaultPrefixModes[i];
1061 // check for success
1062 if (!_prefixes.isNull())
1065 // well... our assumption was obviously wrong...
1066 // check if it's only prefix modes
1067 for (int i = 0; i < defaultPrefixes.size(); i++) {
1068 if (prefix.contains(defaultPrefixModes[i])) {
1069 _prefixes += defaultPrefixes[i];
1070 _prefixModes += defaultPrefixModes[i];
1073 // now we've done all we've could...
1078 /************************************************************************
1080 ************************************************************************/
1082 NetworkInfo::NetworkInfo()
1085 useRandomServer(false),
1086 useAutoIdentify(false),
1087 autoIdentifyService("NickServ"),
1089 useAutoReconnect(true),
1090 autoReconnectInterval(60),
1091 autoReconnectRetries(20),
1092 unlimitedReconnectRetries(false),
1093 rejoinChannels(true),
1094 useCustomMessageRate(false),
1095 messageRateBurstSize(5),
1096 messageRateDelay(2200),
1097 unlimitedMessageRate(false)
1102 bool NetworkInfo::operator==(const NetworkInfo &other) const
1104 if (networkId != other.networkId) return false;
1105 if (networkName != other.networkName) return false;
1106 if (identity != other.identity) return false;
1107 if (codecForServer != other.codecForServer) return false;
1108 if (codecForEncoding != other.codecForEncoding) return false;
1109 if (codecForDecoding != other.codecForDecoding) return false;
1110 if (serverList != other.serverList) return false;
1111 if (useRandomServer != other.useRandomServer) return false;
1112 if (perform != other.perform) return false;
1113 if (useAutoIdentify != other.useAutoIdentify) return false;
1114 if (autoIdentifyService != other.autoIdentifyService) return false;
1115 if (autoIdentifyPassword != other.autoIdentifyPassword) return false;
1116 if (useSasl != other.useSasl) return false;
1117 if (saslAccount != other.saslAccount) return false;
1118 if (saslPassword != other.saslPassword) return false;
1119 if (useAutoReconnect != other.useAutoReconnect) return false;
1120 if (autoReconnectInterval != other.autoReconnectInterval) return false;
1121 if (autoReconnectRetries != other.autoReconnectRetries) return false;
1122 if (unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
1123 if (rejoinChannels != other.rejoinChannels) return false;
1124 // Custom rate limiting
1125 if (useCustomMessageRate != other.useCustomMessageRate) return false;
1126 if (messageRateBurstSize != other.messageRateBurstSize) return false;
1127 if (messageRateDelay != other.messageRateDelay) return false;
1128 if (unlimitedMessageRate != other.unlimitedMessageRate) return false;
1133 bool NetworkInfo::operator!=(const NetworkInfo &other) const
1135 return !(*this == other);
1139 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info)
1142 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
1143 i["NetworkName"] = info.networkName;
1144 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
1145 i["CodecForServer"] = info.codecForServer;
1146 i["CodecForEncoding"] = info.codecForEncoding;
1147 i["CodecForDecoding"] = info.codecForDecoding;
1148 i["ServerList"] = toVariantList(info.serverList);
1149 i["UseRandomServer"] = info.useRandomServer;
1150 i["Perform"] = info.perform;
1151 i["UseAutoIdentify"] = info.useAutoIdentify;
1152 i["AutoIdentifyService"] = info.autoIdentifyService;
1153 i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
1154 i["UseSasl"] = info.useSasl;
1155 i["SaslAccount"] = info.saslAccount;
1156 i["SaslPassword"] = info.saslPassword;
1157 i["UseAutoReconnect"] = info.useAutoReconnect;
1158 i["AutoReconnectInterval"] = info.autoReconnectInterval;
1159 i["AutoReconnectRetries"] = info.autoReconnectRetries;
1160 i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
1161 i["RejoinChannels"] = info.rejoinChannels;
1162 // Custom rate limiting
1163 i["UseCustomMessageRate"] = info.useCustomMessageRate;
1164 i["MessageRateBurstSize"] = info.messageRateBurstSize;
1165 i["MessageRateDelay"] = info.messageRateDelay;
1166 i["UnlimitedMessageRate"] = info.unlimitedMessageRate;
1172 QDataStream &operator>>(QDataStream &in, NetworkInfo &info)
1176 info.networkId = i["NetworkId"].value<NetworkId>();
1177 info.networkName = i["NetworkName"].toString();
1178 info.identity = i["Identity"].value<IdentityId>();
1179 info.codecForServer = i["CodecForServer"].toByteArray();
1180 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
1181 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
1182 info.serverList = fromVariantList<Network::Server>(i["ServerList"].toList());
1183 info.useRandomServer = i["UseRandomServer"].toBool();
1184 info.perform = i["Perform"].toStringList();
1185 info.useAutoIdentify = i["UseAutoIdentify"].toBool();
1186 info.autoIdentifyService = i["AutoIdentifyService"].toString();
1187 info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
1188 info.useSasl = i["UseSasl"].toBool();
1189 info.saslAccount = i["SaslAccount"].toString();
1190 info.saslPassword = i["SaslPassword"].toString();
1191 info.useAutoReconnect = i["UseAutoReconnect"].toBool();
1192 info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
1193 info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
1194 info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
1195 info.rejoinChannels = i["RejoinChannels"].toBool();
1196 // Custom rate limiting
1197 info.useCustomMessageRate = i["UseCustomMessageRate"].toBool();
1198 info.messageRateBurstSize = i["MessageRateBurstSize"].toUInt();
1199 info.messageRateDelay = i["MessageRateDelay"].toUInt();
1200 info.unlimitedMessageRate = i["UnlimitedMessageRate"].toBool();
1205 QDebug operator<<(QDebug dbg, const NetworkInfo &i)
1207 dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
1208 << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
1209 << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
1210 << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
1211 << " useSasl = " << i.useSasl << " saslAccount = " << i.saslAccount << " saslPassword = " << i.saslPassword
1212 << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
1213 << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
1214 << " rejoinChannels = " << i.rejoinChannels
1215 << " useCustomMessageRate = " << i.useCustomMessageRate
1216 << " messageRateBurstSize = " << i.messageRateBurstSize
1217 << " messageRateDelay = " << i.messageRateDelay
1218 << " unlimitedMessageRate = " << i.unlimitedMessageRate
1224 QDataStream &operator<<(QDataStream &out, const Network::Server &server)
1226 QVariantMap serverMap;
1227 serverMap["Host"] = server.host;
1228 serverMap["Port"] = server.port;
1229 serverMap["Password"] = server.password;
1230 serverMap["UseSSL"] = server.useSsl;
1231 serverMap["sslVerify"] = server.sslVerify;
1232 serverMap["sslVersion"] = server.sslVersion;
1233 serverMap["UseProxy"] = server.useProxy;
1234 serverMap["ProxyType"] = server.proxyType;
1235 serverMap["ProxyHost"] = server.proxyHost;
1236 serverMap["ProxyPort"] = server.proxyPort;
1237 serverMap["ProxyUser"] = server.proxyUser;
1238 serverMap["ProxyPass"] = server.proxyPass;
1244 QDataStream &operator>>(QDataStream &in, Network::Server &server)
1246 QVariantMap serverMap;
1248 server.host = serverMap["Host"].toString();
1249 server.port = serverMap["Port"].toUInt();
1250 server.password = serverMap["Password"].toString();
1251 server.useSsl = serverMap["UseSSL"].toBool();
1252 server.sslVerify = serverMap["sslVerify"].toBool();
1253 server.sslVersion = serverMap["sslVersion"].toInt();
1254 server.useProxy = serverMap["UseProxy"].toBool();
1255 server.proxyType = serverMap["ProxyType"].toInt();
1256 server.proxyHost = serverMap["ProxyHost"].toString();
1257 server.proxyPort = serverMap["ProxyPort"].toUInt();
1258 server.proxyUser = serverMap["ProxyUser"].toString();
1259 server.proxyPass = serverMap["ProxyPass"].toString();
1264 bool Network::Server::operator==(const Server &other) const
1266 if (host != other.host) return false;
1267 if (port != other.port) return false;
1268 if (password != other.password) return false;
1269 if (useSsl != other.useSsl) return false;
1270 if (sslVerify != other.sslVerify) return false;
1271 if (sslVersion != other.sslVersion) return false;
1272 if (useProxy != other.useProxy) return false;
1273 if (proxyType != other.proxyType) return false;
1274 if (proxyHost != other.proxyHost) return false;
1275 if (proxyPort != other.proxyPort) return false;
1276 if (proxyUser != other.proxyUser) return false;
1277 if (proxyPass != other.proxyPass) return false;
1282 bool Network::Server::operator!=(const Server &other) const
1284 return !(*this == other);
1288 QDebug operator<<(QDebug dbg, const Network::Server &server)
1290 dbg.nospace() << "Server(host = " << server.host << ":" << server.port << ", useSsl = " <<
1291 server.useSsl << ", sslVerify = " << server.sslVerify << ")";