diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2011-10-21 22:10:39 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2011-10-21 22:10:39 +0100 |
commit | f75891da914f824cd23bbad897304e2221986f43 (patch) | |
tree | e25697898bab49df1a88b44f13a4f3b78c898729 | |
parent | 0192c31e54eb0ea9ddaa7170100e372b6d8cdc20 (diff) |
properly decode and prefer plaintext parts
-rw-r--r-- | alot/commands/thread.py | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/alot/commands/thread.py b/alot/commands/thread.py index 08dbb8e9..b4729971 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -6,6 +6,7 @@ from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.iterators import body_line_iterator +from email.iterators import typed_subpart_iterator from twisted.internet import defer from alot.commands import Command, registerCommand @@ -289,13 +290,26 @@ class PipeCommand(Command): mailstrings = [] if self.decode: for mail in mails: - #displayed = settings.config.getstringlist('general', 'displayed_headers') - displayed = mail.keys() - headertext = extract_headers(mail, displayed) - bodytext = ''.join(body_line_iterator(mail, True)) - - #TODO: add flag to exclude html - mailstrings.append('%s\n\n%s' % (headertext, bodytext)) + headertext = extract_headers(mail) + + html = list(typed_subpart_iterator(mail, 'text', 'html')) + plain = list(typed_subpart_iterator(mail, 'text', 'plain')) + + # body + partsstrings = [] + # only take html parts if no plaintext alternative exists + parts = plain + if html and not plain: + parts = html + + for part in parts: + enc = part.get_content_charset() or 'ascii' + partstring = ''.join(body_line_iterator(part, True)) + decoded = unicode(partstring, enc, errors='replace') + partsstrings.append(decoded) + bodytext = '\n\n'.join(partsstrings) + msg = '%s\n\n %s' % (headertext, bodytext) + mailstrings.append(msg.encode('utf-8')) else: mailstrings = [e.as_string() for e in mails] if not self.separately: |