add delete and mark-as-spam commands to mailroom. started read-mail.

workspace
Brendan Howell 8 years ago
parent d881188296
commit f3f8f7b55c

@ -312,7 +312,9 @@ class Bureau(object):
if (dot < len(msg) - 1) and (dot > 0): if (dot < len(msg) - 1) and (dot > 0):
print("msg length:", len(msg)) print("msg length:", len(msg))
print("dot at", dot) print("dot at", dot)
# TODO: maybe trim off the trailing "." for convenience
data = msg[dot + 1:] data = msg[dot + 1:]
# data = str(data) # force to be a string
else: else:
data = None data = None
print("data: " + str(data)) print("data: " + str(data))

@ -1,4 +1,4 @@
from . import mailroom from . import mailroom
def main(): def main():
ihr.main() mailroom.main()

@ -1,10 +1,18 @@
import email.header import email.header
import imaplib
import imapclient import imapclient
from bureau import Bureau, add_command, add_api 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): class MailRoom(Bureau):
""" """
The Mail Room handles (electronic) post for the other Bureaus of 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.login = self.config["user"]["login"]
self.password = self.config["user"]["password"] self.password = self.config["user"]["password"]
self.host = self.config["user"]["host"] 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.login(self.login, self.password)
self.imapserv.select_folder("INBOX") self.imapserv.select_folder("INBOX")
else: else:
@ -32,6 +49,24 @@ class MailRoom(Bureau):
print(" password = mypassword") print(" password = mypassword")
print(" host = my.imap.server.address.com") 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") @add_command("fax", "Send a Document Camera Image via Email")
def fax(self, data): def fax(self, data):
""" """
@ -39,31 +74,47 @@ class MailRoom(Bureau):
""" """
photo = self.send("PXphoto.") photo = self.send("PXphoto.")
@add_command("rd", "Print full email") @add_command("r", "Print full email")
def read(self, data): def read(self, data):
""" """
Prints out the full detailed version of an email. 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") @add_command("d", "Delete email")
def delete(self, data): def delete(self, data):
""" """
Deletes an email and moves it to the trash folder. Deletes an email and moves it to the trash folder.
""" """
self.imapserv.delete_messages((data)) shortcode, _ = data.split(".")
pass imap_id = self.get_imap_id(shortcode)
self._connect_imap()
@add_command("s", "Mark as spam") 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): def mark_spam(self, data):
""" """
Flags an email as spam, mark as read and move it to the configured SPAM Flags an email as spam, mark as read and move it to the configured SPAM
folder. folder.
""" """
self.imapserv.copy((data), self.spamfolder) shortcode, _ = data.split(".")
# TODO: mark as read? with self.imapserv.add_flags? imap_id = self.get_imap_id(shortcode)
self.delete(data) self._connect_imap()
pass 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") @add_command("re", "Reply with scan")
def reply_scan(self, data): def reply_scan(self, data):

Loading…
Cancel
Save