From 7ad7c6f4a4cea564abe638261636b645a7f3d781 Mon Sep 17 00:00:00 2001 From: tonio Date: Tue, 22 Sep 2009 11:36:48 +0200 Subject: [PATCH] Ticket #11 Some code refactoring. Improvement of emails displaying. Mootools update (new version is 1.2.3). Fx.Slide added (version 1.2.3.1). --- extensions/amavis_quarantine/main.py | 105 +++- .../templates/getmailcontent.html | 1 + .../amavis_quarantine/templates/viewmail.html | 23 +- .../templatetags/amextras.py | 21 +- extensions/amavis_quarantine/urls.py | 1 + static/js/common.js | 16 + static/js/mootools-more.js | 146 +++++ static/js/mootools.js | 508 +++++++++++------- static/stylesheets/basic.css | 30 +- templates/main/menu.html | 23 +- 10 files changed, 620 insertions(+), 254 deletions(-) create mode 100644 extensions/amavis_quarantine/templates/getmailcontent.html create mode 100644 static/js/common.js create mode 100644 static/js/mootools-more.js diff --git a/extensions/amavis_quarantine/main.py b/extensions/amavis_quarantine/main.py index 143def45b..a7d682511 100644 --- a/extensions/amavis_quarantine/main.py +++ b/extensions/amavis_quarantine/main.py @@ -21,7 +21,19 @@ from datetime import datetime import email import re import os +import lxml.html +from lxml.html.clean import Cleaner +attached_map = {} + +def map_cid(url): + map = globals()["attached_map"] + m = re.match(".*cid:([^\"]+)", url) + if m: + if map.has_key(m.group(1)): + return map[m.group(1)] + return url + def init(): events.register("UserMenuDisplay", menu) @@ -74,6 +86,73 @@ ORDER BY msgs.time_num DESC "current_page" : pagenum, "last_page" : paginator._get_num_pages() }) +@login_required +def viewmail_plain(request, content): + body = decode(content) + return (True, body) + +@login_required +def viewmail_html(request, content): + links = request.GET.has_key("links") and request.GET["links"] or "0" + html = lxml.html.fromstring(content) + if links == "0": + html.rewrite_links(lambda x: None) + else: + html.rewrite_links(map_cid) + body = lxml.html.tostring(html) + body = Template(decode(body)).render({}) + return (False, body) + +@login_required +def getmailcontent(request, mail_id): + conn = db.getconnection("amavis_quarantine") + status, cursor = db.execute(conn, """ +SELECT mail_text +FROM quarantine +WHERE quarantine.mail_id='%s' +""" % mail_id) + content = "" + for part in cursor.fetchall(): + content += part[0] + msg = email.message_from_string(content) + links = request.GET.has_key("links") and request.GET["links"] or "0" + mode = request.GET.has_key("mode") and request.GET["mode"] or "plain" + contents = {"html" : "", "plain" : ""} + for part in msg.walk(): + print part.get_content_type() + if part.get_content_maintype() == 'multipart': + continue + if part.get_content_type() in ("text/html", "text/plain"): + contents[part.get_content_subtype()] += part.get_payload(decode=True) + if mode != "html" or links == "0": + continue + + if part.get_content_maintype() == "image": + if part.has_key("Content-Location"): + fname = part["Content-Location"] + if re.match("^http:", fname): + path = fname + else: + path = "/%s" % os.path.join("static/tmp", fname) + fp = open(path, "wb") + fp.write(part.get_payload(decode=True)) + fp.close() + m = re.match("<(.+)>", part["Content-ID"]) + if m: + attached_map[m.group(1)] = path + else: + attached_map[part["Content-ID"]] = path + + if not contents.has_key(mode) or contents[mode] == "": + if mode == "html": + mode = "plain" + else: + mode = "html" + (pre, body) = globals()["viewmail_%s" % mode](request, contents[mode]) + return _render(request, 'amavis_quarantine/getmailcontent.html', { + "body" : body, "pre" : pre + }) + @login_required def viewmail(request, mail_id): conn = db.getconnection("amavis_quarantine") @@ -86,32 +165,10 @@ WHERE quarantine.mail_id='%s' for part in cursor.fetchall(): content += part[0] msg = email.message_from_string(content) - - html_body = "" - text_body = "" - for part in msg.walk(): - print part.get_content_type() - if part.get_content_maintype() == 'multipart': - continue - if part.get_content_type() in ("text/html"): - html_body += part.get_payload() - if part.get_content_type() in ("text/plain"): - text_body += part.get_payload(decode=True) - pre = None - if html_body != "": - html_body = email.utils.quote(html_body) - html_body = re.sub('(\n|\r|\r\n)', '\\\\n', html_body) - body = Template(decode(html_body)).render({}) - else: - text_body = email.utils.quote(text_body) - text_body = re.sub('(\n|\r|\r\n)', '\\\\n', text_body) - body = decode(text_body) - pre = True - return _render(request, 'amavis_quarantine/viewmail.html', { "headers" : msg, "alert" : msg.get("X-Amavis-Alert"), - "body" : body, "pre" : pre, "mail_id" : mail_id, - "page_id" : request.GET["page"] + "mail_id" : mail_id, "page_id" : request.GET["page"], + "params" : request.GET }) @login_required diff --git a/extensions/amavis_quarantine/templates/getmailcontent.html b/extensions/amavis_quarantine/templates/getmailcontent.html new file mode 100644 index 000000000..579454343 --- /dev/null +++ b/extensions/amavis_quarantine/templates/getmailcontent.html @@ -0,0 +1 @@ +{% if pre %}
{% endif %}{{ body }}{% if pre %}
{% endif %} \ No newline at end of file diff --git a/extensions/amavis_quarantine/templates/viewmail.html b/extensions/amavis_quarantine/templates/viewmail.html index 27d78bb0a..8de14a360 100644 --- a/extensions/amavis_quarantine/templates/viewmail.html +++ b/extensions/amavis_quarantine/templates/viewmail.html @@ -3,20 +3,14 @@ {% block html_head %} + + + {% endblock %} {% block content %} -