]> vaikene.ee Git - evaf/blobdiff - src/apps/PswGen/CLI/cli.cpp
Warning fixes and copyright update.
[evaf] / src / apps / PswGen / CLI / cli.cpp
index fbc8450ebe37f2ec2ddba2170994a536587df353..b27fa8a712b905d92f7d7c3383b21493991c5c36 100644 (file)
@@ -3,7 +3,7 @@
  * @brief Command line interface for the PswGen application
  * @author Enar Vaikene
  *
- * Copyright (c) 2011 Enar Vaikene
+ * Copyright (c) 2011-2019 Enar Vaikene
  *
  * This file is part of the eVaf C++ cross-platform application development framework.
  *
@@ -18,7 +18,6 @@
  */
 
 #include "cli.h"
-#include "version.h"
 
 #include <Generator/iGenerator>
 #include <Storage/iStorage>
 #include <Common/iEventQueue>
 #include <Common/iApp>
 #include <Common/Event>
+#include <Common/Util>
 
 #include <QtCore>
 
-#ifdef Q_OS_LINUX
+#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
 #  include <termios.h>
 #  include <unistd.h>
 #endif
@@ -41,7 +41,6 @@
 #endif
 
 VER_EXPORT_VERSION_INFO()
-Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR, eVaf::PswGen::CLI::Module)
 
 
 //-------------------------------------------------------------------
@@ -54,8 +53,8 @@ int const Module::DefaultPasswordLength = 16;
 Module::Module()
     : Plugins::iPlugin()
     , mReady(false)
-    , mGenerator(false)
-    , mStorage(false)
+    , mGenerator(nullptr)
+    , mStorage(nullptr)
     , mEvReady(0)
 {
     setObjectName(QString("%1-Module").arg(VER_MODULE_NAME_STR));
@@ -69,10 +68,10 @@ Module::~Module()
 
 bool Module::init(QString const & args)
 {
-    Q_UNUSED(args);
+    Q_UNUSED(args)
 
     // Get the iGenerator interface
-    EVAF_TEST_X((mGenerator = evafQueryInterface<PswGen::iGenerator>("iGenerator")), "No iGenerator interface");
+    EVAF_TEST_X((mGenerator = evafQueryInterface<PswGen::iGenerator>("iGenerator")), "No iGenerator interface")
 
     // Get the optional iStorage interface
     mStorage = evafQueryInterface<PswGen::iStorage>("iStorage");
@@ -81,10 +80,10 @@ bool Module::init(QString const & args)
 
     // Get the iEventQueue interface and subscribe to the 'ready' event
     Common::iEventQueue * eventQueue = evafQueryInterface<Common::iEventQueue>("iEventQueue");
-    EVAF_TEST_X(eventQueue, "No iEventQueue interface");
+    EVAF_TEST_X(eventQueue, "No iEventQueue interface")
 
     // Subscribe to the 'ready' event
-    EVAF_TEST_X((mEvReady = eventQueue->subscribeEvent(eventQueue->queryEvent(Common::iApp::EV_READY), this)), "No 'ready' event");
+    EVAF_TEST_X((mEvReady = eventQueue->subscribeEvent(eventQueue->queryEvent(Common::iApp::EV_READY), this)), "No 'ready' event")
 
     mReady = true;
 
@@ -123,7 +122,7 @@ bool Module::event(QEvent * e)
 QString Module::readPassword()
 {
     bool noEcho = false;
-#ifdef Q_OS_LINUX
+#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
     termios oldt;
     tcgetattr(STDIN_FILENO, &oldt);
     termios newt = oldt;
@@ -146,7 +145,7 @@ QString Module::readPassword()
         cout << endl;
     }
 
-#ifdef Q_OS_LINUX
+#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
     tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
 #elif defined Q_OS_WIN32
     SetConsoleMode(hStdin, mode);
@@ -159,7 +158,10 @@ void Module::generatePassword()
 {
     QString masterPassword;
     QString appName;
-    int passwordLength = DefaultPasswordLength;
+    QString suffix;
+    int passwordLength = 0;
+    uint flags = 0;
+    int alnum = -1;
 
     // Process command-line arguments
     QStringList args = QCoreApplication::arguments();
@@ -170,6 +172,18 @@ void Module::generatePassword()
             masterPassword = arg.at(1);
         else if (QRegExp("-[-]?n(ame)?").exactMatch(arg.at(0)) && arg.size() > 1)
             appName = arg.at(1);
+        else if (QRegExp("-[-]?s(uffix)?").exactMatch(arg.at(0)) && arg.size() > 1)
+            suffix = arg.at(1);
+        else if (QRegExp("-[-]?a(lphanumeric)?").exactMatch(arg.at(0))) {
+            if (arg.size() > 1) {
+                if (Common::isTrue(arg.at(1)))
+                    alnum = 1;
+                else if (Common::isFalse(arg.at(1)))
+                    alnum = 0;
+            }
+            else
+                alnum = 1;
+        }
         else if (QRegExp("-[-]?l(ength)?").exactMatch(arg.at(0)) && arg.size() > 1) {
             bool ok;
             int t = arg.at(1).toInt(&ok);
@@ -181,6 +195,14 @@ void Module::generatePassword()
         }
     }
 
+    // Set flags
+    if (alnum != -1) {
+        if (alnum == 1)
+            flags |= uint(iGenerator::ALPHANUMERIC);
+        else
+            flags &= ~uint(iGenerator::ALPHANUMERIC);
+    }
+
     QTextStream cin(stdin);
     QTextStream cout(stdout);
 
@@ -199,18 +221,32 @@ void Module::generatePassword()
     QExplicitlySharedDataPointer<PswGen::Storage::Data> data;
     if (mStorage) {
         data = mStorage->query(appName);
-        if (data)
-            passwordLength = data->length();
+        if (data) {
+            if (passwordLength == 0)
+                passwordLength = data->length();
+            if (suffix.isEmpty())
+                suffix = data->suffix();
+            if (alnum == -1)
+                flags = data->flags();
+        }
     }
 
+    // If the length argument is still not initialized, use the default length value
+    if (!passwordLength)
+        passwordLength = DefaultPasswordLength;
+
     // Generate password
-    QString password = mGenerator->generatePassword(appName, masterPassword, passwordLength);
+    QString password = mGenerator->generatePassword(appName + suffix, masterPassword, passwordLength);
     cout << "Generated password : " << password << endl;
 
     // Store arguments for this password
     if (mStorage) {
         if (!data)
-            data = new Storage::Data(appName, passwordLength);
+            data = new Storage::Data(appName, suffix, passwordLength);
+        else {
+            data->setSuffix(suffix);
+            data->setLength(passwordLength);
+        }
         mStorage->save(appName, data);
     }
 }