Handle authentication errors better.
[fulbank.git] / netbank
diff --git a/netbank b/netbank
index d43894a..a1d3566 100755 (executable)
--- a/netbank
+++ b/netbank
@@ -1,26 +1,46 @@
 #!/usr/bin/python3
 
-import sys, os, getopt, pwd
+import sys, os, getopt, pwd, operator
 from fulbank import auth
 
 sesstype = None
 sess = None
 
-def find(seq, *, item=None, match=None, key=None, default=LookupError):
+def pfxmatch(pfx, item):
+    return str(item)[:len(pfx)] == pfx
+
+class ambiguous(LookupError):
+    def __init__(self, a, b):
+        super().__init__("ambigous match: %s and %s" % (a, b))
+        self.a = a
+        self.b = b
+
+def find(seq, *, item=None, test=None, match=None, key=None, default=LookupError):
     if key is None:
         key = lambda o: o
     if match is None and item is not None:
-        match = lambda o: o == item
+        match = lambda o: test(item, o)
+    if test is None:
+        test = operator.eq
+    found = None
     for thing in seq:
         if match(key(thing)):
-            return thing
+            if found is None:
+                found = thing
+            else:
+                if default is LookupError:
+                    raise ambiguous(key(found), key(thing))
+                else:
+                    return default
+    if found is not None:
+        return found
     if default is LookupError:
         raise LookupError()
     else:
         return default
 
 def usage(out):
-    out.write("usage: netbank [-h] BANK-ID COMMAND [ARGS...]")
+    out.write("usage: netbank [-h] BANK-ID COMMAND [ARGS...]\n")
 
 def requiresess(fn):
     def wrap(cmd, args):
@@ -43,7 +63,11 @@ def cmd_login(cmd, args):
             sys.stderr.write("usage: login bankid USER-ID\n")
             sys.exit(1)
         with auth.ttyconv() as conv:
-            sess.auth_bankid(args[1], conv)
+            try:
+                sess.auth_bankid(args[1], conv)
+            except auth.autherror as err:
+                sys.stderr.write("netbank: authentication failed: %s\n" % err)
+                sys.exit(1)
     else:
         sys.stderr.write("netbank: %s: unknown authentication type\n" % (args[0]))
         sys.exit(1)
@@ -51,6 +75,7 @@ commands["login"] = cmd_login
 
 @requiresess
 def cmd_logout(cmd, args):
+    global sess
     if sess is not None:
         sess.close()
         sess = None
@@ -78,7 +103,10 @@ def cmd_lstxn(cmd, args):
         sys.stderr.write("usage: lstxn [-n NUM] ACCOUNT\n")
         sys.exit(1)
     try:
-        acct = find(sess.accounts, item=args[0], key=lambda acct: acct.number)
+        acct = find(sess.accounts, item=args[0], key=lambda acct: acct.number, test=pfxmatch)
+    except ambiguous as exc:
+        sys.stderr.write("netbank: %s: ambiguous match between %s and %s\n" % (args[0], exc.a, exc.b))
+        sys.exit(1)
     except LookupError:
         sys.stderr.write("netbank: %s: no such account\n" % (args[0]))
         sys.exit(1)