-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nim
359 lines (281 loc) · 9.19 KB
/
main.nim
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import nigui
import strutils
import sequtils
import passutils
import safeutils
import dialogs
import osproc
const
windowWidth = 400
windowHeight = 210
labelFontSize = 26
textBoxFontSize = 26
buttonFontSize = 26
separatorFontSize = 5
app.init()
# window setup
var window = newWindow("nimpass")
window.width = windowWidth
window.height = windowHeight
#! WIDGETS AND THEIR LAYOUTS
var layout = newLayoutContainer(Layout_Vertical)
# separator
proc newSeparator(): Label =
var separator = newLabel("")
separator.fontSize = separatorFontSize
return separator
# text box where user puts password's name
var passNameWidget = newTextBox("")
passNameWidget.fontSize = textBoxFontSize
block:
var tempLayout = newLayoutContainer(Layout_Vertical)
var frame = newFrame("Password name")
frame.fontSize = labelFontSize
tempLayout.frame = frame
tempLayout.add(passNameWidget)
layout.add(tempLayout)
# buttons
var saveButton = newButton("Save")
saveButton.widthMode = WidthMode_Expand
saveButton.fontSize = buttonFontSize
var updateButton = newButton("Update")
updateButton.widthMode = WidthMode_Expand
updateButton.fontSize = buttonFontSize
var deleteButton = newButton("Delete")
deleteButton.widthMode = WidthMode_Expand
deleteButton.fontSize = buttonFontSize
var listButton = newButton("List")
listButton.widthMode = WidthMode_Expand
listButton.fontSize = buttonFontSize
var showButton = newButton("Show")
showButton.widthMode = WidthMode_Expand
showButton.fontSize = buttonFontSize
var copyButton = newButton("Copy")
copyButton.widthMode = WidthMode_Expand
copyButton.fontSize = buttonFontSize
var typeButton = newButton("Type")
typeButton.widthMode = WidthMode_Expand
typeButton.fontSize = buttonFontSize
block:
var tempLayout = newLayoutContainer(Layout_Horizontal)
tempLayout.add(saveButton)
tempLayout.add(updateButton)
tempLayout.add(deleteButton)
layout.add(newSeparator())
layout.add(newSeparator())
layout.add(tempLayout)
block:
var tempLayout = newLayoutContainer(Layout_Horizontal)
tempLayout.add(listButton)
tempLayout.add(showButton)
tempLayout.add(copyButton)
tempLayout.add(typeButton)
layout.add(tempLayout)
window.add(layout)
#! CALLBACKS FOR BUTTONS
saveButton.onClick = proc(event: ClickEvent) =
var mkey = masterkeyPrompt()
if mkey == "":
showSuccess("Cancelled the operation.")
return
try:
verifyMasterkey(mkey)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
var
name = passNameWidget.text
password = passwordPrompt()
if password == "":
showSuccess("Cancelled the operation.")
return
try:
insertEntry(mkey, Entry(name: name, password: password))
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
except NameIsTooShortError:
showError("Name is too short (minimum length is $1)." % intToStr(nameMinLength))
return
except EntryAlreadyExistsError:
showError("Entry with name $1 already exists (click Update if you want to update the entry)." % name)
return
except PasswordContainsBadCharacterError:
showError("Password or its name contains illegal character. (allowed characters are $1)." % alphabet.join(""))
return
showSuccess("Saved new entry.")
updateButton.onClick = proc(event: ClickEvent) =
var mkey = masterkeyPrompt()
if mkey == "":
showSuccess("Cancelled the operation.")
return
try:
verifyMasterkey(mkey)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
var
name = passNameWidget.text
password = passwordPrompt()
if password == "":
showSuccess("Cancelled the operation.")
return
try:
deleteEntry(mkey, name)
insertEntry(mkey, Entry(name: name, password: password))
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
except NameIsTooShortError:
showError("Name is too short (minimum length is $1)." % intToStr(nameMinLength))
return
except EntryNotFoundError:
showError("Entry with name $1 was not found (click Save if you want to insert a new entry)." % name)
return
except PasswordContainsBadCharacterError:
showError("Password or its name contains illegal character. (allowed characters are $1)." % alphabet.join(""))
return
showSuccess("Updated the old entry.")
deleteButton.onClick = proc(event: ClickEvent) =
var
mkey = masterkeyPrompt()
name = passNameWidget.text
if mkey == "":
showSuccess("Cancelled the operation.")
return
try:
verifyMasterkey(mkey)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
try:
deleteEntry(mkey, name)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
except NameIsTooShortError:
showError("Name is too short (minimum length is $1)." % intToStr(nameMinLength))
return
except EntryNotFoundError:
showError("Entry with name $1 was not found (click Save if you want to insert a new entry)." % name)
return
showSuccess("Deleted the entry.")
listButton.onClick = proc(event: ClickEvent) =
var
mkey = masterkeyPrompt()
if mkey == "":
showSuccess("Cancelled the operation.")
return
try:
verifyMasterkey(mkey)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
try:
var s = readStorage(mkey)
var text = "Stored passwords(names are shown):\n"
for e in s.entries:
text.add(e.name & "\n")
showSuccess(text)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
showButton.onClick = proc(event: ClickEvent) =
var
mkey = masterkeyPrompt()
name = passNameWidget.text
if mkey == "":
showSuccess("Cancelled the operation.")
return
try:
verifyMasterkey(mkey)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
try:
var e = getEntry(mkey, name)
show(e.name, e.password)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
except NameIsTooShortError:
showError("Name is too short (minimum length is $1)." % intToStr(nameMinLength))
return
except EntryNotFoundError:
showError("Entry with name $1 was not found (click Save if you want to insert a new entry)." % name)
return
copyButton.onClick = proc(event: ClickEvent) =
var
mkey = masterkeyPrompt()
name = passNameWidget.text
if mkey == "":
showSuccess("Cancelled the operation.")
return
try:
verifyMasterkey(mkey)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
try:
var e = getEntry(mkey, name)
var es = execCmd("echo -n '$1' | xclip -selection clipboard" % e.password)
if es == 0:
showSuccess("Copied the password into the clipboard.")
else:
showError("Could not copy the password. Check if xclip is installed.")
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
except NameIsTooShortError:
showError("Name is too short (minimum length is $1)." % intToStr(nameMinLength))
return
except EntryNotFoundError:
showError("Entry with name $1 was not found (click Save if you want to insert a new entry)." % name)
return
var timer: Timer
var timerState = 0
var password = ""
proc tickTimer(event: TimerEvent) =
typeButton.text = intToStr(5 - timerState)
timerState += 1
if timerState == 6:
var code = execCmd("xdotool type '$1'" % password)
if code == 0:
showSuccess("Typed the password.")
else:
showError("Could not type the password. Check if xdotool is installed")
timer.stop()
timerState = 0
typeButton.text = "Type"
typeButton.onClick = proc(event: ClickEvent) =
if timerState != 0:
return
var
mkey = masterkeyPrompt()
name = passNameWidget.text
if mkey == "":
showSuccess("Cancelled the operation.")
return
try:
verifyMasterkey(mkey)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
try:
var e = getEntry(mkey, name)
password = e.password
var tm: TimerEvent
tickTimer(tm)
timer = startRepeatingTimer(1000, tickTimer)
except InvalidMasterkeyError:
showError("Invalid masterkey.")
return
except NameIsTooShortError:
showError("Name is too short (minimum length is $1)." % intToStr(nameMinLength))
return
except EntryNotFoundError:
showError("Entry with name $1 was not found (click Save if you want to insert a new entry)." % name)
return
initStorage()
window.show()
app.run()