diff options
author | Ivan Alejandro <ivanalejandro0@gmail.com> | 2013-10-23 10:18:34 -0300 |
---|---|---|
committer | Ivan Alejandro <ivanalejandro0@gmail.com> | 2013-10-23 10:18:34 -0300 |
commit | f864f21b51b9f767afa8ccb3b3f39967b2f08f60 (patch) | |
tree | 9d1c3e1ab6299c74e617ea1d1c700e3a1aee89fa /src | |
parent | c0834048d564dfdc3e2cedc1a0f81788d14f3cab (diff) |
Add encoding exception catch to avoid crashes.
Diffstat (limited to 'src')
-rw-r--r-- | src/leap/mail/imap/server.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/leap/mail/imap/server.py b/src/leap/mail/imap/server.py index b9a041d..7ae3c45 100644 --- a/src/leap/mail/imap/server.py +++ b/src/leap/mail/imap/server.py @@ -706,7 +706,13 @@ class LeapMessage(WithMsgFields): """ fd = cStringIO.StringIO() charset = get_email_charset(self._doc.content.get(self.RAW_KEY, '')) - fd.write(self._doc.content.get(self.RAW_KEY, '').encode(charset)) + content = self._doc.content.get(self.RAW_KEY, '') + try: + content = content.encode(charset) + except (UnicodeEncodeError, UnicodeDecodeError) as e: + logger.error("Unicode error {0}".format(e)) + content = content.encode(charset, 'replace') + fd.write(content) fd.seek(0) return fd @@ -726,7 +732,13 @@ class LeapMessage(WithMsgFields): """ fd = StringIO.StringIO() charset = get_email_charset(self._doc.content.get(self.RAW_KEY, '')) - fd.write(self._doc.content.get(self.RAW_KEY, '').encode(charset)) + content = self._doc.content.get(self.RAW_KEY, '') + try: + content = content.encode(charset) + except (UnicodeEncodeError, UnicodeDecodeError) as e: + logger.error("Unicode error {0}".format(e)) + content = content.encode(charset, 'replace') + fd.write(content) # SHOULD use a separate BODY FIELD ... fd.seek(0) return fd |