fixes #627
[quassel.git] / src / core / core.cpp
index 542d425..5678429 100644 (file)
@@ -151,11 +151,6 @@ void Core::init() {
     exit(0);
   }
 
-  if(Quassel::isOptionSet("add-user")) {
-    createUser();
-    exit(0);
-  }
-
   if(!_configured) {
     if(!_storageBackends.count()) {
       qWarning() << qPrintable(tr("Could not initialize any storage backend! Exiting..."));
@@ -167,6 +162,16 @@ void Core::init() {
     qWarning() << "Core is currently not configured! Please connect with a Quassel Client for basic setup.";
   }
 
+  if(Quassel::isOptionSet("add-user")) {
+    createUser();
+    exit(0);
+  }
+
+  if(Quassel::isOptionSet("change-userpass")) {
+    changeUserPass(Quassel::optionValue("change-userpass"));
+    exit(0);
+  }
+
   connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
   connect(&_v6server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
   if(!startListening()) exit(1); // TODO make this less brutal
@@ -663,7 +668,13 @@ void Core::setupInternalClientSession(SignalProxy *proxy) {
     setupCoreForInternalUsage();
   }
 
-  UserId uid = _storage->internalUser();
+  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!";
+    return;
+  }
 
   // Find or create session for validated user
   SessionThread *sess;
@@ -808,6 +819,44 @@ void Core::createUser() {
   }
 }
 
+void 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;
+  }
+
+  out << "Change password for user: " << username << endl;
+
+  disableStdInEcho();
+  out << "New Password: ";
+  out.flush();
+  QString password = in.readLine().trimmed();
+  out << endl;
+  out << "Repeat Password: ";
+  out.flush();
+  QString password2 = in.readLine().trimmed();
+  out << endl;
+  enableStdInEcho();
+
+  if(password != password2) {
+    qWarning() << "Passwords don't match!";
+    return;
+  }
+  if(password.isEmpty()) {
+    qWarning() << "Password is empty!";
+    return;
+  }
+
+  if(_storage->updateUser(userId, password)) {
+    out << "Password changed successfuly!" << endl;
+  } else {
+    qWarning() << "Failed to change password!";
+  }
+}
+
 AbstractSqlMigrationReader *Core::getMigrationReader(Storage *storage) {
   if(!storage)
     return 0;