import email.header import imapclient from bureau import Bureau, add_command, add_api class MailRoom(Bureau): """ The Mail Room handles (electronic) post for the other Bureaus of the Screenless Office. """ name = "Mail Room" prefix = "PO" version = 0 def __init__(self): Bureau.__init__(self) # TODO: multiple accounts / folders if "user" in self.config: 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.imapserv.login(self.login, self.password) self.imapserv.select_folder("INBOX") else: print("you need to configure an IMAP account!") print("add a [user] section to PO.ini with:") print(" login = mylogin") print(" password = mypassword") print(" host = my.imap.server.address.com") @add_command("fax", "Send a Document Camera Image via Email") def fax(self, data): """ Takes a photograph using the document camera and sends it in an E-mail. """ self.send("PXphoto.") @add_command("rd", "Print full email") def read(self, data): """ Prints out the full detailed version of an email. """ pass @add_command("d", "Delete email") def delete(self, data): """ Deletes an email and moves it to the trash folder. """ pass @add_command("s", "Mark as spam") def mark_spam(self, data): """ Flags an email as spam, mark as read and move it to the configured SPAM folder. """ pass @add_command("re", "Reply with scan") def reply_scan(self, data): """ Reply to the sender of a mail with the PDF currently queued in the document scanner. """ pass @add_api("unread", "Get unread mails") def unread(self): """ Polls the currently configured IMAP server and returns a dict containing unread emails. """ messages = self.imapserv.sort("ARRIVAL", ["UNSEEN"]) print("%d unread messages in INBOX" % len(messages)) return self.imapserv.fetch(messages, ['FLAGS', 'INTERNALDATE', 'ENVELOPE', 'RFC822.SIZE']) def main(): mr = MailRoom() mr.run() if __name__ == "__main__": main()