Monolithic build features now zero setup configuration: click and run
[quassel.git] / src / core / core.cpp
index 6efa622..bf611aa 100644 (file)
@@ -26,6 +26,7 @@
 #include "core.h"
 #include "coresession.h"
 #include "coresettings.h"
+#include "quassel.h"
 #include "signalproxy.h"
 #include "sqlitestorage.h"
 #include "network.h"
@@ -49,7 +50,7 @@ void Core::destroy() {
 }
 
 Core::Core() : storage(0) {
-  _startTime = QDateTime::currentDateTime();  // for uptime :)
+  _startTime = QDateTime::currentDateTime().toUTC();  // for uptime :)
 
   // Register storage backends here!
   registerStorageBackend(new SqliteStorage(this));
@@ -85,8 +86,9 @@ void Core::init() {
     }
   }
 
-  connect(&server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
-  if(!startListening(cs.port())) exit(1); // TODO make this less brutal
+  connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
+  connect(&_v6server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
+  if(!startListening()) exit(1); // TODO make this less brutal
 }
 
 Core::~Core() {
@@ -137,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()) {
@@ -340,45 +355,65 @@ QHash<BufferId, MsgId> Core::bufferLastSeenMsgIds(UserId user) {
 
 /*** Network Management ***/
 
-bool Core::startListening(uint port) {
+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();
 
-  // let's see if ipv6 is available
-  success = server.listen(QHostAddress::AnyIPv6, port);
-
-  if(!success && server.serverError() == QAbstractSocket::UnsupportedSocketOperationError) {
-    // fall back to v4
-    success = server.listen(QHostAddress::Any, port);
+  if(_server.listen(QHostAddress::Any, port)) {
+    quInfo() << "Listening for GUI clients on IPv6 port" << _server.serverPort()
+             << "using protocol version" << Quassel::buildInfo().protocolVersion;
+    success = true;
+  }
+  if(_v6server.listen(QHostAddress::AnyIPv6, port)) {
+    quInfo() << "Listening for GUI clients on IPv4 port" << _v6server.serverPort()
+             << "using protocol version" << Quassel::buildInfo().protocolVersion;
+    success = true;
   }
 
   if(!success) {
-    quError() << qPrintable(QString("Could not open GUI client port %1: %2").arg(port).arg(server.errorString()));
-  } else {
-    quInfo() << "Listening for GUI clients on port " << server.serverPort() << " using protocol version " << Global::protocolVersion;
+    quError() << qPrintable(QString("Could not open GUI client port %1: %2").arg(port).arg(_server.errorString()));
   }
-  
+
   return success;
 }
 
-void Core::stopListening() {
-  server.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() {
-  while(server.hasPendingConnections()) {
-    QTcpSocket *socket = server.nextPendingConnection();
+  QTcpServer *server = qobject_cast<QTcpServer *>(sender());
+  Q_ASSERT(server);
+  while(server->hasPendingConnections()) {
+    QTcpSocket *socket = server->nextPendingConnection();
     connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
     connect(socket, SIGNAL(readyRead()), this, SLOT(clientHasData()));
     connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
-    
+
     QVariantMap clientInfo;
     blocksizes.insert(socket, (quint32)0);
-    quInfo() << qPrintable(tr("Client connected from "))  << qPrintable(socket->peerAddress().toString());
+    quInfo() << qPrintable(tr("Client connected from"))  << qPrintable(socket->peerAddress().toString());
 
-    if (!configured) {
-      server.close();
-      quDebug() << "Closing server for basic setup.";
+    if(!configured) {
+      stopListening(tr("Closing server for basic setup."));
     }
   }
 }
@@ -406,35 +441,33 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
     QVariantMap reply;
 
     // Just version information -- check it!
-    uint ver = 0;
-    if(!msg.contains("ProtocolVersion") && msg["ClientBuild"].toUInt() >= 732) ver = 1; // FIXME legacy
-    if(msg.contains("ProtocolVersion")) ver = msg["ProtocolVersion"].toUInt();
-    if(ver < Global::coreNeedsProtocol) {
+    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(Global::coreNeedsProtocol);
+      "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."));
       socket->close(); return;
     }
 
-    reply["CoreVersion"] = Global::quasselVersion;
-    reply["CoreDate"] = Global::quasselBuildDate;
-    reply["CoreBuild"] = 860; // FIXME legacy
-    reply["ProtocolVersion"] = Global::protocolVersion;
+    reply["CoreVersion"] = Quassel::buildInfo().fancyVersionString;
+    reply["CoreDate"] = Quassel::buildInfo().buildDate;
+    reply["ProtocolVersion"] = Quassel::buildInfo().protocolVersion;
     // TODO: Make the core info configurable
-    int uptime = startTime().secsTo(QDateTime::currentDateTime());
+    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(Global::quasselVersion).arg(Global::quasselBuildDate)
+                          "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));
 
-#ifndef QT_NO_OPENSSL
-    SslServer *sslServer = qobject_cast<SslServer *>(&server);
+#ifdef HAVE_SSL
+    SslServer *sslServer = qobject_cast<SslServer *>(&_server);
     QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
     bool supportSsl = (bool)sslServer && (bool)sslSocket && sslServer->certIsValid();
 #else
@@ -472,7 +505,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
     reply["MsgType"] = "ClientInitAck";
     SignalProxy::writeDataToDevice(socket, reply);
 
-#ifndef QT_NO_OPENSSL
+#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());
@@ -484,10 +517,10 @@ 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());
+      quDebug() << "Using compression for Client:" << qPrintable(socket->peerAddress().toString());
     }
 #endif
-    
+
   } else {
     // for the rest, we need an initialized connection
     if(!clientInfo.contains(socket)) {
@@ -500,7 +533,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
     }
     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;
@@ -521,7 +554,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
       }
       reply["MsgType"] = "ClientLoginAck";
       SignalProxy::writeDataToDevice(socket, reply);
-      quInfo() << qPrintable(tr("Client ")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr(" initialized and authenticated successfully as \"%1\" (UserId: %2).").arg(msg["User"].toString()).arg(uid.toInt()));
+      quInfo() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("initialized and authenticated successfully as \"%1\" (UserId: %2).").arg(msg["User"].toString()).arg(uid.toInt()));
       setupClientSession(socket, uid);
     }
   }
@@ -542,7 +575,7 @@ void Core::clientDisconnected() {
 
     // DO NOT CALL ANY METHODS ON socket!!
     socket = static_cast<QTcpSocket *>(sender());
-    
+
     QHash<QTcpSocket *, quint32>::iterator blockSizeIter = blocksizes.begin();
     while(blockSizeIter != blocksizes.end()) {
       if(blockSizeIter.key() == socket) {
@@ -586,6 +619,22 @@ void Core::setupClientSession(QTcpSocket *socket, UserId uid) {
   sess->addClient(socket);
 }
 
+void Core::setupInternalClientSession(SignalProxy *proxy) {
+  if(!configured) {
+    stopListening();
+    setupCoreForInternalUsage();
+  }
+
+  UserId uid = 3; // FIXME!!!11
+  // 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!";
@@ -597,7 +646,7 @@ SessionThread *Core::createSession(UserId uid, bool restore) {
   return sess;
 }
 
-#ifndef QT_NO_OPENSSL
+#ifdef HAVE_SSL
 void Core::sslErrors(const QList<QSslError> &errors) {
   Q_UNUSED(errors);
   QSslSocket *socket = qobject_cast<QSslSocket *>(sender());