[#6906] Handle new format and non-post message IDs

The format of the message ID in the In-Reply-To header changed in
[#6328] (commit 734bf8a), as well as possibly referring to the
artifact instead of a parent post.

Signed-off-by: Cory Johns <cjohns@slashdotmedia.com>
diff --git a/Allura/allura/lib/mail_util.py b/Allura/allura/lib/mail_util.py
index 92ca47a..d7d0b4b 100644
--- a/Allura/allura/lib/mail_util.py
+++ b/Allura/allura/lib/mail_util.py
@@ -34,7 +34,7 @@
 
 log = logging.getLogger(__name__)
 
-RE_MESSAGE_ID = re.compile(r'<(.*)>')
+RE_MESSAGE_ID = re.compile(r'<(?:[^>]*/)?([^>]*)>')
 config = ConfigProxy(
     common_suffix='forgemail.domain',
     return_path='forgemail.return_path')
diff --git a/Allura/allura/model/notification.py b/Allura/allura/model/notification.py
index 5da7fd3..5743ffe 100644
--- a/Allura/allura/model/notification.py
+++ b/Allura/allura/model/notification.py
@@ -242,7 +242,7 @@
     @classmethod
     def _references(cls, artifact, post):
         msg_ids = []
-        while post.parent_id:
+        while post and post.parent_id:
             msg_ids.append(artifact.url() + post.parent_id)
             post = post.parent
         msg_ids.append(artifact.message_id())
diff --git a/Allura/allura/tests/test_mail_util.py b/Allura/allura/tests/test_mail_util.py
index 8cd044f..f88b46c 100644
--- a/Allura/allura/tests/test_mail_util.py
+++ b/Allura/allura/tests/test_mail_util.py
@@ -35,6 +35,7 @@
         Header,
         is_autoreply,
         identify_sender,
+        _parse_message_id,
     )
 from allura.lib.exceptions import AddressException
 from allura.tests import decorators as td
@@ -212,3 +213,10 @@
         EA.query.get.side_effect = [None, None]
         assert_equal(identify_sender(None, 'arg', {'From': 'from'}, None), anon)
         assert_equal(EA.query.get.call_args_list, [mock.call(_id='arg'), mock.call(_id='from')])
+
+
+def test_parse_message_id():
+    assert_equal(_parse_message_id('<de31888f6be2d87dc377d9e713876bb514548625.patches@libjpeg-turbo.p.sourceforge.net>, </p/libjpeg-turbo/patches/54/de31888f6be2d87dc377d9e713876bb514548625.patches@libjpeg-turbo.p.sourceforge.net>'), [
+            'de31888f6be2d87dc377d9e713876bb514548625.patches@libjpeg-turbo.p.sourceforge.net',
+            'de31888f6be2d87dc377d9e713876bb514548625.patches@libjpeg-turbo.p.sourceforge.net',
+        ])