-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbw
99 lines (82 loc) · 2.85 KB
/
bw
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
#!/usr/bin/env python3
import json
import os
import sys
from subprocess import run
# App Class
class App(object):
def __init__(self):
field_names = ["id", "name", "user", "folder"]
field_names_string = ",".join(field_names)
p = run(
["rbw", "list", "--fields", field_names_string],
capture_output=True,
encoding="utf-8",
check=True,
)
self.passwords = []
for l in p.stdout.splitlines():
fields = l.split("\t")
d = dict(zip(field_names, fields))
if d["folder"]:
d["path"] = d["folder"] + "/" + d["name"]
else:
d["path"] = d["name"]
self.passwords.append(d)
def activate(self, index):
if not self.matches:
return
f = open("/tmp/bwlog", "w")
f.write(f"rbw get {self.matches[index]['id']}")
f.close()
os.system(f"rbw get {self.matches[index]['id']} | tr -d '\\n' | xclip -selection clipboard -in")
os.system(f'/usr/bin/notify-send --icon=gtk-authentication "Password" "Copied {self.matches[index]["name"]} - {self.matches[index]["user"]} to clipboard"')
os.system(f"rbw get {self.matches[index]['id']} | tr -d '\\n' | xclip -selection clipboard -in")
sys.stdout.write('"Close"\n')
sys.stdout.flush()
def search(self, query):
query = query.split(' ', 1)[1]
self.matches = []
search_fields = ["path", "user"]
words = query.lower().split()
for p in self.passwords:
all_matches = True
for w in words:
for k in search_fields:
if w in p[k].lower():
break
else:
all_matches = False
break
if all_matches:
self.matches.append(p)
for index, password in enumerate(self.matches):
output_name = "{} - {}".format(password['path'],password['user'])
sys.stdout.write(json.dumps({
'Append': {
'id': index,
'name': output_name,
'description': '',
'keywords': None,
'icon': None,
'exec': None,
'window': None
}
}))
sys.stdout.write('\n')
sys.stdout.write('"Finished"\n')
sys.stdout.flush()
# Main Execution
def main():
app = App()
for line in sys.stdin:
try:
request = json.loads(line)
if 'Search' in request:
app.search(request['Search'])
elif 'Activate' in request:
app.activate(request['Activate'])
except json.decoder.JSONDecodeError:
pass
if __name__ == '__main__':
main()