From f3f8f7b55ce2884dde06cf83096e2d2608aab89d Mon Sep 17 00:00:00 2001 From: Brendan Howell Date: Fri, 20 Jan 2017 14:26:29 +0100 Subject: [PATCH] add delete and mark-as-spam commands to mailroom. started read-mail. --- screenless/bureau/bureau.py | 2 + screenless/bureau/mailroom/__init__.py | 2 +- screenless/bureau/mailroom/mailroom.py | 73 ++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/screenless/bureau/bureau.py b/screenless/bureau/bureau.py index 306802c..dc50c5a 100644 --- a/screenless/bureau/bureau.py +++ b/screenless/bureau/bureau.py @@ -312,7 +312,9 @@ class Bureau(object): if (dot < len(msg) - 1) and (dot > 0): print("msg length:", len(msg)) print("dot at", dot) + # TODO: maybe trim off the trailing "." for convenience data = msg[dot + 1:] + # data = str(data) # force to be a string else: data = None print("data: " + str(data)) diff --git a/screenless/bureau/mailroom/__init__.py b/screenless/bureau/mailroom/__init__.py index 76788bf..5192e4a 100644 --- a/screenless/bureau/mailroom/__init__.py +++ b/screenless/bureau/mailroom/__init__.py @@ -1,4 +1,4 @@ from . import mailroom def main(): - ihr.main() + mailroom.main() diff --git a/screenless/bureau/mailroom/mailroom.py b/screenless/bureau/mailroom/mailroom.py index 4c504ed..790a338 100644 --- a/screenless/bureau/mailroom/mailroom.py +++ b/screenless/bureau/mailroom/mailroom.py @@ -1,10 +1,18 @@ import email.header +import imaplib import imapclient from bureau import Bureau, add_command, add_api +class Message(object): + """ + This is just a convenience class for holding email message data. + It could be fancier some day but for now this seems fine. + """ + pass + class MailRoom(Bureau): """ The Mail Room handles (electronic) post for the other Bureaus of @@ -22,7 +30,16 @@ class MailRoom(Bureau): self.login = self.config["user"]["login"] self.password = self.config["user"]["password"] self.host = self.config["user"]["host"] - self.imapserv = imapclient.IMAPClient(self.host, use_uid=True, ssl=True) + self.spamfolder = self.config["user"]["spamfolder"] + self.trashfolder = self.config["user"]["trashfolder"] + + self.imap_ssl = self.config["user"]["ssl"] + if self.imap_ssl.lower() == "true": + self.imap_ssl = True + else: + self.imap_ssl = False + + self.imapserv = imapclient.IMAPClient(self.host, use_uid=True, ssl=self.imap_ssl) self.imapserv.login(self.login, self.password) self.imapserv.select_folder("INBOX") else: @@ -32,6 +49,24 @@ class MailRoom(Bureau): print(" password = mypassword") print(" host = my.imap.server.address.com") + # setup db's for mapping short codes to IMAP msg ids (and reversed) + self.postdb = self.dbenv.open_db(b"postdb") + self.postdb_rev = self.dbenv.open_db(b"postdb_rev") + + def _connect_imap(self): + try: + self.imapserv.select_folder("INBOX") + except imaplib.error: + self.imapserv.login(self.login, self.password) + + def get_imap_id(self, msgid): + """ + take short code and look up the IMAP message id + """ + with self.dbenv.begin(db=self.postdb) as txn: + imap_id = txn.get(msgid.encode()) + return int(imap_id) + @add_command("fax", "Send a Document Camera Image via Email") def fax(self, data): """ @@ -39,31 +74,47 @@ class MailRoom(Bureau): """ photo = self.send("PXphoto.") - @add_command("rd", "Print full email") + @add_command("r", "Print full email") def read(self, data): """ Prints out the full detailed version of an email. """ - pass + shortcode, _ = data.split(".") + imap_id = self.get_imap_id(shortcode) + self._connect_imap() + resp = self.imapserv.fetch([imap_id], ['ENVELOPE', 'INTERNALDATE']) + msg = Message() + # msg.to = resp[imap_id][] + self.imapserv.add_flags(imap_id, [imapclient.SEEN]) + + # self.print_full("email.html", msg=msg, shortcode=shortcode) @add_command("d", "Delete email") def delete(self, data): """ Deletes an email and moves it to the trash folder. """ - self.imapserv.delete_messages((data)) - pass - - @add_command("s", "Mark as spam") + shortcode, _ = data.split(".") + imap_id = self.get_imap_id(shortcode) + self._connect_imap() + self.imapserv.add_flags(imap_id, [imapclient.SEEN]) + self.imapserv.copy((imap_id), self.trashfolder) + self.imapserv.delete_messages((imap_id)) + self.imapserv.expunge() + + @add_command("sp", "Mark as spam") def mark_spam(self, data): """ Flags an email as spam, mark as read and move it to the configured SPAM folder. """ - self.imapserv.copy((data), self.spamfolder) - # TODO: mark as read? with self.imapserv.add_flags? - self.delete(data) - pass + shortcode, _ = data.split(".") + imap_id = self.get_imap_id(shortcode) + self._connect_imap() + self.imapserv.add_flags(imap_id, [imapclient.SEEN]) + self.imapserv.copy((imap_id), self.spamfolder) + self.imapserv.delete_messages((imap_id)) + self.imapserv.expunge() @add_command("re", "Reply with scan") def reply_scan(self, data):