ssoma-mda: Add compatibility decoding for the '\x10\x10' encoding python
authorW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 08:58:34 +0000 (00:58 -0800)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 08:58:34 +0000 (00:58 -0800)
I'm not sure if this was a user error or a Git send-email error, but
notmuch has a message with:

  Subject: =?\x10\x10?q?=5BPATCH=20v7=203/3=5D=20Use=20the=20structured=20formatters=20in=20notmuch-search=2Ec=2E?=
  ...
  Message-Id: <1342766173-1344-4-git-send-email-craven@gmx.net>
  X-Mailer: git-send-email 1.7.11.2

Without this patch, that raises:

  LookupError: unknown encoding:

ssoma-mda

index 484a51daa4b2b6782ce71f6f0fe424c2e3a34aa9..76f22425a234aeb01ded87e6d2481428db2fbfb0 100755 (executable)
--- a/ssoma-mda
+++ b/ssoma-mda
@@ -65,6 +65,10 @@ _LOG.addHandler(_logging.StreamHandler())
 
 _COMMIT_MESSAGE_ENCODING = 'UTF-8'
 _FOLDING_WHITESPACE_REGEX = _re.compile('\s*\n\s*')
+_FALLBACK_ENCODINGS = {  # support non-standard RFC 2047 encodings
+    'no': 'ASCII',
+    '\x10\x10': 'ASCII',
+    }
 
 
 class DirtyIndex(RuntimeError):
@@ -241,8 +245,8 @@ def _decode_header(string):
         if isinstance(decoded, bytes) and not charset:
             charset = 'ASCII'
         if charset:
-            if charset in ['no']:
-                charset = 'ASCII'
+            if charset in _FALLBACK_ENCODINGS:
+                charset = _FALLBACK_ENCODINGS[charset]
             decoded = str(decoded, charset)
         chunks.append(decoded)
     return ''.join(chunks)