mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-21 06:18:45 +02:00
First version of the sieve plugin is over \o/ It needs more testing but all functionalities are implemented. Maybe some extra sieve extensions would be nice but I'll see that based on user feedback.
I also include some general refactoring in this commit. see #139
This commit is contained in:
+17
-4
@@ -6,7 +6,7 @@ import hashlib, crypt, string
|
||||
from random import Random
|
||||
from django.http import HttpResponse
|
||||
from django.conf import settings
|
||||
from django.template import RequestContext
|
||||
from django import template
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.translation import ugettext as _
|
||||
@@ -27,7 +27,7 @@ def _render(request, tpl, user_context):
|
||||
(useful to use settings variables like MEDIA_URL inside templates)
|
||||
"""
|
||||
return render_to_response(tpl, user_context,
|
||||
context_instance=RequestContext(request))
|
||||
context_instance=template.RequestContext(request))
|
||||
|
||||
def _render_to_string(request, tpl, user_context):
|
||||
"""Custom rendering function
|
||||
@@ -35,11 +35,24 @@ def _render_to_string(request, tpl, user_context):
|
||||
Same as _render.
|
||||
"""
|
||||
return render_to_string(tpl, user_context,
|
||||
context_instance=RequestContext(request))
|
||||
context_instance=template.RequestContext(request))
|
||||
|
||||
def _render_error(request, errortpl="error", user_context={}):
|
||||
return render_to_response("common/%s.html" % errortpl, user_context,
|
||||
context_instance=RequestContext(request))
|
||||
context_instance=template.RequestContext(request))
|
||||
|
||||
def render_actions(actions):
|
||||
t = template.Template("""
|
||||
{% for a in actions %}
|
||||
<a href="{{ a.url }}" name="{{ a.name }}" class="{{ a.class }}" rel="{{ a.rel }}"
|
||||
{% if a.confirm %}onclick="return confirm('{{ a.confirm }}')"{% endif %}>
|
||||
<img src="{{ a.img }}" border="0" title="{{ a.title }}" />
|
||||
</a>
|
||||
{% endfor %}
|
||||
""")
|
||||
return t.render(template.Context({
|
||||
"actions" : actions
|
||||
}))
|
||||
|
||||
def exec_as_vuser(cmd):
|
||||
code, output = exec_cmd("sudo -u %s %s" \
|
||||
|
||||
@@ -140,166 +140,6 @@ class EmailListing:
|
||||
context[k] = v
|
||||
return _render(request, self.tpl, context)
|
||||
|
||||
class Email(object):
|
||||
def __init__(self, msg, mformat="plain", dformat="plain", links="0"):
|
||||
self.attached_map = {}
|
||||
self.contents = {"html" : "", "plain" : ""}
|
||||
self.headers = []
|
||||
self.attachments = {}
|
||||
self.mformat = mformat
|
||||
self.dformat = dformat
|
||||
self.links = links
|
||||
|
||||
self.__parse(msg)
|
||||
|
||||
if not self.contents.has_key(mformat) or self.contents[mformat] == "":
|
||||
# Fallback
|
||||
self.mformat = mformat == "html" and "plain" or "html"
|
||||
|
||||
self.body = \
|
||||
getattr(self, "viewmail_%s" % self.mformat) \
|
||||
(self.contents[self.mformat], links=links)
|
||||
|
||||
def __parse_default(self, msg, level):
|
||||
"""Default parser
|
||||
|
||||
All parts handled by this parser will be consireded as
|
||||
attachments.
|
||||
"""
|
||||
fname = msg.get_filename()
|
||||
if fname is not None:
|
||||
decoded = decode_header(fname)
|
||||
value = decoded[0][1] is None and decoded[0][0] \
|
||||
or unicode(decoded[0][0], decoded[0][1])
|
||||
else:
|
||||
value = "part_%s" % level
|
||||
self.attachments[level] = value
|
||||
|
||||
def _parse_text(self, msg, level):
|
||||
"""Displayable content parser
|
||||
|
||||
text, html, calendar, etc. All those contents can be displayed
|
||||
inside a navigator.
|
||||
"""
|
||||
if msg.get_content_subtype() not in ["plain", "html"]:
|
||||
self.__parse_default(msg, level)
|
||||
target = "plain"
|
||||
else:
|
||||
target = msg.get_content_subtype()
|
||||
self.contents[target] += decode(msg.get_payload(decode=True),
|
||||
charset=msg.get_content_charset())
|
||||
|
||||
def _parse_image(self, msg, level):
|
||||
"""image/* parser
|
||||
|
||||
The only reason to make a specific parser for images is that,
|
||||
sometimes, messages embark inline images, which means they
|
||||
must be displayed and not attached.
|
||||
"""
|
||||
if self.dformat == "html" and self.links != "0" \
|
||||
and msg.has_key("Content-Disposition"):
|
||||
if msg["Content-Disposition"].startswith("inline"):
|
||||
cid = None
|
||||
if msg.has_key("Content-ID"):
|
||||
m = re.match("<(.+)>", msg["Content-ID"])
|
||||
cid = m is not None and m.group(1) or msg["Content-ID"]
|
||||
fname = msg.get_filename()
|
||||
if fname is None:
|
||||
if msg.has_key("Content-Location"):
|
||||
fname = msg["Content-Location"]
|
||||
elif cid is not None:
|
||||
fname = cid
|
||||
else:
|
||||
# I give up for now :p
|
||||
return
|
||||
self.attached_map[cid] = re.match("^http:", fname) and fname \
|
||||
or self.__save_image(fname, msg)
|
||||
return
|
||||
self.__parse_default(msg, level)
|
||||
|
||||
def __parse(self, msg, level=None):
|
||||
"""Recursive email parser
|
||||
|
||||
A message structure can be complex. To correctly handle
|
||||
unknown MIME types, a simple rule is applied : if I don't know
|
||||
how to display a specific part, it becomes an attachment! If
|
||||
no name is specified for an attachment, the part number
|
||||
described in the RFC 3501 (which retrieves BODY sections) is
|
||||
used to build a file name (like part_1.1).
|
||||
|
||||
:param msg: message (or part) to parse
|
||||
:param level: current part number
|
||||
"""
|
||||
if msg.is_multipart() and msg.get_content_maintype() != "message":
|
||||
cpt = 1
|
||||
for part in msg.get_payload():
|
||||
nlevel = level is None and ("%d" % cpt) \
|
||||
or "%s.%d" % (level, cpt)
|
||||
self.__parse(part, nlevel)
|
||||
cpt += 1
|
||||
return
|
||||
|
||||
if level is None:
|
||||
level = "1"
|
||||
try:
|
||||
getattr(self, "_parse_%s" % msg.get_content_maintype())(msg, level)
|
||||
except AttributeError:
|
||||
self.__parse_default(msg, level)
|
||||
|
||||
def __save_image(self, fname, part):
|
||||
"""Save an inline image on the filesystem.
|
||||
|
||||
Some HTML messages are using inline images (attached images
|
||||
with links on them inside the body). In order to display them,
|
||||
images are saved on the filesystem and links contained in the
|
||||
message are modified.
|
||||
|
||||
:param fname: the image associated filename
|
||||
:param part: the email part that contains the image payload
|
||||
"""
|
||||
if re.search("\.\.", fname):
|
||||
return None
|
||||
path = "/static/tmp/" + fname
|
||||
fp = open(settings.MODOBOA_DIR + path, "wb")
|
||||
fp.write(part.get_payload(decode=True))
|
||||
fp.close()
|
||||
return path
|
||||
|
||||
def map_cid(self, url):
|
||||
import re
|
||||
|
||||
m = re.match(".*cid:(.+)", url)
|
||||
if m:
|
||||
if self.attached_map.has_key(m.group(1)):
|
||||
return self.attached_map[m.group(1)]
|
||||
return url
|
||||
|
||||
def render_headers(self, **kwargs):
|
||||
return render_to_string("common/mailheaders.html", {
|
||||
"headers" : self.headers,
|
||||
})
|
||||
|
||||
def viewmail_plain(self, content, **kwargs):
|
||||
return "<pre>%s</pre>" % content
|
||||
|
||||
def viewmail_html(self, content, **kwargs):
|
||||
if content is None or content == "":
|
||||
return (False, "")
|
||||
links = kwargs.has_key("links") and kwargs["links"] or "0"
|
||||
html = lxml.html.fromstring(content)
|
||||
if links == "0":
|
||||
html.rewrite_links(lambda x: None)
|
||||
else:
|
||||
html.rewrite_links(self.map_cid)
|
||||
body = html.find("body")
|
||||
if body is None:
|
||||
body = lxml.html.tostring(html)
|
||||
else:
|
||||
body = lxml.html.tostring(body)
|
||||
body = re.sub("<(/?)body", lambda m: "<%sdiv" % m.group(1), body)
|
||||
body = Template(body).render(Context({}))
|
||||
return body
|
||||
|
||||
def parse_search_parameters(request):
|
||||
if request.GET.has_key("pattern"):
|
||||
request.session["pattern"] = re.escape(request.GET["pattern"])
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
# coding: utf-8
|
||||
|
||||
import email
|
||||
import lxml
|
||||
from django.template import Template, Context
|
||||
from django.template.loader import render_to_string
|
||||
import u2u_decode
|
||||
from lib import decode
|
||||
|
||||
class EmailAddress(object):
|
||||
def __init__(self, address):
|
||||
self.fulladdress = u2u_decode.u2u_decode(address).strip("\r\t\n")
|
||||
(self.name, self.address) = email.utils.parseaddr(self.fulladdress)
|
||||
if self.name == "":
|
||||
self.fulladdress = self.address
|
||||
|
||||
def __str__(self):
|
||||
return self.fulladdress
|
||||
|
||||
class Email(object):
|
||||
def __init__(self, msg, mformat="plain", dformat="plain", links="0"):
|
||||
self.attached_map = {}
|
||||
self.contents = {"html" : "", "plain" : ""}
|
||||
self.headers = []
|
||||
self.attachments = {}
|
||||
self.mformat = mformat
|
||||
self.dformat = dformat
|
||||
self.links = links
|
||||
|
||||
self.__parse(msg)
|
||||
|
||||
if not self.contents.has_key(mformat) or self.contents[mformat] == "":
|
||||
# Fallback
|
||||
self.mformat = mformat == "html" and "plain" or "html"
|
||||
|
||||
self.body = \
|
||||
getattr(self, "viewmail_%s" % self.mformat) \
|
||||
(self.contents[self.mformat], links=links)
|
||||
|
||||
def __parse_default(self, msg, level):
|
||||
"""Default parser
|
||||
|
||||
All parts handled by this parser will be consireded as
|
||||
attachments.
|
||||
"""
|
||||
fname = msg.get_filename()
|
||||
if fname is not None:
|
||||
decoded = decode_header(fname)
|
||||
value = decoded[0][1] is None and decoded[0][0] \
|
||||
or unicode(decoded[0][0], decoded[0][1])
|
||||
else:
|
||||
value = "part_%s" % level
|
||||
self.attachments[level] = value
|
||||
|
||||
def _parse_text(self, msg, level):
|
||||
"""Displayable content parser
|
||||
|
||||
text, html, calendar, etc. All those contents can be displayed
|
||||
inside a navigator.
|
||||
"""
|
||||
if msg.get_content_subtype() not in ["plain", "html"]:
|
||||
self.__parse_default(msg, level)
|
||||
target = "plain"
|
||||
else:
|
||||
target = msg.get_content_subtype()
|
||||
self.contents[target] += decode(msg.get_payload(decode=True),
|
||||
charset=msg.get_content_charset())
|
||||
|
||||
def _parse_image(self, msg, level):
|
||||
"""image/* parser
|
||||
|
||||
The only reason to make a specific parser for images is that,
|
||||
sometimes, messages embark inline images, which means they
|
||||
must be displayed and not attached.
|
||||
"""
|
||||
if self.dformat == "html" and self.links != "0" \
|
||||
and msg.has_key("Content-Disposition"):
|
||||
if msg["Content-Disposition"].startswith("inline"):
|
||||
cid = None
|
||||
if msg.has_key("Content-ID"):
|
||||
m = re.match("<(.+)>", msg["Content-ID"])
|
||||
cid = m is not None and m.group(1) or msg["Content-ID"]
|
||||
fname = msg.get_filename()
|
||||
if fname is None:
|
||||
if msg.has_key("Content-Location"):
|
||||
fname = msg["Content-Location"]
|
||||
elif cid is not None:
|
||||
fname = cid
|
||||
else:
|
||||
# I give up for now :p
|
||||
return
|
||||
self.attached_map[cid] = re.match("^http:", fname) and fname \
|
||||
or self.__save_image(fname, msg)
|
||||
return
|
||||
self.__parse_default(msg, level)
|
||||
|
||||
def __parse(self, msg, level=None):
|
||||
"""Recursive email parser
|
||||
|
||||
A message structure can be complex. To correctly handle
|
||||
unknown MIME types, a simple rule is applied : if I don't know
|
||||
how to display a specific part, it becomes an attachment! If
|
||||
no name is specified for an attachment, the part number
|
||||
described in the RFC 3501 (which retrieves BODY sections) is
|
||||
used to build a file name (like part_1.1).
|
||||
|
||||
:param msg: message (or part) to parse
|
||||
:param level: current part number
|
||||
"""
|
||||
if msg.is_multipart() and msg.get_content_maintype() != "message":
|
||||
cpt = 1
|
||||
for part in msg.get_payload():
|
||||
nlevel = level is None and ("%d" % cpt) \
|
||||
or "%s.%d" % (level, cpt)
|
||||
self.__parse(part, nlevel)
|
||||
cpt += 1
|
||||
return
|
||||
|
||||
if level is None:
|
||||
level = "1"
|
||||
try:
|
||||
getattr(self, "_parse_%s" % msg.get_content_maintype())(msg, level)
|
||||
except AttributeError:
|
||||
self.__parse_default(msg, level)
|
||||
|
||||
def __save_image(self, fname, part):
|
||||
"""Save an inline image on the filesystem.
|
||||
|
||||
Some HTML messages are using inline images (attached images
|
||||
with links on them inside the body). In order to display them,
|
||||
images are saved on the filesystem and links contained in the
|
||||
message are modified.
|
||||
|
||||
:param fname: the image associated filename
|
||||
:param part: the email part that contains the image payload
|
||||
"""
|
||||
if re.search("\.\.", fname):
|
||||
return None
|
||||
path = "/static/tmp/" + fname
|
||||
fp = open(settings.MODOBOA_DIR + path, "wb")
|
||||
fp.write(part.get_payload(decode=True))
|
||||
fp.close()
|
||||
return path
|
||||
|
||||
def map_cid(self, url):
|
||||
import re
|
||||
|
||||
m = re.match(".*cid:(.+)", url)
|
||||
if m:
|
||||
if self.attached_map.has_key(m.group(1)):
|
||||
return self.attached_map[m.group(1)]
|
||||
return url
|
||||
|
||||
def render_headers(self, **kwargs):
|
||||
return render_to_string("common/mailheaders.html", {
|
||||
"headers" : self.headers,
|
||||
})
|
||||
|
||||
def viewmail_plain(self, content, **kwargs):
|
||||
return "<pre>%s</pre>" % content
|
||||
|
||||
def viewmail_html(self, content, **kwargs):
|
||||
if content is None or content == "":
|
||||
return (False, "")
|
||||
links = kwargs.has_key("links") and kwargs["links"] or "0"
|
||||
html = lxml.html.fromstring(content)
|
||||
if links == "0":
|
||||
html.rewrite_links(lambda x: None)
|
||||
else:
|
||||
html.rewrite_links(self.map_cid)
|
||||
body = html.find("body")
|
||||
if body is None:
|
||||
body = lxml.html.tostring(html)
|
||||
else:
|
||||
body = lxml.html.tostring(body)
|
||||
body = re.sub("<(/?)body", lambda m: "<%sdiv" % m.group(1), body)
|
||||
body = Template(body).render(Context({}))
|
||||
return body
|
||||
Reference in New Issue
Block a user