summaryrefslogtreecommitdiff
path: root/src/leap/mail/imap/messages.py
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2014-01-22 11:03:58 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2014-01-22 11:03:58 -0300
commit61af338b0dee8a56cd0f302502fe7cd9dc8bc5d1 (patch)
tree7934002e2f76018713dab0a0515f36fdfc71e9e5 /src/leap/mail/imap/messages.py
parentc2e052a08789057d550a0442caa28b27ebc4b416 (diff)
Handle non-ascii headers. Closes #5021.
Diffstat (limited to 'src/leap/mail/imap/messages.py')
-rw-r--r--src/leap/mail/imap/messages.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/leap/mail/imap/messages.py b/src/leap/mail/imap/messages.py
index 862a9f2..5bb5f1c 100644
--- a/src/leap/mail/imap/messages.py
+++ b/src/leap/mail/imap/messages.py
@@ -605,18 +605,26 @@ class LeapMessage(fields, MailParser, MBoxParser):
if isinstance(headers, list):
headers = dict(headers)
+ # default to most likely standard
+ charset = find_charset(headers, "utf-8")
+
# twisted imap server expects *some* headers to be lowercase
# XXX refactor together with MessagePart method
- headers = dict(
- (str(key), str(value)) if key.lower() != "content-type"
- else (str(key.lower()), str(value))
- for (key, value) in headers.items())
+ headers2 = dict()
+ for key, value in headers.items():
+ if key.lower() == "content-type":
+ key = key.lower()
- # unpack and filter original dict by negate-condition
- filter_by_cond = [(key, val) for key, val
- in headers.items() if cond(key)]
+ if not isinstance(key, str):
+ key = key.encode(charset, 'replace')
+ if not isinstance(value, str):
+ value = value.encode(charset, 'replace')
+
+ # filter original dict by negate-condition
+ if cond(key):
+ headers2[key] = value
- return dict(filter_by_cond)
+ return headers2
def _get_headers(self):
"""