2 * @file PswGen/CLI/cli.cpp
3 * @brief Command line interface for the PswGen application
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
23 #include <Generator/iGenerator>
24 #include <Storage/iStorage>
26 #include <Common/Globals>
27 #include <Common/iLogger>
28 #include <Common/iRegistry>
29 #include <Common/iEventQueue>
30 #include <Common/iApp>
31 #include <Common/Event>
43 VER_EXPORT_VERSION_INFO()
44 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR
, eVaf::PswGen::CLI::Module
)
47 //-------------------------------------------------------------------
50 using namespace eVaf::PswGen::CLI
;
52 int const Module::DefaultPasswordLength
= 16;
61 setObjectName(QString("%1-Module").arg(VER_MODULE_NAME_STR
));
62 EVAF_INFO("%s created", qPrintable(objectName()));
67 EVAF_INFO("%s destroyed", qPrintable(objectName()));
70 bool Module::init(QString
const & args
)
74 // Get the iGenerator interface
75 EVAF_TEST_X((mGenerator
= evafQueryInterface
<PswGen::iGenerator
>("iGenerator")), "No iGenerator interface");
77 // Get the optional iStorage interface
78 mStorage
= evafQueryInterface
<PswGen::iStorage
>("iStorage");
80 EVAF_WARNING("No iStorage interface");
82 // Get the iEventQueue interface and subscribe to the 'ready' event
83 Common::iEventQueue
* eventQueue
= evafQueryInterface
<Common::iEventQueue
>("iEventQueue");
84 EVAF_TEST_X(eventQueue
, "No iEventQueue interface");
86 // Subscribe to the 'ready' event
87 EVAF_TEST_X((mEvReady
= eventQueue
->subscribeEvent(eventQueue
->queryEvent(Common::iApp::EV_READY
), this)), "No 'ready' event");
91 EVAF_INFO("%s initialized", qPrintable(objectName()));
100 EVAF_INFO("%s finalized", qPrintable(objectName()));
103 bool Module::event(QEvent
* e
)
105 if (e
->type() == Common::Event::eVafEvent
) {
106 Common::Event
* event
= static_cast<Common::Event
*>(e
);
108 if (event
->id() == mEvReady
) {
110 // Generate the password
113 // Quit the application
114 Common::iApp::instance()->quit();
120 return Plugins::iPlugin::event(e
);
123 QString
Module::readPassword()
128 tcgetattr(STDIN_FILENO
, &oldt
);
130 newt
.c_lflag
&= ~ECHO
;
131 tcsetattr(STDIN_FILENO
, TCSANOW
, &newt
);
133 #elif defined Q_OS_WIN32
134 HANDLE hStdin
= GetStdHandle(STD_INPUT_HANDLE
);
136 GetConsoleMode(hStdin
, &mode
);
137 SetConsoleMode(hStdin
, mode
& (~ENABLE_ECHO_INPUT
));
141 QTextStream
cin(stdin
);
142 QString rval
= cin
.readLine();
145 QTextStream
cout(stdout
);
150 tcsetattr(STDIN_FILENO
, TCSANOW
, &oldt
);
151 #elif defined Q_OS_WIN32
152 SetConsoleMode(hStdin
, mode
);
158 void Module::generatePassword()
160 QString masterPassword
;
162 int passwordLength
= 0;
164 // Process command-line arguments
165 QStringList args
= QCoreApplication::arguments();
166 for (int i
= 0; i
< args
.size(); ++i
) {
167 QStringList arg
= args
.at(i
).simplified().split('=');
169 if (QRegExp("-[-]?p(assword)?").exactMatch(arg
.at(0)) && arg
.size() > 1)
170 masterPassword
= arg
.at(1);
171 else if (QRegExp("-[-]?n(ame)?").exactMatch(arg
.at(0)) && arg
.size() > 1)
173 else if (QRegExp("-[-]?l(ength)?").exactMatch(arg
.at(0)) && arg
.size() > 1) {
175 int t
= arg
.at(1).toInt(&ok
);
177 EVAF_FATAL_ERROR("Invalid argument : %s", qPrintable(args
.at(i
)));
184 QTextStream
cin(stdin
);
185 QTextStream
cout(stdout
);
187 // Get missing arguments
188 while (masterPassword
.isEmpty()) {
189 cout
<< tr("Master password : ") << flush
;
190 masterPassword
= readPassword();
193 while (appName
.isEmpty()) {
194 cout
<< tr("Application name : ") << flush
;
198 // Get more arguments from the storage
199 QExplicitlySharedDataPointer
<PswGen::Storage::Data
> data
;
201 data
= mStorage
->query(appName
);
202 if (data
&& passwordLength
== 0)
203 passwordLength
= data
->length();
206 // If the length argument is still not initialized, use the default length value
208 passwordLength
= DefaultPasswordLength
;
211 QString password
= mGenerator
->generatePassword(appName
, masterPassword
, passwordLength
);
212 cout
<< "Generated password : " << password
<< endl
;
214 // Store arguments for this password
217 data
= new Storage::Data(appName
, passwordLength
);
219 data
->setLength(passwordLength
);
220 mStorage
->save(appName
, data
);