Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt @ 0:a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 11 Sep 2022 16:11:37 -0700 |
parents | |
children | eafa3779aef8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a6cfdffcaa94 |
---|---|
1 package name.blackcap.passman | |
2 | |
3 import org.apache.commons.cli.CommandLine | |
4 import org.apache.commons.cli.DefaultParser | |
5 import org.apache.commons.cli.Options | |
6 import org.apache.commons.cli.ParseException | |
7 | |
8 class ReadSubcommand(): Subcommand() { | |
9 private lateinit var commandLine: CommandLine | |
10 | |
11 override fun run(args: Array<String>) { | |
12 val options = Options().apply { | |
13 addOption("c", "clipboard", false, "Copy username and password into clipboard.") | |
14 addOption("l", "long", false, "Long format listing.") | |
15 } | |
16 try { | |
17 commandLine = DefaultParser().parse(options, args) | |
18 } catch (e: ParseException) { | |
19 die(e.message ?: "syntax error", 2) | |
20 } | |
21 val clipboard = commandLine.hasOption("clipboard") | |
22 val long = commandLine.hasOption("long") | |
23 if (commandLine.args.size != 1) { | |
24 die("expecting site name", 2) | |
25 } | |
26 val nameIn = commandLine.args[0]; | |
27 val db = Database.open() | |
28 val id = db.makeKey(nameIn) | |
29 | |
30 db.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { | |
31 it.setLong(1, id) | |
32 val result = it.executeQuery() | |
33 if (!result.next()) { | |
34 die("no record matches $nameIn") | |
35 } | |
36 val entry = Entry( | |
37 name = result.getDecryptedString(1, db.encryption), | |
38 username = result.getDecryptedString(2, db.encryption), | |
39 password = result.getDecrypted(3, db.encryption), | |
40 notes = result.getDecryptedString(4, db.encryption), | |
41 created = result.getDate(5), | |
42 modified = result.getDate(6), | |
43 accessed = result.getDate(7) | |
44 ) | |
45 try { | |
46 val redaction = if (clipboard) { "(in clipboard)" } else { null } | |
47 if (long) { | |
48 entry.printLong(redaction) | |
49 } else { | |
50 entry.print(redaction) | |
51 } | |
52 if (clipboard) { | |
53 writeToClipboard(entry.password) | |
54 } | |
55 } finally { | |
56 entry.password.clear() | |
57 } | |
58 } | |
59 | |
60 db.connection.prepareStatement("update passwords set accessed = ? where id = ?").use { | |
61 it.setLong(1, System.currentTimeMillis()) | |
62 it.setLong(2, id) | |
63 it.execute() | |
64 } | |
65 } | |
66 } |