+
+# ifdef Q_OS_WIN
+ QSettings::Format format = QSettings::IniFormat;
+# else
+ QSettings::Format format = QSettings::NativeFormat;
+# endif
+ QString newFilePath = Quassel::configDirPath() + "quasselcore"
+ + ((format == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
+ QSettings newSettings(newFilePath, format);
+#endif /* Q_OS_MAC */
+
+ if (newSettings.value("Config/Version").toUInt() == 0) {
+# ifdef Q_OS_MAC
+ QString org = "quassel-irc.org";
+# else
+ QString org = "Quassel Project";
+# endif
+ QSettings oldSettings(org, "Quassel Core");
+ if (oldSettings.allKeys().count()) {
+ qWarning() << "\n\n*** IMPORTANT: Config and data file locations have changed. Attempting to auto-migrate your core settings...";
+ foreach(QString key, oldSettings.allKeys())
+ newSettings.setValue(key, oldSettings.value(key));
+ newSettings.setValue("Config/Version", 1);
+ qWarning() << "* Your core settings have been migrated to" << newSettings.fileName();
+
+#ifndef Q_OS_MAC /* we don't need to move the db and cert for mac */
+#ifdef Q_OS_WIN
+ QString quasselDir = qgetenv("APPDATA") + "/quassel/";
+#elif defined Q_OS_MAC
+ QString quasselDir = QDir::homePath() + "/Library/Application Support/Quassel/";
+#else
+ QString quasselDir = QDir::homePath() + "/.quassel/";
+#endif
+
+ QFileInfo info(Quassel::configDirPath() + "quassel-storage.sqlite");
+ if (!info.exists()) {
+ // move database, if we found it
+ QFile oldDb(quasselDir + "quassel-storage.sqlite");
+ if (oldDb.exists()) {
+ bool success = oldDb.rename(Quassel::configDirPath() + "quassel-storage.sqlite");
+ if (success)
+ qWarning() << "* Your database has been moved to" << Quassel::configDirPath() + "quassel-storage.sqlite";
+ else
+ qWarning() << "!!! Moving your database has failed. Please move it manually into" << Quassel::configDirPath();
+ }
+ }
+ // move certificate
+ QFileInfo certInfo(quasselDir + "quasselCert.pem");
+ if (certInfo.exists()) {
+ QFile cert(quasselDir + "quasselCert.pem");
+ bool success = cert.rename(Quassel::configDirPath() + "quasselCert.pem");
+ if (success)
+ qWarning() << "* Your certificate has been moved to" << Quassel::configDirPath() + "quasselCert.pem";
+ else
+ qWarning() << "!!! Moving your certificate has failed. Please move it manually into" << Quassel::configDirPath();
+ }
+#endif /* !Q_OS_MAC */
+ qWarning() << "*** Migration completed.\n\n";
+ }
+ }
+ // MIGRATION end
+
+ // check settings version
+ // so far, we only have 1
+ CoreSettings s;
+ if (s.version() != 1) {
+ qCritical() << "Invalid core settings version, terminating!";
+ exit(EXIT_FAILURE);
+ }
+
+ registerStorageBackends();
+ registerAuthenticators();
+
+ connect(&_storageSyncTimer, SIGNAL(timeout()), this, SLOT(syncStorage()));
+ _storageSyncTimer.start(10 * 60 * 1000); // 10 minutes
+}
+
+
+void Core::init()
+{
+ CoreSettings cs;
+ // legacy
+ QVariantMap dbsettings = cs.storageSettings().toMap();
+ _configured = initStorage(dbsettings.value("Backend").toString(), dbsettings.value("ConnectionProperties").toMap());
+
+ // Not entirely sure what is 'legacy' about the above, but it seems to be the way things work!
+ QVariantMap authSettings = cs.authSettings().toMap();
+ initAuthenticator(authSettings.value("Authenticator").toString(), authSettings.value("ConnectionProperties").toMap());
+
+ if (Quassel::isOptionSet("select-backend") || Quassel::isOptionSet("select-authenticator")) {
+ if (Quassel::isOptionSet("select-backend")) {
+ selectBackend(Quassel::optionValue("select-backend"));
+ }
+ if (Quassel::isOptionSet("select-authenticator")) {
+ selectAuthenticator(Quassel::optionValue("select-authenticator"));
+ }
+ exit(0);
+ }
+
+ if (!_configured) {
+ if (!_storageBackends.count()) {
+ qWarning() << qPrintable(tr("Could not initialize any storage backend! Exiting..."));
+ qWarning() << qPrintable(tr("Currently, Quassel supports SQLite3 and PostgreSQL. You need to build your\n"
+ "Qt library with the sqlite or postgres plugin enabled in order for quasselcore\n"
+ "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")) {
+ exit(createUser() ? EXIT_SUCCESS : EXIT_FAILURE);
+
+ }
+
+ if (Quassel::isOptionSet("change-userpass")) {
+ exit(changeUserPass(Quassel::optionValue("change-userpass")) ?
+ EXIT_SUCCESS : EXIT_FAILURE);
+ }
+
+ connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
+ connect(&_v6server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
+ if (!startListening()) exit(1); // TODO make this less brutal
+
+ if (Quassel::isOptionSet("oidentd"))
+ _oidentdConfigGenerator = new OidentdConfigGenerator(this);
+}
+
+
+Core::~Core()
+{
+ // FIXME do we need more cleanup for handlers?
+ foreach(CoreAuthHandler *handler, _connectingClients) {
+ handler->deleteLater(); // disconnect non authed clients
+ }
+ qDeleteAll(_sessions);
+ qDeleteAll(_storageBackends);
+ qDeleteAll(_authenticators);
+}
+
+
+/*** Session Restore ***/
+
+void Core::saveState()
+{
+ CoreSettings s;
+ QVariantMap state;
+ QVariantList activeSessions;
+ foreach(UserId user, instance()->_sessions.keys())
+ activeSessions << QVariant::fromValue<UserId>(user);
+ state["CoreStateVersion"] = 1;
+ state["ActiveSessions"] = activeSessions;
+ s.setCoreState(state);
+}
+
+
+void Core::restoreState()
+{
+ if (!instance()->_configured) {
+ // qWarning() << qPrintable(tr("Cannot restore a state for an unconfigured core!"));
+ return;
+ }
+ if (instance()->_sessions.count()) {
+ qWarning() << qPrintable(tr("Calling restoreState() even though active sessions exist!"));
+ return;
+ }
+ CoreSettings s;
+ /* We don't check, since we are at the first version since switching to Git
+ uint statever = s.coreState().toMap()["CoreStateVersion"].toUInt();
+ if(statever < 1) {
+ qWarning() << qPrintable(tr("Core state too old, ignoring..."));
+ return;
+ }
+ */
+
+ QVariantList activeSessions = s.coreState().toMap()["ActiveSessions"].toList();
+ if (activeSessions.count() > 0) {
+ quInfo() << "Restoring previous core state...";
+ foreach(QVariant v, activeSessions) {
+ UserId user = v.value<UserId>();
+ instance()->sessionForUser(user, true);
+ }
+ }
+}
+
+
+/*** Core Setup ***/
+
+QString Core::setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authenticator, const QVariantMap &authSetupData)
+{
+ return instance()->setupCore(adminUser, adminPassword, backend, setupData, authenticator, authSetupData);
+}
+
+
+QString Core::setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authenticator, const QVariantMap &authSetupData)
+{
+ if (_configured)
+ return tr("Core is already configured! Not configuring again...");
+
+ if (adminUser.isEmpty() || adminPassword.isEmpty()) {
+ return tr("Admin user or password not set.");
+ }
+ if (!(_configured = initStorage(backend, setupData, true))) {
+ return tr("Could not setup storage!");
+ }
+
+ quInfo() << "Selected authenticator: " << authenticator;
+ if (!(_configured = initAuthenticator(authenticator, authSetupData, true)))
+ {
+ return tr("Could not setup authenticator!");
+ }
+
+ if (!saveBackendSettings(backend, setupData)) {
+ return tr("Could not save backend settings, probably a permission problem.");
+ }
+ saveAuthenticatorSettings(authenticator, authSetupData);
+
+ quInfo() << qPrintable(tr("Creating admin user..."));
+ _storage->addUser(adminUser, adminPassword);
+ startListening(); // TODO check when we need this
+ return QString();
+}
+
+
+QString Core::setupCoreForInternalUsage()
+{
+ Q_ASSERT(!_storageBackends.isEmpty());
+
+ qsrand(QDateTime::currentDateTime().toTime_t());
+ int pass = 0;
+ for (int i = 0; i < 10; i++) {
+ pass *= 10;
+ pass += qrand() % 10;
+ }
+
+ // mono client currently needs sqlite
+ return setupCore("AdminUser", QString::number(pass), "SQLite", QVariantMap(), "StorageAuth", QVariantMap());
+}
+
+
+/*** Storage Handling ***/
+void Core::registerStorageBackends()
+{
+ // Register storage backends here!
+ registerStorageBackend(new SqliteStorage(this));
+ registerStorageBackend(new PostgreSqlStorage(this));
+}
+
+
+bool Core::registerStorageBackend(Storage *backend)
+{
+ if (backend->isAvailable()) {
+ _storageBackends[backend->displayName()] = backend;
+ return true;
+ }
+ else {
+ backend->deleteLater();
+ return false;
+ }
+}
+
+void Core::unregisterStorageBackends()
+{
+ foreach(Storage *s, _storageBackends.values()) {
+ s->deleteLater();
+ }
+ _storageBackends.clear();
+}
+
+
+void Core::unregisterStorageBackend(Storage *backend)
+{
+ _storageBackends.remove(backend->displayName());
+ backend->deleteLater();
+}
+
+// Authentication handling, now independent from storage.
+// Register and unregister authenticators.
+
+void Core::registerAuthenticators()
+{
+ // Register new authentication backends here!
+ registerAuthenticator(new SqlAuthenticator(this));
+#ifdef HAVE_LDAP
+ registerAuthenticator(new LdapAuthenticator(this));