diff --git a/auth/views.py b/auth/views.py index 7cd567b80..75e730a90 100755 --- a/auth/views.py +++ b/auth/views.py @@ -16,6 +16,7 @@ def dologin(request): if user: if user.is_active: login(request, user) + request.session["password"] = request.POST["password"] next = request.POST["next"] if next == "None": diff --git a/extensions/amavis_quarantine/main.py b/extensions/amavis_quarantine/main.py index c6f319ce2..946e2bf4d 100644 --- a/extensions/amavis_quarantine/main.py +++ b/extensions/amavis_quarantine/main.py @@ -7,10 +7,7 @@ import time import email import re import os -import lxml.html -from lxml.html.clean import Cleaner from django.http import HttpResponseRedirect, HttpResponse -from django.template import Template from django.utils import simplejson from django.conf.urls.defaults import * from django.utils.translation import ugettext as _ @@ -24,16 +21,6 @@ from mailng.admin.models import Mailbox from lib import AMrelease from sql_listing import * -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) parameters.register("amavis_quarantine", "MAX_MESSAGES_AGE", "int", 14, @@ -69,25 +56,10 @@ def index(request, message=None): lst = SQLlisting(filter) return lst.render(request, pageid=int(pageid), start=start) -@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): + from mailng.lib.email_listing import Email + conn = db.getconnection("amavis_quarantine") status, cursor = db.execute(conn, """ SELECT mail_text @@ -100,41 +72,8 @@ WHERE quarantine.mail_id='%s' 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 - }) + mail = Email(msg, mode, links) + return mail.render(request) @login_required def viewmail(request, mail_id): diff --git a/extensions/webmail/imap_listing.py b/extensions/webmail/imap_listing.py index b6ff4ad19..c5977d064 100644 --- a/extensions/webmail/imap_listing.py +++ b/extensions/webmail/imap_listing.py @@ -82,20 +82,27 @@ class IMAPconnector(MBconnector): result += [name] return sorted(result) - def fetch(self, start=None, stop=None, folder=None): - if not start or not stop: + def fetch(self, start=None, stop=None, folder=None, all=False): + if not start and not stop: return [] result = [] self.m.select(self._encodefolder(folder)) - range = "%d:%d" % (start, stop) - typ, data = self.m.fetch(range, '(BODY[HEADER.FIELDS (DATE FROM SUBJECT)])') - imapid = start + if start and stop: + range = "%d:%d" % (start, stop) + else: + range = start + if not all: + query = '(BODY[HEADER.FIELDS (DATE FROM TO SUBJECT)])' + else: + query = '(RFC822)' + typ, data = self.m.fetch(range, query) + imapid = int(start) for response_part in data: if isinstance(response_part, tuple): msg = email.message_from_string(response_part[1]) msg["imapid"] = imapid result += [msg] - imapid += 1 + imapid = imapid + 1 return result class ImapListing(EmailListing): diff --git a/extensions/webmail/main.py b/extensions/webmail/main.py index 684587404..cc2dc6381 100644 --- a/extensions/webmail/main.py +++ b/extensions/webmail/main.py @@ -41,7 +41,8 @@ def index(request): @login_required def folder(request, folder): - lst = ImapListing(folder=folder) + lst = ImapListing(request.user.username, request.session["password"], + folder=folder) try: pageid = request.GET["page"] except KeyError: @@ -55,3 +56,28 @@ def folder(request, folder): navbar = "" ctx = {"status" : "ok", "listing" : content, "navbar" : navbar} return HttpResponse(simplejson.dumps(ctx), mimetype="application/json") + +def fetchmail(request, mail_id, all=False): + mbc = IMAPconnector(parameters.get("webmail", "SERVER_ADDRESS"), 143) + mbc.login(request.user.username, request.session["password"]) + res = mbc.fetch(start=mail_id, all=all) + if len(res): + return res[0] + return None + +@login_required +def viewmail(request, mail_id): + header = fetchmail(request, mail_id) + pageid = request.GET.has_key("page") and request.GET["page"] or "1" + if header: + return _render(request, "webmail/viewmail.html", { + "header" : header, "pageid" : pageid + }) + +@login_required +def getmailcontent(request, mail_id): + from mailng.lib.email_listing import Email + + msg = fetchmail(request, mail_id, True) + email = Email(msg) + return email.render(request) diff --git a/extensions/webmail/templates/index.html b/extensions/webmail/templates/index.html index af4971081..eb15bc75d 100644 --- a/extensions/webmail/templates/index.html +++ b/extensions/webmail/templates/index.html @@ -57,6 +57,8 @@ $("navbar").set("html", resp.navbar); }}).get(); }); + {% url extensions.webmail.main.viewmail as view_url %} + bindRows("{{ view_url }}", "{{ current_page }}"); {% endblock %} {% block middletitle %}{% trans "Webmail" %}{% endblock %} {% block menu %} diff --git a/extensions/webmail/templates/viewmail.html b/extensions/webmail/templates/viewmail.html new file mode 100644 index 000000000..b2cb5e310 --- /dev/null +++ b/extensions/webmail/templates/viewmail.html @@ -0,0 +1,47 @@ +{% extends "nlayout.html" %} +{% load i18n %} +{% block html_head %} + + +{% endblock %} +{% block middletitle %}{% trans "Email content" %}{% endblock %} +{% block menu %} + {% load webextras %} + {% viewm_menu "" header.imapid pageid perms %} +{% endblock %} +{% block content %} +
| {% trans "Subject" %} | {{ header.Subject }} |
|---|---|
| {% trans "From" %} | {{ header.From }} |
| {% trans "To" %} | {{ header.To }} |
| {% trans "Date" %} | {{ header.Date }} |
{% endif %}{{ body }}{% if pre %}{% endif %}
diff --git a/templates/nlayout.html b/templates/nlayout.html
index 85ef5d431..f86d4ab33 100644
--- a/templates/nlayout.html
+++ b/templates/nlayout.html
@@ -22,8 +22,13 @@