-void Core::clientDisconnected() {
- QTcpSocket *socket = dynamic_cast<QTcpSocket*>(sender());
- blocksizes.remove(socket);
- clientInfo.remove(socket);
- qDebug() << qPrintable(tr("Client %1 disconnected.").arg(socket->peerAddress().toString()));
- socket->deleteLater();
- socket = 0;
-
- // make server listen again if still not configured FIXME
- if (!configured) {
- startListening();
- }
-
- // TODO remove unneeded sessions - if necessary/possible...
- // Suggestion: kill sessions if they are not connected to any network and client.
-}
-
-void Core::processCoreSetup(QTcpSocket *socket, QVariantMap &msg) {
- if(msg["HasSettings"].toBool()) {
- QVariantMap auth;
- auth["User"] = msg["User"];
- auth["Password"] = msg["Password"];
- msg.remove("User");
- msg.remove("Password");
- qDebug() << "Initializing storage provider" << msg["Type"].toString();
-
- if(!initStorage(msg, true)) {
- // notify client to start wizard again
- qWarning("Core is currently not configured!");
- QVariantMap reply;
- reply["StartWizard"] = true;
- reply["StorageProviders"] = availableStorageProviders();
- SignalProxy::writeDataToDevice(socket, reply);
- } else {
- // write coresettings
- CoreSettings s;
- s.setDatabaseSettings(msg);
- // write admin user to database & make the core listen again to connections
- storage->addUser(auth["User"].toString(), auth["Password"].toString());
- startListening();
- // continue the normal procedure
- //processClientInit(socket, auth);
- }
- } else {
- // notify client to start wizard
- QVariantMap reply;
- reply["StartWizard"] = true;
- reply["StorageProviders"] = availableStorageProviders();
- SignalProxy::writeDataToDevice(socket, reply);
- }
-}
-
-void Core::setupClientSession(QTcpSocket *socket, UserId uid) {
- // Find or create session for validated user
- SessionThread *sess;
- if(sessions.contains(uid)) sess = sessions[uid];
- else sess = createSession(uid);
- // Hand over socket, session then sends state itself
- disconnect(socket, 0, this, 0);
- if(!sess) {
- qWarning() << qPrintable(tr("Could not initialize session for client %1!").arg(socket->peerAddress().toString()));
- socket->close();
- }
- sess->addClient(socket);
-}
-
-SessionThread *Core::createSession(UserId uid, bool restore) {
- if(sessions.contains(uid)) {
- qWarning() << "Calling createSession() when a session for the user already exists!";
- return 0;
- }
- SessionThread *sess = new SessionThread(uid, restore, this);
- sessions[uid] = sess;
- sess->start();
- return sess;
-}
-
-QStringList Core::availableStorageProviders() {
- QStringList storageProviders;
- if (SqliteStorage::isAvailable()) {
- storageProviders.append(SqliteStorage::displayName());
- }
- // TODO: temporary
- // storageProviders.append("MySQL");
-
- return storageProviders;
+void Core::clientDisconnected()
+{
+ CoreAuthHandler *handler = qobject_cast<CoreAuthHandler *>(sender());
+ Q_ASSERT(handler);
+
+ quInfo() << qPrintable(tr("Non-authed client disconnected:")) << qPrintable(handler->socket()->peerAddress().toString());
+ _connectingClients.remove(handler);
+ handler->deleteLater();
+
+ // make server listen again if still not configured
+ if (!_configured) {
+ startListening();
+ }
+
+ // TODO remove unneeded sessions - if necessary/possible...
+ // Suggestion: kill sessions if they are not connected to any network and client.
+}
+
+
+void Core::setupClientSession(RemotePeer *peer, UserId uid)
+{
+ CoreAuthHandler *handler = qobject_cast<CoreAuthHandler *>(sender());
+ Q_ASSERT(handler);
+
+ // From now on everything is handled by the client session
+ disconnect(handler, 0, this, 0);
+ _connectingClients.remove(handler);
+ handler->deleteLater();
+
+ // Find or create session for validated user
+ sessionForUser(uid);
+
+ // as we are currently handling an event triggered by incoming data on this socket
+ // it is unsafe to directly move the socket to the client thread.
+ QCoreApplication::postEvent(this, new AddClientEvent(peer, uid));
+}
+
+
+void Core::customEvent(QEvent *event)
+{
+ if (event->type() == AddClientEventId) {
+ AddClientEvent *addClientEvent = static_cast<AddClientEvent *>(event);
+ addClientHelper(addClientEvent->peer, addClientEvent->userId);
+ return;
+ }
+}
+
+
+void Core::addClientHelper(RemotePeer *peer, UserId uid)
+{
+ // Find or create session for validated user
+ SessionThread *session = sessionForUser(uid);
+ session->addClient(peer);
+}
+
+
+void Core::connectInternalPeer(QPointer<InternalPeer> peer)
+{
+ if (_initialized && peer) {
+ setupInternalClientSession(peer);
+ }
+ else {
+ _pendingInternalConnection = peer;
+ }
+}
+
+
+void Core::setupInternalClientSession(QPointer<InternalPeer> clientPeer)
+{
+ if (!_configured) {
+ stopListening();
+ auto errorString = setupCoreForInternalUsage();
+ if (!errorString.isEmpty()) {
+ emit exitRequested(EXIT_FAILURE, errorString);
+ return;
+ }
+ }
+
+ UserId uid;
+ if (_storage) {
+ uid = _storage->internalUser();
+ }
+ else {
+ quWarning() << "Core::setupInternalClientSession(): You're trying to run monolithic Quassel with an unusable Backend! Go fix it!";
+ emit exitRequested(EXIT_FAILURE, tr("Cannot setup storage backend."));
+ return;
+ }
+
+ if (!clientPeer) {
+ quWarning() << "Client peer went away, not starting a session";
+ return;
+ }
+
+ InternalPeer *corePeer = new InternalPeer(this);
+ corePeer->setPeer(clientPeer);
+ clientPeer->setPeer(corePeer);
+
+ // Find or create session for validated user
+ SessionThread *sessionThread = sessionForUser(uid);
+ sessionThread->addClient(corePeer);
+}
+
+
+SessionThread *Core::sessionForUser(UserId uid, bool restore)
+{
+ if (_sessions.contains(uid))
+ return _sessions[uid];
+
+ SessionThread *session = new SessionThread(uid, restore, strictIdentEnabled(), this);
+ _sessions[uid] = session;
+ session->start();
+ return session;
+}
+
+
+void Core::socketError(QAbstractSocket::SocketError err, const QString &errorString)
+{
+ quWarning() << QString("Socket error %1: %2").arg(err).arg(errorString);
+}
+
+
+QVariantList Core::backendInfo()
+{
+ instance()->registerStorageBackends();
+
+ QVariantList backendInfos;
+ for (auto &&backend : instance()->_registeredStorageBackends) {
+ QVariantMap v;
+ v["BackendId"] = backend->backendId();
+ v["DisplayName"] = backend->displayName();
+ v["Description"] = backend->description();
+ v["SetupData"] = backend->setupData(); // ignored by legacy clients
+
+ // TODO Protocol Break: Remove legacy (cf. authenticatorInfo())
+ const auto &setupData = backend->setupData();
+ QStringList setupKeys;
+ QVariantMap setupDefaults;
+ for (int i = 0; i + 2 < setupData.size(); i += 3) {
+ setupKeys << setupData[i].toString();
+ setupDefaults[setupData[i].toString()] = setupData[i + 2];
+ }
+ v["SetupKeys"] = setupKeys;
+ v["SetupDefaults"] = setupDefaults;
+ // TODO Protocol Break: Remove
+ v["IsDefault"] = (backend->backendId() == "SQLite"); // newer clients will just use the first in the list
+
+ backendInfos << v;
+ }
+ return backendInfos;
+}
+
+
+QVariantList Core::authenticatorInfo()
+{
+ instance()->registerAuthenticators();
+
+ QVariantList authInfos;
+ for(auto &&backend : instance()->_registeredAuthenticators) {
+ QVariantMap v;
+ v["BackendId"] = backend->backendId();
+ v["DisplayName"] = backend->displayName();
+ v["Description"] = backend->description();
+ v["SetupData"] = backend->setupData();
+ authInfos << v;
+ }
+ return authInfos;
+}
+
+// migration / backend selection
+bool Core::selectBackend(const QString &backend)
+{
+ // reregister all storage backends
+ registerStorageBackends();
+ auto storage = storageBackend(backend);
+ if (!storage) {
+ QStringList backends;
+ std::transform(_registeredStorageBackends.begin(), _registeredStorageBackends.end(),
+ std::back_inserter(backends), [](const DeferredSharedPtr<Storage>& backend) {
+ return backend->displayName();
+ });
+ quWarning() << qPrintable(tr("Unsupported storage backend: %1").arg(backend));
+ quWarning() << qPrintable(tr("Supported backends are:")) << qPrintable(backends.join(", "));
+ return false;
+ }
+
+ QVariantMap settings = promptForSettings(storage.get());
+
+ Storage::State storageState = storage->init(settings);
+ switch (storageState) {
+ case Storage::IsReady:
+ if (!saveBackendSettings(backend, settings)) {
+ qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+ }
+ quWarning() << qPrintable(tr("Switched storage backend to: %1").arg(backend));
+ quWarning() << qPrintable(tr("Backend already initialized. Skipping Migration..."));
+ return true;
+ case Storage::NotAvailable:
+ qCritical() << qPrintable(tr("Storage backend is not available: %1").arg(backend));
+ return false;
+ case Storage::NeedsSetup:
+ if (!storage->setup(settings)) {
+ quWarning() << qPrintable(tr("Unable to setup storage backend: %1").arg(backend));
+ return false;
+ }
+
+ if (storage->init(settings) != Storage::IsReady) {
+ quWarning() << qPrintable(tr("Unable to initialize storage backend: %1").arg(backend));
+ return false;
+ }
+
+ if (!saveBackendSettings(backend, settings)) {
+ qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+ }
+ quWarning() << qPrintable(tr("Switched storage backend to: %1").arg(backend));
+ break;
+ }
+
+ // let's see if we have a current storage object we can migrate from
+ auto reader = getMigrationReader(_storage.get());
+ auto writer = getMigrationWriter(storage.get());
+ if (reader && writer) {
+ qDebug() << qPrintable(tr("Migrating storage backend %1 to %2...").arg(_storage->displayName(), storage->displayName()));
+ _storage.reset();
+ storage.reset();
+ if (reader->migrateTo(writer.get())) {
+ qDebug() << "Migration finished!";
+ qDebug() << qPrintable(tr("Migration finished!"));
+ if (!saveBackendSettings(backend, settings)) {
+ qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+ return false;
+ }
+ return true;
+ }
+ quWarning() << qPrintable(tr("Unable to migrate storage backend! (No migration writer for %1)").arg(backend));
+ return false;
+ }
+
+ // inform the user why we cannot merge
+ if (!_storage) {
+ quWarning() << qPrintable(tr("No currently active storage backend. Skipping migration..."));
+ }
+ else if (!reader) {
+ quWarning() << qPrintable(tr("Currently active storage backend does not support migration: %1").arg(_storage->displayName()));
+ }
+ if (writer) {
+ quWarning() << qPrintable(tr("New storage backend does not support migration: %1").arg(backend));
+ }
+
+ // so we were unable to merge, but let's create a user \o/
+ _storage = std::move(storage);
+ createUser();
+ return true;