From 7d9334e879857f8a380bc9509b6cbf9972cecc25 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Sat, 1 Aug 2009 16:05:37 +1000 Subject: [PATCH] parser: allow short-form List-Id headers Some lists (eg, nongnu.org) have short-form List-Id headers. For example: List-Id: qemu-devel.nongnu.org Although RFC 2919 prescribes that: list-id-header = "List-ID:" [phrase] "<" list-id ">" CRLF we should allow for these headers anyway. Based on an original patch from "J.H." . Signed-off-by: Jeremy Kerr --- apps/patchwork/bin/parsemail.py | 10 ++++-- apps/patchwork/tests/patchparser.py | 49 ++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/apps/patchwork/bin/parsemail.py b/apps/patchwork/bin/parsemail.py index 7f6727f..68bd94c 100755 --- a/apps/patchwork/bin/parsemail.py +++ b/apps/patchwork/bin/parsemail.py @@ -57,11 +57,17 @@ def clean_header(header): def find_project(mail): project = None - listid_re = re.compile('.*<([^>]+)>.*', re.S) + listid_res = [re.compile('.*<([^>]+)>.*', re.S), + re.compile('^([\S]+)$', re.S)] for header in list_id_headers: if header in mail: - match = listid_re.match(mail.get(header)) + + for listid_re in listid_res: + match = listid_re.match(mail.get(header)) + if match: + break + if not match: continue diff --git a/apps/patchwork/tests/patchparser.py b/apps/patchwork/tests/patchparser.py index 0fad67b..f6909ce 100644 --- a/apps/patchwork/tests/patchparser.py +++ b/apps/patchwork/tests/patchparser.py @@ -34,7 +34,8 @@ class PatchTest(unittest.TestCase): default_subject = defaults.subject project = defaults.project -from patchwork.bin.parsemail import find_content, find_author, parse_mail +from patchwork.bin.parsemail import find_content, find_author, find_project, \ + parse_mail class InlinePatchTest(PatchTest): patch_filename = '0001-add-line.patch' @@ -275,3 +276,49 @@ class MultipleProjectPatchCommentTest(MultipleProjectPatchTest): # and the one we parsed in setUp() self.assertEquals(Comment.objects.filter(patch = patch).count(), 2) +class ListIdHeaderTest(unittest.TestCase): + """ Test that we parse List-Id headers from mails correctly """ + def setUp(self): + self.project = Project(linkname = 'test-project-1', name = 'Project 1', + listid = '1.example.com', listemail='1@example.com') + self.project.save() + + def testNoListId(self): + email = MIMEText('') + project = find_project(email) + self.assertEquals(project, None) + + def testBlankListId(self): + email = MIMEText('') + email['List-Id'] = '' + project = find_project(email) + self.assertEquals(project, None) + + def testWhitespaceListId(self): + email = MIMEText('') + email['List-Id'] = ' ' + project = find_project(email) + self.assertEquals(project, None) + + def testSubstringListId(self): + email = MIMEText('') + email['List-Id'] = 'example.com' + project = find_project(email) + self.assertEquals(project, None) + + def testShortListId(self): + """ Some mailing lists have List-Id headers in short formats, where it + is only the list ID itself (without enclosing angle-brackets). """ + email = MIMEText('') + email['List-Id'] = self.project.listid + project = find_project(email) + self.assertEquals(project, self.project) + + def testLongListId(self): + email = MIMEText('') + email['List-Id'] = 'Test text <%s>' % self.project.listid + project = find_project(email) + self.assertEquals(project, self.project) + + def tearDown(self): + self.project.delete() -- 2.39.2