18
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19
19
***************************************************************************/
21
#include <QHostAddress>
21
22
#include <QTcpSocket>
23
24
#include "legacypeer.h"
27
/* version.inc is no longer used for this */
28
const uint protocolVersion = 10;
29
const uint coreNeedsProtocol = protocolVersion;
30
const uint clientNeedsProtocol = protocolVersion;
25
32
using namespace Protocol;
27
LegacyPeer::LegacyPeer(QTcpSocket *socket, QObject *parent)
28
: RemotePeer(socket, parent),
34
LegacyPeer::LegacyPeer(::AuthHandler *authHandler, QTcpSocket *socket, QObject *parent)
35
: RemotePeer(authHandler, socket, Compressor::NoCompression, parent),
30
36
_useCompression(false)
32
_stream.setDevice(socket);
33
_stream.setVersion(QDataStream::Qt_4_2);
35
connect(socket, SIGNAL(readyRead()), SLOT(socketDataAvailable()));
41
44
RemotePeer::setSignalProxy(proxy);
46
// FIXME only in compat mode
44
48
// enable compression now if requested - the initial handshake is uncompressed in the legacy protocol!
45
49
_useCompression = socket()->property("UseCompression").toBool();
51
qDebug() << "Using compression for peer:" << qPrintable(socket()->peerAddress().toString());
51
void LegacyPeer::socketDataAvailable()
57
void LegacyPeer::processMessage(const QByteArray &msg)
59
QDataStream stream(msg);
60
stream.setVersion(QDataStream::Qt_4_2);
54
while (readSocketData(item)) {
55
// if no sigproxy is set, we're in handshake mode and let the data be handled elsewhere
57
emit dataReceived(item);
59
handlePackedFunc(item);
64
bool LegacyPeer::readSocketData(QVariant &item)
66
if (_blockSize == 0) {
67
if (socket()->bytesAvailable() < 4)
69
_stream >> _blockSize;
72
if (_blockSize > 1 << 22) {
73
close("Peer tried to send package larger than max package size!");
77
if (_blockSize == 0) {
78
close("Peer tried to send 0 byte package!");
82
if (socket()->bytesAvailable() < _blockSize) {
83
emit transferProgress(socket()->bytesAvailable(), _blockSize);
87
emit transferProgress(_blockSize, _blockSize);
91
63
if (_useCompression) {
92
64
QByteArray rawItem;
95
67
int nbytes = rawItem.size();
97
69
const char *data = rawItem.constData();
98
70
if (nbytes < 4 || (data[0] != 0 || data[1] != 0 || data[2] != 0 || data[3] != 0)) {
99
71
close("Peer sent corrupted compressed data!");
148
_stream << block; // also writes the length as part of the serialization format
123
/*** Handshake messages ***/
125
/* These messages are transmitted during handshake phase, which in case of the legacy protocol means they have
126
* a structure different from those being used after the handshake.
127
* Also, the legacy handshake does not fully match the redesigned one, so we'll have to do various mappings here.
130
void LegacyPeer::handleHandshakeMessage(const QVariant &msg)
132
QVariantMap m = msg.toMap();
134
QString msgType = m["MsgType"].toString();
135
if (msgType.isEmpty()) {
136
emit protocolError(tr("Invalid handshake message!"));
140
if (msgType == "ClientInit") {
141
// FIXME only in compat mode
142
uint ver = m["ProtocolVersion"].toUInt();
143
if (ver < coreNeedsProtocol) {
144
emit protocolVersionMismatch((int)ver, (int)coreNeedsProtocol);
148
#ifndef QT_NO_COMPRESS
149
// FIXME only in compat mode
150
if (m["UseCompression"].toBool()) {
151
socket()->setProperty("UseCompression", true);
154
handle(RegisterClient(m["ClientVersion"].toString(), m["UseSsl"].toBool()));
157
else if (msgType == "ClientInitReject") {
158
handle(ClientDenied(m["Error"].toString()));
161
else if (msgType == "ClientInitAck") {
162
// FIXME only in compat mode
163
uint ver = m["ProtocolVersion"].toUInt(); // actually an UInt
164
if (ver < clientNeedsProtocol) {
165
emit protocolVersionMismatch((int)ver, (int)clientNeedsProtocol);
168
#ifndef QT_NO_COMPRESS
169
if (m["SupportsCompression"].toBool())
170
socket()->setProperty("UseCompression", true);
173
handle(ClientRegistered(m["CoreFeatures"].toUInt(), m["Configured"].toBool(), m["StorageBackends"].toList(), m["SupportSsl"].toBool(), QDateTime()));
176
else if (msgType == "CoreSetupData") {
177
QVariantMap map = m["SetupData"].toMap();
178
handle(SetupData(map["AdminUser"].toString(), map["AdminPasswd"].toString(), map["Backend"].toString(), map["ConnectionProperties"].toMap()));
181
else if (msgType == "CoreSetupReject") {
182
handle(SetupFailed(m["Error"].toString()));
185
else if (msgType == "CoreSetupAck") {
189
else if (msgType == "ClientLogin") {
190
handle(Login(m["User"].toString(), m["Password"].toString()));
193
else if (msgType == "ClientLoginReject") {
194
handle(LoginFailed(m["Error"].toString()));
197
else if (msgType == "ClientLoginAck") {
198
handle(LoginSuccess());
201
else if (msgType == "SessionInit") {
202
QVariantMap map = m["SessionState"].toMap();
203
handle(SessionState(map["Identities"].toList(), map["BufferInfos"].toList(), map["NetworkIds"].toList()));
207
emit protocolError(tr("Unknown protocol message of type %1").arg(msgType));
212
void LegacyPeer::dispatch(const RegisterClient &msg) {
214
m["MsgType"] = "ClientInit";
215
m["ClientVersion"] = msg.clientVersion;
216
m["ClientDate"] = Quassel::buildInfo().buildDate;
218
// FIXME only in compat mode
219
m["ProtocolVersion"] = protocolVersion;
220
m["UseSsl"] = msg.sslSupported;
221
#ifndef QT_NO_COMPRESS
222
m["UseCompression"] = true;
224
m["UseCompression"] = false;
231
void LegacyPeer::dispatch(const ClientDenied &msg) {
233
m["MsgType"] = "ClientInitReject";
234
m["Error"] = msg.errorString;
240
void LegacyPeer::dispatch(const ClientRegistered &msg) {
242
m["MsgType"] = "ClientInitAck";
243
m["CoreFeatures"] = msg.coreFeatures;
244
m["StorageBackends"] = msg.backendInfo;
246
// FIXME only in compat mode
247
m["ProtocolVersion"] = protocolVersion;
248
m["SupportSsl"] = msg.sslSupported;
249
m["SupportsCompression"] = socket()->property("UseCompression").toBool(); // this property gets already set in the ClientInit handler
251
// This is only used for old v10 clients (pre-0.5)
252
int uptime = msg.coreStartTime.secsTo(QDateTime::currentDateTime().toUTC());
253
int updays = uptime / 86400; uptime %= 86400;
254
int uphours = uptime / 3600; uptime %= 3600;
255
int upmins = uptime / 60;
256
m["CoreInfo"] = tr("<b>Quassel Core Version %1</b><br>"
258
"Up %3d%4h%5m (since %6)").arg(Quassel::buildInfo().fancyVersionString)
259
.arg(Quassel::buildInfo().buildDate)
260
.arg(updays).arg(uphours, 2, 10, QChar('0')).arg(upmins, 2, 10, QChar('0')).arg(msg.coreStartTime.toString(Qt::TextDate));
262
m["LoginEnabled"] = m["Configured"] = msg.coreConfigured;
268
void LegacyPeer::dispatch(const SetupData &msg)
271
map["AdminUser"] = msg.adminUser;
272
map["AdminPasswd"] = msg.adminPassword;
273
map["Backend"] = msg.backend;
274
map["ConnectionProperties"] = msg.setupData;
277
m["MsgType"] = "CoreSetupData";
278
m["SetupData"] = map;
283
void LegacyPeer::dispatch(const SetupFailed &msg)
286
m["MsgType"] = "CoreSetupReject";
287
m["Error"] = msg.errorString;
293
void LegacyPeer::dispatch(const SetupDone &msg)
298
m["MsgType"] = "CoreSetupAck";
304
void LegacyPeer::dispatch(const Login &msg)
307
m["MsgType"] = "ClientLogin";
308
m["User"] = msg.user;
309
m["Password"] = msg.password;
315
void LegacyPeer::dispatch(const LoginFailed &msg)
318
m["MsgType"] = "ClientLoginReject";
319
m["Error"] = msg.errorString;
325
void LegacyPeer::dispatch(const LoginSuccess &msg)
330
m["MsgType"] = "ClientLoginAck";
336
void LegacyPeer::dispatch(const SessionState &msg)
339
m["MsgType"] = "SessionInit";
342
map["BufferInfos"] = msg.bufferInfos;
343
map["NetworkIds"] = msg.networkIds;
344
map["Identities"] = msg.identities;
345
m["SessionState"] = map;
351
/*** Standard messages ***/
152
353
void LegacyPeer::handlePackedFunc(const QVariant &packedFunc)
233
439
void LegacyPeer::dispatch(const Protocol::SyncMessage &msg)
235
dispatchPackedFunc(QVariantList() << (qint16)Sync << msg.className() << msg.objectName() << msg.slotName() << msg.params());
441
dispatchPackedFunc(QVariantList() << (qint16)Sync << msg.className << msg.objectName << msg.slotName << msg.params);
239
445
void LegacyPeer::dispatch(const Protocol::RpcCall &msg)
241
dispatchPackedFunc(QVariantList() << (qint16)RpcCall << msg.slotName() << msg.params());
447
dispatchPackedFunc(QVariantList() << (qint16)RpcCall << msg.slotName << msg.params);
245
451
void LegacyPeer::dispatch(const Protocol::InitRequest &msg)
247
dispatchPackedFunc(QVariantList() << (qint16)InitRequest << msg.className() << msg.objectName());
453
dispatchPackedFunc(QVariantList() << (qint16)InitRequest << msg.className << msg.objectName);
251
457
void LegacyPeer::dispatch(const Protocol::InitData &msg)
253
dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className() << msg.objectName() << msg.initData());
459
// We need to special-case IrcUsersAndChannels, as the format changed
460
if (msg.className == "Network") {
461
QVariantMap initData = msg.initData;
462
toLegacyIrcUsersAndChannels(initData);
463
dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName << initData);
466
dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className << msg.objectName << msg.initData);
257
470
void LegacyPeer::dispatch(const Protocol::HeartBeat &msg)
259
dispatchPackedFunc(QVariantList() << (qint16)HeartBeat << msg.timestamp().time());
472
dispatchPackedFunc(QVariantList() << (qint16)HeartBeat << msg.timestamp.time());
263
476
void LegacyPeer::dispatch(const Protocol::HeartBeatReply &msg)
265
dispatchPackedFunc(QVariantList() << (qint16)HeartBeatReply << msg.timestamp().time());
478
dispatchPackedFunc(QVariantList() << (qint16)HeartBeatReply << msg.timestamp.time());
269
482
void LegacyPeer::dispatchPackedFunc(const QVariantList &packedFunc)
271
writeSocketData(QVariant(packedFunc));
484
writeMessage(QVariant(packedFunc));
488
// Handle the changed format for Network's initData
489
// cf. Network::initIrcUsersAndChannels()
490
void LegacyPeer::fromLegacyIrcUsersAndChannels(QVariantMap &initData)
492
const QVariantMap &legacyMap = initData["IrcUsersAndChannels"].toMap();
495
QHash<QString, QVariantList> users;
496
foreach(const QVariant &v, legacyMap["users"].toMap().values()) {
497
const QVariantMap &map = v.toMap();
498
foreach(const QString &key, map.keys())
499
users[key] << map[key];
502
foreach(const QString &key, users.keys())
503
userMap[key] = users[key];
504
newMap["Users"] = userMap;
506
QHash<QString, QVariantList> channels;
507
foreach(const QVariant &v, legacyMap["channels"].toMap().values()) {
508
const QVariantMap &map = v.toMap();
509
foreach(const QString &key, map.keys())
510
channels[key] << map[key];
512
QVariantMap channelMap;
513
foreach(const QString &key, channels.keys())
514
channelMap[key] = channels[key];
515
newMap["Channels"] = channelMap;
517
initData["IrcUsersAndChannels"] = newMap;
521
void LegacyPeer::toLegacyIrcUsersAndChannels(QVariantMap &initData)
523
const QVariantMap &usersAndChannels = initData["IrcUsersAndChannels"].toMap();
524
QVariantMap legacyMap;
526
// toMap() and toList() are cheap, so no need to copy to a hash
529
const QVariantMap &users = usersAndChannels["Users"].toMap();
531
int size = users["nick"].toList().size(); // we know this key exists
532
for(int i = 0; i < size; i++) {
534
foreach(const QString &key, users.keys())
535
map[key] = users[key].toList().at(i);
536
QString hostmask = QString("%1!%2@%3").arg(map["nick"].toString(), map["user"].toString(), map["host"].toString());
537
userMap[hostmask.toLower()] = map;
539
legacyMap["users"] = userMap;
541
QVariantMap channelMap;
542
const QVariantMap &channels = usersAndChannels["Channels"].toMap();
544
size = channels["name"].toList().size();
545
for(int i = 0; i < size; i++) {
547
foreach(const QString &key, channels.keys())
548
map[key] = channels[key].toList().at(i);
549
channelMap[map["name"].toString().toLower()] = map;
551
legacyMap["channels"] = channelMap;
553
initData["IrcUsersAndChannels"] = legacyMap;