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):
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))

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

@ -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):

Loading…
Cancel
Save