Move login/password prompt/read code to SecureInbox.
[easyotp.git] / cli.py
blob2f019e0bd992b88892cdb56860e9e7da857d46ec
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
11 import os
13 def help():
14 print """commands:
15 h Help
16 l List messages
17 q Quit
18 r# Read message number #
19 """
21 def main():
22 ms = SecureInbox.SecureInbox()
24 msgs = []
26 while True:
27 try:
28 line = raw_input(">> ")
29 except EOFError:
30 break
31 if len(line.strip()) == 0:
32 continue
34 if line[0] == "h":
35 help()
36 elif line[0] == "l":
37 print "Fetching messages..."
38 i = 0
39 for m in ms:
40 print i, m["sender"], m["subject"]
41 i += 1
42 elif line[0] == "q":
43 raise SystemExit
44 elif line[0] == "w":
45 to = raw_input("To: ")
46 subject = raw_input("Subject: ")
47 print "Enter body, ending with '.' on a line by itself."
48 body = ""
49 while True:
50 line = raw_input()
51 if line == ".":
52 break
53 body += line + "\r\n"
54 print "Sending %s bytes" % (len(body,))
55 enc_body = cotp.encode(body)
56 print ms.send(to, subject, enc_body)
57 elif line[0] == "r":
58 try:
59 num = int(line[1:])
60 except:
61 print "Usage: r#"
62 continue
63 try:
64 msg = ms[num]
65 except:
66 print "Bad message number: %s" % (num,)
67 print "Are you sure it is listed with 'l'?"
68 print "Message count: %s" % (len(msgs),)
69 continue
71 print "From: %s" % (msg["sender"],)
72 print "Subject: %s" % (msg["subject"],)
73 print
74 try:
75 print cotp.decode(msg["body"])
76 except:
77 print "Exception when decoding", sys.exc_info()
78 else:
79 print "Unknown command, type h for help"
81 if __name__ == "__main__":
82 main()