]> git.phdru.name Git - mimedecode.git/commitdiff
Convert body to the current charset for mailcap filter
authorOleg Broytman <phd@phdru.name>
Wed, 3 Aug 2016 15:22:45 +0000 (18:22 +0300)
committerOleg Broytman <phd@phdru.name>
Wed, 3 Aug 2016 15:22:45 +0000 (18:22 +0300)
Convert text body from mail charset to the current locale's charset
before passing it to the mailcap filter.

mimedecode.py

index 25d3db0efe83e1e5288442a79291acec9ca06eca..e7381f78cdb495bc9a1aa765ef4c2ca116098fc1 100755 (executable)
@@ -218,6 +218,10 @@ def decode_body(msg, s):
         caps = mailcap.getcaps()
 
     content_type = msg.get_content_type()
+    if content_type.startswith('text/'):
+        charset = msg.get_content_charset()
+    else:
+        charset = None
     filename = tempfile.mktemp()
     command = None
 
@@ -235,6 +239,8 @@ def decode_body(msg, s):
         return s
 
     outfile = open(filename, 'wb')
+    if charset and isinstance(s, bytes):
+        s = s.decode(charset, "replace")
     if not isinstance(s, bytes):
         s = s.encode(g.default_encoding, "replace")
     outfile.write(s)