1 /***************************************************************************
2 * Copyright (C) 2005-2020 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 "legacypeer.h"
23 #include <QDataStream>
24 #include <QHostAddress>
29 #include "serializers/serializers.h"
31 /* version.inc is no longer used for this */
32 const uint protocolVersion = 10;
33 const uint coreNeedsProtocol = protocolVersion;
34 const uint clientNeedsProtocol = protocolVersion;
36 using namespace Protocol;
38 LegacyPeer::LegacyPeer(::AuthHandler* authHandler, QTcpSocket* socket, Compressor::CompressionLevel level, QObject* parent)
39 : RemotePeer(authHandler, socket, level, parent)
40 , _useCompression(false)
43 void LegacyPeer::setSignalProxy(::SignalProxy* proxy)
45 RemotePeer::setSignalProxy(proxy);
47 // FIXME only in compat mode
49 // enable compression now if requested - the initial handshake is uncompressed in the legacy protocol!
50 _useCompression = socket()->property("UseCompression").toBool();
52 qDebug() << "Using compression for peer:" << qPrintable(address());
56 void LegacyPeer::processMessage(const QByteArray& msg)
58 QDataStream stream(msg);
59 stream.setVersion(QDataStream::Qt_4_2);
62 if (_useCompression) {
64 if (!Serializers::deserialize(stream, features(), rawItem)) {
65 close("Peer sent corrupt data: unable to load QVariant!");
69 int nbytes = rawItem.size();
71 const char* data = rawItem.constData();
72 if (nbytes < 4 || (data[0] != 0 || data[1] != 0 || data[2] != 0 || data[3] != 0)) {
73 close("Peer sent corrupted compressed data!");
78 rawItem = qUncompress(rawItem);
80 QDataStream itemStream(&rawItem, QIODevice::ReadOnly);
81 itemStream.setVersion(QDataStream::Qt_4_2);
82 if (!Serializers::deserialize(itemStream, features(), item)) {
83 close("Peer sent corrupt data: unable to load QVariant!");
88 if (!Serializers::deserialize(stream, features(), item)) {
89 close("Peer sent corrupt data: unable to load QVariant!");
94 if (stream.status() != QDataStream::Ok || !item.isValid()) {
95 close("Peer sent corrupt data: unable to load QVariant!");
99 // if no sigproxy is set, we're in handshake mode and let the data be handled elsewhere
101 handleHandshakeMessage(item);
103 handlePackedFunc(item);
106 void LegacyPeer::writeMessage(const QVariant& item)
109 QDataStream out(&block, QIODevice::WriteOnly);
110 out.setVersion(QDataStream::Qt_4_2);
112 if (_useCompression) {
114 QDataStream itemStream(&rawItem, QIODevice::WriteOnly);
115 itemStream.setVersion(QDataStream::Qt_4_2);
118 rawItem = qCompress(rawItem);
129 /*** Handshake messages ***/
131 /* These messages are transmitted during handshake phase, which in case of the legacy protocol means they have
132 * a structure different from those being used after the handshake.
133 * Also, the legacy handshake does not fully match the redesigned one, so we'll have to do various mappings here.
136 void LegacyPeer::handleHandshakeMessage(const QVariant& msg)
138 QVariantMap m = msg.toMap();
140 QString msgType = m["MsgType"].toString();
141 if (msgType.isEmpty()) {
142 emit protocolError(tr("Invalid handshake message!"));
146 if (msgType == "ClientInit") {
147 // FIXME only in compat mode
148 uint ver = m["ProtocolVersion"].toUInt();
149 if (ver < coreNeedsProtocol) {
150 emit protocolVersionMismatch((int)ver, (int)coreNeedsProtocol);
154 #ifndef QT_NO_COMPRESS
155 // FIXME only in compat mode
156 if (m["UseCompression"].toBool()) {
157 socket()->setProperty("UseCompression", true);
160 handle(RegisterClient{Quassel::Features{m["FeatureList"].toStringList(), Quassel::LegacyFeatures(m["Features"].toUInt())},
161 m["ClientVersion"].toString(),
162 m["ClientDate"].toString(),
163 m["UseSsl"].toBool()});
166 else if (msgType == "ClientInitReject") {
167 handle(ClientDenied(m["Error"].toString()));
170 else if (msgType == "ClientInitAck") {
171 // FIXME only in compat mode
172 uint ver = m["ProtocolVersion"].toUInt(); // actually an UInt
173 if (ver < clientNeedsProtocol) {
174 emit protocolVersionMismatch((int)ver, (int)clientNeedsProtocol);
177 #ifndef QT_NO_COMPRESS
178 if (m["SupportsCompression"].toBool())
179 socket()->setProperty("UseCompression", true);
182 handle(ClientRegistered{Quassel::Features{m["FeatureList"].toStringList(), Quassel::LegacyFeatures(m["CoreFeatures"].toUInt())},
183 m["Configured"].toBool(),
184 m["StorageBackends"].toList(),
185 m["Authenticators"].toList(),
186 m["SupportSsl"].toBool()});
189 else if (msgType == "CoreSetupData") {
190 QVariantMap map = m["SetupData"].toMap();
191 handle(SetupData(map["AdminUser"].toString(),
192 map["AdminPasswd"].toString(),
193 map["Backend"].toString(),
194 map["ConnectionProperties"].toMap(),
195 map["Authenticator"].toString(),
196 map["AuthProperties"].toMap()));
199 else if (msgType == "CoreSetupReject") {
200 handle(SetupFailed(m["Error"].toString()));
203 else if (msgType == "CoreSetupAck") {
207 else if (msgType == "ClientLogin") {
208 handle(Login(m["User"].toString(), m["Password"].toString()));
211 else if (msgType == "ClientLoginReject") {
212 handle(LoginFailed(m["Error"].toString()));
215 else if (msgType == "ClientLoginAck") {
216 handle(LoginSuccess());
219 else if (msgType == "SessionInit") {
220 QVariantMap map = m["SessionState"].toMap();
221 handle(SessionState(map["Identities"].toList(), map["BufferInfos"].toList(), map["NetworkIds"].toList()));
225 emit protocolError(tr("Unknown protocol message of type %1").arg(msgType));
229 void LegacyPeer::dispatch(const RegisterClient& msg)
232 m["MsgType"] = "ClientInit";
233 m["Features"] = static_cast<quint32>(msg.features.toLegacyFeatures());
234 m["FeatureList"] = msg.features.toStringList();
235 m["ClientVersion"] = msg.clientVersion;
236 m["ClientDate"] = msg.buildDate;
238 // FIXME only in compat mode
239 m["ProtocolVersion"] = protocolVersion;
241 #ifndef QT_NO_COMPRESS
242 m["UseCompression"] = true;
244 m["UseCompression"] = false;
250 void LegacyPeer::dispatch(const ClientDenied& msg)
253 m["MsgType"] = "ClientInitReject";
254 m["Error"] = msg.errorString;
259 void LegacyPeer::dispatch(const ClientRegistered& msg)
262 m["MsgType"] = "ClientInitAck";
263 if (hasFeature(Quassel::Feature::ExtendedFeatures)) {
264 m["FeatureList"] = msg.features.toStringList();
267 m["CoreFeatures"] = static_cast<quint32>(msg.features.toLegacyFeatures());
269 m["StorageBackends"] = msg.backendInfo;
270 if (hasFeature(Quassel::Feature::Authenticators)) {
271 m["Authenticators"] = msg.authenticatorInfo;
274 // FIXME only in compat mode
275 m["ProtocolVersion"] = protocolVersion;
276 m["SupportSsl"] = msg.sslSupported;
277 m["SupportsCompression"] = socket()->property("UseCompression").toBool(); // this property gets already set in the ClientInit handler
279 // This is only used for display by really old v10 clients (pre-0.5), and we no longer set this
280 m["CoreInfo"] = QString();
282 m["LoginEnabled"] = m["Configured"] = msg.coreConfigured;
287 void LegacyPeer::dispatch(const SetupData& msg)
290 map["AdminUser"] = msg.adminUser;
291 map["AdminPasswd"] = msg.adminPassword;
292 map["Backend"] = msg.backend;
293 map["ConnectionProperties"] = msg.setupData;
295 // Auth backend properties.
296 map["Authenticator"] = msg.authenticator;
297 map["AuthProperties"] = msg.authSetupData;
300 m["MsgType"] = "CoreSetupData";
301 m["SetupData"] = map;
305 void LegacyPeer::dispatch(const SetupFailed& msg)
308 m["MsgType"] = "CoreSetupReject";
309 m["Error"] = msg.errorString;
314 void LegacyPeer::dispatch(const SetupDone& msg)
319 m["MsgType"] = "CoreSetupAck";
324 void LegacyPeer::dispatch(const Login& msg)
327 m["MsgType"] = "ClientLogin";
328 m["User"] = msg.user;
329 m["Password"] = msg.password;
334 void LegacyPeer::dispatch(const LoginFailed& msg)
337 m["MsgType"] = "ClientLoginReject";
338 m["Error"] = msg.errorString;
343 void LegacyPeer::dispatch(const LoginSuccess& msg)
348 m["MsgType"] = "ClientLoginAck";
353 void LegacyPeer::dispatch(const SessionState& msg)
356 m["MsgType"] = "SessionInit";
359 map["BufferInfos"] = msg.bufferInfos;
360 map["NetworkIds"] = msg.networkIds;
361 map["Identities"] = msg.identities;
362 m["SessionState"] = map;
367 /*** Standard messages ***/
369 void LegacyPeer::handlePackedFunc(const QVariant& packedFunc)
371 QVariantList params(packedFunc.toList());
373 if (params.isEmpty()) {
374 qWarning() << Q_FUNC_INFO << "Received incompatible data:" << packedFunc;
378 // TODO: make sure that this is a valid request type
379 RequestType requestType = (RequestType)params.takeFirst().value<int>();
380 switch (requestType) {
382 if (params.count() < 3) {
383 qWarning() << Q_FUNC_INFO << "Received invalid sync call:" << params;
386 QByteArray className = params.takeFirst().toByteArray();
387 QString objectName = params.takeFirst().toString();
388 QByteArray slotName = params.takeFirst().toByteArray();
389 handle(Protocol::SyncMessage(className, objectName, slotName, params));
393 if (params.empty()) {
394 qWarning() << Q_FUNC_INFO << "Received empty RPC call!";
397 QByteArray signalName = params.takeFirst().toByteArray();
398 handle(Protocol::RpcCall(signalName, params));
402 if (params.count() != 2) {
403 qWarning() << Q_FUNC_INFO << "Received invalid InitRequest:" << params;
406 QByteArray className = params[0].toByteArray();
407 QString objectName = params[1].toString();
408 handle(Protocol::InitRequest(className, objectName));
412 if (params.count() != 3) {
413 qWarning() << Q_FUNC_INFO << "Received invalid InitData:" << params;
416 QByteArray className = params[0].toByteArray();
417 QString objectName = params[1].toString();
418 QVariantMap initData = params[2].toMap();
420 // we need to special-case IrcUsersAndChannels here, since the format changed
421 if (className == "Network")
422 fromLegacyIrcUsersAndChannels(initData);
423 handle(Protocol::InitData(className, objectName, initData));
427 if (params.count() != 1) {
428 qWarning() << Q_FUNC_INFO << "Received invalid HeartBeat:" << params;
431 // The legacy protocol would only send a QTime, no QDateTime
432 // so we assume it's sent today, which works in exactly the same cases as it did in the old implementation
433 QDateTime dateTime = QDateTime::currentDateTime().toUTC();
434 dateTime.setTime(params[0].toTime());
435 handle(Protocol::HeartBeat(dateTime));
438 case HeartBeatReply: {
439 if (params.count() != 1) {
440 qWarning() << Q_FUNC_INFO << "Received invalid HeartBeat:" << params;
443 // The legacy protocol would only send a QTime, no QDateTime
444 // so we assume it's sent today, which works in exactly the same cases as it did in the old implementation
445 QDateTime dateTime = QDateTime::currentDateTime().toUTC();
446 dateTime.setTime(params[0].toTime());
447 handle(Protocol::HeartBeatReply(dateTime));
453 void LegacyPeer::dispatch(const Protocol::SyncMessage& msg)
455 dispatchPackedFunc(QVariantList() << (qint16)Sync << msg.className << msg.objectName << msg.slotName << msg.params);
458 void LegacyPeer::dispatch(const Protocol::RpcCall& msg)
460 dispatchPackedFunc(QVariantList() << (qint16)RpcCall << msg.signalName << msg.params);
463 void LegacyPeer::dispatch(const Protocol::InitRequest& msg)
465 dispatchPackedFunc(QVariantList() << (qint16)InitRequest << msg.className << msg.objectName);
468 void LegacyPeer::dispatch(const Protocol::InitData& msg)
470 // We need to special-case IrcUsersAndChannels, as the format changed
471 if (msg.className == "Network") {
472 QVariantMap initData = msg.initData;
473 toLegacyIrcUsersAndChannels(initData);
474 dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName << initData);
477 dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName << msg.initData);
480 void LegacyPeer::dispatch(const Protocol::HeartBeat& msg)
482 dispatchPackedFunc(QVariantList() << (qint16)HeartBeat << msg.timestamp.time());
485 void LegacyPeer::dispatch(const Protocol::HeartBeatReply& msg)
487 dispatchPackedFunc(QVariantList() << (qint16)HeartBeatReply << msg.timestamp.time());
490 void LegacyPeer::dispatchPackedFunc(const QVariantList& packedFunc)
492 writeMessage(QVariant(packedFunc));
495 // Handle the changed format for Network's initData
496 // cf. Network::initIrcUsersAndChannels()
497 void LegacyPeer::fromLegacyIrcUsersAndChannels(QVariantMap& initData)
499 const QVariantMap& legacyMap = initData["IrcUsersAndChannels"].toMap();
502 QHash<QString, QVariantList> users;
503 foreach (const QVariant& v, legacyMap["users"].toMap().values()) {
504 const QVariantMap& map = v.toMap();
505 foreach (const QString& key, map.keys())
506 users[key] << map[key];
509 foreach (const QString& key, users.keys())
510 userMap[key] = users[key];
511 newMap["Users"] = userMap;
513 QHash<QString, QVariantList> channels;
514 foreach (const QVariant& v, legacyMap["channels"].toMap().values()) {
515 const QVariantMap& map = v.toMap();
516 foreach (const QString& key, map.keys())
517 channels[key] << map[key];
519 QVariantMap channelMap;
520 foreach (const QString& key, channels.keys())
521 channelMap[key] = channels[key];
522 newMap["Channels"] = channelMap;
524 initData["IrcUsersAndChannels"] = newMap;
527 void LegacyPeer::toLegacyIrcUsersAndChannels(QVariantMap& initData)
529 const QVariantMap& usersAndChannels = initData["IrcUsersAndChannels"].toMap();
530 QVariantMap legacyMap;
532 // toMap() and toList() are cheap, so no need to copy to a hash
535 const QVariantMap& users = usersAndChannels["Users"].toMap();
537 int size = users["nick"].toList().size(); // we know this key exists
538 for (int i = 0; i < size; i++) {
540 foreach (const QString& key, users.keys())
541 map[key] = users[key].toList().at(i);
542 QString hostmask = QString("%1!%2@%3").arg(map["nick"].toString(), map["user"].toString(), map["host"].toString());
543 userMap[hostmask.toLower()] = map;
545 legacyMap["users"] = userMap;
547 QVariantMap channelMap;
548 const QVariantMap& channels = usersAndChannels["Channels"].toMap();
550 size = channels["name"].toList().size();
551 for (int i = 0; i < size; i++) {
553 foreach (const QString& key, channels.keys())
554 map[key] = channels[key].toList().at(i);
555 channelMap[map["name"].toString().toLower()] = map;
557 legacyMap["channels"] = channelMap;
559 initData["IrcUsersAndChannels"] = legacyMap;