-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
188 lines (149 loc) · 6.03 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "passwordStore.hpp"
#include "passwordGenerator.hpp"
#include "XClipboard.hpp"
#include "dmenu.hpp"
#include "notifications.hpp"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <random>
#include <optional>
#include <functional>
#include <stdexcept>
using namespace std::literals;
const int maxLines = 20;
const DmenuFlags defaultFlags = { .showPos = DmenuFlags::CENTER };
constexpr static auto generators = passwordGeneratorList(
[] { return "!-~"sv; },
[] { return "0-9A-Za-z!?+_()"sv; }
);
using namespace std::placeholders;
PasswordStore passwordStore;
XClipboard clipboard;
Notifications notifier("passDmenu");
template<typename T>
struct DmenuResult {
std::string value, flags;
std::optional<T> entry;
DmenuResult(const std::string& inValue, const std::vector<T>& entries, std::function<bool(const std::string&, const T&)> predicate) {
auto slashPos = std::find(begin(inValue), end(inValue), '/');
value = std::string(begin(inValue), slashPos);
if (slashPos != end(inValue)) flags = std::string(slashPos, end(inValue));
auto entryIt = std::find_if(begin(entries), end(entries), std::bind(predicate, value, _1));
if (entryIt != end(entries)) entry = *entryIt;
}
bool isEmpty() { return value.empty() && flags.empty(); }
bool isCommand() { return !entry || !flags.empty(); }
T& operator*() { return *entry; }
T* operator->() { return &*entry; }
};
DmenuResult<std::vector<PasswordEntry>> askService(std::vector<std::vector<PasswordEntry>>& services) {
std::vector<std::string> serviceOptions(services.size());
std::transform(begin(services), end(services), begin(serviceOptions), [](const auto& service){ return service[0].service; });
DmenuFlags flags = defaultFlags;
flags.lines = std::min((int)services.size(), maxLines);
Dmenu d(serviceOptions, flags);
return DmenuResult<std::vector<PasswordEntry>>(d.result(), services, [](const auto& needle, const auto& v) { return v[0].service == needle; });
}
DmenuResult<PasswordEntry> askUser(std::vector<PasswordEntry> users) {
std::vector<std::string> userOptions(users.size());
std::transform(begin(users), end(users), begin(userOptions), [](const auto& entry){ return entry.username; });
DmenuFlags flags = defaultFlags;
flags.lines = std::min((int)users.size(), maxLines);
flags.prompt = "User:";
Dmenu d(userOptions, flags);
return DmenuResult<PasswordEntry>(d.result(), users, [](const auto& needle, const auto& v) { return v.username == needle; });
}
bool askYesNo(std::string prompt, std::string yesOption = "Yes", std::string noOption = "No") {
DmenuFlags flags = defaultFlags;
flags.lines = 2;
if (!prompt.empty()) flags.prompt = prompt;
Dmenu d({ yesOption, noOption }, flags);
return d.result() == yesOption;
}
std::string askValue(std::string prompt) {
DmenuFlags flags = defaultFlags;
flags.lines = 2;
if (!prompt.empty()) flags.prompt = prompt;
Dmenu d({}, flags);
return d.result();
}
std::string askPassword(std::string prompt) {
std::mt19937 rng(std::random_device{}());
std::vector<std::string> suggestions(generators.size());
std::transform(begin(generators), end(generators), begin(suggestions), [&rng](const auto& gen){ return gen(rng, 10); });
DmenuFlags flags = defaultFlags;
flags.lines = 2;
if (!prompt.empty()) flags.prompt = prompt;
Dmenu d(suggestions, flags);
return d.result();
}
int handleUserCommand(const std::string& service, DmenuResult<PasswordEntry>& result) {
if (result.flags.empty()) {
if (!askYesNo("Do you want to:", "Add " + result.value + " to " + service, "Exit")) return EXIT_SUCCESS;
std::string password = askPassword("Enter Password:");
PasswordEntry newEntry(result.value, result.value, password);
passwordStore.serializeEntry(newEntry, notifier);
return EXIT_SUCCESS;
}
if (result.flags == "/e") {
PasswordEntry toEdit = *result;
passwordStore.decryptEntry(toEdit);
toEdit.password = askPassword("New Password:");
passwordStore.serializeEntry(toEdit, notifier);
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
int handleServiceCommand(DmenuResult<std::vector<PasswordEntry>>& result) {
if (result.flags == "/e") {
if (result->size() != 1) throw std::runtime_error("Cannot edit service directory");
PasswordEntry toEdit = result->at(0);
passwordStore.decryptEntry(toEdit);
toEdit.password = askPassword("New Password:");
passwordStore.serializeEntry(toEdit, notifier);
return EXIT_SUCCESS;
}
if (result.flags == "/n" || result.flags.empty()) {
std::string service;
if (result.value.empty() || result.flags.empty()) {
if (!askYesNo("Want to add service: ", "Yes", "No, exit program")) return EXIT_SUCCESS;
service = askValue("Enter service:");
} else if (askYesNo("Want to add user: ", "Yes, Add to " + result.value, "No, exit program"))
service = result.value;
else
return EXIT_SUCCESS;
std::string username = askValue("Enter Username:");
if (username.empty()) return EXIT_FAILURE; // TODO: notify this
std::string password = askPassword("Enter Password:");
PasswordEntry newEntry(service, username, password);
passwordStore.serializeEntry(newEntry, notifier);
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
void copyInfo(PasswordEntry& entry) {
passwordStore.decryptEntry(entry);
auto userNotification = notifier.create("Copied username", "Copied username for " + entry.service).timeout(5000).show();
if (!clipboard.waitPaste(entry.username)) return;
userNotification.clear();
notifier.create("Copied password", "Copied password for " + entry.service).timeout(5000).show();
clipboard.waitPaste(entry.password);
}
int main() {
auto entries = passwordStore.getEntries();
auto serviceResult = askService(entries);
if (serviceResult.isEmpty()) return EXIT_SUCCESS;
if (serviceResult.isCommand()) return handleServiceCommand(serviceResult);
if (serviceResult->size() == 1) {
copyInfo(serviceResult->at(0));
return EXIT_SUCCESS;
}
auto userResult = askUser(*serviceResult);
if (!userResult.isCommand()) {
copyInfo(*userResult);
return EXIT_SUCCESS;
}
if (userResult.isEmpty()) return EXIT_SUCCESS;
return handleUserCommand(serviceResult.value, userResult);
}