-void Core::processClientMessage(const QVariant &data)
-{
- RemoteConnection *connection = qobject_cast<RemoteConnection *>(sender());
- if (!connection) {
- qWarning() << Q_FUNC_INFO << "Message not sent by RemoteConnection!";
- return;
- }
-
- QVariantMap msg = data.toMap();
- if (!msg.contains("MsgType")) {
- // Client is way too old, does not even use the current init format
- qWarning() << qPrintable(tr("Antique client trying to connect... refusing."));
- connection->close();
- return;
- }
-
- // OK, so we have at least an init message format we can understand
- if (msg["MsgType"] == "ClientInit") {
- QVariantMap reply;
-
- // Just version information -- check it!
- uint ver = msg["ProtocolVersion"].toUInt();
- if (ver < Quassel::buildInfo().coreNeedsProtocol) {
- reply["MsgType"] = "ClientInitReject";
- reply["Error"] = tr("<b>Your Quassel Client is too old!</b><br>"
- "This core needs at least client/core protocol version %1.<br>"
- "Please consider upgrading your client.").arg(Quassel::buildInfo().coreNeedsProtocol);
- connection->writeSocketData(reply);
- qWarning() << qPrintable(tr("Client")) << connection->description() << qPrintable(tr("too old, rejecting."));
- connection->close();
- return;
- }
-
- reply["ProtocolVersion"] = Quassel::buildInfo().protocolVersion;
- reply["CoreVersion"] = Quassel::buildInfo().fancyVersionString;
- reply["CoreDate"] = Quassel::buildInfo().buildDate;
- reply["CoreStartTime"] = startTime(); // v10 clients don't necessarily parse this, see below
-
- // FIXME: newer clients no longer use the hardcoded CoreInfo (for now), since it gets the
- // time zone wrong. With the next protocol bump (10 -> 11), we should remove this
- // or make it properly configurable.
-
- int uptime = startTime().secsTo(QDateTime::currentDateTime().toUTC());
- int updays = uptime / 86400; uptime %= 86400;
- int uphours = uptime / 3600; uptime %= 3600;
- int upmins = uptime / 60;
- reply["CoreInfo"] = tr("<b>Quassel Core Version %1</b><br>"
- "Built: %2<br>"
- "Up %3d%4h%5m (since %6)").arg(Quassel::buildInfo().fancyVersionString)
- .arg(Quassel::buildInfo().buildDate)
- .arg(updays).arg(uphours, 2, 10, QChar('0')).arg(upmins, 2, 10, QChar('0')).arg(startTime().toString(Qt::TextDate));
-
- reply["CoreFeatures"] = (int)Quassel::features();
-
-#ifdef HAVE_SSL
- SslServer *sslServer = qobject_cast<SslServer *>(&_server);
- QSslSocket *sslSocket = qobject_cast<QSslSocket *>(connection->socket());
- bool supportSsl = sslServer && sslSocket && sslServer->isCertValid();
-#else
- bool supportSsl = false;
-#endif
-
-#ifndef QT_NO_COMPRESS
- bool supportsCompression = true;
-#else
- bool supportsCompression = false;
-#endif
-
- reply["SupportSsl"] = supportSsl;
- reply["SupportsCompression"] = supportsCompression;
- // switch to ssl/compression after client has been informed about our capabilities (see below)
-
- reply["LoginEnabled"] = true;
-
- // check if we are configured, start wizard otherwise
- if (!_configured) {
- reply["Configured"] = false;
- QList<QVariant> backends;
- foreach(Storage *backend, _storageBackends.values()) {
- QVariantMap v;
- v["DisplayName"] = backend->displayName();
- v["Description"] = backend->description();
- v["SetupKeys"] = backend->setupKeys();
- v["SetupDefaults"] = backend->setupDefaults();
- backends.append(v);
- }
- reply["StorageBackends"] = backends;
- reply["LoginEnabled"] = false;
- }
- else {
- reply["Configured"] = true;
- }
- clientInfo[connection] = msg; // store for future reference
- reply["MsgType"] = "ClientInitAck";
- connection->writeSocketData(reply);
- connection->socket()->flush(); // ensure that the write cache is flushed before we switch to ssl
-
-#ifdef HAVE_SSL
- // after we told the client that we are ssl capable we switch to ssl mode
- if (supportSsl && msg["UseSsl"].toBool()) {
- qDebug() << qPrintable(tr("Starting TLS for Client:")) << connection->description();
- connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), SLOT(sslErrors(const QList<QSslError> &)));
- sslSocket->startServerEncryption();
- }
-#endif
-
-#ifndef QT_NO_COMPRESS
- if (supportsCompression && msg["UseCompression"].toBool()) {
- connection->socket()->setProperty("UseCompression", true);
- qDebug() << "Using compression for Client:" << qPrintable(connection->socket()->peerAddress().toString());
- }
-#endif
- }
- else {
- // for the rest, we need an initialized connection
- if (!clientInfo.contains(connection)) {
- QVariantMap reply;
- reply["MsgType"] = "ClientLoginReject";
- reply["Error"] = tr("<b>Client not initialized!</b><br>You need to send an init message before trying to login.");
- connection->writeSocketData(reply);
- qWarning() << qPrintable(tr("Client")) << qPrintable(connection->socket()->peerAddress().toString()) << qPrintable(tr("did not send an init message before trying to login, rejecting."));
- connection->close(); return;
- }
- if (msg["MsgType"] == "CoreSetupData") {
- QVariantMap reply;
- QString result = setupCore(msg["SetupData"].toMap());
- if (!result.isEmpty()) {
- reply["MsgType"] = "CoreSetupReject";
- reply["Error"] = result;
- }
- else {
- reply["MsgType"] = "CoreSetupAck";
- }
- connection->writeSocketData(reply);
- }
- else if (msg["MsgType"] == "ClientLogin") {
- QVariantMap reply;
- UserId uid = _storage->validateUser(msg["User"].toString(), msg["Password"].toString());
- if (uid == 0) {
- reply["MsgType"] = "ClientLoginReject";
- reply["Error"] = tr("<b>Invalid username or password!</b><br>The username/password combination you supplied could not be found in the database.");
- connection->writeSocketData(reply);
- return;
- }
- reply["MsgType"] = "ClientLoginAck";
- connection->writeSocketData(reply);
- quInfo() << qPrintable(tr("Client")) << qPrintable(connection->socket()->peerAddress().toString()) << qPrintable(tr("initialized and authenticated successfully as \"%1\" (UserId: %2).").arg(msg["User"].toString()).arg(uid.toInt()));
- setupClientSession(connection, uid);
- }
- }
-}
-
-