Skip to content

Commit 8fed049

Browse files
author
Richard Jones
committed
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)
1 parent cbfad5f commit 8fed049

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

roundup/mailgw.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class node. Any parts of other types are each stored in separate files
8181
import time, random, sys, logging
8282
import traceback, MimeWriter, rfc822
8383

84+
from email.Header import decode_header
85+
8486
from roundup import configuration, hyperdb, date, password, rfc2822, exceptions
8587
from roundup.mailer import Mailer, MessageSendError
8688
from roundup.i18n import _
@@ -261,9 +263,30 @@ def getparts(self):
261263

262264
def getheader(self, name, default=None):
263265
hdr = mimetools.Message.getheader(self, name, default)
266+
if not hdr:
267+
return ''
264268
if hdr:
265269
hdr = hdr.replace('\n','') # Inserted by rfc822.readheaders
266-
return rfc2822.decode_header(hdr)
270+
# historically this method has returned utf-8 encoded string
271+
l = []
272+
for part, encoding in decode_header(hdr):
273+
if encoding:
274+
part = part.decode(encoding)
275+
l.append(part)
276+
return ''.join([s.encode('utf-8') for s in l])
277+
278+
def getaddrlist(self, name):
279+
# overload to decode the name part of the address
280+
l = []
281+
for (name, addr) in mimetools.Message.getaddrlist(self, name):
282+
p = []
283+
for part, encoding in decode_header(name):
284+
if encoding:
285+
part = part.decode(encoding)
286+
p.append(part)
287+
name = ''.join([s.encode('utf-8') for s in p])
288+
l.append((name, addr))
289+
return l
267290

268291
def getname(self):
269292
"""Find an appropriate name for this message."""

test/test_mailgw.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- encoding: utf-8 -*-
12
#
23
# Copyright (c) 2001 Richard Jones, [email protected].
34
# This module is free software, and you may redistribute it and/or modify
@@ -23,6 +24,7 @@
2324
from roundup.mailgw import MailGW, Unauthorized, uidFromAddress, \
2425
parseContent, IgnoreLoop, IgnoreBulk, MailUsageError, MailUsageHelp
2526
from roundup import init, instance, password, rfc2822, __version__
27+
from roundup.anypy.sets_ import set
2628

2729
import db_test_base
2830

@@ -1062,6 +1064,29 @@ def testNewUserAuthor(self):
10621064
m.sort()
10631065
self.assertNotEqual(l, m)
10641066

1067+
def testNewUserAuthorHighBit(self):
1068+
l = set(self.db.user.list())
1069+
# From: name has Euro symbol in it
1070+
message = '''Content-Type: text/plain;
1071+
charset="iso-8859-1"
1072+
From: =?utf8?b?SOKCrGxsbw==?= <[email protected]>
1073+
1074+
Message-Id: <dummy_test_message_id>
1075+
Subject: [issue] Testing...
1076+
1077+
This is a test submission of a new issue.
1078+
'''
1079+
p = [
1080+
self.db.security.getPermission('Create', 'user'),
1081+
self.db.security.getPermission('Email Access', None),
1082+
]
1083+
self.db.security.role['anonymous'].permissions=p
1084+
self._handle_mail(message)
1085+
m = set(self.db.user.list())
1086+
new = list(m - l)[0]
1087+
name = self.db.user.get(new, 'realname')
1088+
self.assertEquals(name, 'H€llo')
1089+
10651090
def testEnc01(self):
10661091
self.doNewIssue()
10671092
self._handle_mail('''Content-Type: text/plain;
@@ -1102,6 +1127,53 @@ def testEnc01(self):
11021127
----------
11031128
status: unread -> chatting
11041129
1130+
_______________________________________________________________________
1131+
Roundup issue tracker <[email protected]>
1132+
<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>
1133+
_______________________________________________________________________
1134+
''')
1135+
1136+
def testEncNonUTF8(self):
1137+
self.doNewIssue()
1138+
self.instance.config.EMAIL_CHARSET = 'iso-8859-1'
1139+
self._handle_mail('''Content-Type: text/plain;
1140+
charset="iso-8859-1"
1141+
From: mary <[email protected]>
1142+
1143+
Message-Id: <followup_dummy_id>
1144+
In-Reply-To: <dummy_test_message_id>
1145+
Subject: [issue1] Testing...
1146+
Content-Type: text/plain;
1147+
charset="iso-8859-1"
1148+
Content-Transfer-Encoding: quoted-printable
1149+
1150+
A message with encoding (encoded oe =F6)
1151+
1152+
''')
1153+
self.compareMessages(self._get_mail(),
1154+
1155+
1156+
Content-Type: text/plain; charset="iso-8859-1"
1157+
Subject: [issue1] Testing...
1158+
1159+
From: "Contrary, Mary" <[email protected]>
1160+
Reply-To: Roundup issue tracker <[email protected]>
1161+
MIME-Version: 1.0
1162+
Message-Id: <followup_dummy_id>
1163+
In-Reply-To: <dummy_test_message_id>
1164+
X-Roundup-Name: Roundup issue tracker
1165+
X-Roundup-Loop: hello
1166+
X-Roundup-Issue-Status: chatting
1167+
Content-Transfer-Encoding: quoted-printable
1168+
1169+
1170+
Contrary, Mary <[email protected]> added the comment:
1171+
1172+
A message with encoding (encoded oe =F6)
1173+
1174+
----------
1175+
status: unread -> chatting
1176+
11051177
_______________________________________________________________________
11061178
Roundup issue tracker <[email protected]>
11071179
<http://tracker.example/cgi-bin/roundup.cgi/bugs/issue1>

0 commit comments

Comments
 (0)