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
continue
payload = part.get_payload(decode=True)
- if not isinstance(payload, unicode):
- payload = unicode(payload, part.get_content_charset())
+ charset = part.get_content_charset()
subtype = part.get_content_subtype()
+ # if we don't have a charset, assume utf-8
+ if charset is None:
+ charset = 'utf-8'
+
+ if not isinstance(payload, unicode):
+ payload = unicode(payload, charset)
+
if subtype in ['x-patch', 'x-diff']:
patchbuf = payload
if patch:
cpatch = patch
else:
- cpatch = find_patch_for_comment(mail)
+ cpatch = find_patch_for_comment(project, mail)
if not cpatch:
return (None, None)
comment = Comment(patch = cpatch, date = mail_date(mail),
return (patch, comment)
-def find_patch_for_comment(mail):
+def find_patch_for_comment(project, mail):
# construct a list of possible reply message ids
refs = []
if 'In-Reply-To' in mail:
# first, check for a direct reply
try:
- patch = Patch.objects.get(msgid = ref)
+ patch = Patch.objects.get(project = project, msgid = ref)
return patch
except Patch.DoesNotExist:
pass
# see if we have comments that refer to a patch
try:
- comment = Comment.objects.get(msgid = ref)
+ comment = Comment.objects.get(patch__project = project, msgid = ref)
return comment.patch
except Comment.DoesNotExist:
pass
str = sig_re.sub('', str)
return str.strip()
-def main(args):
- mail = message_from_file(sys.stdin)
+def parse_mail(mail):
# some basic sanity checks
if 'From' not in mail:
return 0
+def main(args):
+ mail = message_from_file(sys.stdin)
+ return parse_mail(mail)
+
if __name__ == '__main__':
sys.exit(main(sys.argv))