From 5a4ea784c5ab7fd51de8d17cda4b550a4385d424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Touceda?= Date: Wed, 2 Oct 2013 17:08:21 -0300 Subject: Improve charset handling for email --- changes/better_charset_handling | 2 ++ src/leap/mail/imap/server.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 changes/better_charset_handling diff --git a/changes/better_charset_handling b/changes/better_charset_handling new file mode 100644 index 0000000..832f7b2 --- /dev/null +++ b/changes/better_charset_handling @@ -0,0 +1,2 @@ + o Improve charset handling when exposing mails to the mail client. Related to + #3660. diff --git a/src/leap/mail/imap/server.py b/src/leap/mail/imap/server.py index ae76833..10d338a 100644 --- a/src/leap/mail/imap/server.py +++ b/src/leap/mail/imap/server.py @@ -18,7 +18,9 @@ Soledad-backed IMAP Server. """ import copy +import email import logging +import re import StringIO import cStringIO import time @@ -693,6 +695,26 @@ class LeapMessage(WithMsgFields): the more complex MIME-based interface. """ + def _get_charset(self, content): + """ + Mini parser to retrieve the charset of an email + + :param content: mail contents + :type content: unicode + + :returns: the charset as parsed from the contents + :rtype: str + """ + charset = "UTF-8" + try: + em = email.message_from_string(content.encode("utf-8")) + # Miniparser for: Content-Type: ; charset= + charset_re = r'''charset=(?P[\w|\d|-]*)''' + charset = re.findall(charset_re, em["Content-Type"])[0] + except Exception: + pass + return charset + def open(self): """ Return an file-like object opened for reading. @@ -704,7 +726,8 @@ class LeapMessage(WithMsgFields): :rtype: StringIO """ fd = cStringIO.StringIO() - fd.write(str(self._doc.content.get(self.RAW_KEY, ''))) + charset = self._get_charset(self._doc.content.get(self.RAW_KEY, '')) + fd.write(self._doc.content.get(self.RAW_KEY, '').encode(charset)) fd.seek(0) return fd @@ -723,7 +746,8 @@ class LeapMessage(WithMsgFields): :rtype: StringIO """ fd = StringIO.StringIO() - fd.write(str(self._doc.content.get(self.RAW_KEY, ''))) + charset = self._get_charset(self._doc.content.get(self.RAW_KEY, '')) + fd.write(self._doc.content.get(self.RAW_KEY, '').encode(charset)) # SHOULD use a separate BODY FIELD ... fd.seek(0) return fd -- cgit v1.2.3 From 740a0a224c058d37ffcbaae64b97714ebde2b313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Touceda?= Date: Thu, 3 Oct 2013 20:38:26 -0300 Subject: Return smtp's Port to be able to stop listening to it --- changes/return_smtp_port | 2 ++ src/leap/mail/smtp/__init__.py | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 changes/return_smtp_port diff --git a/changes/return_smtp_port b/changes/return_smtp_port new file mode 100644 index 0000000..1cf58ef --- /dev/null +++ b/changes/return_smtp_port @@ -0,0 +1,2 @@ + o Return Twisted's smtp Port object to be able to stop listening to + it whenever we want. Related to #3873. \ No newline at end of file diff --git a/src/leap/mail/smtp/__init__.py b/src/leap/mail/smtp/__init__.py index 2a4abc5..4e5d2a0 100644 --- a/src/leap/mail/smtp/__init__.py +++ b/src/leap/mail/smtp/__init__.py @@ -54,7 +54,7 @@ def setup_smtp_relay(port, keymanager, smtp_host, smtp_port, or not. :type encrypted_only: bool - :returns: SMTPFactory + :returns: tuple of SMTPFactory, twisted.internet.tcp.Port """ # The configuration for the SMTP relay is a dict with the following # format: @@ -77,10 +77,10 @@ def setup_smtp_relay(port, keymanager, smtp_host, smtp_port, # configure the use of this service with twistd factory = SMTPFactory(keymanager, config) try: - reactor.listenTCP(port, factory, - interface="localhost") + tport = reactor.listenTCP(port, factory, + interface="localhost") signal(proto.SMTP_SERVICE_STARTED, str(smtp_port)) - return factory + return factory, tport except CannotListenError: logger.error("STMP Service failed to start: " "cannot listen in port %s" % ( -- cgit v1.2.3 From fdf28dc83babe9b511e9c01eb5f0c79fc8fb9faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Touceda?= Date: Fri, 4 Oct 2013 11:46:49 -0300 Subject: Fold in changes --- CHANGELOG | 6 ++++++ changes/better_charset_handling | 2 -- changes/return_smtp_port | 2 -- 3 files changed, 6 insertions(+), 4 deletions(-) delete mode 100644 changes/better_charset_handling delete mode 100644 changes/return_smtp_port diff --git a/CHANGELOG b/CHANGELOG index fb62efd..45f1a7f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,9 @@ +0.3.4 Oct 4: + o Improve charset handling when exposing mails to the mail + client. Related to #3660. + o Return Twisted's smtp Port object to be able to stop listening to + it whenever we want. Related to #3873. + 0.3.3 Sep 20: o Remove cleartext mail from logs. Closes: #3877. diff --git a/changes/better_charset_handling b/changes/better_charset_handling deleted file mode 100644 index 832f7b2..0000000 --- a/changes/better_charset_handling +++ /dev/null @@ -1,2 +0,0 @@ - o Improve charset handling when exposing mails to the mail client. Related to - #3660. diff --git a/changes/return_smtp_port b/changes/return_smtp_port deleted file mode 100644 index 1cf58ef..0000000 --- a/changes/return_smtp_port +++ /dev/null @@ -1,2 +0,0 @@ - o Return Twisted's smtp Port object to be able to stop listening to - it whenever we want. Related to #3873. \ No newline at end of file -- cgit v1.2.3