X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fcore.cpp;h=383526a05910078524e6ac244f27c1f3fa2f2a32;hp=8320dc08b216f95ee58ea2f96a9a283a7e4f86bb;hb=4ec0f5fc03c28c7bc810d88ecfdf6b07efc9afcd;hpb=e5f3a784b8c6e6b4fe9b299c3a3553f31fba5601 diff --git a/src/core/core.cpp b/src/core/core.cpp index 8320dc08..383526a0 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -56,8 +56,8 @@ Core::Core() : storage(0) { registerStorageBackend(new SqliteStorage(this)); if(!_storageBackends.count()) { - quWarning() << qPrintable(tr("Could not initialize any storage backend! Exiting...")); - quWarning() << qPrintable(tr("Currently, Quassel only supports SQLite3. You need to build your\n" + qWarning() << qPrintable(tr("Could not initialize any storage backend! Exiting...")); + qWarning() << qPrintable(tr("Currently, Quassel only supports SQLite3. You need to build your\n" "Qt library with the sqlite plugin enabled in order for quasselcore\n" "to work.")); exit(1); // TODO make this less brutal (especially for mono client -> popup) @@ -72,7 +72,7 @@ void Core::init() { CoreSettings cs; if(!(configured = initStorage(cs.storageSettings().toMap()))) { - quWarning() << "Core is currently not configured! Please connect with a Quassel Client for basic setup."; + qWarning() << "Core is currently not configured! Please connect with a Quassel Client for basic setup."; // try to migrate old settings QVariantMap old = cs.oldDbSettings().toMap(); @@ -80,7 +80,7 @@ void Core::init() { QVariantMap newSettings; newSettings["Backend"] = "SQLite"; if((configured = initStorage(newSettings))) { - quWarning() << "...but thankfully I found some old settings to migrate!"; + qWarning() << "...but thankfully I found some old settings to migrate!"; cs.setStorageSettings(newSettings); } } @@ -117,7 +117,7 @@ void Core::restoreState() { return; } if(instance()->sessions.count()) { - quWarning() << qPrintable(tr("Calling restoreState() even though active sessions exist!")); + qWarning() << qPrintable(tr("Calling restoreState() even though active sessions exist!")); return; } CoreSettings s; @@ -139,9 +139,22 @@ void Core::restoreState() { } /*** Core Setup ***/ +QString Core::setupCoreForInternalUsage() { + Q_ASSERT(!_storageBackends.isEmpty()); + QVariantMap setupData; + qsrand(QDateTime::currentDateTime().toTime_t()); + int pass = 0; + for(int i = 0; i < 10; i++) { + pass *= 10; + pass += qrand() % 10; + } + setupData["AdminUser"] = "AdminUser"; + setupData["AdminPasswd"] = QString::number(pass); + setupData["Backend"] = _storageBackends[_storageBackends.keys().first()]->displayName(); + return setupCore(setupData); +} -QString Core::setupCore(const QVariant &setupData_) { - QVariantMap setupData = setupData_.toMap(); +QString Core::setupCore(QVariantMap setupData) { QString user = setupData.take("AdminUser").toString(); QString password = setupData.take("AdminPasswd").toString(); if(user.isEmpty() || password.isEmpty()) { @@ -189,12 +202,12 @@ bool Core::initStorage(QVariantMap dbSettings, bool setup) { if(_storageBackends.contains(backend)) { storage = _storageBackends[backend]; } else { - quError() << "Selected storage backend is not available:" << backend; + qCritical() << "Selected storage backend is not available:" << backend; return configured = false; } if(!storage->init(dbSettings)) { if(!setup || !(storage->setup(dbSettings) && storage->init(dbSettings))) { - quError() << "Could not init storage!"; + qCritical() << "Could not init storage!"; storage = 0; return configured = false; } @@ -343,6 +356,10 @@ QHash Core::bufferLastSeenMsgIds(UserId user) { /*** Network Management ***/ bool Core::startListening() { + // in mono mode we only start a local port if a port is specified in the cli call + if(Quassel::runMode() == Quassel::Monolithic && !Quassel::isOptionSet("port")) + return true; + bool success = false; uint port = Quassel::optionValue("port").toUInt(); @@ -358,16 +375,28 @@ bool Core::startListening() { } if(!success) { - quError() << qPrintable(QString("Could not open GUI client port %1: %2").arg(port).arg(_server.errorString())); + qCritical() << qPrintable(QString("Could not open GUI client port %1: %2").arg(port).arg(_server.errorString())); } return success; } -void Core::stopListening() { - _server.close(); - _v6server.close(); - quInfo() << "No longer listening for GUI clients."; +void Core::stopListening(const QString &reason) { + bool wasListening = false; + if(_server.isListening()) { + wasListening = true; + _server.close(); + } + if(_v6server.isListening()) { + wasListening = true; + _v6server.close(); + } + if(wasListening) { + if(reason.isEmpty()) + quInfo() << "No longer listening for GUI clients."; + else + quInfo() << qPrintable(reason); + } } void Core::incomingConnection() { @@ -384,9 +413,7 @@ void Core::incomingConnection() { quInfo() << qPrintable(tr("Client connected from")) << qPrintable(socket->peerAddress().toString()); if(!configured) { - _server.close(); - _v6server.close(); - quDebug() << "Closing server for basic setup."; + stopListening(tr("Closing server for basic setup.")); } } } @@ -405,7 +432,7 @@ void Core::clientHasData() { void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) { if(!msg.contains("MsgType")) { // Client is way too old, does not even use the current init format - quWarning() << qPrintable(tr("Antique client trying to connect... refusing.")); + qWarning() << qPrintable(tr("Antique client trying to connect... refusing.")); socket->close(); return; } @@ -421,7 +448,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) { "This core needs at least client/core protocol version %1.
" "Please consider upgrading your client.").arg(Quassel::buildInfo().coreNeedsProtocol); SignalProxy::writeDataToDevice(socket, reply); - quWarning() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("too old, rejecting.")); + qWarning() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("too old, rejecting.")); socket->close(); return; } @@ -481,7 +508,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) { #ifdef HAVE_SSL // after we told the client that we are ssl capable we switch to ssl mode if(supportSsl && msg["UseSsl"].toBool()) { - quDebug() << qPrintable(tr("Starting TLS for Client:")) << qPrintable(socket->peerAddress().toString()); + qDebug() << qPrintable(tr("Starting TLS for Client:")) << qPrintable(socket->peerAddress().toString()); connect(sslSocket, SIGNAL(sslErrors(const QList &)), this, SLOT(sslErrors(const QList &))); sslSocket->startServerEncryption(); } @@ -490,7 +517,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) { #ifndef QT_NO_COMPRESS if(supportsCompression && msg["UseCompression"].toBool()) { socket->setProperty("UseCompression", true); - quDebug() << "Using compression for Client:" << qPrintable(socket->peerAddress().toString()); + qDebug() << "Using compression for Client:" << qPrintable(socket->peerAddress().toString()); } #endif @@ -501,12 +528,12 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) { reply["MsgType"] = "ClientLoginReject"; reply["Error"] = tr("Client not initialized!
You need to send an init message before trying to login."); SignalProxy::writeDataToDevice(socket, reply); - quWarning() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("did not send an init message before trying to login, rejecting.")); + qWarning() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("did not send an init message before trying to login, rejecting.")); socket->close(); return; } if(msg["MsgType"] == "CoreSetupData") { QVariantMap reply; - QString result = setupCore(msg["SetupData"]); + QString result = setupCore(msg["SetupData"].toMap()); if(!result.isEmpty()) { reply["MsgType"] = "CoreSetupReject"; reply["Error"] = result; @@ -544,7 +571,7 @@ void Core::clientDisconnected() { socket->deleteLater(); } else { // we have to crawl through the hashes and see if we find a victim to remove - quDebug() << qPrintable(tr("Non-authed client disconnected. (socket allready destroyed)")); + qDebug() << qPrintable(tr("Non-authed client disconnected. (socket allready destroyed)")); // DO NOT CALL ANY METHODS ON socket!! socket = static_cast(sender()); @@ -586,15 +613,34 @@ void Core::setupClientSession(QTcpSocket *socket, UserId uid) { blocksizes.remove(socket); clientInfo.remove(socket); if(!sess) { - quWarning() << qPrintable(tr("Could not initialize session for client:")) << qPrintable(socket->peerAddress().toString()); + qWarning() << qPrintable(tr("Could not initialize session for client:")) << qPrintable(socket->peerAddress().toString()); socket->close(); } sess->addClient(socket); } +void Core::setupInternalClientSession(SignalProxy *proxy) { + if(!configured) { + stopListening(); + setupCoreForInternalUsage(); + } + + mutex.lock(); + UserId uid = storage->internalUser(); + mutex.unlock(); + + // Find or create session for validated user + SessionThread *sess; + if(sessions.contains(uid)) + sess = sessions[uid]; + else + sess = createSession(uid); + sess->addClient(proxy); +} + SessionThread *Core::createSession(UserId uid, bool restore) { if(sessions.contains(uid)) { - quWarning() << "Calling createSession() when a session for the user already exists!"; + qWarning() << "Calling createSession() when a session for the user already exists!"; return 0; } SessionThread *sess = new SessionThread(uid, restore, this); @@ -615,5 +661,5 @@ void Core::sslErrors(const QList &errors) { void Core::socketError(QAbstractSocket::SocketError err) { QAbstractSocket *socket = qobject_cast(sender()); if(socket && err != QAbstractSocket::RemoteHostClosedError) - quWarning() << "Core::socketError()" << socket << err << socket->errorString(); + qWarning() << "Core::socketError()" << socket << err << socket->errorString(); }