From 78d8f8b9147473be5bed287edd64042b3f90210f Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Mon, 8 Feb 2021 14:51:56 +0100 Subject: db/message: handle text MIME with invalid charsets --- alot/db/message.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/alot/db/message.py b/alot/db/message.py index 29ca11dc..9d5bb958 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -2,6 +2,7 @@ # This file is released under the GNU GPL, version 3 or a later revision. # For further details see the COPYING file +import codecs import email import email.charset as charset import email.policy @@ -125,9 +126,25 @@ class _MimeTree: self.attachment = Attachment(data, self.content_type, fn, part.get_params()) + if self.content_maintype == 'text': + self._fixup_charset() + def __str__(self): return 'MimePart(%s)' % self.content_type + def _fixup_charset(self): + """ + If a text MIME part declares an invalid charset, replace it with UTF-8 + """ + charset = self._part.get_param('charset') + if charset is None: + return + + try: + codecs.lookup(charset) + except LookupError: + self._part.set_param('charset', 'utf-8') + def render_str(self, alt_preference = None): if self.children is not None: if self.is_alternative and len(self.children) > 0: -- cgit v1.2.3