Add Python interface to cotp.
[easyotp.git] / cli.py
bloba6932ad64e64f8cb3f9c11cbbff254cf03644e59
1 #!/bin/env python
2 # Created:20080603
3 # By Jeff Connelly
5 # CLI for EasyOTP
7 import SecureInbox
8 import cotp
9 import getpass
10 import sys
12 def help():
13 print """commands:
14 h Help
15 l List messages
16 q Quit
17 r# Read message number #
18 """
20 def main():
21 username = raw_input("Username: ")
22 ms = SecureInbox.SecureInbox(username, getpass.getpass())
23 msgs = []
24 while True:
25 try:
26 line = raw_input(">> ")
27 except EOFError:
28 break
29 if len(line.strip()) == 0:
30 continue
32 if line[0] == "h":
33 help()
34 elif line[0] == "l":
35 print "Fetching messages..."
36 msgs = ms.get_messages()
37 i = 0
38 for m in msgs:
39 print i, m["sender"], m["subject"]
40 i += 1
41 elif line[0] == "q":
42 raise SystemExit
43 elif line[0] == "r":
44 num = int(line[1:])
45 try:
46 msg = msgs[num]
47 except:
48 print "Bad message: %s" % (msg,)
49 print "Are you sure it is listed with 'l'?"
50 print "Message count: %s" % (len(msgs),)
51 continue
53 print "From: %s" % (msg["sender"],)
54 print "Subject: %s" % (msg["subject"],)
55 print
56 try:
57 print cotp.decode(msg["body"])
58 except:
59 print "Exception when decoding", sys.exc_info()
60 else:
61 print "Unknown command, type h for help"
63 if __name__ == "__main__":
64 main()