1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 ***************************************************************************/
26 QTextCodec *Network::_defaultCodecForServer = 0;
27 QTextCodec *Network::_defaultCodecForEncoding = 0;
28 QTextCodec *Network::_defaultCodecForDecoding = 0;
30 // ====================
32 // ====================
33 INIT_SYNCABLE_OBJECT(Network)
34 Network::Network(const NetworkId &networkid, QObject *parent)
35 : SyncableObject(parent),
37 _networkId(networkid),
41 _networkName(QString("<not initialized>")),
42 _currentServer(QString()),
44 _connectionState(Disconnected),
46 _prefixModes(QString()),
47 _useRandomServer(false),
48 _useAutoIdentify(false),
50 _useAutoReconnect(false),
51 _autoReconnectInterval(60),
52 _autoReconnectRetries(10),
53 _unlimitedReconnectRetries(false),
54 _useCustomMessageRate(false),
55 _messageRateBurstSize(5),
56 _messageRateDelay(2200),
57 _unlimitedMessageRate(false),
61 _autoAwayActive(false)
63 setObjectName(QString::number(networkid.toInt()));
69 emit aboutToBeDestroyed();
73 bool Network::isChannelName(const QString &channelname) const
75 if (channelname.isEmpty())
78 if (supports("CHANTYPES"))
79 return support("CHANTYPES").contains(channelname[0]);
81 return QString("#&!+").contains(channelname[0]);
85 bool Network::isStatusMsg(const QString &target) const
90 if (supports("STATUSMSG"))
91 return support("STATUSMSG").contains(target[0]);
93 return QString("@+").contains(target[0]);
97 NetworkInfo Network::networkInfo() const
100 info.networkName = networkName();
101 info.networkId = networkId();
102 info.identity = identity();
103 info.codecForServer = codecForServer();
104 info.codecForEncoding = codecForEncoding();
105 info.codecForDecoding = codecForDecoding();
106 info.serverList = serverList();
107 info.useRandomServer = useRandomServer();
108 info.perform = perform();
109 info.useAutoIdentify = useAutoIdentify();
110 info.autoIdentifyService = autoIdentifyService();
111 info.autoIdentifyPassword = autoIdentifyPassword();
112 info.useSasl = useSasl();
113 info.saslAccount = saslAccount();
114 info.saslPassword = saslPassword();
115 info.useAutoReconnect = useAutoReconnect();
116 info.autoReconnectInterval = autoReconnectInterval();
117 info.autoReconnectRetries = autoReconnectRetries();
118 info.unlimitedReconnectRetries = unlimitedReconnectRetries();
119 info.rejoinChannels = rejoinChannels();
120 info.useCustomMessageRate = useCustomMessageRate();
121 info.messageRateBurstSize = messageRateBurstSize();
122 info.messageRateDelay = messageRateDelay();
123 info.unlimitedMessageRate = unlimitedMessageRate();
128 void Network::setNetworkInfo(const NetworkInfo &info)
130 // we don't set our ID!
131 if (!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
132 if (info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
133 if (info.codecForServer != codecForServer()) setCodecForServer(QTextCodec::codecForName(info.codecForServer));
134 if (info.codecForEncoding != codecForEncoding()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
135 if (info.codecForDecoding != codecForDecoding()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
136 if (info.serverList.count()) setServerList(toVariantList(info.serverList)); // FIXME compare components
137 if (info.useRandomServer != useRandomServer()) setUseRandomServer(info.useRandomServer);
138 if (info.perform != perform()) setPerform(info.perform);
139 if (info.useAutoIdentify != useAutoIdentify()) setUseAutoIdentify(info.useAutoIdentify);
140 if (info.autoIdentifyService != autoIdentifyService()) setAutoIdentifyService(info.autoIdentifyService);
141 if (info.autoIdentifyPassword != autoIdentifyPassword()) setAutoIdentifyPassword(info.autoIdentifyPassword);
142 if (info.useSasl != useSasl()) setUseSasl(info.useSasl);
143 if (info.saslAccount != saslAccount()) setSaslAccount(info.saslAccount);
144 if (info.saslPassword != saslPassword()) setSaslPassword(info.saslPassword);
145 if (info.useAutoReconnect != useAutoReconnect()) setUseAutoReconnect(info.useAutoReconnect);
146 if (info.autoReconnectInterval != autoReconnectInterval()) setAutoReconnectInterval(info.autoReconnectInterval);
147 if (info.autoReconnectRetries != autoReconnectRetries()) setAutoReconnectRetries(info.autoReconnectRetries);
148 if (info.unlimitedReconnectRetries != unlimitedReconnectRetries()) setUnlimitedReconnectRetries(info.unlimitedReconnectRetries);
149 if (info.rejoinChannels != rejoinChannels()) setRejoinChannels(info.rejoinChannels);
150 // Custom rate limiting
151 if (info.useCustomMessageRate != useCustomMessageRate())
152 setUseCustomMessageRate(info.useCustomMessageRate);
153 if (info.messageRateBurstSize != messageRateBurstSize())
154 setMessageRateBurstSize(info.messageRateBurstSize);
155 if (info.messageRateDelay != messageRateDelay())
156 setMessageRateDelay(info.messageRateDelay);
157 if (info.unlimitedMessageRate != unlimitedMessageRate())
158 setUnlimitedMessageRate(info.unlimitedMessageRate);
162 QString Network::prefixToMode(const QString &prefix) const
164 if (prefixes().contains(prefix))
165 return QString(prefixModes()[prefixes().indexOf(prefix)]);
171 QString Network::modeToPrefix(const QString &mode) const
173 if (prefixModes().contains(mode))
174 return QString(prefixes()[prefixModes().indexOf(mode)]);
180 QStringList Network::nicks() const
182 // we don't use _ircUsers.keys() since the keys may be
183 // not up to date after a nick change
185 foreach(IrcUser *ircuser, _ircUsers.values()) {
186 nicks << ircuser->nick();
192 QString Network::prefixes() const
194 if (_prefixes.isNull())
201 QString Network::prefixModes() const
203 if (_prefixModes.isNull())
210 // example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG
211 Network::ChannelModeType Network::channelModeType(const QString &mode)
214 return NOT_A_CHANMODE;
216 QString chanmodes = support("CHANMODES");
217 if (chanmodes.isEmpty())
218 return NOT_A_CHANMODE;
220 ChannelModeType modeType = A_CHANMODE;
221 for (int i = 0; i < chanmodes.count(); i++) {
222 if (chanmodes[i] == mode[0])
224 else if (chanmodes[i] == ',')
225 modeType = (ChannelModeType)(modeType << 1);
227 if (modeType > D_CHANMODE) {
228 qWarning() << "Network" << networkId() << "supplied invalid CHANMODES:" << chanmodes;
229 modeType = NOT_A_CHANMODE;
235 QString Network::support(const QString ¶m) const
237 QString support_ = param.toUpper();
238 if (_supports.contains(support_))
239 return _supports[support_];
245 bool Network::saslMaybeSupports(const QString &saslMechanism) const
247 if (!capAvailable(IrcCap::SASL)) {
248 // If SASL's not advertised at all, it's likely the mechanism isn't supported, as per specs.
249 // Unfortunately, we don't know for sure, but Quassel won't request SASL without it being
250 // advertised, anyways.
251 // This may also occur if the network's disconnected or negotiation hasn't yet happened.
255 // Get the SASL capability value
256 QString saslCapValue = capValue(IrcCap::SASL);
257 // SASL mechanisms are only specified in capability values as part of SASL 3.2. In SASL 3.1,
258 // it's handled differently. If we don't know via capability value, assume it's supported to
259 // reduce the risk of breaking existing setups.
260 // See: http://ircv3.net/specs/extensions/sasl-3.1.html
261 // And: http://ircv3.net/specs/extensions/sasl-3.2.html
262 return (saslCapValue.length() == 0)
263 || (saslCapValue.contains(saslMechanism, Qt::CaseInsensitive));
267 IrcUser *Network::newIrcUser(const QString &hostmask, const QVariantMap &initData)
269 QString nick(nickFromMask(hostmask).toLower());
270 if (!_ircUsers.contains(nick)) {
271 IrcUser *ircuser = ircUserFactory(hostmask);
272 if (!initData.isEmpty()) {
273 ircuser->fromVariantMap(initData);
274 ircuser->setInitialized();
278 proxy()->synchronize(ircuser);
280 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
282 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
284 _ircUsers[nick] = ircuser;
286 // This method will be called with a nick instead of hostmask by setInitIrcUsersAndChannels().
287 // Not a problem because initData contains all we need; however, making sure here to get the real
288 // hostmask out of the IrcUser afterwards.
289 QString mask = ircuser->hostmask();
290 SYNC_OTHER(addIrcUser, ARG(mask));
291 // emit ircUserAdded(mask);
292 emit ircUserAdded(ircuser);
295 return _ircUsers[nick];
299 IrcUser *Network::ircUser(QString nickname) const
301 nickname = nickname.toLower();
302 if (_ircUsers.contains(nickname))
303 return _ircUsers[nickname];
309 void Network::removeIrcUser(IrcUser *ircuser)
311 QString nick = _ircUsers.key(ircuser);
315 _ircUsers.remove(nick);
316 disconnect(ircuser, 0, this, 0);
317 ircuser->deleteLater();
321 void Network::removeIrcChannel(IrcChannel *channel)
323 QString chanName = _ircChannels.key(channel);
324 if (chanName.isNull())
327 _ircChannels.remove(chanName);
328 disconnect(channel, 0, this, 0);
329 channel->deleteLater();
333 void Network::removeChansAndUsers()
335 QList<IrcUser *> users = ircUsers();
337 QList<IrcChannel *> channels = ircChannels();
338 _ircChannels.clear();
341 qDeleteAll(channels);
345 IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap &initData)
347 if (!_ircChannels.contains(channelname.toLower())) {
348 IrcChannel *channel = ircChannelFactory(channelname);
349 if (!initData.isEmpty()) {
350 channel->fromVariantMap(initData);
351 channel->setInitialized();
355 proxy()->synchronize(channel);
357 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
359 _ircChannels[channelname.toLower()] = channel;
361 SYNC_OTHER(addIrcChannel, ARG(channelname))
362 // emit ircChannelAdded(channelname);
363 emit ircChannelAdded(channel);
365 return _ircChannels[channelname.toLower()];
369 IrcChannel *Network::ircChannel(QString channelname) const
371 channelname = channelname.toLower();
372 if (_ircChannels.contains(channelname))
373 return _ircChannels[channelname];
379 QByteArray Network::defaultCodecForServer()
381 if (_defaultCodecForServer)
382 return _defaultCodecForServer->name();
387 void Network::setDefaultCodecForServer(const QByteArray &name)
389 _defaultCodecForServer = QTextCodec::codecForName(name);
393 QByteArray Network::defaultCodecForEncoding()
395 if (_defaultCodecForEncoding)
396 return _defaultCodecForEncoding->name();
401 void Network::setDefaultCodecForEncoding(const QByteArray &name)
403 _defaultCodecForEncoding = QTextCodec::codecForName(name);
407 QByteArray Network::defaultCodecForDecoding()
409 if (_defaultCodecForDecoding)
410 return _defaultCodecForDecoding->name();
415 void Network::setDefaultCodecForDecoding(const QByteArray &name)
417 _defaultCodecForDecoding = QTextCodec::codecForName(name);
421 QByteArray Network::codecForServer() const
424 return _codecForServer->name();
429 void Network::setCodecForServer(const QByteArray &name)
431 setCodecForServer(QTextCodec::codecForName(name));
435 void Network::setCodecForServer(QTextCodec *codec)
437 _codecForServer = codec;
438 QByteArray codecName = codecForServer();
439 SYNC_OTHER(setCodecForServer, ARG(codecName))
440 emit configChanged();
444 QByteArray Network::codecForEncoding() const
446 if (_codecForEncoding)
447 return _codecForEncoding->name();
452 void Network::setCodecForEncoding(const QByteArray &name)
454 setCodecForEncoding(QTextCodec::codecForName(name));
458 void Network::setCodecForEncoding(QTextCodec *codec)
460 _codecForEncoding = codec;
461 QByteArray codecName = codecForEncoding();
462 SYNC_OTHER(setCodecForEncoding, ARG(codecName))
463 emit configChanged();
467 QByteArray Network::codecForDecoding() const
469 if (_codecForDecoding)
470 return _codecForDecoding->name();
471 else return QByteArray();
475 void Network::setCodecForDecoding(const QByteArray &name)
477 setCodecForDecoding(QTextCodec::codecForName(name));
481 void Network::setCodecForDecoding(QTextCodec *codec)
483 _codecForDecoding = codec;
484 QByteArray codecName = codecForDecoding();
485 SYNC_OTHER(setCodecForDecoding, ARG(codecName))
486 emit configChanged();
490 // FIXME use server encoding if appropriate
491 QString Network::decodeString(const QByteArray &text) const
493 if (_codecForDecoding)
494 return ::decodeString(text, _codecForDecoding);
495 else return ::decodeString(text, _defaultCodecForDecoding);
499 QByteArray Network::encodeString(const QString &string) const
501 if (_codecForEncoding) {
502 return _codecForEncoding->fromUnicode(string);
504 if (_defaultCodecForEncoding) {
505 return _defaultCodecForEncoding->fromUnicode(string);
507 return string.toLatin1();
511 QString Network::decodeServerString(const QByteArray &text) const
514 return ::decodeString(text, _codecForServer);
516 return ::decodeString(text, _defaultCodecForServer);
520 QByteArray Network::encodeServerString(const QString &string) const
522 if (_codecForServer) {
523 return _codecForServer->fromUnicode(string);
525 if (_defaultCodecForServer) {
526 return _defaultCodecForServer->fromUnicode(string);
528 return string.toLatin1();
532 // ====================
534 // ====================
535 void Network::setNetworkName(const QString &networkName)
537 _networkName = networkName;
538 SYNC(ARG(networkName))
539 emit networkNameSet(networkName);
540 emit configChanged();
544 void Network::setCurrentServer(const QString ¤tServer)
546 _currentServer = currentServer;
547 SYNC(ARG(currentServer))
548 emit currentServerSet(currentServer);
552 void Network::setConnected(bool connected)
554 if (_connected == connected)
557 _connected = connected;
559 setMyNick(QString());
560 setCurrentServer(QString());
561 removeChansAndUsers();
564 emit connectedSet(connected);
568 //void Network::setConnectionState(ConnectionState state) {
569 void Network::setConnectionState(int state)
571 _connectionState = (ConnectionState)state;
572 //qDebug() << "netstate" << networkId() << networkName() << state;
574 emit connectionStateSet(_connectionState);
578 void Network::setMyNick(const QString &nickname)
581 if (!_myNick.isEmpty() && !ircUser(myNick())) {
582 newIrcUser(myNick());
585 emit myNickSet(nickname);
589 void Network::setLatency(int latency)
591 if (_latency == latency)
598 void Network::setIdentity(IdentityId id)
602 emit identitySet(id);
603 emit configChanged();
607 void Network::setServerList(const QVariantList &serverList)
609 _serverList = fromVariantList<Server>(serverList);
610 SYNC(ARG(serverList))
611 emit configChanged();
615 void Network::setUseRandomServer(bool use)
617 _useRandomServer = use;
619 emit configChanged();
623 void Network::setPerform(const QStringList &perform)
627 emit configChanged();
631 void Network::setUseAutoIdentify(bool use)
633 _useAutoIdentify = use;
635 emit configChanged();
639 void Network::setAutoIdentifyService(const QString &service)
641 _autoIdentifyService = service;
643 emit configChanged();
647 void Network::setAutoIdentifyPassword(const QString &password)
649 _autoIdentifyPassword = password;
651 emit configChanged();
655 void Network::setUseSasl(bool use)
659 emit configChanged();
663 void Network::setSaslAccount(const QString &account)
665 _saslAccount = account;
667 emit configChanged();
671 void Network::setSaslPassword(const QString &password)
673 _saslPassword = password;
675 emit configChanged();
679 void Network::setUseAutoReconnect(bool use)
681 _useAutoReconnect = use;
683 emit configChanged();
687 void Network::setAutoReconnectInterval(quint32 interval)
689 _autoReconnectInterval = interval;
691 emit configChanged();
695 void Network::setAutoReconnectRetries(quint16 retries)
697 _autoReconnectRetries = retries;
699 emit configChanged();
703 void Network::setUnlimitedReconnectRetries(bool unlimited)
705 _unlimitedReconnectRetries = unlimited;
707 emit configChanged();
711 void Network::setRejoinChannels(bool rejoin)
713 _rejoinChannels = rejoin;
715 emit configChanged();
719 void Network::setUseCustomMessageRate(bool useCustomRate)
721 if (_useCustomMessageRate != useCustomRate) {
722 _useCustomMessageRate = useCustomRate;
723 SYNC(ARG(useCustomRate))
724 emit configChanged();
725 emit useCustomMessageRateSet(_useCustomMessageRate);
730 void Network::setMessageRateBurstSize(quint32 burstSize)
733 // Can't go slower than one message at a time. Also blocks old clients from trying to set
735 qDebug() << "Received invalid setMessageRateBurstSize data - message burst size must be "
736 "non-zero positive, given" << burstSize;
739 if (_messageRateBurstSize != burstSize) {
740 _messageRateBurstSize = burstSize;
742 emit configChanged();
743 emit messageRateBurstSizeSet(_messageRateBurstSize);
748 void Network::setMessageRateDelay(quint32 messageDelay)
750 if (messageDelay == 0) {
751 // Nonsensical to have no delay - just check the Unlimited box instead. Also blocks old
752 // clients from trying to set this to 0.
753 qDebug() << "Received invalid setMessageRateDelay data - message delay must be non-zero "
754 "positive, given" << messageDelay;
757 if (_messageRateDelay != messageDelay) {
758 _messageRateDelay = messageDelay;
759 SYNC(ARG(messageDelay))
760 emit configChanged();
761 emit messageRateDelaySet(_messageRateDelay);
766 void Network::setUnlimitedMessageRate(bool unlimitedRate)
768 if (_unlimitedMessageRate != unlimitedRate) {
769 _unlimitedMessageRate = unlimitedRate;
770 SYNC(ARG(unlimitedRate))
771 emit configChanged();
772 emit unlimitedMessageRateSet(_unlimitedMessageRate);
777 void Network::addSupport(const QString ¶m, const QString &value)
779 if (!_supports.contains(param)) {
780 _supports[param] = value;
781 SYNC(ARG(param), ARG(value))
786 void Network::removeSupport(const QString ¶m)
788 if (_supports.contains(param)) {
789 _supports.remove(param);
795 QVariantMap Network::initSupports() const
797 QVariantMap supports;
798 QHashIterator<QString, QString> iter(_supports);
799 while (iter.hasNext()) {
801 supports[iter.key()] = iter.value();
806 void Network::addCap(const QString &capability, const QString &value)
808 // IRCv3 specs all use lowercase capability names
809 QString _capLowercase = capability.toLower();
810 if (!_caps.contains(_capLowercase)) {
811 _caps[_capLowercase] = value;
812 SYNC(ARG(capability), ARG(value))
813 emit capAdded(_capLowercase);
817 void Network::acknowledgeCap(const QString &capability)
819 // IRCv3 specs all use lowercase capability names
820 QString _capLowercase = capability.toLower();
821 if (!_capsEnabled.contains(_capLowercase)) {
822 _capsEnabled.append(_capLowercase);
823 SYNC(ARG(capability))
824 emit capAcknowledged(_capLowercase);
828 void Network::removeCap(const QString &capability)
830 // IRCv3 specs all use lowercase capability names
831 QString _capLowercase = capability.toLower();
832 if (_caps.contains(_capLowercase)) {
833 // Remove from the list of available capabilities.
834 _caps.remove(_capLowercase);
835 // Remove it from the acknowledged list if it was previously acknowledged. The SYNC call
836 // ensures this propogates to the other side.
837 // Use removeOne() for speed; no more than one due to contains() check in acknowledgeCap().
838 _capsEnabled.removeOne(_capLowercase);
839 SYNC(ARG(capability))
840 emit capRemoved(_capLowercase);
844 void Network::clearCaps()
846 // IRCv3 specs all use lowercase capability names
847 if (_caps.empty() && _capsEnabled.empty()) {
848 // Avoid the sync call if there's nothing to clear (e.g. failed reconnects)
851 // To ease core-side configuration, loop through the list and emit capRemoved for each entry.
852 // If performance issues arise, this can be converted to a more-efficient setup without breaking
853 // protocol (in theory).
854 QString _capLowercase;
855 foreach (const QString &capability, _caps) {
856 _capLowercase = capability.toLower();
857 emit capRemoved(_capLowercase);
859 // Clear capabilities from the stored list
861 _capsEnabled.clear();
866 QVariantMap Network::initCaps() const
869 QHashIterator<QString, QString> iter(_caps);
870 while (iter.hasNext()) {
872 caps[iter.key()] = iter.value();
878 // There's potentially a lot of users and channels, so it makes sense to optimize the format of this.
879 // Rather than sending a thousand maps with identical keys, we convert this into one map containing lists
880 // where each list index corresponds to a particular IrcUser. This saves sending the key names a thousand times.
881 // Benchmarks have shown space savings of around 56%, resulting in saving several MBs worth of data on sync
882 // (without compression) with a decent amount of IrcUsers.
883 QVariantMap Network::initIrcUsersAndChannels() const
886 Q_ASSERT(proxy()->targetPeer());
887 QVariantMap usersAndChannels;
889 if (_ircUsers.count()) {
890 QHash<QString, QVariantList> users;
891 QHash<QString, IrcUser *>::const_iterator it = _ircUsers.begin();
892 QHash<QString, IrcUser *>::const_iterator end = _ircUsers.end();
894 QVariantMap map = it.value()->toVariantMap();
895 // If the peer doesn't support LongTime, replace the lastAwayMessageTime field
896 // with the 32-bit numerical seconds value (lastAwayMessage) used in older versions
897 if (!proxy()->targetPeer()->hasFeature(Quassel::Feature::LongTime)) {
898 #if QT_VERSION >= 0x050800
899 int lastAwayMessage = it.value()->lastAwayMessageTime().toSecsSinceEpoch();
901 // toSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now.
902 // See https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch
903 int lastAwayMessage = it.value()->lastAwayMessageTime().toMSecsSinceEpoch() / 1000;
905 map.remove("lastAwayMessageTime");
906 map["lastAwayMessage"] = lastAwayMessage;
909 QVariantMap::const_iterator mapiter = map.begin();
910 while (mapiter != map.end()) {
911 users[mapiter.key()] << mapiter.value();
916 // Can't have a container with a value type != QVariant in a QVariant :(
917 // However, working directly on a QVariantMap is awkward for appending, thus the detour via the hash above.
919 foreach(const QString &key, users.keys())
920 userMap[key] = users[key];
921 usersAndChannels["Users"] = userMap;
924 if (_ircChannels.count()) {
925 QHash<QString, QVariantList> channels;
926 QHash<QString, IrcChannel *>::const_iterator it = _ircChannels.begin();
927 QHash<QString, IrcChannel *>::const_iterator end = _ircChannels.end();
929 const QVariantMap &map = it.value()->toVariantMap();
930 QVariantMap::const_iterator mapiter = map.begin();
931 while (mapiter != map.end()) {
932 channels[mapiter.key()] << mapiter.value();
937 QVariantMap channelMap;
938 foreach(const QString &key, channels.keys())
939 channelMap[key] = channels[key];
940 usersAndChannels["Channels"] = channelMap;
943 return usersAndChannels;
947 void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels)
950 Q_ASSERT(proxy()->sourcePeer());
951 if (isInitialized()) {
952 qWarning() << "Network" << networkId() << "received init data for users and channels although there already are known users or channels!";
956 // toMap() and toList() are cheap, so we can avoid copying to lists...
957 // However, we really have to make sure to never accidentally detach from the shared data!
959 const QVariantMap &users = usersAndChannels["Users"].toMap();
962 int count = users["nick"].toList().count();
963 foreach(const QString &key, users.keys()) {
964 if (users[key].toList().count() != count) {
965 qWarning() << "Received invalid usersAndChannels init data, sizes of attribute lists don't match!";
970 // now create the individual IrcUsers
971 for(int i = 0; i < count; i++) {
973 foreach(const QString &key, users.keys())
974 map[key] = users[key].toList().at(i);
976 // If the peer doesn't support LongTime, upconvert the lastAwayMessageTime field
977 // from the 32-bit numerical seconds value used in older versions to QDateTime
978 if (!proxy()->sourcePeer()->hasFeature(Quassel::Feature::LongTime)) {
979 QDateTime lastAwayMessageTime = QDateTime();
980 lastAwayMessageTime.setTimeSpec(Qt::UTC);
981 #if QT_VERSION >= 0x050800
982 lastAwayMessageTime.fromSecsSinceEpoch(map.take("lastAwayMessage").toInt());
984 // toSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now.
985 // See https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch
986 lastAwayMessageTime.fromMSecsSinceEpoch(map.take("lastAwayMessage").toInt() * 1000);
988 map["lastAwayMessageTime"] = lastAwayMessageTime;
991 newIrcUser(map["nick"].toString(), map); // newIrcUser() properly handles the hostmask being just the nick
994 // same thing for IrcChannels
995 const QVariantMap &channels = usersAndChannels["Channels"].toMap();
998 count = channels["name"].toList().count();
999 foreach(const QString &key, channels.keys()) {
1000 if (channels[key].toList().count() != count) {
1001 qWarning() << "Received invalid usersAndChannels init data, sizes of attribute lists don't match!";
1005 // now create the individual IrcChannels
1006 for(int i = 0; i < count; i++) {
1008 foreach(const QString &key, channels.keys())
1009 map[key] = channels[key].toList().at(i);
1010 newIrcChannel(map["name"].toString(), map);
1015 void Network::initSetSupports(const QVariantMap &supports)
1017 QMapIterator<QString, QVariant> iter(supports);
1018 while (iter.hasNext()) {
1020 addSupport(iter.key(), iter.value().toString());
1025 void Network::initSetCaps(const QVariantMap &caps)
1027 QMapIterator<QString, QVariant> iter(caps);
1028 while (iter.hasNext()) {
1030 addCap(iter.key(), iter.value().toString());
1035 IrcUser *Network::updateNickFromMask(const QString &mask)
1037 QString nick(nickFromMask(mask).toLower());
1040 if (_ircUsers.contains(nick)) {
1041 ircuser = _ircUsers[nick];
1042 ircuser->updateHostmask(mask);
1045 ircuser = newIrcUser(mask);
1051 void Network::ircUserNickChanged(QString newnick)
1053 QString oldnick = _ircUsers.key(qobject_cast<IrcUser *>(sender()));
1055 if (oldnick.isNull())
1058 if (newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
1060 if (myNick().toLower() == oldnick)
1065 void Network::emitConnectionError(const QString &errorMsg)
1067 emit connectionError(errorMsg);
1071 // ====================
1073 // ====================
1074 void Network::determinePrefixes() const
1076 // seems like we have to construct them first
1077 QString prefix = support("PREFIX");
1079 if (prefix.startsWith("(") && prefix.contains(")")) {
1080 _prefixes = prefix.section(")", 1);
1081 _prefixModes = prefix.mid(1).section(")", 0, 0);
1084 QString defaultPrefixes("~&@%+");
1085 QString defaultPrefixModes("qaohv");
1087 if (prefix.isEmpty()) {
1088 _prefixes = defaultPrefixes;
1089 _prefixModes = defaultPrefixModes;
1092 // clear the existing modes, just in case we're run multiple times
1093 _prefixes = QString();
1094 _prefixModes = QString();
1096 // we just assume that in PREFIX are only prefix chars stored
1097 for (int i = 0; i < defaultPrefixes.size(); i++) {
1098 if (prefix.contains(defaultPrefixes[i])) {
1099 _prefixes += defaultPrefixes[i];
1100 _prefixModes += defaultPrefixModes[i];
1103 // check for success
1104 if (!_prefixes.isNull())
1107 // well... our assumption was obviously wrong...
1108 // check if it's only prefix modes
1109 for (int i = 0; i < defaultPrefixes.size(); i++) {
1110 if (prefix.contains(defaultPrefixModes[i])) {
1111 _prefixes += defaultPrefixes[i];
1112 _prefixModes += defaultPrefixModes[i];
1115 // now we've done all we've could...
1120 /************************************************************************
1122 ************************************************************************/
1124 NetworkInfo::NetworkInfo()
1127 useRandomServer(false),
1128 useAutoIdentify(false),
1129 autoIdentifyService("NickServ"),
1131 useAutoReconnect(true),
1132 autoReconnectInterval(60),
1133 autoReconnectRetries(20),
1134 unlimitedReconnectRetries(false),
1135 rejoinChannels(true),
1136 useCustomMessageRate(false),
1137 messageRateBurstSize(5),
1138 messageRateDelay(2200),
1139 unlimitedMessageRate(false)
1144 bool NetworkInfo::operator==(const NetworkInfo &other) const
1146 if (networkId != other.networkId) return false;
1147 if (networkName != other.networkName) return false;
1148 if (identity != other.identity) return false;
1149 if (codecForServer != other.codecForServer) return false;
1150 if (codecForEncoding != other.codecForEncoding) return false;
1151 if (codecForDecoding != other.codecForDecoding) return false;
1152 if (serverList != other.serverList) return false;
1153 if (useRandomServer != other.useRandomServer) return false;
1154 if (perform != other.perform) return false;
1155 if (useAutoIdentify != other.useAutoIdentify) return false;
1156 if (autoIdentifyService != other.autoIdentifyService) return false;
1157 if (autoIdentifyPassword != other.autoIdentifyPassword) return false;
1158 if (useSasl != other.useSasl) return false;
1159 if (saslAccount != other.saslAccount) return false;
1160 if (saslPassword != other.saslPassword) return false;
1161 if (useAutoReconnect != other.useAutoReconnect) return false;
1162 if (autoReconnectInterval != other.autoReconnectInterval) return false;
1163 if (autoReconnectRetries != other.autoReconnectRetries) return false;
1164 if (unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
1165 if (rejoinChannels != other.rejoinChannels) return false;
1166 // Custom rate limiting
1167 if (useCustomMessageRate != other.useCustomMessageRate) return false;
1168 if (messageRateBurstSize != other.messageRateBurstSize) return false;
1169 if (messageRateDelay != other.messageRateDelay) return false;
1170 if (unlimitedMessageRate != other.unlimitedMessageRate) return false;
1175 bool NetworkInfo::operator!=(const NetworkInfo &other) const
1177 return !(*this == other);
1181 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info)
1184 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
1185 i["NetworkName"] = info.networkName;
1186 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
1187 i["CodecForServer"] = info.codecForServer;
1188 i["CodecForEncoding"] = info.codecForEncoding;
1189 i["CodecForDecoding"] = info.codecForDecoding;
1190 i["ServerList"] = toVariantList(info.serverList);
1191 i["UseRandomServer"] = info.useRandomServer;
1192 i["Perform"] = info.perform;
1193 i["UseAutoIdentify"] = info.useAutoIdentify;
1194 i["AutoIdentifyService"] = info.autoIdentifyService;
1195 i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
1196 i["UseSasl"] = info.useSasl;
1197 i["SaslAccount"] = info.saslAccount;
1198 i["SaslPassword"] = info.saslPassword;
1199 i["UseAutoReconnect"] = info.useAutoReconnect;
1200 i["AutoReconnectInterval"] = info.autoReconnectInterval;
1201 i["AutoReconnectRetries"] = info.autoReconnectRetries;
1202 i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
1203 i["RejoinChannels"] = info.rejoinChannels;
1204 // Custom rate limiting
1205 i["UseCustomMessageRate"] = info.useCustomMessageRate;
1206 i["MessageRateBurstSize"] = info.messageRateBurstSize;
1207 i["MessageRateDelay"] = info.messageRateDelay;
1208 i["UnlimitedMessageRate"] = info.unlimitedMessageRate;
1214 QDataStream &operator>>(QDataStream &in, NetworkInfo &info)
1218 info.networkId = i["NetworkId"].value<NetworkId>();
1219 info.networkName = i["NetworkName"].toString();
1220 info.identity = i["Identity"].value<IdentityId>();
1221 info.codecForServer = i["CodecForServer"].toByteArray();
1222 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
1223 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
1224 info.serverList = fromVariantList<Network::Server>(i["ServerList"].toList());
1225 info.useRandomServer = i["UseRandomServer"].toBool();
1226 info.perform = i["Perform"].toStringList();
1227 info.useAutoIdentify = i["UseAutoIdentify"].toBool();
1228 info.autoIdentifyService = i["AutoIdentifyService"].toString();
1229 info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
1230 info.useSasl = i["UseSasl"].toBool();
1231 info.saslAccount = i["SaslAccount"].toString();
1232 info.saslPassword = i["SaslPassword"].toString();
1233 info.useAutoReconnect = i["UseAutoReconnect"].toBool();
1234 info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
1235 info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
1236 info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
1237 info.rejoinChannels = i["RejoinChannels"].toBool();
1238 // Custom rate limiting
1239 info.useCustomMessageRate = i["UseCustomMessageRate"].toBool();
1240 info.messageRateBurstSize = i["MessageRateBurstSize"].toUInt();
1241 info.messageRateDelay = i["MessageRateDelay"].toUInt();
1242 info.unlimitedMessageRate = i["UnlimitedMessageRate"].toBool();
1247 QDebug operator<<(QDebug dbg, const NetworkInfo &i)
1249 dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
1250 << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
1251 << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
1252 << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
1253 << " useSasl = " << i.useSasl << " saslAccount = " << i.saslAccount << " saslPassword = " << i.saslPassword
1254 << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
1255 << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
1256 << " rejoinChannels = " << i.rejoinChannels
1257 << " useCustomMessageRate = " << i.useCustomMessageRate
1258 << " messageRateBurstSize = " << i.messageRateBurstSize
1259 << " messageRateDelay = " << i.messageRateDelay
1260 << " unlimitedMessageRate = " << i.unlimitedMessageRate
1266 QDataStream &operator<<(QDataStream &out, const Network::Server &server)
1268 QVariantMap serverMap;
1269 serverMap["Host"] = server.host;
1270 serverMap["Port"] = server.port;
1271 serverMap["Password"] = server.password;
1272 serverMap["UseSSL"] = server.useSsl;
1273 serverMap["sslVerify"] = server.sslVerify;
1274 serverMap["sslVersion"] = server.sslVersion;
1275 serverMap["UseProxy"] = server.useProxy;
1276 serverMap["ProxyType"] = server.proxyType;
1277 serverMap["ProxyHost"] = server.proxyHost;
1278 serverMap["ProxyPort"] = server.proxyPort;
1279 serverMap["ProxyUser"] = server.proxyUser;
1280 serverMap["ProxyPass"] = server.proxyPass;
1286 QDataStream &operator>>(QDataStream &in, Network::Server &server)
1288 QVariantMap serverMap;
1290 server.host = serverMap["Host"].toString();
1291 server.port = serverMap["Port"].toUInt();
1292 server.password = serverMap["Password"].toString();
1293 server.useSsl = serverMap["UseSSL"].toBool();
1294 server.sslVerify = serverMap["sslVerify"].toBool();
1295 server.sslVersion = serverMap["sslVersion"].toInt();
1296 server.useProxy = serverMap["UseProxy"].toBool();
1297 server.proxyType = serverMap["ProxyType"].toInt();
1298 server.proxyHost = serverMap["ProxyHost"].toString();
1299 server.proxyPort = serverMap["ProxyPort"].toUInt();
1300 server.proxyUser = serverMap["ProxyUser"].toString();
1301 server.proxyPass = serverMap["ProxyPass"].toString();
1306 bool Network::Server::operator==(const Server &other) const
1308 if (host != other.host) return false;
1309 if (port != other.port) return false;
1310 if (password != other.password) return false;
1311 if (useSsl != other.useSsl) return false;
1312 if (sslVerify != other.sslVerify) return false;
1313 if (sslVersion != other.sslVersion) return false;
1314 if (useProxy != other.useProxy) return false;
1315 if (proxyType != other.proxyType) return false;
1316 if (proxyHost != other.proxyHost) return false;
1317 if (proxyPort != other.proxyPort) return false;
1318 if (proxyUser != other.proxyUser) return false;
1319 if (proxyPass != other.proxyPass) return false;
1324 bool Network::Server::operator!=(const Server &other) const
1326 return !(*this == other);
1330 QDebug operator<<(QDebug dbg, const Network::Server &server)
1332 dbg.nospace() << "Server(host = " << server.host << ":" << server.port << ", useSsl = " <<
1333 server.useSsl << ", sslVerify = " << server.sslVerify << ")";