From 287c21c20cbcab073d263ae50a6342dec793d0a6 Mon Sep 17 00:00:00 2001 From: Jeff Connelly Date: Tue, 3 Jun 2008 16:11:08 -0700 Subject: [PATCH] Add beginnings of a Python user interface: MailServers: get messages from server using IMAP. EasyOTP: calls cotp to decode and encode Split from ui.py. --- .gitignore | 3 +++ EasyOTP.py | 31 ++++++++++++++++++++++++ MailServers.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ push | 2 ++ ui.py | 74 ---------------------------------------------------------- 5 files changed, 108 insertions(+), 74 deletions(-) create mode 100644 EasyOTP.py create mode 100644 MailServers.py create mode 100755 push delete mode 100644 ui.py diff --git a/.gitignore b/.gitignore index 32daf10..c62d71d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ try *.swp *.o *.pyc +otp.conf +cotp + diff --git a/EasyOTP.py b/EasyOTP.py new file mode 100644 index 0000000..aa086de --- /dev/null +++ b/EasyOTP.py @@ -0,0 +1,31 @@ +#!/bin/env python +import popen2 + +def decode(ct): + o, i, e = popen2.popen3("./cotp -d") + i.write(ct) + i.close() + pt = o.read() + errors = e.read() + if errors: + raise errors + return pt + +# TODO: support padname +def encode(pt): + o, i, e = popen2.popen3("./cotp -e") + i.write(pt) + i.close() + ct = o.read() + errors = e.read() + if errors: + raise errors + return ct + +if __name__ == "__main__": + ct = encode("hello world") + print ct + pt = decode(ct) + print ct + assert ct == "hello world", "decode failed to match" + diff --git a/MailServers.py b/MailServers.py new file mode 100644 index 0000000..9d27868 --- /dev/null +++ b/MailServers.py @@ -0,0 +1,72 @@ +#!/bin/env python +# Created:20080603 +# By Jeff Connelly +# +# Communicate with mail servers +# +import imaplib +import getpass +import email + +class MailServers(object): + def __init__(self, username, password): + self.mail_in = imaplib.IMAP4_SSL("imap.gmail.com", 993) + + try: + typ, data = self.mail_in.login(username, password) + except imaplib.error, e: + raise "Login failure: %s" % (e,) + else: + assert typ == "OK", "imap login returned: %s %s" % (status, message) + print "Logged in:", typ, data + + # Always have "Secure" mailbox selected + typ, num_msgs = self.mail_in.select(mailbox="Secure") + if typ != "OK": + raise ("imap select failure: %s %s" % (typ, num_msgs) + + "The 'Secure' tag doesn't exist. Tag some of your EMOTP messages" + + "using a new label, named 'Secure'") + + def get_messages(self): + msgs = [] + + try: + typ, all_msgs_string = self.mail_in.search(None, 'ALL') + except imaplib.error, e: + raise "imap search failed: %s" % (e,) + + all_msgs = all_msgs_string[0].split() + for num in all_msgs: + typ, body = self.mail_in.fetch(num, "(BODY[])") + msg = email.message_from_string(body[0][1]) + + body = str(msg.get_payload()) + subject = msg.get("Subject") + sender = msg.get("From") + id = msg.get("Message-ID") + + #print 'Message %s\n%s\n' % (num, data[0][1]) + if "--EMOTP_BEGIN--" not in body: + continue + + msgs.append({"body": body, + "subject": subject, + "sender": sender, + "id": id, + "num": num}) + + return msgs + + def __del__(self): + self.mail_in.close() + self.mail_in.logout() + +def main(): + ms = MailServers("shellreef", getpass.getpass()) + + for m in ms.get_messages(): + print m["sender"], m["subject"] + +if __name__ == "__main__": + main() + diff --git a/push b/push new file mode 100755 index 0000000..3f1d197 --- /dev/null +++ b/push @@ -0,0 +1,2 @@ +#!/bin/sh +git push git+ssh://shellreef@repo.or.cz/srv/git/easyotp.git master diff --git a/ui.py b/ui.py deleted file mode 100644 index 3cb857b..0000000 --- a/ui.py +++ /dev/null @@ -1,74 +0,0 @@ -#!/bin/env python - -import imaplib -import getpass -import email -import popen2 - -def setup_imap(): - mail_in = imaplib.IMAP4_SSL("imap.gmail.com", 993) - - try: - typ, data = mail_in.login("shellreef", getpass.getpass()) - except imaplib.error, e: - print "Login failure: %s" % (e,) - raise SystemExit - else: - assert typ == "OK", "imap login returned: %s %s" % (status, message) - print "Logged in:", typ, data - - typ, num_msgs = mail_in.select(mailbox="Secure") - if typ != "OK": - print "imap select failure: %s %s" % (typ, num_msgs) - print "The 'Secure' tag doesn't exist. Tag some of your EMOTP messages" - print "using a new label, named 'Secure'" - raise SystemExit - - try: - typ, all_msgs_string = mail_in.search(None, 'ALL') - except imaplib.error, e: - print "imap search failed: %s" % (e,) - raise SystemExit - - all_msgs = all_msgs_string[0].split() - for num in all_msgs: - typ, body = mail_in.fetch(num, "(BODY[])") - msg = email.message_from_string(body[0][1]) - - body = str(msg.get_payload()) - subject = msg.get("Subject") - sender = msg.get("From") - id = msg.get("Message-ID") - - #print 'Message %s\n%s\n' % (num, data[0][1]) - if "--EMOTP_BEGIN--" not in body: - continue - print "%s %s" % (num, subject) - print body - - print otp_decode(body) - - mail_in.close() - mail_in.logout() - -def otp_decode(ct): - o, i, e = popen2.popen3("./cotp -d") - i.write(ct) - i.close() - pt = o.read() - errors = e.read() - if errors: - raise errors - return pt - -# TODO: support padname -def otp_encode(pt): - o, i, e = popen2.popen3("./cotp -e") - i.write(pt) - i.close() - ct = o.read() - errors = e.read() - if errors: - raise errors - return ct - -- 2.11.4.GIT