Save Core settings synchronously and report errors.
[quassel.git] / src / core / core.cpp
index 2049b63..ad9eab8 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2015 by the Quassel Project                        *
+ *   Copyright (C) 2005-2016 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -194,17 +194,24 @@ void Core::init()
                                         "to work."));
             exit(1); // TODO make this less brutal (especially for mono client -> popup)
         }
+
         qWarning() << "Core is currently not configured! Please connect with a Quassel Client for basic setup.";
+
+        if (!cs.isWritable()) {
+            qWarning() << "Cannot write quasselcore configuration; probably a permission problem.";
+            exit(EXIT_FAILURE);
+        }
+
     }
 
     if (Quassel::isOptionSet("add-user")) {
-        createUser();
-        exit(0);
+        exit(createUser() ? EXIT_SUCCESS : EXIT_FAILURE);
+
     }
 
     if (Quassel::isOptionSet("change-userpass")) {
-        changeUserPass(Quassel::optionValue("change-userpass"));
-        exit(0);
+        exit(changeUserPass(Quassel::optionValue("change-userpass")) ?
+                       EXIT_SUCCESS : EXIT_FAILURE);
     }
 
     connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
@@ -292,7 +299,9 @@ QString Core::setupCore(const QString &adminUser, const QString &adminPassword,
         return tr("Could not setup storage!");
     }
 
-    saveBackendSettings(backend, setupData);
+    if (!saveBackendSettings(backend, setupData)) {
+        return tr("Could not save backend settings, probably a permission problem.");
+    }
 
     quInfo() << qPrintable(tr("Creating admin user..."));
     _storage->addUser(adminUser, adminPassword);
@@ -428,6 +437,23 @@ bool Core::sslSupported()
 }
 
 
+bool Core::reloadCerts()
+{
+#ifdef HAVE_SSL
+    SslServer *sslServerv4 = qobject_cast<SslServer *>(&instance()->_server);
+    bool retv4 = sslServerv4->reloadCerts();
+
+    SslServer *sslServerv6 = qobject_cast<SslServer *>(&instance()->_v6server);
+    bool retv6 = sslServerv6->reloadCerts();
+
+    return retv4 && retv6;
+#else
+    // SSL not supported, don't mark configuration reload as failed
+    return true;
+#endif
+}
+
+
 bool Core::startListening()
 {
     // in mono mode we only start a local port if a port is specified in the cli call
@@ -657,6 +683,7 @@ QVariantList Core::backendInfo()
         v["Description"] = backend->description();
         v["SetupKeys"] = backend->setupKeys();
         v["SetupDefaults"] = backend->setupDefaults();
+        v["IsDefault"] = isStorageBackendDefault(backend);
         backends.append(v);
     }
     return backends;
@@ -680,7 +707,9 @@ bool Core::selectBackend(const QString &backend)
     Storage::State storageState = storage->init(settings);
     switch (storageState) {
     case Storage::IsReady:
-        saveBackendSettings(backend, settings);
+        if (!saveBackendSettings(backend, settings)) {
+            qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+        }
         qWarning() << "Switched backend to:" << qPrintable(backend);
         qWarning() << "Backend already initialized. Skipping Migration";
         return true;
@@ -698,7 +727,9 @@ bool Core::selectBackend(const QString &backend)
             return false;
         }
 
-        saveBackendSettings(backend, settings);
+        if (!saveBackendSettings(backend, settings)) {
+            qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+        }
         qWarning() << "Switched backend to:" << qPrintable(backend);
         break;
     }
@@ -714,7 +745,10 @@ bool Core::selectBackend(const QString &backend)
         storage = 0;
         if (reader->migrateTo(writer)) {
             qDebug() << "Migration finished!";
-            saveBackendSettings(backend, settings);
+            if (!saveBackendSettings(backend, settings)) {
+                qCritical() << qPrintable(QString("Could not save backend settings, probably a permission problem."));
+                return false;
+            }
             return true;
         }
         return false;
@@ -739,7 +773,7 @@ bool Core::selectBackend(const QString &backend)
 }
 
 
-void Core::createUser()
+bool Core::createUser()
 {
     QTextStream out(stdout);
     QTextStream in(stdin);
@@ -761,30 +795,32 @@ void Core::createUser()
 
     if (password != password2) {
         qWarning() << "Passwords don't match!";
-        return;
+        return false;
     }
     if (password.isEmpty()) {
         qWarning() << "Password is empty!";
-        return;
+        return false;
     }
 
     if (_configured && _storage->addUser(username, password).isValid()) {
         out << "Added user " << username << " successfully!" << endl;
+        return true;
     }
     else {
         qWarning() << "Unable to add user:" << qPrintable(username);
+        return false;
     }
 }
 
 
-void Core::changeUserPass(const QString &username)
+bool Core::changeUserPass(const QString &username)
 {
     QTextStream out(stdout);
     QTextStream in(stdin);
     UserId userId = _storage->getUserId(username);
     if (!userId.isValid()) {
         out << "User " << username << " does not exist." << endl;
-        return;
+        return false;
     }
 
     out << "Change password for user: " << username << endl;
@@ -802,18 +838,20 @@ void Core::changeUserPass(const QString &username)
 
     if (password != password2) {
         qWarning() << "Passwords don't match!";
-        return;
+        return false;
     }
     if (password.isEmpty()) {
         qWarning() << "Password is empty!";
-        return;
+        return false;
     }
 
     if (_configured && _storage->updateUser(userId, password)) {
         out << "Password changed successfully!" << endl;
+        return true;
     }
     else {
         qWarning() << "Failed to change password!";
+        return false;
     }
 }
 
@@ -857,12 +895,14 @@ AbstractSqlMigrationWriter *Core::getMigrationWriter(Storage *storage)
 }
 
 
-void Core::saveBackendSettings(const QString &backend, const QVariantMap &settings)
+bool Core::saveBackendSettings(const QString &backend, const QVariantMap &settings)
 {
     QVariantMap dbsettings;
     dbsettings["Backend"] = backend;
     dbsettings["ConnectionProperties"] = settings;
-    CoreSettings().setStorageSettings(dbsettings);
+    CoreSettings s = CoreSettings();
+    s.setStorageSettings(dbsettings);
+    return s.sync();
 }