+ignore = {'user': 1, 'addr': 1}
+def show(db, a):
+ keys = a.keys()
+ keys.sort()
+ for k in keys:
+ if k in ignore:
+ continue
+ for v in a[k]:
+ print '\t%s: %s' % (k, v)
+ expwhen = db.expiry(a.id)
+ if expwhen:
+ print '\texpires: %s' % expwhen
+ else:
+ print '\tno-expiry'
+
+def cmd_info(argv):
+ try:
+ opts, argv = getopt(argv, '', [])
+ except GetoptError:
+ return 1
+ if len(argv) != 1:
+ return 1
+ local = argv[0]
+ db = CMDB(dbfile)
+ try:
+ id = getid(local)
+ a = AttrMultiMap(db, id)
+ if user is not None and user != a.get('user', [None])[0]:
+ raise Reject, 'not your token'
+ if 'addr' not in a:
+ die('unknown token (expired?)')
+ print 'addr: %s' % a['addr'][0]
+ show(db, a)
+ except Reject, msg:
+ die('invalid token')
+commands['info'] = \
+ (cmd_info, 'LOCAL', """
+Exaimne the address token LOCAL, and print information about it to standard
+output.""")
+
+def cmd_revoke(argv):
+ try:
+ opts, argv = getopt(argv, '', [])
+ except GetoptError:
+ return 1
+ if len(argv) != 1:
+ return 1
+ local = argv[0]
+ db = CMDB(dbfile)
+ try:
+ id = getid(local)
+ a = AttrMultiMap(db, id)
+ if user is not None and user != a.get('user', [None])[0]:
+ raise Reject, 'not your token'
+ if 'addr' not in a:
+ die('unknown token (expired?)')
+ a.clear()
+ db.cleanup()
+ db.commit()
+ except Reject, msg:
+ die('invalid token')
+commands['revoke'] = \
+ (cmd_revoke, 'LOCAL', """
+Revoke the token LOCAL.""")
+
+def cmd_list(argv):
+ try:
+ opts, argv = getopt(argv, '', [])
+ except GetoptError:
+ return 1
+ if argv:
+ return 1
+ c = Crypto(C.KeyFile(keyfile, C.KOPEN_READ)[tag])
+ db = CMDB(dbfile)
+ if not user:
+ gen = db.select('SELECT DISTINCT id FROM attrset')
+ else:
+ gen = db.select('''SELECT DISTINCT attrset.id
+ FROM attrset, attr ON attrset.attr = attr.id
+ WHERE attr.key = 'user' AND attr.value = ?''',
+ [user])
+ for id, in gen:
+ a = AttrMultiMap(db, id)
+ print '%s %s%s' % \
+ (token(c, id),
+ a.get('addr', '<no-address>')[0],
+ (not user and ' [%s]' % a.get('user', ['<no-user>'])[0] or ''))
+ show(db, a)
+commands['list'] = \
+ (cmd_list, '', """
+List the user's tokens and information about them.""")
+