DataStreamPeer: Optimize IrcUsersAndChannels init data
[quassel.git] / src / common / protocols / legacy / legacypeer.cpp
index 08f5b15..b292223 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2013 by the Quassel Project                        *
+ *   Copyright (C) 2005-2014 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -25,9 +25,9 @@
 #include "quassel.h"
 
 /* version.inc is no longer used for this */
-const int protocolVersion = 10;
-const int coreNeedsProtocol = protocolVersion;
-const int clientNeedsProtocol = protocolVersion;
+const uint protocolVersion = 10;
+const uint coreNeedsProtocol = protocolVersion;
+const uint clientNeedsProtocol = protocolVersion;
 
 using namespace Protocol;
 
@@ -38,8 +38,6 @@ LegacyPeer::LegacyPeer(::AuthHandler *authHandler, QTcpSocket *socket, QObject *
 {
     _stream.setDevice(socket);
     _stream.setVersion(QDataStream::Qt_4_2);
-
-    connect(socket, SIGNAL(readyRead()), SLOT(socketDataAvailable()));
 }
 
 
@@ -48,7 +46,6 @@ void LegacyPeer::setSignalProxy(::SignalProxy *proxy)
     RemotePeer::setSignalProxy(proxy);
 
     // FIXME only in compat mode
-    socket()->flush();
     if (proxy) {
         // enable compression now if requested - the initial handshake is uncompressed in the legacy protocol!
         _useCompression = socket()->property("UseCompression").toBool();
@@ -59,7 +56,7 @@ void LegacyPeer::setSignalProxy(::SignalProxy *proxy)
 }
 
 
-void LegacyPeer::socketDataAvailable()
+void LegacyPeer::onSocketDataAvailable()
 {
     QVariant item;
     while (readSocketData(item)) {
@@ -179,9 +176,9 @@ void LegacyPeer::handleHandshakeMessage(const QVariant &msg)
 
     if (msgType == "ClientInit") {
         // FIXME only in compat mode
-        int ver = m["ProtocolVersion"].toInt();
+        uint ver = m["ProtocolVersion"].toUInt();
         if (ver < coreNeedsProtocol) {
-            emit protocolVersionMismatch(ver, coreNeedsProtocol);
+            emit protocolVersionMismatch((int)ver, (int)coreNeedsProtocol);
             return;
         }
 
@@ -200,9 +197,9 @@ void LegacyPeer::handleHandshakeMessage(const QVariant &msg)
 
     else if (msgType == "ClientInitAck") {
         // FIXME only in compat mode
-        int ver = m["ProtocolVersion"].toInt();
+        uint ver = m["ProtocolVersion"].toUInt(); // actually an UInt
         if (ver < clientNeedsProtocol) {
-            emit protocolVersionMismatch(ver, clientNeedsProtocol);
+            emit protocolVersionMismatch((int)ver, (int)clientNeedsProtocol);
             return;
         }
 #ifndef QT_NO_COMPRESS
@@ -286,7 +283,7 @@ void LegacyPeer::dispatch(const ClientRegistered &msg) {
     // FIXME only in compat mode
     m["ProtocolVersion"] = protocolVersion;
     m["SupportSsl"] = msg.sslSupported;
-    m["SupportsCompression"] = socket()->property("UseCompression"); // this property gets already set in the ClientInit handler
+    m["SupportsCompression"] = socket()->property("UseCompression").toBool(); // this property gets already set in the ClientInit handler
 
     // This is only used for old v10 clients (pre-0.5)
     int uptime = msg.coreStartTime.secsTo(QDateTime::currentDateTime().toUTC());
@@ -302,7 +299,6 @@ void LegacyPeer::dispatch(const ClientRegistered &msg) {
     m["LoginEnabled"] = m["Configured"] = msg.coreConfigured;
 
     writeSocketData(m);
-    socket()->flush(); // ensure that the write cache is flushed before we switch to ssl
 }
 
 
@@ -441,6 +437,10 @@ void LegacyPeer::handlePackedFunc(const QVariant &packedFunc)
             QByteArray className = params[0].toByteArray();
             QString objectName = params[1].toString();
             QVariantMap initData = params[2].toMap();
+
+            // we need to special-case IrcUsersAndChannels here, since the format changed
+            if (className == "Network")
+                fromLegacyIrcUsersAndChannels(initData);
             handle(Protocol::InitData(className, objectName, initData));
             break;
         }
@@ -493,7 +493,14 @@ void LegacyPeer::dispatch(const Protocol::InitRequest &msg)
 
 void LegacyPeer::dispatch(const Protocol::InitData &msg)
 {
-    dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName << msg.initData);
+    // We need to special-case IrcUsersAndChannels, as the format changed
+    if (msg.className == "Network") {
+        QVariantMap initData = msg.initData;
+        toLegacyIrcUsersAndChannels(initData);
+        dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName << initData);
+    }
+    else
+        dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName << msg.initData);
 }
 
 
@@ -513,3 +520,72 @@ void LegacyPeer::dispatchPackedFunc(const QVariantList &packedFunc)
 {
     writeSocketData(QVariant(packedFunc));
 }
+
+
+// Handle the changed format for Network's initData
+// cf. Network::initIrcUsersAndChannels()
+void LegacyPeer::fromLegacyIrcUsersAndChannels(QVariantMap &initData)
+{
+    const QVariantMap &legacyMap = initData["IrcUsersAndChannels"].toMap();
+    QVariantMap newMap;
+
+    QHash<QString, QVariantList> users;
+    foreach(const QVariant &v, legacyMap["users"].toMap().values()) {
+        const QVariantMap &map = v.toMap();
+        foreach(const QString &key, map.keys())
+            users[key] << map[key];
+    }
+    QVariantMap userMap;
+    foreach(const QString &key, users.keys())
+        userMap[key] = users[key];
+    newMap["Users"] = userMap;
+
+    QHash<QString, QVariantList> channels;
+    foreach(const QVariant &v, legacyMap["channels"].toMap().values()) {
+        const QVariantMap &map = v.toMap();
+        foreach(const QString &key, map.keys())
+            channels[key] << map[key];
+    }
+    QVariantMap channelMap;
+    foreach(const QString &key, channels.keys())
+        channelMap[key] = channels[key];
+    newMap["Channels"] = channelMap;
+
+    initData["IrcUsersAndChannels"] = newMap;
+}
+
+
+void LegacyPeer::toLegacyIrcUsersAndChannels(QVariantMap &initData)
+{
+    const QVariantMap &usersAndChannels = initData["IrcUsersAndChannels"].toMap();
+    QVariantMap legacyMap;
+
+    // toMap() and toList() are cheap, so no need to copy to a hash
+
+    QVariantMap userMap;
+    const QVariantMap &users = usersAndChannels["Users"].toMap();
+
+    int size = users["nick"].toList().size(); // we know this key exists
+    for(int i = 0; i < size; i++) {
+        QVariantMap map;
+        foreach(const QString &key, users.keys())
+            map[key] = users[key].toList().at(i);
+        QString hostmask = QString("%1!%2@%3").arg(map["nick"].toString(), map["user"].toString(), map["host"].toString());
+        userMap[hostmask.toLower()] = map;
+    }
+    legacyMap["users"] = userMap;
+
+    QVariantMap channelMap;
+    const QVariantMap &channels = usersAndChannels["Channels"].toMap();
+
+    size = channels["name"].toList().size();
+    for(int i = 0; i < size; i++) {
+        QVariantMap map;
+        foreach(const QString &key, channels.keys())
+            map[key] = channels[key].toList().at(i);
+        channelMap[map["name"].toString().toLower()] = map;
+    }
+    legacyMap["channels"] = channelMap;
+
+    initData["IrcUsersAndChannels"] = legacyMap;
+}