Integrate pad rewriting into cli.
[easyotp.git] / cli.py
blobb3b486d3f46d26079f6ef8554b97c8eeacd4091e
1 #!/bin/env python
2 # Created:20080603
3 # By Jeff Connelly
5 # CLI for EasyOTP
7 import SecureMail
8 import cotp
9 import getpass
10 import sys
11 import os
12 import time
14 ms = None
16 def help():
17 print """commands:
18 h Help
19 l List messages
20 q Quit
21 r# Read message number #
22 w Write a new message
23 x# Replace what an old message decrypts to
24 """
26 def read_message():
27 subject = raw_input("Subject: ")
28 body = multiline_input("Enter body, ending with '.' on a line by itself.")
30 return subject, body
32 def multiline_input(prompt=None):
33 """Read possibly multiple lines of input, terminated by '.'."""
34 if prompt is not None:
35 print prompt
36 inp = ""
37 while True:
38 line = raw_input()
39 if line == ".":
40 break
41 inp += line + "\r\n"
42 return inp
44 def main():
45 global ms
46 ms = SecureMail.SecureMail()
47 msgs = []
49 while True:
50 try:
51 line = raw_input(">> ")
52 except EOFError:
53 break
54 if len(line.strip()) == 0:
55 continue
57 if line[0] == "h":
58 help()
59 elif line[0] == "l":
60 print "Fetching messages..."
61 i = 0
62 for m in ms:
63 print i, m["sender"], m["subject"]
64 i += 1
65 elif line[0] == "q":
66 raise SystemExit
67 elif line[0] == "w":
68 to = raw_input("To: ")
70 subject, body = read_message()
71 print ms.send(to, subject, body)
72 elif line[0] == "r":
73 try:
74 num = int(line[1:])
75 except:
76 print "Usage: r#"
77 continue
78 try:
79 msg = ms[num]
80 except:
81 print "Bad message number: %s" % (num,)
82 print "Are you sure it is listed with 'l'?"
83 print "Message count: %s" % (len(msgs),)
84 continue
86 print "From: %s" % (msg["sender"],)
87 print "Subject: %s" % (msg["subject"],)
88 print msg["body"]
89 elif line[0] == "x":
90 try:
91 num = int(line[1:])
92 except:
93 print "Usage: x#"
94 continue
95 print "After-The-Fact Message Replacement"
96 subject, body = read_message()
97 print ms.replace(num, subject, body)
98 else:
99 print "Unknown command, type h for help"
101 if __name__ == "__main__":
102 main()