refactor email message formatting. redo unread lister. keep track of imapid-shortcode mappings.

workspace
Brendan Howell 8 years ago
parent 1ff5b3e886
commit 00fe6eb843

@ -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. util tidies up message headers deals with encoding
""" """
photo = self.send("PXphoto.") internaldate = resp_obj[imap_id][b'INTERNALDATE']
@add_command("r", "Print full email") msg_data = resp_obj[imap_id][b'RFC822'].decode('utf-8')
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'])
internaldate = resp[imap_id][b'INTERNALDATE']
msg_data = resp[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)

@ -52,6 +52,9 @@
.clearfloats { .clearfloats {
clear: both; clear: both;
} }
.entry {
margin-bottom: 1em;
}
.entry .title{ .entry .title{
font-weight: bold; font-weight: bold;
font-variant: small-caps; font-variant: small-caps;
@ -60,7 +63,13 @@
.entry .source{ .entry .source{
font-style: italic; font-style: italic;
} }
.entry .barcode{} .entry .barcode{
text-align: left;
}
.entry .barcode svg {
width: 100%;
height: 7mm;
}
.entry .summary{ .entry .summary{
text-align: justify; text-align: justify;
hyphens: auto; hyphens: auto;
@ -92,11 +101,11 @@
% for msg in inbox: % for msg in inbox:
<tr> <tr>
<td>${msg["fromname"]} &lt;${msg["fromaddr"]}&gt;</td> <td>${msg.fromstr}</td>
<td>${msg["subject"]}</td><td>${msg["date"]}</td> <td>${msg.subject}</td><td>${msg.date}</td>
</tr> </tr>
<tr><td> </td><td>READ</td><td>DELETE</td><td>MARK AS SPAM</td></tr> <tr><td> </td><td>READ</td><td>DELETE</td><td>MARK AS SPAM</td></tr>
<tr><td> </td><td>BC-READ</td><td>BC-DELETE</td><td>BC-MARK AS SPAM</td></tr> <tr><td> </td><td>${msg.r_bc}</td><td>${msg.d_bc}</td><td>${msg.sp_bc}</td></tr>
% endfor % endfor
</table> </table>
</div> </div>

@ -9,6 +9,7 @@ import urllib.request, urllib.parse, urllib.error
import code128 import code128
import feedparser import feedparser
import html5tidy
import lxml.html import lxml.html
import PIL import PIL
from readability import readability from readability import readability
@ -203,6 +204,8 @@ class Publications(Bureau):
end = entry.summary.rfind(" ", 0, 499) end = entry.summary.rfind(" ", 0, 499)
entry.summary = entry.summary[0:end] + "" entry.summary = entry.summary[0:end] + ""
entry.summary = html5tidy.tidy(entry.summary, fragment=True)
entries.append(entry) entries.append(entry)
return entries return entries

Loading…
Cancel
Save