summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-02-08 14:51:56 +0100
committerAnton Khirnov <anton@khirnov.net>2021-02-08 14:51:56 +0100
commit78d8f8b9147473be5bed287edd64042b3f90210f (patch)
treedd3116fd0003e2526be7675504a66af62c348193
parent5546c658163b7ef64045171c1884ec4cb0c61f06 (diff)
db/message: handle text MIME with invalid charsets
-rw-r--r--alot/db/message.py17
1 files changed, 17 insertions, 0 deletions
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: