-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepasscli.js
executable file
·189 lines (174 loc) · 4.07 KB
/
keepasscli.js
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
#!/usr/bin/env node
var keepass = require('./keepass'),
path = require('path'),
spawn = require('child_process').spawn,
exec = require('child_process').exec;
function readPassword(result) {
require('tty').setRawMode(true);
process.stdin.resume();
var r = '';
process.stderr.write('Password: ');
process.stdin.on('keypress', function (chunk, key) {
switch(chunk) {
case "\n": case "\r": case "\u0004":
// quit input mode
require('tty').setRawMode(false);
process.stdin.pause();
process.stdout.write("\n");
// run callback
result(r);
break;
// backspace
case "\u007f":
r = r.substr(0, r.length-1);
break;
// empty input / send null to callback
case "\u0003": // CRTL + C
//case "\u001b": // escape
process.exit();
break;
default:
r += chunk;
break;
}
});
}
function main(args) {
var databaseFile;
var userKeys = [];
var filters = [];
var actions = [];
function eachNext(arr, handler, next) {
(function nextItem() {
var item = arr.shift();
if (item) {
handler(item, nextItem);
} else { // last
next();
}
})();
}
eachNext(args, parseArgument, readDatabase);
function parseArgument(arg, next) {
if (arg[0] !== '-') {
databaseFile = arg;
return next();
}
({ p: function() {
readPassword(function(pw) {
userKeys.push(keepass.userPassword(pw));
next();
});
},
f: function() {
var filter = args.shift().split(':', 2);
var filterName = filter[0];
var filterValue = filter[1].toLowerCase();
filters.push(function(e) {
if (e[filterName]) {
var value = e[filterName].apply(e);
return (value && value.toLowerCase().indexOf(filterValue) >= 0);
} else {
return false;
}
});
next();
},
n: function() {
var left = parseInt(args.shift());
filters.push(function(e) {
return left > 0 && left-- && true;
});
next();
},
'1': function() {
var left = 1;
filters.push(function(e) {
return left > 0 && left-- && true;
});
next();
},
o: function() {
var names = args.shift();
actions.push(function(e, n) {
names.split(',').forEach(function(name) {
if (e[name]) {
process.stdout.write(e[name].apply(e));
process.stdout.write('\t');
}
});
process.stdout.write('\n');
n();
});
next();
},
t: function() {
var text = args.shift();
actions.push(function(e, n) {
process.stdout.write(text + '\n');
n();
});
next();
},
c: function() {
var name = args.shift();
actions.push(function(e, n) {
spawn('notify-send', [ '-c', 'keepass', '-t', '5000', name + ' was copied to clipboard' ]);
var value = '';
if (e[name]) { value = e[name].apply(e); }
var cmd = './clipboard "' + value + '"';
var clip = exec(cmd, function() {
n();
});
});
next();
},
w: function() {
var time = parseInt(args.shift());
actions.push(function(e, n) {
setTimeout(n, time);
});
next();
}
}[arg.substr(1)] || function() {
console.error('Invalid option', arg);
process.exit(1);
})();
}
function readDatabase() {
keepass.readDatabaseFromFile(userKeys, databaseFile, handleDatabase, function(error) {
if (error instanceof Error) {
console.error(error.message);
} else if (typeof error === 'string') {
console.error(error);
} else {
console.error('Unknown error:', error);
}
process.exit(1);
});
}
function handleDatabase(db) {
handlegroup(db.root(), end);
function handlegroup(group, next) {
eachNext(group.groups(), handlegroup, function() {
eachNext(group.entries(), handleentry, next);
});
}
function filter(entry) {
return filters.every(function(f) { return f(entry); });
}
function handleentry(entry, next) {
if (filter(entry)) {
eachNext(actions.slice(0), function(action, next) {
action(entry, next);
}, next);
} else {
next();
}
}
}
function end() {
}
}
// Slice 'node keepasscli.js' from the actual arguments.
main(process.argv.slice(2));