core: Clarify name of capsPendingNegotiation flag
[quassel.git] / src / core / corenetwork.cpp
index b1923c3..0dfb33b 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2019 by the Quassel Project                        *
+ *   Copyright (C) 2005-2020 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -22,6 +22,7 @@
 
 #include <QDebug>
 #include <QHostInfo>
+#include <QTextBoundaryFinder>
 
 #include "core.h"
 #include "coreidentity.h"
@@ -37,6 +38,7 @@ CoreNetwork::CoreNetwork(const NetworkId& networkid, CoreSession* session)
     : Network(networkid, session)
     , _coreSession(session)
     , _userInputHandler(new CoreUserInputHandler(this))
+    , _metricsServer(Core::instance()->metricsServer())
     , _autoReconnectCount(0)
     , _quitRequested(false)
     , _disconnectExpected(false)
@@ -184,6 +186,10 @@ void CoreNetwork::connectToIrc(bool reconnecting)
         _socketId = Core::instance()->identServer()->addWaitingSocket();
     }
 
+    if (_metricsServer) {
+        _metricsServer->addNetwork(userId());
+    }
+
     if (!reconnecting && useAutoReconnect() && _autoReconnectCount == 0) {
         _autoReconnectTimer.setInterval(autoReconnectInterval() * 1000);
         if (unlimitedReconnectRetries())
@@ -290,6 +296,9 @@ void CoreNetwork::disconnectFromIrc(bool requested, const QString& reason, bool
     }
     disablePingTimeout();
     _msgQueue.clear();
+    if (_metricsServer) {
+        _metricsServer->messageQueue(userId(), 0);
+    }
 
     IrcUser* me_ = me();
     if (me_) {
@@ -366,6 +375,9 @@ void CoreNetwork::putRawLine(const QByteArray& s, bool prepend)
             // Add to back, waiting in order
             _msgQueue.append(s);
         }
+        if (_metricsServer) {
+            _metricsServer->messageQueue(userId(), _msgQueue.size());
+        }
     }
 }
 
@@ -505,6 +517,9 @@ void CoreNetwork::onSocketHasData()
 {
     while (socket.canReadLine()) {
         QByteArray s = socket.readLine();
+        if (_metricsServer) {
+            _metricsServer->receiveDataNetwork(userId(), s.size());
+        }
         if (s.endsWith("\r\n"))
             s.chop(2);
         else if (s.endsWith("\n"))
@@ -605,6 +620,9 @@ void CoreNetwork::onSocketDisconnected()
 {
     disablePingTimeout();
     _msgQueue.clear();
+    if (_metricsServer) {
+        _metricsServer->messageQueue(userId(), 0);
+    }
 
     _autoWhoCycleTimer.stop();
     _autoWhoTimer.stop();
@@ -644,6 +662,10 @@ void CoreNetwork::onSocketDisconnected()
         else
             _autoReconnectTimer.start();
     }
+
+    if (_metricsServer) {
+        _metricsServer->removeNetwork(userId());
+    }
 }
 
 void CoreNetwork::onSocketStateChanged(QAbstractSocket::SocketState socketState)
@@ -1245,16 +1267,37 @@ void CoreNetwork::retryCapsIndividually()
 
 void CoreNetwork::beginCapNegotiation()
 {
-    // Don't begin negotiation if no capabilities are queued to request
-    if (!capNegotiationInProgress()) {
-        // If the server doesn't have any capabilities, but supports CAP LS, continue on with the
-        // normal connection.
+    if (!capsPendingNegotiation()) {
+        // No capabilities are queued for request, determine the reason why
+        QString capStatusMsg;
+        if (caps().empty()) {
+            // The server doesn't provide any capabilities, but supports CAP LS
+            capStatusMsg = tr("No capabilities available");
+        }
+        else if (capsEnabled().empty()) {
+            // The server supports capabilities (caps() is not empty) but Quassel doesn't support
+            // anything offered.  This should be uncommon.
+            capStatusMsg =
+                    tr("None of the capabilities provided by the server are supported (found: %1)")
+                    .arg(caps().join(", "));
+        }
+        else {
+            // Quassel has enabled some capabilities, but there are no further capabilities that can
+            // be negotiated.
+            // (E.g. the user has manually run "/cap ls 302" after initial negotiation.)
+            capStatusMsg =
+                    tr("No additional capabilities are supported (found: %1; currently enabled: %2)")
+                    .arg(caps().join(", "), capsEnabled().join(", "));
+        }
+        // Inform the user of the situation
         showMessage(NetworkInternalMessage(
             Message::Server,
             BufferInfo::StatusBuffer,
             "",
-            tr("No capabilities available")
+            capStatusMsg
         ));
+
+        // End any ongoing capability negotiation, allowing connection to continue
         endCapNegotiation();
         return;
     }
@@ -1284,7 +1327,7 @@ void CoreNetwork::beginCapNegotiation()
 
 void CoreNetwork::sendNextCap()
 {
-    if (capNegotiationInProgress()) {
+    if (capsPendingNegotiation()) {
         // Request the next set of capabilities and remove them from the list
         putRawLine(serverEncode(QString("CAP REQ :%1").arg(takeQueuedCaps())));
     }
@@ -1405,7 +1448,9 @@ void CoreNetwork::sendAutoWho()
             // And https://github.com/quakenet/snircd/blob/master/doc/readme.who
             // And https://github.com/hexchat/hexchat/blob/57478b65758e6b697b1d82ce21075e74aa475efc/src/common/proto-irc.c#L752
             putRawLine(serverEncode(
-                QString("WHO %1 n%chtsunfra,%2").arg(serverEncode(chanOrNick), QString::number(IrcCap::ACCOUNT_NOTIFY_WHOX_NUM))));
+                QString("WHO %1 n%chtsunfra,%2")
+                    .arg(chanOrNick, QString::number(IrcCap::ACCOUNT_NOTIFY_WHOX_NUM))
+            ));
         }
         else {
             // Fall back to normal WHO
@@ -1501,6 +1546,9 @@ void CoreNetwork::fillBucketAndProcessQueue()
     // As long as there's tokens available and messages remaining, sending messages from the queue
     while (!_msgQueue.empty() && _tokenBucket > 0) {
         writeToSocket(_msgQueue.takeFirst());
+        if (_metricsServer) {
+            _metricsServer->messageQueue(userId(), _msgQueue.size());
+        }
     }
 }
 
@@ -1513,6 +1561,9 @@ void CoreNetwork::writeToSocket(const QByteArray& data)
     }
     socket.write(data);
     socket.write("\r\n");
+    if (_metricsServer) {
+        _metricsServer->transmitDataNetwork(userId(), data.size() + 2);
+    }
     if (!_skipMessageRates) {
         // Only subtract from the token bucket if message rate limiting is enabled
         _tokenBucket--;