diff roundup/mailgw.py @ 4093:1ebab2e397d0

Add some new encoding tests to mailgw: - confirm that non-utf8 encodings work - confirm that non-ASCII headers work (and they didn't but do now) (roundup.rfc2822 is almost entirely gone from Roundup use now)
author Richard Jones <richard@users.sourceforge.net>
date Thu, 12 Mar 2009 06:25:05 +0000
parents 4b0ddce43d08
children 0388a5926974
line wrap: on
line diff
--- a/roundup/mailgw.py	Thu Mar 12 05:55:16 2009 +0000
+++ b/roundup/mailgw.py	Thu Mar 12 06:25:05 2009 +0000
@@ -81,6 +81,8 @@
 import time, random, sys, logging
 import traceback, MimeWriter, rfc822
 
+from email.Header import decode_header
+
 from roundup import configuration, hyperdb, date, password, rfc2822, exceptions
 from roundup.mailer import Mailer, MessageSendError
 from roundup.i18n import _
@@ -261,9 +263,30 @@
 
     def getheader(self, name, default=None):
         hdr = mimetools.Message.getheader(self, name, default)
+        if not hdr:
+            return ''
         if hdr:
             hdr = hdr.replace('\n','') # Inserted by rfc822.readheaders
-        return rfc2822.decode_header(hdr)
+        # 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])
+
+    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])
+            l.append((name, addr))
+        return l
 
     def getname(self):
         """Find an appropriate name for this message."""

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