Commit | Line | Data |
---|---|---|
7f511828 FT |
1 | #!/usr/bin/python3 |
2 | ||
6532632d | 3 | import sys, os, getopt, pwd, operator |
be69f65b | 4 | from fulbank import auth, data |
7f511828 | 5 | |
be69f65b | 6 | sessname = data.defaultsess() |
7f511828 FT |
7 | sess = None |
8 | ||
6532632d FT |
9 | def pfxmatch(pfx, item): |
10 | return str(item)[:len(pfx)] == pfx | |
11 | ||
12 | class ambiguous(LookupError): | |
13 | def __init__(self, a, b): | |
14 | super().__init__("ambigous match: %s and %s" % (a, b)) | |
15 | self.a = a | |
16 | self.b = b | |
17 | ||
18 | def find(seq, *, item=None, test=None, match=None, key=None, default=LookupError): | |
7f511828 FT |
19 | if key is None: |
20 | key = lambda o: o | |
21 | if match is None and item is not None: | |
6532632d FT |
22 | match = lambda o: test(item, o) |
23 | if test is None: | |
24 | test = operator.eq | |
25 | found = None | |
7f511828 FT |
26 | for thing in seq: |
27 | if match(key(thing)): | |
6532632d FT |
28 | if found is None: |
29 | found = thing | |
30 | else: | |
31 | if default is LookupError: | |
32 | raise ambiguous(key(found), key(thing)) | |
33 | else: | |
34 | return default | |
35 | if found is not None: | |
36 | return found | |
7f511828 FT |
37 | if default is LookupError: |
38 | raise LookupError() | |
39 | else: | |
40 | return default | |
41 | ||
42 | def usage(out): | |
be69f65b | 43 | out.write("usage: netbank [-h] [-s SESSION-ID] COMMAND [ARGS...]\n") |
7f511828 FT |
44 | |
45 | def requiresess(fn): | |
46 | def wrap(cmd, args): | |
47 | if sess is None: | |
48 | sys.stderr.write("netbank: %s: no current session\n" % (cmd)) | |
49 | sys.exit(1) | |
50 | return fn(cmd, args) | |
51 | return wrap | |
52 | ||
53 | commands = {} | |
54 | ||
55 | def cmd_login(cmd, args): | |
56 | global sess | |
be69f65b FT |
57 | opts, args = getopt.getopt(args, "t:") |
58 | typename = sessname | |
59 | for o, a in opts: | |
60 | if o == "-t": | |
61 | typename = a | |
7f511828 | 62 | if len(args) < 1: |
be69f65b | 63 | sys.stderr.write("usage: login [-t BANK-ID] TYPE\n") |
7f511828 | 64 | sys.exit(1) |
be69f65b | 65 | sess = data.getsessnam(typename).create() |
7f511828 | 66 | if args[0] == "bankid": |
a094b3b5 FT |
67 | authfun = sess.auth_bankid |
68 | elif args[0] == "token": | |
69 | authfun = sess.auth_token | |
7f511828 FT |
70 | else: |
71 | sys.stderr.write("netbank: %s: unknown authentication type\n" % (args[0])) | |
72 | sys.exit(1) | |
a094b3b5 FT |
73 | if len(args) < 2: |
74 | sys.stderr.write("usage: login bankid USER-ID\n") | |
75 | sys.exit(1) | |
76 | with auth.ttyconv() as conv: | |
77 | try: | |
78 | authfun(args[1], conv) | |
79 | except auth.autherror as err: | |
80 | sys.stderr.write("netbank: authentication failed: %s\n" % err) | |
81 | sys.exit(1) | |
7f511828 FT |
82 | commands["login"] = cmd_login |
83 | ||
84 | @requiresess | |
85 | def cmd_logout(cmd, args): | |
d6d73d98 | 86 | global sess |
7f511828 FT |
87 | if sess is not None: |
88 | sess.close() | |
89 | sess = None | |
90 | commands["logout"] = cmd_logout | |
91 | ||
92 | @requiresess | |
93 | def cmd_ping(cmd, args): | |
94 | sess.keepalive() | |
95 | commands["ping"] = cmd_ping | |
96 | ||
97 | @requiresess | |
98 | def cmd_lsacct(cmd, args): | |
99 | for acct in sess.accounts: | |
100 | sys.stdout.write("%s (%s): %s\n" % (acct.number, acct.name, acct.balance)) | |
101 | commands["lsacct"] = cmd_lsacct | |
102 | ||
103 | @requiresess | |
104 | def cmd_lstxn(cmd, args): | |
105 | opts, args = getopt.getopt(args, "n:") | |
106 | num = 10 | |
107 | for o, a in opts: | |
108 | if o == "-n": | |
109 | num = int(a) | |
110 | if len(args) < 1: | |
111 | sys.stderr.write("usage: lstxn [-n NUM] ACCOUNT\n") | |
112 | sys.exit(1) | |
113 | try: | |
6532632d FT |
114 | acct = find(sess.accounts, item=args[0], key=lambda acct: acct.number, test=pfxmatch) |
115 | except ambiguous as exc: | |
116 | sys.stderr.write("netbank: %s: ambiguous match between %s and %s\n" % (args[0], exc.a, exc.b)) | |
117 | sys.exit(1) | |
7f511828 FT |
118 | except LookupError: |
119 | sys.stderr.write("netbank: %s: no such account\n" % (args[0])) | |
120 | sys.exit(1) | |
121 | for i, txn in zip(range(num), acct.transactions()): | |
122 | sys.stdout.write("%s %s: %s\n" % (txn.date.isoformat(), txn.value, txn.message)) | |
123 | commands["lstxn"] = cmd_lstxn | |
124 | ||
125 | def main(): | |
be69f65b | 126 | global sess, sessname |
7f511828 | 127 | |
be69f65b | 128 | opts, args = getopt.getopt(sys.argv[1:], "hs:") |
7f511828 FT |
129 | for o, a in opts: |
130 | if o == "-h": | |
131 | usage(sys.stdout) | |
132 | sys.exit(0) | |
be69f65b FT |
133 | if o == "-s": |
134 | sessname = a | |
135 | if len(args) < 1: | |
7f511828 FT |
136 | usage(sys.stderr) |
137 | sys.exit(1) | |
138 | ||
be69f65b FT |
139 | cmd = args[0] |
140 | args = args[1:] | |
7f511828 | 141 | |
be69f65b | 142 | sess = data.loadsess(sessname, None) |
7f511828 FT |
143 | if cmd in commands: |
144 | commands[cmd](cmd, args) | |
145 | else: | |
146 | sys.stderr.write("netbank: %s: unknown command\n" % (cmd)) | |
147 | sys.exit(1) | |
be69f65b | 148 | data.savesess(sess, sessname) |
7f511828 FT |
149 | |
150 | try: | |
151 | if __name__ == "__main__": | |
152 | main() | |
153 | except KeyboardInterrupt: | |
154 | sys.exit(1) |