1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 ***************************************************************************/
21 #include "corenetwork.h"
27 #include "coreidentity.h"
28 #include "corenetworkconfig.h"
29 #include "coresession.h"
30 #include "coreuserinputhandler.h"
31 #include "ircencoder.h"
34 #include "networkevent.h"
36 CoreNetwork::CoreNetwork(const NetworkId& networkid, CoreSession* session)
37 : Network(networkid, session)
38 , _coreSession(session)
39 , _userInputHandler(new CoreUserInputHandler(this))
40 , _metricsServer(Core::instance()->metricsServer())
41 , _autoReconnectCount(0)
42 , _quitRequested(false)
43 , _disconnectExpected(false)
44 , _previousConnectionAttemptFailed(false)
45 , _lastUsedServerIndex(0)
46 , _requestedUserModes('-')
48 // Check if raw IRC logging is enabled
49 _debugLogRawIrc = (Quassel::isOptionSet("debug-irc") || Quassel::isOptionSet("debug-irc-id"));
50 _debugLogRawNetId = Quassel::optionValue("debug-irc-id").toInt();
52 _autoReconnectTimer.setSingleShot(true);
53 connect(&_socketCloseTimer, &QTimer::timeout, this, &CoreNetwork::onSocketCloseTimeout);
55 setPingInterval(networkConfig()->pingInterval());
56 connect(&_pingTimer, &QTimer::timeout, this, &CoreNetwork::sendPing);
58 setAutoWhoDelay(networkConfig()->autoWhoDelay());
59 setAutoWhoInterval(networkConfig()->autoWhoInterval());
61 QHash<QString, QString> channels = coreSession()->persistentChannels(networkId());
62 for (const QString& chan : channels.keys()) {
63 _channelKeys[chan.toLower()] = channels[chan];
66 QHash<QString, QByteArray> bufferCiphers = coreSession()->bufferCiphers(networkId());
67 for (const QString& buffer : bufferCiphers.keys()) {
68 storeChannelCipherKey(buffer.toLower(), bufferCiphers[buffer]);
71 connect(networkConfig(), &NetworkConfig::pingTimeoutEnabledSet, this, &CoreNetwork::enablePingTimeout);
72 connect(networkConfig(), &NetworkConfig::pingIntervalSet, this, &CoreNetwork::setPingInterval);
73 connect(networkConfig(), &NetworkConfig::autoWhoEnabledSet, this, &CoreNetwork::setAutoWhoEnabled);
74 connect(networkConfig(), &NetworkConfig::autoWhoIntervalSet, this, &CoreNetwork::setAutoWhoInterval);
75 connect(networkConfig(), &NetworkConfig::autoWhoDelaySet, this, &CoreNetwork::setAutoWhoDelay);
77 connect(&_autoReconnectTimer, &QTimer::timeout, this, &CoreNetwork::doAutoReconnect);
78 connect(&_autoWhoTimer, &QTimer::timeout, this, &CoreNetwork::sendAutoWho);
79 connect(&_autoWhoCycleTimer, &QTimer::timeout, this, &CoreNetwork::startAutoWhoCycle);
80 connect(&_tokenBucketTimer, &QTimer::timeout, this, &CoreNetwork::checkTokenBucket);
82 connect(&socket, &QAbstractSocket::connected, this, &CoreNetwork::onSocketInitialized);
83 connect(&socket, selectOverload<QAbstractSocket::SocketError>(&QAbstractSocket::error), this, &CoreNetwork::onSocketError);
84 connect(&socket, &QAbstractSocket::stateChanged, this, &CoreNetwork::onSocketStateChanged);
85 connect(&socket, &QIODevice::readyRead, this, &CoreNetwork::onSocketHasData);
87 connect(&socket, &QSslSocket::encrypted, this, &CoreNetwork::onSocketInitialized);
88 connect(&socket, selectOverload<const QList<QSslError>&>(&QSslSocket::sslErrors), this, &CoreNetwork::onSslErrors);
90 connect(this, &CoreNetwork::newEvent, coreSession()->eventManager(), &EventManager::postEvent);
92 // Custom rate limiting
93 // These react to the user changing settings in the client
94 connect(this, &Network::useCustomMessageRateSet, this, &CoreNetwork::updateRateLimiting);
95 connect(this, &Network::messageRateBurstSizeSet, this, &CoreNetwork::updateRateLimiting);
96 connect(this, &Network::messageRateDelaySet, this, &CoreNetwork::updateRateLimiting);
97 connect(this, &Network::unlimitedMessageRateSet, this, &CoreNetwork::updateRateLimiting);
99 // IRCv3 capability handling
100 // These react to CAP messages from the server
101 connect(this, &Network::capAdded, this, &CoreNetwork::serverCapAdded);
102 connect(this, &Network::capAcknowledged, this, &CoreNetwork::serverCapAcknowledged);
103 connect(this, &Network::capRemoved, this, &CoreNetwork::serverCapRemoved);
105 if (Quassel::isOptionSet("oidentd")) {
106 connect(this, &CoreNetwork::socketInitialized, Core::instance()->oidentdConfigGenerator(), &OidentdConfigGenerator::addSocket);
107 connect(this, &CoreNetwork::socketDisconnected, Core::instance()->oidentdConfigGenerator(), &OidentdConfigGenerator::removeSocket);
110 if (Quassel::isOptionSet("ident-daemon")) {
111 connect(this, &CoreNetwork::socketInitialized, Core::instance()->identServer(), &IdentServer::addSocket);
112 connect(this, &CoreNetwork::socketDisconnected, Core::instance()->identServer(), &IdentServer::removeSocket);
116 CoreNetwork::~CoreNetwork()
118 // Ensure we don't get any more signals from the socket while shutting down
119 disconnect(&socket, nullptr, this, nullptr);
120 if (!forceDisconnect()) {
121 qWarning() << QString{"Could not disconnect from network %1 (network ID: %2, user ID: %3)"}
123 .arg(networkId().toInt())
124 .arg(userId().toInt());
128 bool CoreNetwork::forceDisconnect(int msecs)
130 if (socket.state() == QAbstractSocket::UnconnectedState) {
131 // Socket already disconnected.
134 // Request a socket-level disconnect if not already happened
135 socket.disconnectFromHost();
136 if (socket.state() != QAbstractSocket::UnconnectedState) {
137 return socket.waitForDisconnected(msecs);
142 QString CoreNetwork::channelDecode(const QString& bufferName, const QByteArray& string) const
144 if (!bufferName.isEmpty()) {
145 IrcChannel* channel = ircChannel(bufferName);
147 return channel->decodeString(string);
149 return decodeString(string);
152 QString CoreNetwork::userDecode(const QString& userNick, const QByteArray& string) const
154 IrcUser* user = ircUser(userNick);
156 return user->decodeString(string);
157 return decodeString(string);
160 QByteArray CoreNetwork::channelEncode(const QString& bufferName, const QString& string) const
162 if (!bufferName.isEmpty()) {
163 IrcChannel* channel = ircChannel(bufferName);
165 return channel->encodeString(string);
167 return encodeString(string);
170 QByteArray CoreNetwork::userEncode(const QString& userNick, const QString& string) const
172 IrcUser* user = ircUser(userNick);
174 return user->encodeString(string);
175 return encodeString(string);
178 void CoreNetwork::connectToIrc(bool reconnecting)
184 if (Core::instance()->identServer()) {
185 _socketId = Core::instance()->identServer()->addWaitingSocket();
188 if (_metricsServer) {
189 _metricsServer->addNetwork(userId());
192 if (!reconnecting && useAutoReconnect() && _autoReconnectCount == 0) {
193 _autoReconnectTimer.setInterval(autoReconnectInterval() * 1000);
194 if (unlimitedReconnectRetries())
195 _autoReconnectCount = -1;
197 _autoReconnectCount = autoReconnectRetries();
199 if (serverList().isEmpty()) {
200 qWarning() << "Server list empty, ignoring connect request!";
203 CoreIdentity* identity = identityPtr();
205 qWarning() << "Invalid identity configures, ignoring connect request!";
209 // cleaning up old quit reason
212 // Reset capability negotiation tracking, also handling server changes during reconnect
213 _capsQueuedIndividual.clear();
214 _capsQueuedBundled.clear();
216 _capNegotiationActive = false;
217 _capInitialNegotiationEnded = false;
219 // use a random server?
220 if (useRandomServer()) {
221 _lastUsedServerIndex = qrand() % serverList().size();
223 else if (_previousConnectionAttemptFailed) {
224 // cycle to next server if previous connection attempt failed
225 _previousConnectionAttemptFailed = false;
226 showMessage(NetworkInternalMessage(
228 BufferInfo::StatusBuffer,
230 tr("Connection failed. Cycling to next server...")
232 if (++_lastUsedServerIndex >= serverList().size()) {
233 _lastUsedServerIndex = 0;
237 // Start out with the top server in the list
238 _lastUsedServerIndex = 0;
241 Server server = usedServer();
242 displayStatusMsg(tr("Connecting to %1:%2...").arg(server.host).arg(server.port));
243 showMessage(NetworkInternalMessage(
245 BufferInfo::StatusBuffer,
247 tr("Connecting to %1:%2...").arg(server.host).arg(server.port)
250 if (server.useProxy) {
251 QNetworkProxy proxy((QNetworkProxy::ProxyType) server.proxyType, server.proxyHost, server.proxyPort, server.proxyUser, server.proxyPass);
252 socket.setProxy(proxy);
255 socket.setProxy(QNetworkProxy::NoProxy);
260 // Reset tracking for valid timestamps in PONG replies
261 setPongTimestampValid(false);
263 // Qt caches DNS entries for a minute, resulting in round-robin (e.g. for chat.freenode.net) not working if several users
264 // connect at a similar time. QHostInfo::fromName(), however, always performs a fresh lookup, overwriting the cache entry.
265 if (!server.useProxy) {
266 // Avoid hostname lookups when a proxy is specified. The lookups won't use the proxy and may therefore leak the DNS
267 // hostname of the server. Qt's DNS cache also isn't used by the proxy so we don't need to refresh the entry.
268 QHostInfo::fromName(server.host);
272 CoreIdentity* identity = identityPtr();
274 socket.setLocalCertificate(identity->sslCert());
275 socket.setPrivateKey(identity->sslKey());
277 socket.connectToHostEncrypted(server.host, server.port);
280 socket.connectToHost(server.host, server.port);
283 socket.connectToHost(server.host, server.port);
287 void CoreNetwork::disconnectFromIrc(bool requested, const QString& reason, bool withReconnect)
289 // Disconnecting from the network, should expect a socket close or error
290 _disconnectExpected = true;
291 _quitRequested = requested; // see socketDisconnected();
292 if (!withReconnect) {
293 _autoReconnectTimer.stop();
294 _autoReconnectCount = 0; // prohibiting auto reconnect
296 disablePingTimeout();
298 if (_metricsServer) {
299 _metricsServer->messageQueue(userId(), 0);
306 awayMsg = me_->awayMessage();
307 Core::setAwayMessage(userId(), networkId(), awayMsg);
310 if (reason.isEmpty() && identityPtr())
311 _quitReason = identityPtr()->quitReason();
313 _quitReason = reason;
315 showMessage(NetworkInternalMessage(
317 BufferInfo::StatusBuffer,
319 tr("Disconnecting. (%1)").arg((!requested && !withReconnect) ? tr("Core Shutdown") : _quitReason)
321 if (socket.state() == QAbstractSocket::UnconnectedState) {
322 onSocketDisconnected();
325 if (socket.state() == QAbstractSocket::ConnectedState) {
326 // If shutting down, prioritize the QUIT command
327 userInputHandler()->issueQuit(_quitReason, _shuttingDown);
332 if (socket.state() != QAbstractSocket::UnconnectedState) {
333 // Wait for up to 10 seconds for the socket to close cleanly, then it will be forcefully aborted
334 _socketCloseTimer.start(10000);
339 void CoreNetwork::onSocketCloseTimeout()
341 qWarning() << QString{"Timed out quitting network %1 (network ID: %2, user ID: %3)"}
343 .arg(networkId().toInt())
344 .arg(userId().toInt());
348 void CoreNetwork::shutdown()
350 _shuttingDown = true;
351 disconnectFromIrc(false, {}, false);
354 void CoreNetwork::userInput(const BufferInfo& buf, QString msg)
356 userInputHandler()->handleUserInput(buf, msg);
359 void CoreNetwork::putRawLine(const QByteArray& s, bool prepend)
361 if (_tokenBucket > 0 || (_skipMessageRates && _msgQueue.isEmpty())) {
362 // If there's tokens remaining, ...
363 // Or rate limits don't apply AND no messages are in queue (to prevent out-of-order), ...
364 // Send the message now.
368 // Otherwise, queue the message for later
370 // Jump to the start, skipping other messages
371 _msgQueue.prepend(s);
374 // Add to back, waiting in order
377 if (_metricsServer) {
378 _metricsServer->messageQueue(userId(), _msgQueue.size());
383 void CoreNetwork::putCmd(const QString& cmd, const QList<QByteArray>& params, const QByteArray& prefix, const QHash<IrcTagKey, QString>& tags, bool prepend)
385 putRawLine(IrcEncoder::writeMessage(tags, prefix, cmd, params), prepend);
388 void CoreNetwork::putCmd(const QString& cmd, const QList<QList<QByteArray>>& params, const QByteArray& prefix, const QHash<IrcTagKey, QString>& tags, bool prependAll)
390 QListIterator<QList<QByteArray>> i(params);
391 while (i.hasNext()) {
392 QList<QByteArray> msg = i.next();
393 putCmd(cmd, msg, prefix, tags, prependAll);
397 void CoreNetwork::setChannelJoined(const QString& channel)
399 queueAutoWhoOneshot(channel); // check this new channel first
401 Core::setChannelPersistent(userId(), networkId(), channel, true);
402 Core::setPersistentChannelKey(userId(), networkId(), channel, _channelKeys[channel.toLower()]);
405 void CoreNetwork::setChannelParted(const QString& channel)
407 removeChannelKey(channel);
408 _autoWhoQueue.removeAll(channel.toLower());
409 _autoWhoPending.remove(channel.toLower());
411 Core::setChannelPersistent(userId(), networkId(), channel, false);
414 void CoreNetwork::addChannelKey(const QString& channel, const QString& key)
417 removeChannelKey(channel);
420 _channelKeys[channel.toLower()] = key;
424 void CoreNetwork::removeChannelKey(const QString& channel)
426 _channelKeys.remove(channel.toLower());
430 Cipher* CoreNetwork::cipher(const QString& target)
432 if (target.isEmpty())
435 if (!Cipher::neededFeaturesAvailable())
438 auto* channel = qobject_cast<CoreIrcChannel*>(ircChannel(target));
440 return channel->cipher();
442 auto* user = qobject_cast<CoreIrcUser*>(ircUser(target));
444 return user->cipher();
446 else if (!isChannelName(target)) {
447 return qobject_cast<CoreIrcUser*>(newIrcUser(target))->cipher();
452 QByteArray CoreNetwork::cipherKey(const QString& target) const
454 auto* c = qobject_cast<CoreIrcChannel*>(ircChannel(target));
456 return c->cipher()->key();
458 auto* u = qobject_cast<CoreIrcUser*>(ircUser(target));
460 return u->cipher()->key();
465 void CoreNetwork::setCipherKey(const QString& target, const QByteArray& key)
467 auto* c = qobject_cast<CoreIrcChannel*>(ircChannel(target));
469 c->setEncrypted(c->cipher()->setKey(key));
470 coreSession()->setBufferCipher(networkId(), target, key);
474 auto* u = qobject_cast<CoreIrcUser*>(ircUser(target));
475 if (!u && !isChannelName(target))
476 u = qobject_cast<CoreIrcUser*>(newIrcUser(target));
479 u->setEncrypted(u->cipher()->setKey(key));
480 coreSession()->setBufferCipher(networkId(), target, key);
485 bool CoreNetwork::cipherUsesCBC(const QString& target)
487 auto* c = qobject_cast<CoreIrcChannel*>(ircChannel(target));
489 return c->cipher()->usesCBC();
490 auto* u = qobject_cast<CoreIrcUser*>(ircUser(target));
492 return u->cipher()->usesCBC();
496 #endif /* HAVE_QCA2 */
498 bool CoreNetwork::setAutoWhoDone(const QString& name)
500 QString chanOrNick = name.toLower();
501 if (_autoWhoPending.value(chanOrNick, 0) <= 0)
503 if (--_autoWhoPending[chanOrNick] <= 0)
504 _autoWhoPending.remove(chanOrNick);
508 void CoreNetwork::setMyNick(const QString& mynick)
510 Network::setMyNick(mynick);
511 if (connectionState() == Network::Initializing)
512 networkInitialized();
515 void CoreNetwork::onSocketHasData()
517 while (socket.canReadLine()) {
518 QByteArray s = socket.readLine();
519 if (_metricsServer) {
520 _metricsServer->receiveDataNetwork(userId(), s.size());
522 if (s.endsWith("\r\n"))
524 else if (s.endsWith("\n"))
526 NetworkDataEvent* event = new NetworkDataEvent(EventManager::NetworkIncoming, this, s);
527 event->setTimestamp(QDateTime::currentDateTimeUtc());
528 emit newEvent(event);
532 void CoreNetwork::onSocketError(QAbstractSocket::SocketError error)
534 // Ignore socket closed errors if expected
535 if (_disconnectExpected && error == QAbstractSocket::RemoteHostClosedError) {
539 _previousConnectionAttemptFailed = true;
540 qWarning() << qPrintable(tr("Could not connect to %1 (%2)").arg(networkName(), socket.errorString()));
541 emit connectionError(socket.errorString());
542 showMessage(NetworkInternalMessage(
544 BufferInfo::StatusBuffer,
546 tr("Connection failure: %1").arg(socket.errorString())
548 emitConnectionError(socket.errorString());
549 if (socket.state() < QAbstractSocket::ConnectedState) {
550 onSocketDisconnected();
554 void CoreNetwork::onSocketInitialized()
556 CoreIdentity* identity = identityPtr();
558 qCritical() << "Identity invalid!";
563 Server server = usedServer();
566 // Non-SSL connections enter here only once, always emit socketInitialized(...) in these cases
567 // SSL connections call socketInitialized() twice, only emit socketInitialized(...) on the first (not yet encrypted) run
568 if (!server.useSsl || !socket.isEncrypted()) {
569 emit socketInitialized(identity, localAddress(), localPort(), peerAddress(), peerPort(), _socketId);
572 if (server.useSsl && !socket.isEncrypted()) {
573 // We'll finish setup once we're encrypted, and called again
577 emit socketInitialized(identity, localAddress(), localPort(), peerAddress(), peerPort(), _socketId);
580 socket.setSocketOption(QAbstractSocket::KeepAliveOption, true);
582 // Update the TokenBucket, force-enabling unlimited message rates for initial registration and
583 // capability negotiation. networkInitialized() will call updateRateLimiting() without the
584 // force flag to apply user preferences. When making changes, ensure that this still happens!
585 // As Quassel waits for CAP ACK/NAK and AUTHENTICATE replies, this shouldn't ever fill the IRC
586 // server receive queue and cause a kill. "Shouldn't" being the operative word; the real world
588 updateRateLimiting(true);
589 // Fill up the token bucket as we're connecting from scratch
592 // Request capabilities as per IRCv3.2 specifications
593 // Older servers should ignore this; newer servers won't downgrade to RFC1459
594 showMessage(NetworkInternalMessage(
596 BufferInfo::StatusBuffer,
598 tr("Requesting capability list...")
600 putRawLine(serverEncode(QString("CAP LS 302")));
602 if (!server.password.isEmpty()) {
603 putRawLine(serverEncode(QString("PASS %1").arg(server.password)));
606 if (identity->nicks().isEmpty()) {
608 qWarning() << "CoreNetwork::socketInitialized(): no nicks supplied for identity Id" << identity->id();
611 nick = identity->nicks()[0];
613 putRawLine(serverEncode(QString("NICK %1").arg(nick)));
614 // Only allow strict-compliant idents when strict mode is enabled
615 putRawLine(serverEncode(QString("USER %1 8 * :%2").arg(coreSession()->strictCompliantIdent(identity), identity->realName())));
618 void CoreNetwork::onSocketDisconnected()
620 disablePingTimeout();
622 if (_metricsServer) {
623 _metricsServer->messageQueue(userId(), 0);
626 _autoWhoCycleTimer.stop();
627 _autoWhoTimer.stop();
628 _autoWhoQueue.clear();
629 _autoWhoPending.clear();
631 _socketCloseTimer.stop();
633 _tokenBucketTimer.stop();
637 for (const QString& channel : me_->channels()) {
638 showMessage(NetworkInternalMessage(
640 BufferInfo::ChannelBuffer,
642 _quitReason, me_->hostmask()
648 emit disconnected(networkId());
649 emit socketDisconnected(identityPtr(), localAddress(), localPort(), peerAddress(), peerPort(), _socketId);
650 // Reset disconnect expectations
651 _disconnectExpected = false;
652 if (_quitRequested) {
653 _quitRequested = false;
654 setConnectionState(Network::Disconnected);
655 Core::setNetworkConnected(userId(), networkId(), false);
657 else if (_autoReconnectCount != 0) {
658 setConnectionState(Network::Reconnecting);
659 if (_autoReconnectCount == -1 || _autoReconnectCount == autoReconnectRetries())
660 doAutoReconnect(); // first try is immediate
662 _autoReconnectTimer.start();
665 if (_metricsServer) {
666 _metricsServer->removeNetwork(userId());
670 void CoreNetwork::onSocketStateChanged(QAbstractSocket::SocketState socketState)
672 Network::ConnectionState state;
673 switch (socketState) {
674 case QAbstractSocket::UnconnectedState:
675 state = Network::Disconnected;
676 onSocketDisconnected();
678 case QAbstractSocket::HostLookupState:
679 case QAbstractSocket::ConnectingState:
680 state = Network::Connecting;
682 case QAbstractSocket::ConnectedState:
683 state = Network::Initializing;
685 case QAbstractSocket::ClosingState:
686 state = Network::Disconnecting;
689 state = Network::Disconnected;
691 setConnectionState(state);
694 void CoreNetwork::networkInitialized()
696 setConnectionState(Network::Initialized);
698 _disconnectExpected = false;
699 _quitRequested = false;
701 // Update the TokenBucket with specified rate-limiting settings, removing the force-unlimited
702 // flag used for initial registration and capability negotiation.
703 updateRateLimiting();
705 if (useAutoReconnect()) {
707 _autoReconnectCount = unlimitedReconnectRetries() ? -1 : autoReconnectRetries();
710 // restore away state
711 QString awayMsg = Core::awayMessage(userId(), networkId());
712 if (!awayMsg.isEmpty()) {
713 // Don't re-apply any timestamp formatting in order to preserve escaped percent signs, e.g.
714 // '%%%%%%%%' -> '%%%%' If processed again, it'd result in '%%'.
715 userInputHandler()->handleAway(BufferInfo(), awayMsg, true);
722 if (networkConfig()->autoWhoEnabled()) {
723 _autoWhoCycleTimer.start();
724 _autoWhoTimer.start();
725 startAutoWhoCycle(); // FIXME wait for autojoin to be completed
728 Core::bufferInfo(userId(), networkId(), BufferInfo::StatusBuffer); // create status buffer
729 Core::setNetworkConnected(userId(), networkId(), true);
732 void CoreNetwork::sendPerform()
734 BufferInfo statusBuf = BufferInfo::fakeStatusBuffer(networkId());
737 if (useAutoIdentify() && !autoIdentifyService().isEmpty() && !autoIdentifyPassword().isEmpty()) {
738 userInputHandler()->handleMsg(statusBuf, QString("%1 IDENTIFY %2").arg(autoIdentifyService(), autoIdentifyPassword()));
741 // restore old user modes if server default mode is set.
744 if (!me_->userModes().isEmpty()) {
748 connect(me_, &IrcUser::userModesSet, this, &CoreNetwork::restoreUserModes);
749 connect(me_, &IrcUser::userModesAdded, this, &CoreNetwork::restoreUserModes);
754 for (const QString& line : perform()) {
756 userInput(statusBuf, line);
759 // rejoin channels we've been in
760 if (rejoinChannels()) {
761 QStringList channels, keys;
762 for (const QString& chan : coreSession()->persistentChannels(networkId()).keys()) {
763 QString key = channelKey(chan);
764 if (!key.isEmpty()) {
765 channels.prepend(chan);
769 channels.append(chan);
772 QString joinString = QString("%1 %2").arg(channels.join(",")).arg(keys.join(",")).trimmed();
773 if (!joinString.isEmpty())
774 userInputHandler()->handleJoin(statusBuf, joinString);
778 void CoreNetwork::restoreUserModes()
783 disconnect(me_, &IrcUser::userModesSet, this, &CoreNetwork::restoreUserModes);
784 disconnect(me_, &IrcUser::userModesAdded, this, &CoreNetwork::restoreUserModes);
786 QString modesDelta = Core::userModes(userId(), networkId());
787 QString currentModes = me_->userModes();
789 QString addModes, removeModes;
790 if (modesDelta.contains('-')) {
791 addModes = modesDelta.section('-', 0, 0);
792 removeModes = modesDelta.section('-', 1);
795 addModes = modesDelta;
798 addModes.remove(QRegExp(QString("[%1]").arg(currentModes)));
799 if (currentModes.isEmpty())
800 removeModes = QString();
802 removeModes.remove(QRegExp(QString("[^%1]").arg(currentModes)));
804 if (addModes.isEmpty() && removeModes.isEmpty())
807 if (!addModes.isEmpty())
808 addModes = '+' + addModes;
809 if (!removeModes.isEmpty())
810 removeModes = '-' + removeModes;
812 // don't use InputHandler::handleMode() as it keeps track of our persistent mode changes
813 putRawLine(serverEncode(QString("MODE %1 %2%3").arg(me_->nick()).arg(addModes).arg(removeModes)));
816 void CoreNetwork::updateIssuedModes(const QString& requestedModes)
822 for (auto requestedMode : requestedModes) {
823 if (requestedMode == '+') {
827 if (requestedMode == '-') {
832 addModes += requestedMode;
835 removeModes += requestedMode;
839 QString addModesOld = _requestedUserModes.section('-', 0, 0);
840 QString removeModesOld = _requestedUserModes.section('-', 1);
842 addModes.remove(QRegExp(QString("[%1]").arg(addModesOld))); // deduplicate
843 addModesOld.remove(QRegExp(QString("[%1]").arg(removeModes))); // update
844 addModes += addModesOld;
846 removeModes.remove(QRegExp(QString("[%1]").arg(removeModesOld))); // deduplicate
847 removeModesOld.remove(QRegExp(QString("[%1]").arg(addModes))); // update
848 removeModes += removeModesOld;
850 _requestedUserModes = QString("%1-%2").arg(addModes).arg(removeModes);
853 void CoreNetwork::updatePersistentModes(QString addModes, QString removeModes)
855 QString persistentUserModes = Core::userModes(userId(), networkId());
857 QString requestedAdd = _requestedUserModes.section('-', 0, 0);
858 QString requestedRemove = _requestedUserModes.section('-', 1);
860 QString persistentAdd, persistentRemove;
861 if (persistentUserModes.contains('-')) {
862 persistentAdd = persistentUserModes.section('-', 0, 0);
863 persistentRemove = persistentUserModes.section('-', 1);
866 persistentAdd = persistentUserModes;
869 // remove modes we didn't issue
870 if (requestedAdd.isEmpty())
871 addModes = QString();
873 addModes.remove(QRegExp(QString("[^%1]").arg(requestedAdd)));
875 if (requestedRemove.isEmpty())
876 removeModes = QString();
878 removeModes.remove(QRegExp(QString("[^%1]").arg(requestedRemove)));
881 persistentAdd.remove(QRegExp(QString("[%1]").arg(addModes)));
882 persistentRemove.remove(QRegExp(QString("[%1]").arg(removeModes)));
885 persistentAdd.remove(QRegExp(QString("[%1]").arg(removeModes)));
886 persistentRemove.remove(QRegExp(QString("[%1]").arg(addModes)));
888 // update issued mode list
889 requestedAdd.remove(QRegExp(QString("[%1]").arg(addModes)));
890 requestedRemove.remove(QRegExp(QString("[%1]").arg(removeModes)));
891 _requestedUserModes = QString("%1-%2").arg(requestedAdd).arg(requestedRemove);
893 persistentAdd += addModes;
894 persistentRemove += removeModes;
895 Core::setUserModes(userId(), networkId(), QString("%1-%2").arg(persistentAdd).arg(persistentRemove));
898 void CoreNetwork::resetPersistentModes()
900 _requestedUserModes = QString('-');
901 Core::setUserModes(userId(), networkId(), QString());
904 void CoreNetwork::setUseAutoReconnect(bool use)
906 Network::setUseAutoReconnect(use);
908 _autoReconnectTimer.stop();
911 void CoreNetwork::setAutoReconnectInterval(quint32 interval)
913 Network::setAutoReconnectInterval(interval);
914 _autoReconnectTimer.setInterval(interval * 1000);
917 void CoreNetwork::setAutoReconnectRetries(quint16 retries)
919 Network::setAutoReconnectRetries(retries);
920 if (_autoReconnectCount != 0) {
921 if (unlimitedReconnectRetries())
922 _autoReconnectCount = -1;
924 _autoReconnectCount = autoReconnectRetries();
928 void CoreNetwork::doAutoReconnect()
930 if (connectionState() != Network::Disconnected && connectionState() != Network::Reconnecting) {
931 qWarning() << "CoreNetwork::doAutoReconnect(): Cannot reconnect while not being disconnected!";
934 if (_autoReconnectCount > 0 || _autoReconnectCount == -1)
935 _autoReconnectCount--; // -2 means we delay the next reconnect
939 void CoreNetwork::sendPing()
941 qint64 now = QDateTime::currentDateTime().toMSecsSinceEpoch();
942 if (_pingCount != 0) {
943 qDebug() << "UserId:" << userId() << "Network:" << networkName() << "missed" << _pingCount << "pings."
944 << "BA:" << socket.bytesAvailable() << "BTW:" << socket.bytesToWrite();
946 if ((int) _pingCount >= networkConfig()->maxPingCount() && (now - _lastPingTime) <= (_pingTimer.interval() + (1 * 1000))) {
947 // In transitioning to 64-bit time, the interval no longer needs converted down to seconds.
948 // However, to reduce the risk of breaking things by changing past behavior, we still allow
949 // up to 1 second missed instead of enforcing a stricter 1 millisecond allowance.
951 // the second check compares the actual elapsed time since the last ping and the pingTimer interval
952 // if the interval is shorter then the actual elapsed time it means that this thread was somehow blocked
953 // and unable to even handle a ping answer. So we ignore those misses.
954 disconnectFromIrc(false,
955 QString("No Ping reply in %1 seconds.").arg(_pingCount * _pingTimer.interval() / 1000),
956 true /* withReconnect */);
961 // Don't send pings until the network is initialized
963 // Mark as waiting for a reply
964 _pongReplyPending = true;
965 // Send default timestamp ping
966 userInputHandler()->handlePing(BufferInfo(), QString());
971 void CoreNetwork::enablePingTimeout(bool enable)
974 disablePingTimeout();
977 resetPongReplyPending();
978 if (networkConfig()->pingTimeoutEnabled())
983 void CoreNetwork::disablePingTimeout()
988 resetPongReplyPending();
991 void CoreNetwork::setPingInterval(int interval)
993 _pingTimer.setInterval(interval * 1000);
996 void CoreNetwork::setPongTimestampValid(bool validTimestamp)
998 _pongTimestampValid = validTimestamp;
1001 /******** Custom Rate Limiting ********/
1003 void CoreNetwork::updateRateLimiting(bool forceUnlimited)
1005 // Verify and apply custom rate limiting options, always resetting the delay and burst size
1006 // (safe-guarding against accidentally starting the timer), but don't reset the token bucket as
1007 // this may be called while connected to a server.
1009 if (useCustomMessageRate() || forceUnlimited) {
1010 // Custom message rates enabled, or chosen by means of forcing unlimited. Let's go for it!
1012 _messageDelay = messageRateDelay();
1014 _burstSize = messageRateBurstSize();
1015 if (_burstSize < 1) {
1016 qWarning() << "Invalid messageRateBurstSize data, cannot have zero message burst size!" << _burstSize;
1017 // Can't go slower than one message at a time
1021 if (_tokenBucket > _burstSize) {
1022 // Don't let the token bucket exceed the maximum
1023 _tokenBucket = _burstSize;
1024 // To fill up the token bucket, use resetRateLimiting(). Don't do that here, otherwise
1025 // changing the rate-limit settings while connected to a server will incorrectly reset
1026 // the token bucket.
1029 // Toggle the timer according to whether or not rate limiting is enabled
1030 // If we're here, either useCustomMessageRate or forceUnlimited is true. Thus, the logic is
1031 // _skipMessageRates = ((useCustomMessageRate && unlimitedMessageRate) || forceUnlimited)
1032 // Override user preferences if called with force unlimited, only used during connect.
1033 _skipMessageRates = (unlimitedMessageRate() || forceUnlimited);
1034 if (_skipMessageRates) {
1035 // If the message queue already contains messages, they need sent before disabling the
1036 // timer. Set the timer to a rapid pace and let it disable itself.
1037 if (!_msgQueue.isEmpty()) {
1038 qDebug() << "Outgoing message queue contains messages while disabling rate "
1039 "limiting. Sending remaining queued messages...";
1040 // Promptly run the timer again to clear the messages. Rate limiting is disabled,
1041 // so nothing should cause this to block.. in theory. However, don't directly call
1042 // fillBucketAndProcessQueue() in order to keep it on a separate thread.
1044 // TODO If testing shows this isn't needed, it can be simplified to a direct call.
1045 // Hesitant to change it without a wide variety of situations to verify behavior.
1046 _tokenBucketTimer.start(100);
1049 // No rate limiting, disable the timer
1050 _tokenBucketTimer.stop();
1054 // Rate limiting enabled, enable the timer
1055 _tokenBucketTimer.start(_messageDelay);
1059 // Custom message rates disabled. Go for the default.
1061 _skipMessageRates = false; // Enable rate-limiting by default
1062 _messageDelay = 2200; // This seems to be a safe value (2.2 seconds delay)
1063 _burstSize = 5; // 5 messages at once
1064 if (_tokenBucket > _burstSize) {
1065 // TokenBucket to avoid sending too much at once. Don't let the token bucket exceed the
1067 _tokenBucket = _burstSize;
1068 // To fill up the token bucket, use resetRateLimiting(). Don't do that here, otherwise
1069 // changing the rate-limit settings while connected to a server will incorrectly reset
1070 // the token bucket.
1072 // Rate limiting enabled, enable the timer
1073 _tokenBucketTimer.start(_messageDelay);
1077 void CoreNetwork::resetTokenBucket()
1079 // Fill up the token bucket to the maximum
1080 _tokenBucket = _burstSize;
1083 /******** IRCv3 Capability Negotiation ********/
1085 void CoreNetwork::serverCapAdded(const QString& capability)
1087 // Check if it's a known capability; if so, add it to the list
1088 // Handle special cases first
1089 if (capability == IrcCap::SASL) {
1090 // Only request SASL if it's enabled
1091 if (networkInfo().useSasl)
1092 queueCap(capability);
1094 else if (IrcCap::knownCaps.contains(capability)) {
1095 // Handling for general known capabilities
1096 queueCap(capability);
1100 void CoreNetwork::serverCapAcknowledged(const QString& capability)
1102 // This may be called multiple times in certain situations.
1104 // Handle core-side configuration
1105 if (capability == IrcCap::AWAY_NOTIFY) {
1106 // away-notify enabled, stop the autoWho timers, handle manually
1107 setAutoWhoEnabled(false);
1110 // Handle capabilities that require further messages sent to the IRC server
1111 // If you change this list, ALSO change the list in CoreNetwork::capsRequiringServerMessages
1112 if (capability == IrcCap::SASL) {
1113 // If SASL mechanisms specified, limit to what's accepted for authentication
1114 // if the current identity has a cert set, use SASL EXTERNAL
1117 if (!identityPtr()->sslCert().isNull()) {
1118 if (saslMaybeSupports(IrcCap::SaslMech::EXTERNAL)) {
1119 // EXTERNAL authentication supported, send request
1120 putRawLine(serverEncode("AUTHENTICATE EXTERNAL"));
1123 showMessage(NetworkInternalMessage(
1125 BufferInfo::StatusBuffer,
1127 tr("SASL EXTERNAL authentication not supported")
1134 if (saslMaybeSupports(IrcCap::SaslMech::PLAIN)) {
1135 // PLAIN authentication supported, send request
1136 // Only working with PLAIN atm, blowfish later
1137 putRawLine(serverEncode("AUTHENTICATE PLAIN"));
1140 showMessage(NetworkInternalMessage(
1142 BufferInfo::StatusBuffer,
1144 tr("SASL PLAIN authentication not supported")
1154 void CoreNetwork::serverCapRemoved(const QString& capability)
1156 // This may be called multiple times in certain situations.
1158 // Handle special cases here
1159 if (capability == IrcCap::AWAY_NOTIFY) {
1160 // away-notify disabled, enable autoWho according to configuration
1161 setAutoWhoEnabled(networkConfig()->autoWhoEnabled());
1165 void CoreNetwork::queueCap(const QString& capability)
1167 // IRCv3 specs all use lowercase capability names
1168 QString _capLowercase = capability.toLower();
1170 if (capsRequiringConfiguration.contains(_capLowercase)) {
1171 // The capability requires additional configuration before being acknowledged (e.g. SASL),
1172 // so we should negotiate it separately from all other capabilities. Otherwise new
1173 // capabilities will be requested while still configuring the previous one.
1174 if (!_capsQueuedIndividual.contains(_capLowercase)) {
1175 _capsQueuedIndividual.append(_capLowercase);
1179 // The capability doesn't need any special configuration, so it should be safe to try
1180 // bundling together with others. "Should" being the imperative word, as IRC servers can do
1182 if (!_capsQueuedBundled.contains(_capLowercase)) {
1183 _capsQueuedBundled.append(_capLowercase);
1188 QString CoreNetwork::takeQueuedCaps()
1190 // Clear the record of the most recently negotiated capability bundle. Does nothing if the list
1192 _capsQueuedLastBundle.clear();
1194 // First, negotiate all the standalone capabilities that require additional configuration.
1195 if (!_capsQueuedIndividual.empty()) {
1196 // We have an individual capability available. Take the first and pass it back.
1197 return _capsQueuedIndividual.takeFirst();
1199 else if (!_capsQueuedBundled.empty()) {
1200 // We have capabilities available that can be grouped. Try to fit in as many as within the
1202 // See CoreNetwork::maxCapRequestLength
1204 // Response must have at least one capability regardless of max length for anything to
1206 QString capBundle = _capsQueuedBundled.takeFirst();
1207 QString nextCap("");
1208 while (!_capsQueuedBundled.empty()) {
1209 // As long as capabilities remain, get the next...
1210 nextCap = _capsQueuedBundled.first();
1211 if ((capBundle.length() + 1 + nextCap.length()) <= maxCapRequestLength) {
1212 // [capability + 1 for a space + this new capability] fit within length limits
1213 // Add it to formatted list
1214 capBundle.append(" " + nextCap);
1215 // Add it to most recent bundle of requested capabilities (simplifies retry logic)
1216 _capsQueuedLastBundle.append(nextCap);
1217 // Then remove it from the queue
1218 _capsQueuedBundled.removeFirst();
1221 // We've reached the length limit for a single capability request, stop adding more
1225 // Return this space-separated set of capabilities, removing any extra spaces
1226 return capBundle.trimmed();
1229 // No capabilities left to negotiate, return an empty string.
1234 void CoreNetwork::retryCapsIndividually()
1236 // The most recent set of capabilities got denied by the IRC server. As we don't know what got
1237 // denied, try each capability individually.
1238 if (_capsQueuedLastBundle.empty()) {
1239 // No most recently tried capability set, just return.
1241 // Note: there's little point in retrying individually requested caps during negotiation.
1242 // We know the individual capability was the one that failed, and it's not likely it'll
1243 // suddenly start working within a few seconds. 'cap-notify' provides a better system for
1244 // handling capability removal and addition.
1247 // This should be fairly rare, e.g. services restarting during negotiation, so simplicity wins
1248 // over efficiency. If this becomes an issue, implement a binary splicing system instead,
1249 // keeping track of which halves of the group fail, dividing the set each time.
1251 // Add most recently tried capability set to individual list, re-requesting them one at a time
1252 _capsQueuedIndividual.append(_capsQueuedLastBundle);
1253 // Warn of this issue to explain the slower login. Servers usually shouldn't trigger this.
1254 showMessage(NetworkInternalMessage(
1256 BufferInfo::StatusBuffer,
1258 tr("Could not negotiate some capabilities, retrying individually (%1)...").arg(_capsQueuedLastBundle.join(", "))
1260 // Capabilities are already removed from the capability bundle queue via takeQueuedCaps(), no
1261 // need to remove them here.
1262 // Clear the most recently tried set to reduce risk that mistakes elsewhere causes retrying
1264 _capsQueuedLastBundle.clear();
1267 void CoreNetwork::beginCapNegotiation()
1269 // Don't begin negotiation if no capabilities are queued to request
1270 if (!capNegotiationInProgress()) {
1271 // If the server doesn't have any capabilities, but supports CAP LS, continue on with the
1272 // normal connection.
1273 showMessage(NetworkInternalMessage(
1275 BufferInfo::StatusBuffer,
1277 tr("No capabilities available")
1279 endCapNegotiation();
1283 _capNegotiationActive = true;
1284 showMessage(NetworkInternalMessage(
1286 BufferInfo::StatusBuffer,
1288 tr("Ready to negotiate (found: %1)").arg(caps().join(", "))
1291 // Build a list of queued capabilities, starting with individual, then bundled, only adding the
1292 // comma separator between the two if needed (both individual and bundled caps exist).
1293 QString queuedCapsDisplay = _capsQueuedIndividual.join(", ")
1294 + ((!_capsQueuedIndividual.empty() && !_capsQueuedBundled.empty()) ? ", " : "")
1295 + _capsQueuedBundled.join(", ");
1296 showMessage(NetworkInternalMessage(
1298 BufferInfo::StatusBuffer,
1300 tr("Negotiating capabilities (requesting: %1)...").arg(queuedCapsDisplay)
1306 void CoreNetwork::sendNextCap()
1308 if (capNegotiationInProgress()) {
1309 // Request the next set of capabilities and remove them from the list
1310 putRawLine(serverEncode(QString("CAP REQ :%1").arg(takeQueuedCaps())));
1313 // No pending desired capabilities, capability negotiation finished
1314 // If SASL requested but not available, print a warning
1315 if (networkInfo().useSasl && !capEnabled(IrcCap::SASL))
1316 showMessage(NetworkInternalMessage(
1318 BufferInfo::StatusBuffer,
1320 tr("SASL authentication currently not supported by server")
1323 if (_capNegotiationActive) {
1324 showMessage(NetworkInternalMessage(
1326 BufferInfo::StatusBuffer,
1328 tr("Capability negotiation finished (enabled: %1)").arg(capsEnabled().join(", "))
1330 _capNegotiationActive = false;
1333 endCapNegotiation();
1337 void CoreNetwork::endCapNegotiation()
1339 // If nick registration is already complete, CAP END is not required
1340 if (!_capInitialNegotiationEnded) {
1341 putRawLine(serverEncode(QString("CAP END")));
1342 _capInitialNegotiationEnded = true;
1346 /******** AutoWHO ********/
1348 void CoreNetwork::startAutoWhoCycle()
1350 if (!_autoWhoQueue.isEmpty()) {
1351 _autoWhoCycleTimer.stop();
1354 _autoWhoQueue = channels();
1357 void CoreNetwork::queueAutoWhoOneshot(const QString& name)
1359 // Prepend so these new channels/nicks are the first to be checked
1360 // Don't allow duplicates
1361 if (!_autoWhoQueue.contains(name.toLower())) {
1362 _autoWhoQueue.prepend(name.toLower());
1364 if (capEnabled(IrcCap::AWAY_NOTIFY)) {
1365 // When away-notify is active, the timer's stopped. Start a new cycle to who this channel.
1366 setAutoWhoEnabled(true);
1370 void CoreNetwork::setAutoWhoDelay(int delay)
1372 _autoWhoTimer.setInterval(delay * 1000);
1375 void CoreNetwork::setAutoWhoInterval(int interval)
1377 _autoWhoCycleTimer.setInterval(interval * 1000);
1380 void CoreNetwork::setAutoWhoEnabled(bool enabled)
1382 if (enabled && isConnected() && !_autoWhoTimer.isActive())
1383 _autoWhoTimer.start();
1384 else if (!enabled) {
1385 _autoWhoTimer.stop();
1386 _autoWhoCycleTimer.stop();
1390 void CoreNetwork::sendAutoWho()
1392 // Don't send autowho if there are still some pending
1393 if (_autoWhoPending.count())
1396 while (!_autoWhoQueue.isEmpty()) {
1397 QString chanOrNick = _autoWhoQueue.takeFirst();
1398 // Check if it's a known channel or nick
1399 IrcChannel* ircchan = ircChannel(chanOrNick);
1400 IrcUser* ircuser = ircUser(chanOrNick);
1402 // Apply channel limiting rules
1403 // If using away-notify, don't impose channel size limits in order to capture away
1404 // state of everyone. Auto-who won't run on a timer so network impact is minimal.
1405 if (networkConfig()->autoWhoNickLimit() > 0 && ircchan->ircUsers().count() >= networkConfig()->autoWhoNickLimit()
1406 && !capEnabled(IrcCap::AWAY_NOTIFY))
1408 _autoWhoPending[chanOrNick.toLower()]++;
1411 // Checking a nick, add it to the pending list
1412 _autoWhoPending[ircuser->nick().toLower()]++;
1415 // Not a channel or a nick, skip it
1416 qDebug() << "Skipping who polling of unknown channel or nick" << chanOrNick;
1419 if (supports("WHOX")) {
1420 // Use WHO extended to poll away users and/or user accounts
1421 // Explicitly only match on nickname ("n"), don't rely on server defaults
1423 // WHO <nickname> n%chtsunfra,<unique_number>
1425 // See http://faerion.sourceforge.net/doc/irc/whox.var
1426 // And https://github.com/quakenet/snircd/blob/master/doc/readme.who
1427 // And https://github.com/hexchat/hexchat/blob/57478b65758e6b697b1d82ce21075e74aa475efc/src/common/proto-irc.c#L752
1428 putRawLine(serverEncode(
1429 QString("WHO %1 n%chtsunfra,%2")
1430 .arg(chanOrNick, QString::number(IrcCap::ACCOUNT_NOTIFY_WHOX_NUM))
1434 // Fall back to normal WHO
1436 // Note: According to RFC 1459, "WHO <phrase>" can fall back to searching realname,
1437 // hostmask, etc. There's nothing we can do about that :(
1439 // See https://tools.ietf.org/html/rfc1459#section-4.5.1
1440 putRawLine(serverEncode(QString("WHO %1").arg(chanOrNick)));
1445 if (_autoWhoQueue.isEmpty() && networkConfig()->autoWhoEnabled() && !_autoWhoCycleTimer.isActive() && !capEnabled(IrcCap::AWAY_NOTIFY)) {
1446 // Timer was stopped, means a new cycle is due immediately
1447 // Don't run a new cycle if using away-notify; server will notify as appropriate
1448 _autoWhoCycleTimer.start();
1449 startAutoWhoCycle();
1451 else if (capEnabled(IrcCap::AWAY_NOTIFY) && _autoWhoCycleTimer.isActive()) {
1452 // Don't run another who cycle if away-notify is enabled
1453 _autoWhoCycleTimer.stop();
1458 void CoreNetwork::onSslErrors(const QList<QSslError>& sslErrors)
1460 Server server = usedServer();
1461 if (server.sslVerify) {
1462 // Treat the SSL error as a hard error
1463 QString sslErrorMessage = tr("Encrypted connection couldn't be verified, disconnecting "
1464 "since verification is required");
1465 if (!sslErrors.empty()) {
1466 // Add the error reason if known
1467 sslErrorMessage.append(tr(" (Reason: %1)").arg(sslErrors.first().errorString()));
1469 showMessage(NetworkInternalMessage(
1471 BufferInfo::StatusBuffer,
1476 // Disconnect, triggering a reconnect in case it's a temporary issue with certificate
1477 // validity, network trouble, etc.
1478 disconnectFromIrc(false, QString("Encrypted connection not verified"), true /* withReconnect */);
1481 // Treat the SSL error as a warning, continue to connect anyways
1482 QString sslErrorMessage = tr("Encrypted connection couldn't be verified, continuing "
1483 "since verification is not required");
1484 if (!sslErrors.empty()) {
1485 // Add the error reason if known
1486 sslErrorMessage.append(tr(" (Reason: %1)").arg(sslErrors.first().errorString()));
1488 showMessage(NetworkInternalMessage(
1490 BufferInfo::StatusBuffer,
1495 // Proceed with the connection
1496 socket.ignoreSslErrors();
1502 void CoreNetwork::checkTokenBucket()
1504 if (_skipMessageRates) {
1505 if (_msgQueue.empty()) {
1506 // Message queue emptied; stop the timer and bail out
1507 _tokenBucketTimer.stop();
1510 // Otherwise, we're emptying the queue, continue on as normal
1513 // Process whatever messages are pending
1514 fillBucketAndProcessQueue();
1517 void CoreNetwork::fillBucketAndProcessQueue()
1519 // If there's less tokens than burst size, refill the token bucket by 1
1520 if (_tokenBucket < _burstSize) {
1524 // As long as there's tokens available and messages remaining, sending messages from the queue
1525 while (!_msgQueue.empty() && _tokenBucket > 0) {
1526 writeToSocket(_msgQueue.takeFirst());
1527 if (_metricsServer) {
1528 _metricsServer->messageQueue(userId(), _msgQueue.size());
1533 void CoreNetwork::writeToSocket(const QByteArray& data)
1535 // Log the message if enabled and network ID matches or allows all
1536 if (_debugLogRawIrc && (_debugLogRawNetId == -1 || networkId().toInt() == _debugLogRawNetId)) {
1537 // Include network ID
1538 qDebug() << "IRC net" << networkId() << ">>" << data;
1541 socket.write("\r\n");
1542 if (_metricsServer) {
1543 _metricsServer->transmitDataNetwork(userId(), data.size() + 2);
1545 if (!_skipMessageRates) {
1546 // Only subtract from the token bucket if message rate limiting is enabled
1551 Network::Server CoreNetwork::usedServer() const
1553 if (_lastUsedServerIndex < serverList().count())
1554 return serverList()[_lastUsedServerIndex];
1556 if (!serverList().isEmpty())
1557 return serverList()[0];
1559 return Network::Server();
1562 void CoreNetwork::requestConnect() const
1564 if (_shuttingDown) {
1567 if (connectionState() != Disconnected) {
1568 qWarning() << "Requesting connect while already being connected!";
1571 QMetaObject::invokeMethod(const_cast<CoreNetwork*>(this), "connectToIrc", Qt::QueuedConnection);
1574 void CoreNetwork::requestDisconnect() const
1576 if (_shuttingDown) {
1579 if (connectionState() == Disconnected) {
1580 qWarning() << "Requesting disconnect while not being connected!";
1583 userInputHandler()->handleQuit(BufferInfo(), QString());
1586 void CoreNetwork::requestSetNetworkInfo(const NetworkInfo& info)
1588 Network::Server currentServer = usedServer();
1589 setNetworkInfo(info);
1590 Core::updateNetwork(coreSession()->user(), info);
1592 // the order of the servers might have changed,
1593 // so we try to find the previously used server
1594 _lastUsedServerIndex = 0;
1595 for (int i = 0; i < serverList().count(); i++) {
1596 Network::Server server = serverList()[i];
1597 if (server.host == currentServer.host && server.port == currentServer.port) {
1598 _lastUsedServerIndex = i;
1604 QList<QList<QByteArray>> CoreNetwork::splitMessage(const QString& cmd,
1605 const QString& message,
1606 const std::function<QList<QByteArray>(QString&)>& cmdGenerator)
1608 QString wrkMsg(message);
1609 QList<QList<QByteArray>> msgsToSend;
1611 // do while (wrkMsg.size() > 0)
1613 // First, check to see if the whole message can be sent at once. The
1614 // cmdGenerator function is passed in by the caller and is used to encode
1615 // and encrypt (if applicable) the message, since different callers might
1616 // want to use different encoding or encode different values.
1617 int splitPos = wrkMsg.size();
1618 QList<QByteArray> initialSplitMsgEnc = cmdGenerator(wrkMsg);
1619 int initialOverrun = userInputHandler()->lastParamOverrun(cmd, initialSplitMsgEnc);
1621 if (initialOverrun) {
1622 // If the message was too long to be sent, first try splitting it along
1623 // word boundaries with QTextBoundaryFinder.
1624 QString splitMsg(wrkMsg);
1625 QTextBoundaryFinder qtbf(QTextBoundaryFinder::Word, splitMsg);
1626 qtbf.setPosition(initialSplitMsgEnc[1].size() - initialOverrun);
1627 QList<QByteArray> splitMsgEnc;
1628 int overrun = initialOverrun;
1631 splitPos = qtbf.toPreviousBoundary();
1633 // splitPos==-1 means the QTBF couldn't find a split point at all and
1634 // splitPos==0 means the QTBF could only find a boundary at the beginning of
1635 // the string. Neither one of these works for us.
1637 // If a split point could be found, split the message there, calculate the
1638 // overrun, and continue with the loop.
1639 splitMsg = splitMsg.left(splitPos);
1640 splitMsgEnc = cmdGenerator(splitMsg);
1641 overrun = userInputHandler()->lastParamOverrun(cmd, splitMsgEnc);
1644 // If a split point could not be found (the beginning of the message
1645 // is reached without finding a split point short enough to send) and we
1646 // are still in Word mode, switch to Grapheme mode. We also need to restore
1647 // the full wrkMsg to splitMsg, since splitMsg may have been cut down during
1648 // the previous attempt to find a split point.
1649 if (qtbf.type() == QTextBoundaryFinder::Word) {
1651 splitPos = splitMsg.size();
1652 QTextBoundaryFinder graphemeQtbf(QTextBoundaryFinder::Grapheme, splitMsg);
1653 graphemeQtbf.setPosition(initialSplitMsgEnc[1].size() - initialOverrun);
1654 qtbf = graphemeQtbf;
1657 // If the QTBF fails to find a split point in Grapheme mode, we give up.
1658 // This should never happen, but it should be handled anyway.
1659 qWarning() << "Unexpected failure to split message!";
1665 // Once a message of sendable length has been found, remove it from the wrkMsg and
1666 // add it to the list of messages to be sent.
1667 wrkMsg.remove(0, splitPos);
1668 msgsToSend.append(splitMsgEnc);
1671 // If the entire remaining message is short enough to be sent all at once, remove
1672 // it from the wrkMsg and add it to the list of messages to be sent.
1673 wrkMsg.remove(0, splitPos);
1674 msgsToSend.append(initialSplitMsgEnc);
1676 } while (wrkMsg.size() > 0);