diff roundup/mailgw.py @ 4505:a00e0e73bb26

Yet another fix to the mail gateway... ...messages got *all* files of an issue, not just the new ones. Thanks to Rafal Bisingier for reporting and proposing a fix. The regression test was updated. Fix version numbers in upgrade documentation, the file-unlink defect was in 1.4.17 not 1.4.16. Thanks to Rafal Bisingier.
author Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
date Mon, 06 Jun 2011 20:00:17 +0000
parents 9f488541802f
children ddff9669361b
line wrap: on
line diff
--- a/roundup/mailgw.py	Mon Jun 06 07:23:49 2011 +0000
+++ b/roundup/mailgw.py	Mon Jun 06 20:00:17 2011 +0000
@@ -247,6 +247,22 @@
             parts.append(part)
         return parts
 
+    def _decode_header_to_utf8(self, hdr):
+        l = []
+        prev_encoded = False
+        for part, encoding in decode_header(hdr):
+            if encoding:
+                part = part.decode(encoding)
+            # RFC 2047 specifies that between encoded parts spaces are
+            # swallowed while at the borders from encoded to non-encoded
+            # or vice-versa we must preserve a space. Multiple adjacent
+            # non-encoded parts should not occur.
+            if l and prev_encoded != bool(encoding):
+                l.append(' ')
+            prev_encoded = bool(encoding)
+            l.append(part)
+        return ''.join([s.encode('utf-8') for s in l])
+
     def getheader(self, name, default=None):
         hdr = mimetools.Message.getheader(self, name, default)
         # TODO are there any other False values possible?
@@ -257,24 +273,13 @@
             return ''
         if hdr:
             hdr = hdr.replace('\n','') # Inserted by rfc822.readheaders
-        # historically this method has returned utf-8 encoded string
-        l = []
-        for part, encoding in decode_header(hdr):
-            if encoding:
-                part = part.decode(encoding)
-            l.append(part)
-        return ''.join([s.encode('utf-8') for s in l])
+        return self._decode_header_to_utf8(hdr)
 
     def getaddrlist(self, name):
         # overload to decode the name part of the address
         l = []
         for (name, addr) in mimetools.Message.getaddrlist(self, name):
-            p = []
-            for part, encoding in decode_header(name):
-                if encoding:
-                    part = part.decode(encoding)
-                p.append(part)
-            name = ''.join([s.encode('utf-8') for s in p])
+            name = self._decode_header_to_utf8(name)
             l.append((name, addr))
         return l
 

Roundup Issue Tracker: http://roundup-tracker.org/