From 330a989452c93d391e5e83b6b3cd24cb38fb8a43 Mon Sep 17 00:00:00 2001 From: Stephane Blondon Date: Sat, 28 Jul 2018 12:45:19 +0200 Subject: [PATCH] Remove the minus in charset if it's not found to fix Lookup error when searching the appropriate codec --- imbox/parser.py | 2 ++ tests/parser_tests.py | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/imbox/parser.py b/imbox/parser.py index 3351452..189d664 100644 --- a/imbox/parser.py +++ b/imbox/parser.py @@ -126,6 +126,8 @@ def decode_content(message): charset = message.get_content_charset('utf-8') try: return content.decode(charset, 'ignore') + except LookupError: + return content.decode(charset.replace("-", ""), 'ignore') except AttributeError: return content diff --git a/tests/parser_tests.py b/tests/parser_tests.py index 73f8de0..0eb5ea7 100644 --- a/tests/parser_tests.py +++ b/tests/parser_tests.py @@ -274,6 +274,44 @@ R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw== --____NOIBTUQXSYRVOOAFLCHY____-- """ +raw_email_encoded_encoding_charset_contains_a_minus = b"""Delivered-To: +Return-Path: +Message-ID: <74836CF6FF9B1965927DE7EE8A087483@NXOFGRQFQW2> +From: +To: +Subject: Salut, mon cher. +Date: 30 May 2018 22:47:37 +0200 +MIME-Version: 1.0 +Content-Type: multipart/alternative; + boundary="----=_NextPart_000_0038_01D3F85C.02934C4A" + +------=_NextPart_000_0038_01D3F85C.02934C4A +Content-Type: text/plain; + charset="cp-850" +Content-Transfer-Encoding: quoted-printable + +spam here + + +cliquez ici +------=_NextPart_000_0038_01D3F85C.02934C4A +Content-Type: text/html; + charset="cp-850" +Content-Transfer-Encoding: quoted-printable + + + + + + + + +spam here
+
+cliquez = +ici
+------=_NextPart_000_0038_01D3F85C.02934C4A-- +""" class TestParser(unittest.TestCase): @@ -324,6 +362,12 @@ class TestParser(unittest.TestCase): self.assertEqual('abc.xyz', attachment['filename']) self.assertTrue(attachment['content']) + def test_parse_email_accept_if_declared_charset_contains_a_minus_character(self): + parsed_email = parse_email(raw_email_encoded_encoding_charset_contains_a_minus) + self.assertEqual("Salut, mon cher.", parsed_email.subject) + self.assertTrue(parsed_email.body['plain']) + self.assertTrue(parsed_email.body['html']) + # TODO - Complete the test suite def test_decode_mail_header(self): pass