|
|
@ -77,43 +77,20 @@ class MailRoom(Bureau):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.imapserv.select_folder("INBOX")
|
|
|
|
self.imapserv.select_folder("INBOX")
|
|
|
|
except self.imapserv.AbortError as err:
|
|
|
|
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.logout()
|
|
|
|
self.imapserv = imapclient.IMAPClient(self.host, use_uid=True, ssl=self.imap_ssl)
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
def get_imap_id(self, msgid):
|
|
|
|
def _make_msg_object(self, imap_id, resp_obj):
|
|
|
|
"""
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
photo = self.send("PXphoto.")
|
|
|
|
util tidies up message headers deals with encoding
|
|
|
|
|
|
|
|
|
|
|
|
@add_command("r", "Print full email")
|
|
|
|
|
|
|
|
def read(self, data):
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Prints out the full detailed version of an email.
|
|
|
|
internaldate = resp_obj[imap_id][b'INTERNALDATE']
|
|
|
|
"""
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
msg_obj = email.message_from_string(msg_data)
|
|
|
|
print("message fields:", msg_obj.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# format and tidy header data
|
|
|
|
# format and tidy header data
|
|
|
|
msg = Message()
|
|
|
|
msg = Message()
|
|
|
@ -125,8 +102,7 @@ class MailRoom(Bureau):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
msg.cc = None
|
|
|
|
msg.cc = None
|
|
|
|
msg.date = internaldate
|
|
|
|
msg.date = internaldate
|
|
|
|
msg.shortcode = shortcode
|
|
|
|
msg.attachments = [] # TODO: deal with attachments
|
|
|
|
msg.attachments = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: should use msg_obj.get_body and deal with HTML
|
|
|
|
# TODO: should use msg_obj.get_body and deal with HTML
|
|
|
|
msg.content = ""
|
|
|
|
msg.content = ""
|
|
|
@ -141,6 +117,54 @@ class MailRoom(Bureau):
|
|
|
|
msg.content = msg.content.replace(">", ">")
|
|
|
|
msg.content = msg.content.replace(">", ">")
|
|
|
|
msg.content = msg.content.replace("\n", "<br />")
|
|
|
|
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
|
|
|
|
# make action barcodes
|
|
|
|
barcode_png = os.path.join("/tmp", "POun." + msg.shortcode + ".png")
|
|
|
|
barcode_png = os.path.join("/tmp", "POun." + msg.shortcode + ".png")
|
|
|
|
code128.image("POun." + shortcode).save(barcode_png)
|
|
|
|
code128.image("POun." + shortcode).save(barcode_png)
|
|
|
@ -269,16 +293,13 @@ class MailRoom(Bureau):
|
|
|
|
msgs = []
|
|
|
|
msgs = []
|
|
|
|
|
|
|
|
|
|
|
|
for msgid, data in resp.items():
|
|
|
|
for msgid, data in resp.items():
|
|
|
|
msg = {}
|
|
|
|
msg = self._make_msg_object(msgid, data)
|
|
|
|
env = data[b"ENVELOPE"]
|
|
|
|
shortcode = self._imap2shortcode(msgid)
|
|
|
|
|
|
|
|
|
|
|
|
msg["msgid"] = str(msgid)
|
|
|
|
# make action barcodes
|
|
|
|
sender = env.from_[0]
|
|
|
|
msg.d_bc = code128.svg("POd." + shortcode).encode()
|
|
|
|
msg["fromaddr"] = str(sender.mailbox) + "@" + str(sender.host)
|
|
|
|
msg.sp_bc = code128.svg("POsp." + shortcode).encode()
|
|
|
|
msg["fromname"] = str(sender.name)
|
|
|
|
msg.r_bc = code128.svg("POr." + shortcode).encode()
|
|
|
|
msg["date"] = str(data[b"INTERNALDATE"])
|
|
|
|
|
|
|
|
msg["size"] = str(data[b"RFC822.SIZE"])
|
|
|
|
|
|
|
|
msg["subject"] = str(env.subject)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msgs.append(msg)
|
|
|
|
msgs.append(msg)
|
|
|
|
|
|
|
|
|
|
|
|