Add SMTP support to SecureInbox.
[easyotp.git] / cli.py
blobc273b3ee1ec51e71adc3a420e55ac9b5b26cd878
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 i = 0
37 for m in ms:
38 print i, m["sender"], m["subject"]
39 i += 1
40 elif line[0] == "q":
41 raise SystemExit
42 elif line[0] == "w":
43 to = raw_input("To: ")
44 subject = raw_input("Subject: ")
45 print "Enter body, ending with '.' on a line by itself."
46 body = ""
47 while True:
48 line = raw_input()
49 if line == ".":
50 break
51 body += line + "\r\n"
52 print "Sending %s bytes" % (len(body,))
53 print ms.send(to, subject, body)
54 elif line[0] == "r":
55 num = int(line[1:])
56 try:
57 msg = ms[num]
58 except:
59 print "Bad message: %s" % (msg,)
60 print "Are you sure it is listed with 'l'?"
61 print "Message count: %s" % (len(msgs),)
62 continue
64 print "From: %s" % (msg["sender"],)
65 print "Subject: %s" % (msg["subject"],)
66 print
67 try:
68 print cotp.decode(msg["body"])
69 except:
70 print "Exception when decoding", sys.exc_info()
71 else:
72 print "Unknown command, type h for help"
74 if __name__ == "__main__":
75 main()