|
|
|
@ -77,43 +77,20 @@ class MailRoom(Bureau):
|
|
|
|
|
try:
|
|
|
|
|
self.imapserv.select_folder("INBOX")
|
|
|
|
|
except self.imapserv.AbortError as err:
|
|
|
|
|
print("imap connection error: ", err)
|
|
|
|
|
self.log.debug("reconnecting after imap connection error: ", err)
|
|
|
|
|
self.imapserv.logout()
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
"""
|
|
|
|
|
Takes a photograph using the document camera and sends it in an E-mail.
|
|
|
|
|
def _make_msg_object(self, imap_id, resp_obj):
|
|
|
|
|
"""
|
|
|
|
|
photo = self.send("PXphoto.")
|
|
|
|
|
|
|
|
|
|
@add_command("r", "Print full email")
|
|
|
|
|
def read(self, data):
|
|
|
|
|
util tidies up message headers deals with encoding
|
|
|
|
|
"""
|
|
|
|
|
Prints out the full detailed version of an email.
|
|
|
|
|
"""
|
|
|
|
|
shortcode, _ = data.split(".")
|
|
|
|
|
imap_id = self.get_imap_id(shortcode)
|
|
|
|
|
self._connect_imap()
|
|
|
|
|
resp = self.imapserv.fetch([imap_id],
|
|
|
|
|
['INTERNALDATE', 'RFC822'])
|
|
|
|
|
|
|
|
|
|
internaldate = resp[imap_id][b'INTERNALDATE']
|
|
|
|
|
internaldate = resp_obj[imap_id][b'INTERNALDATE']
|
|
|
|
|
|
|
|
|
|
msg_data = resp[imap_id][b'RFC822'].decode('utf-8')
|
|
|
|
|
msg_data = resp_obj[imap_id][b'RFC822'].decode('utf-8')
|
|
|
|
|
msg_obj = email.message_from_string(msg_data)
|
|
|
|
|
print("message fields:", msg_obj.keys())
|
|
|
|
|
|
|
|
|
|
# format and tidy header data
|
|
|
|
|
msg = Message()
|
|
|
|
@ -125,8 +102,7 @@ class MailRoom(Bureau):
|
|
|
|
|
else:
|
|
|
|
|
msg.cc = None
|
|
|
|
|
msg.date = internaldate
|
|
|
|
|
msg.shortcode = shortcode
|
|
|
|
|
msg.attachments = []
|
|
|
|
|
msg.attachments = [] # TODO: deal with attachments
|
|
|
|
|
|
|
|
|
|
# TODO: should use msg_obj.get_body and deal with HTML
|
|
|
|
|
msg.content = ""
|
|
|
|
@ -141,6 +117,54 @@ class MailRoom(Bureau):
|
|
|
|
|
msg.content = msg.content.replace(">", ">")
|
|
|
|
|
msg.content = msg.content.replace("\n", "<br />")
|
|
|
|
|
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
def _imap2shortcode(self, msgid):
|
|
|
|
|
"""
|
|
|
|
|
returns a short code for a given IMAP id (creating a new mapping if
|
|
|
|
|
needed)
|
|
|
|
|
"""
|
|
|
|
|
with self.dbenv.begin(db=self.postdb_rev) as txn:
|
|
|
|
|
shortcode = txn.get(msgid.encode())
|
|
|
|
|
if shortcode is not None:
|
|
|
|
|
return shortcode
|
|
|
|
|
else:
|
|
|
|
|
shortcode = ''.join(random.choice(string.ascii_letters +
|
|
|
|
|
string.digits) for _ in range(5))
|
|
|
|
|
txn.put(msgid.encode(), shortcode.encode())
|
|
|
|
|
with self.dbenv.begin(db=self.postdb) as txn:
|
|
|
|
|
txn.put(shortcode.encode(), msgid.encode())
|
|
|
|
|
return shortcode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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.
|
|
|
|
|
"""
|
|
|
|
|
photo = self.send("PXphoto.")
|
|
|
|
|
|
|
|
|
|
@add_command("r", "Print full email")
|
|
|
|
|
def read(self, data):
|
|
|
|
|
"""
|
|
|
|
|
Prints out the full detailed version of an email.
|
|
|
|
|
"""
|
|
|
|
|
shortcode, _ = data.split(".")
|
|
|
|
|
imap_id = self.get_imap_id(shortcode)
|
|
|
|
|
self._connect_imap()
|
|
|
|
|
resp = self.imapserv.fetch([imap_id],
|
|
|
|
|
['INTERNALDATE', 'RFC822'])
|
|
|
|
|
|
|
|
|
|
msg = self._make_msg_object(imap_id, resp)
|
|
|
|
|
|
|
|
|
|
# make action barcodes
|
|
|
|
|
barcode_png = os.path.join("/tmp", "POun." + msg.shortcode + ".png")
|
|
|
|
|
code128.image("POun." + shortcode).save(barcode_png)
|
|
|
|
@ -269,16 +293,13 @@ class MailRoom(Bureau):
|
|
|
|
|
msgs = []
|
|
|
|
|
|
|
|
|
|
for msgid, data in resp.items():
|
|
|
|
|
msg = {}
|
|
|
|
|
env = data[b"ENVELOPE"]
|
|
|
|
|
|
|
|
|
|
msg["msgid"] = str(msgid)
|
|
|
|
|
sender = env.from_[0]
|
|
|
|
|
msg["fromaddr"] = str(sender.mailbox) + "@" + str(sender.host)
|
|
|
|
|
msg["fromname"] = str(sender.name)
|
|
|
|
|
msg["date"] = str(data[b"INTERNALDATE"])
|
|
|
|
|
msg["size"] = str(data[b"RFC822.SIZE"])
|
|
|
|
|
msg["subject"] = str(env.subject)
|
|
|
|
|
msg = self._make_msg_object(msgid, data)
|
|
|
|
|
shortcode = self._imap2shortcode(msgid)
|
|
|
|
|
|
|
|
|
|
# make action barcodes
|
|
|
|
|
msg.d_bc = code128.svg("POd." + shortcode).encode()
|
|
|
|
|
msg.sp_bc = code128.svg("POsp." + shortcode).encode()
|
|
|
|
|
msg.r_bc = code128.svg("POr." + shortcode).encode()
|
|
|
|
|
|
|
|
|
|
msgs.append(msg)
|
|
|
|
|
|
|
|
|
|