Move message decryption to SecureMail instead of cli, and add subject encryption...
[easyotp.git] / cli.py
blob2e76eef6f2be4ba74365a1236a6fd97468fced7a
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 threading
13 import time
14 import Queue
16 FILL_INTERVAL = None
17 ms = None
18 sendq = Queue.Queue()
20 def help():
21 print """commands:
22 h Help
23 l List messages
24 q Quit
25 r# Read message number #
26 """
28 # Problem: computer needs to be online to fill channel!
29 class FillingThread(threading.Thread):
30 def run(self):
31 global sendq
33 if FILL_INTERVAL is None:
34 print "Channel filling disabled - messages will send immediately"
35 return
37 while True:
38 try:
39 item = sendq.get_nowait()
40 except Queue.Empty:
41 print "Sending filler message"
42 enc_body = cotp.encode(body)
43 # TODO: subject!!
44 print ms.send(to, subject, enc_body)
45 else:
46 print "Sending queued message"
47 to, subject, body = item
48 enc_body = cotp.encode(body)
49 print ms.send(to, subject, enc_body)
51 time.sleep(FILL_INTERVAL)
53 def main():
54 global ms
55 ms = SecureMail.SecureMail()
56 msgs = []
58 FillingThread().start()
60 while True:
61 try:
62 line = raw_input(">> ")
63 except EOFError:
64 break
65 if len(line.strip()) == 0:
66 continue
68 if line[0] == "h":
69 help()
70 elif line[0] == "l":
71 print "Fetching messages..."
72 i = 0
73 for m in ms:
74 print i, m["sender"], m["subject"]
75 i += 1
76 elif line[0] == "q":
77 raise SystemExit
78 elif line[0] == "w":
79 to = raw_input("To: ")
80 subject = raw_input("Subject: ")
81 print "Enter body, ending with '.' on a line by itself."
82 body = ""
83 while True:
84 line = raw_input()
85 if line == ".":
86 break
87 body += line + "\r\n"
89 if FILL_INTERVAL is None:
90 print "Sending %s bytes now" % (len(body,))
91 print ms.send(to, subject, body)
92 else:
93 sendq.put((to, subject, body))
94 print "Enqueued to send at next interval, pending: %s" % (sendq.qsize(),)
96 elif line[0] == "r":
97 try:
98 num = int(line[1:])
99 except:
100 print "Usage: r#"
101 continue
102 try:
103 msg = ms[num]
104 except:
105 print "Bad message number: %s" % (num,)
106 print "Are you sure it is listed with 'l'?"
107 print "Message count: %s" % (len(msgs),)
108 continue
110 print "From: %s" % (msg["sender"],)
111 print "Subject: %s" % (msg["subject"],)
112 print msg["body"]
113 else:
114 print "Unknown command, type h for help"
116 if __name__ == "__main__":
117 main()