| 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import sys, os, getopt, pwd, operator |
| 4 | from fulbank import auth, data |
| 5 | |
| 6 | sessname = data.defaultsess() |
| 7 | sess = None |
| 8 | |
| 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): |
| 19 | if key is None: |
| 20 | key = lambda o: o |
| 21 | if match is None and item is not None: |
| 22 | match = lambda o: test(item, o) |
| 23 | if test is None: |
| 24 | test = operator.eq |
| 25 | found = None |
| 26 | for thing in seq: |
| 27 | if match(key(thing)): |
| 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 |
| 37 | if default is LookupError: |
| 38 | raise LookupError() |
| 39 | else: |
| 40 | return default |
| 41 | |
| 42 | def usage(out): |
| 43 | out.write("usage: netbank [-h] [-s SESSION-ID] COMMAND [ARGS...]\n") |
| 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 |
| 57 | opts, args = getopt.getopt(args, "t:") |
| 58 | typename = sessname |
| 59 | for o, a in opts: |
| 60 | if o == "-t": |
| 61 | typename = a |
| 62 | if len(args) < 1: |
| 63 | sys.stderr.write("usage: login [-t BANK-ID] TYPE\n") |
| 64 | sys.exit(1) |
| 65 | sess = data.getsessnam(typename).create() |
| 66 | if args[0] == "bankid": |
| 67 | authfun = sess.auth_bankid |
| 68 | elif args[0] == "token": |
| 69 | authfun = sess.auth_token |
| 70 | else: |
| 71 | sys.stderr.write("netbank: %s: unknown authentication type\n" % (args[0])) |
| 72 | sys.exit(1) |
| 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) |
| 82 | commands["login"] = cmd_login |
| 83 | |
| 84 | @requiresess |
| 85 | def cmd_logout(cmd, args): |
| 86 | global sess |
| 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: |
| 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) |
| 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(): |
| 126 | global sess, sessname |
| 127 | |
| 128 | opts, args = getopt.getopt(sys.argv[1:], "hs:") |
| 129 | for o, a in opts: |
| 130 | if o == "-h": |
| 131 | usage(sys.stdout) |
| 132 | sys.exit(0) |
| 133 | if o == "-s": |
| 134 | sessname = a |
| 135 | if len(args) < 1: |
| 136 | usage(sys.stderr) |
| 137 | sys.exit(1) |
| 138 | |
| 139 | cmd = args[0] |
| 140 | args = args[1:] |
| 141 | |
| 142 | sess = data.loadsess(sessname, None) |
| 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) |
| 148 | data.savesess(sess, sessname) |
| 149 | |
| 150 | try: |
| 151 | if __name__ == "__main__": |
| 152 | main() |
| 153 | except KeyboardInterrupt: |
| 154 | sys.exit(1) |