|
|
|
@ -3,9 +3,11 @@ import email.mime.application
|
|
|
|
|
import email.mime.multipart
|
|
|
|
|
import email.mime.text
|
|
|
|
|
from email.header import decode_header, make_header
|
|
|
|
|
import imaplib
|
|
|
|
|
#import imaplib
|
|
|
|
|
import os.path
|
|
|
|
|
import random
|
|
|
|
|
import smtplib
|
|
|
|
|
import string
|
|
|
|
|
|
|
|
|
|
import code128
|
|
|
|
|
import imapclient
|
|
|
|
@ -87,9 +89,9 @@ class MailRoom(Bureau):
|
|
|
|
|
"""
|
|
|
|
|
util tidies up message headers deals with encoding
|
|
|
|
|
"""
|
|
|
|
|
internaldate = resp_obj[imap_id][b'INTERNALDATE']
|
|
|
|
|
internaldate = resp_obj[b'INTERNALDATE']
|
|
|
|
|
|
|
|
|
|
msg_data = resp_obj[imap_id][b'RFC822'].decode('utf-8')
|
|
|
|
|
msg_data = resp_obj[b'RFC822'].decode('utf-8')
|
|
|
|
|
msg_obj = email.message_from_string(msg_data)
|
|
|
|
|
|
|
|
|
|
# format and tidy header data
|
|
|
|
@ -112,7 +114,9 @@ class MailRoom(Bureau):
|
|
|
|
|
# TODO: save interesting attachments to files
|
|
|
|
|
# should clean these up on delete
|
|
|
|
|
if part.get_content_type() == "text/plain":
|
|
|
|
|
msg.content = part.get_payload(decode=True).decode("utf-8")
|
|
|
|
|
msg.content = part.get_payload(decode=True)
|
|
|
|
|
print("msg content:", msg.content)
|
|
|
|
|
msg.content = msg.content.decode("utf-8")
|
|
|
|
|
msg.content = msg.content.replace("<", "<")
|
|
|
|
|
msg.content = msg.content.replace(">", ">")
|
|
|
|
|
msg.content = msg.content.replace("\n", "<br />")
|
|
|
|
@ -132,18 +136,19 @@ class MailRoom(Bureau):
|
|
|
|
|
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())
|
|
|
|
|
msgid = str(msgid).encode()
|
|
|
|
|
with self.dbenv.begin(db=self.postdb_rev, write=True) as txn:
|
|
|
|
|
shortcode = txn.get(msgid)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
string.digits) for _ in range(5)).encode()
|
|
|
|
|
print("saving msgid", msgid, shortcode)
|
|
|
|
|
txn.put(msgid, shortcode)
|
|
|
|
|
with self.dbenv.begin(db=self.postdb, write=True) as txn:
|
|
|
|
|
txn.put(shortcode, msgid)
|
|
|
|
|
return shortcode.decode()
|
|
|
|
|
|
|
|
|
|
@add_command("fax", "Send a Document Camera Image via Email")
|
|
|
|
|
def fax(self, data):
|
|
|
|
@ -293,15 +298,25 @@ class MailRoom(Bureau):
|
|
|
|
|
msgs = []
|
|
|
|
|
|
|
|
|
|
for msgid, data in resp.items():
|
|
|
|
|
msg = self._make_msg_object(msgid, data)
|
|
|
|
|
shortcode = self._imap2shortcode(msgid)
|
|
|
|
|
#msg = self._make_msg_object(msgid, data)
|
|
|
|
|
msg = Message()
|
|
|
|
|
shortcode = self._imap2shortcode(msgid).decode("utf-8")
|
|
|
|
|
envelope = data[b"ENVELOPE"]
|
|
|
|
|
|
|
|
|
|
msg.msgid = str(msgid)
|
|
|
|
|
sender = envelope.from_[0]
|
|
|
|
|
msg.fromaddr = sender.mailbox + b"@" + sender.host
|
|
|
|
|
msg.fromaddr = clean_header(msg.fromaddr.decode("utf-8"))
|
|
|
|
|
msg.fromname = clean_header(sender.name.decode("utf-8"))
|
|
|
|
|
msg.date = data[b"INTERNALDATE"].strftime("%d. %B %Y %I:%M%p")
|
|
|
|
|
msg.subject = clean_header(envelope.subject.decode("utf-8"))
|
|
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
msg.d_bc = code128.svg("POd." + shortcode)
|
|
|
|
|
msg.sp_bc = code128.svg("POsp." + shortcode)
|
|
|
|
|
msg.r_bc = code128.svg("POr." + shortcode)
|
|
|
|
|
|
|
|
|
|
msgs.append(msg)
|
|
|
|
|
msgs.append(msg.__dict__)
|
|
|
|
|
|
|
|
|
|
return msgs
|
|
|
|
|
|
|
|
|
|