| 1 |
hertzog |
344 |
# vim: expandtab
|
| 2 |
|
|
|
| 3 |
|
|
import os, os.path, re, rfc822, email.Utils
|
| 4 |
|
|
|
| 5 |
|
|
def save_msg_in_dir(msg, dir, nb=20):
|
| 6 |
|
|
"""Add the message in the directory.
|
| 7 |
|
|
Keep only the nb last messages."""
|
| 8 |
|
|
if not os.path.isdir(dir):
|
| 9 |
|
|
os.makedirs(dir)
|
| 10 |
|
|
if os.path.isfile("%s/%d.txt" % (dir, nb)):
|
| 11 |
|
|
os.unlink("%s/%d.txt" % (dir, nb))
|
| 12 |
|
|
for i in range(nb - 1, 0, -1):
|
| 13 |
|
|
if os.path.isfile("%s/%d.txt" % (dir, i)):
|
| 14 |
|
|
os.rename("%s/%d.txt" % (dir, i), "%s/%d.txt" % (dir, i+1))
|
| 15 |
|
|
f = open("%s/1.txt" % dir, "w")
|
| 16 |
|
|
f.write(msg.as_string())
|
| 17 |
|
|
f.close()
|
| 18 |
|
|
|
| 19 |
|
|
def generate_html_msg(src, dst, nb=20):
|
| 20 |
|
|
"""Convert all message in src directory
|
| 21 |
|
|
in HTML messages in dst directory"""
|
| 22 |
|
|
# Clean old attachments first
|
| 23 |
|
|
os.system("find %s -maxdepth 1 -type f -not -name '*.html' | xargs -r rm -f" % dst)
|
| 24 |
|
|
# Regenerate HTML messages
|
| 25 |
|
|
for i in range(nb):
|
| 26 |
|
|
if os.path.isfile("%s/%d.txt" % (src, i + 1)):
|
| 27 |
|
|
os.system("cd %s; /usr/bin/mhonarc -single %s/%d.txt > %d.html" % (dst, src, i+1, i+1))
|
| 28 |
|
|
|
| 29 |
|
|
def extract_info(msg):
|
| 30 |
|
|
"""Extract pseudo-header informations in a dictionnary"""
|
| 31 |
|
|
info = {}
|
| 32 |
|
|
if msg.is_multipart():
|
| 33 |
|
|
for part in msg.walk():
|
| 34 |
|
|
if part.get_type("text/plain") == "text/plain":
|
| 35 |
|
|
body = part.get_payload(None, 1)
|
| 36 |
|
|
break
|
| 37 |
|
|
else:
|
| 38 |
|
|
body = msg.get_payload(None, 1)
|
| 39 |
|
|
lines = body.split("\n", 3)
|
| 40 |
|
|
for i in range(3):
|
| 41 |
|
|
res = re.match(r"^(\w+): (\S+)(.*)", lines[i])
|
| 42 |
|
|
if res:
|
| 43 |
|
|
field = res.group(1).lower()
|
| 44 |
|
|
info[field] = res.group(2)
|
| 45 |
|
|
if field == "subject":
|
| 46 |
|
|
info[field] = info[field] + res.group(3)
|
| 47 |
|
|
else:
|
| 48 |
|
|
break
|
| 49 |
|
|
if not info.has_key("subject"):
|
| 50 |
|
|
info["subject"] = email.Utils.decode(msg.get("subject"))
|
| 51 |
|
|
if msg.has_key("X-PTS-Url"):
|
| 52 |
|
|
info["url"] = msg.get("X-PTS-Url")
|
| 53 |
|
|
if msg.has_key("X-PTS-Package"):
|
| 54 |
|
|
info["package"] = msg.get("X-PTS-Package")
|
| 55 |
|
|
if msg.has_key("date"):
|
| 56 |
|
|
t = rfc822.parsedate(msg["date"])
|
| 57 |
|
|
if t:
|
| 58 |
|
|
info["date"] = "%04d-%02d-%02d" % (t[0:3])
|
| 59 |
|
|
return info
|
| 60 |
|
|
|