+ if (_identServer) {
+ _identServer->startListening();
+ }
+
+ if (_metricsServer) {
+ _metricsServer->startListening();
+ }
+
+ return success;
+}
+
+void Core::stopListening(const QString& reason)
+{
+ if (_identServer) {
+ _identServer->stopListening(reason);
+ }
+
+ if (_metricsServer) {
+ _metricsServer->stopListening(reason);
+ }
+
+ bool wasListening = false;
+ if (_server.isListening()) {
+ wasListening = true;
+ _server.close();
+ }
+ if (_v6server.isListening()) {
+ wasListening = true;
+ _v6server.close();
+ }
+ if (wasListening) {
+ if (reason.isEmpty())
+ qInfo() << "No longer listening for GUI clients.";
+ else
+ qInfo() << qPrintable(reason);
+ }
+}
+
+void Core::incomingConnection()
+{
+ auto* server = qobject_cast<SslServer*>(sender());
+ Q_ASSERT(server);
+ while (server->hasPendingConnections()) {
+ auto socket = qobject_cast<QSslSocket*>(server->nextPendingConnection());
+ Q_ASSERT(socket);
+
+ auto* handler = new CoreAuthHandler(socket, this);
+ _connectingClients.insert(handler);
+
+ connect(handler, &AuthHandler::disconnected, this, &Core::clientDisconnected);
+ connect(handler, &AuthHandler::socketError, this, &Core::socketError);
+ connect(handler, &CoreAuthHandler::handshakeComplete, this, &Core::setupClientSession);
+
+ qInfo() << qPrintable(tr("Client connected from")) << qPrintable(handler->hostAddress().toString());
+
+ if (!_configured) {
+ stopListening(tr("Closing server for basic setup."));
+ }
+ }
+}
+
+// Potentially called during the initialization phase (before handing the connection off to the session)
+void Core::clientDisconnected()
+{
+ auto* handler = qobject_cast<CoreAuthHandler*>(sender());
+ Q_ASSERT(handler);
+
+ qInfo() << qPrintable(tr("Non-authed client disconnected:")) << qPrintable(handler->hostAddress().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)
+{
+ auto* handler = qobject_cast<CoreAuthHandler*>(sender());
+ Q_ASSERT(handler);
+
+ // From now on everything is handled by the client session
+ disconnect(handler, nullptr, this, nullptr);
+ _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) {
+ auto* 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 {
+ qWarning() << "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) {
+ qWarning() << "Client peer went away, not starting a session";
+ return;
+ }
+
+ auto* 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];
+
+ return (_sessions[uid] = new SessionThread(uid, restore, strictIdentEnabled(), this));
+}
+
+void Core::socketError(QAbstractSocket::SocketError err, const QString& errorString)
+{
+ qWarning() << QString("Socket error %1: %2").arg(err).arg(errorString);
+}
+
+QVariantList Core::backendInfo()
+{
+ instance()->registerStorageBackends();