[FIX] mail.thread: avoid overwriting thread_id/model with foreign values when reply...
authorOlivier Dony <odo@openerp.com>
Fri, 11 Apr 2014 14:24:29 +0000 (16:24 +0200)
committerOlivier Dony <odo@openerp.com>
Fri, 11 Apr 2014 14:24:29 +0000 (16:24 +0200)
This improve previous commit by making sure we
never consider the thread_id/model values in the
In-Reply-To/References header if the host name
did not match.
Also fixes the tests that were using the
6.1 compatibility mode to post in a mail group
thread instead of specifying the right
message-id.

bzr revid: odo@openerp.com-20140411142429-y0rpkzqbrsabxqsg

addons/mail/mail_thread.py
addons/mail/tests/test_mail_gateway.py

index 97f09f1..83f3693 100644 (file)
@@ -538,18 +538,19 @@ class mail_thread(osv.AbstractModel):
         thread_references = references or in_reply_to
         ref_match = thread_references and tools.reference_re.search(thread_references)
         if ref_match:
-            thread_id = int(ref_match.group(1))
-            model = ref_match.group(2) or model
-            our_hostname = socket.gethostname()
-            reply_to_hostname = ref_match.group(3)
+            reply_thread_id = int(ref_match.group(1))
+            reply_model = ref_match.group(2) or model
+            reply_hostname = ref_match.group(3)
+            local_hostname = socket.gethostname()
             # do not match forwarded emails from another OpenERP system (thread_id collision!)
-            is_reply_to_local = (our_hostname == reply_to_hostname)
-            model_pool = self.pool.get(model)
-            if thread_id and is_reply_to_local and model and model_pool and model_pool.exists(cr, uid, thread_id) \
-                and hasattr(model_pool, 'message_update'):
-                _logger.info('Routing mail from %s to %s with Message-Id %s: direct reply to model: %s, thread_id: %s, custom_values: %s, uid: %s',
-                                email_from, email_to, message_id, model, thread_id, custom_values, uid)
-                return [(model, thread_id, custom_values, uid)]
+            if local_hostname == reply_hostname:
+                thread_id, model = reply_thread_id, reply_model
+                model_pool = self.pool.get(model)
+                if thread_id and model and model_pool and model_pool.exists(cr, uid, thread_id) \
+                    and hasattr(model_pool, 'message_update'):
+                    _logger.info('Routing mail from %s to %s with Message-Id %s: direct reply to model: %s, thread_id: %s, custom_values: %s, uid: %s',
+                                    email_from, email_to, message_id, model, thread_id, custom_values, uid)
+                    return [(model, thread_id, custom_values, uid)]
 
         # Verify whether this is a reply to a private message
         if in_reply_to:
index e7a2206..bc2f207 100644 (file)
@@ -318,11 +318,11 @@ class TestMailgateway(TestMailBase):
         # Test2: discussion update
         # --------------------------------------------------
 
-        # Do: even with a wrong destination, a reply should end up in the correct thread
+        # Do: even with a wrong destination, a reply should end up in the correct thread 
         frog_groups = format_and_process(MAIL_TEMPLATE, email_from='other@gmail.com',
                                             msg_id='<1198923581.41972151344608186760.JavaMail.diff1@agrolait.com>',
                                             to='erroneous@example.com>', subject='Re: news',
-                                            extra='In-Reply-To: <12321321-openerp-%d-mail.group@example.com>\n' % frog_group.id)
+                                            extra='In-Reply-To: <1198923581.41972151344608186760.JavaMail@agrolait.com>\n')
         # Test: no group 'Re: news' created, still only 1 Frogs group
         self.assertEqual(len(frog_groups), 0,
                             'message_process: reply on Frogs should not have created a new group with new subject')
@@ -339,8 +339,8 @@ class TestMailgateway(TestMailBase):
 
         # Do: due to some issue, same email goes back into the mailgateway
         frog_groups = format_and_process(MAIL_TEMPLATE, email_from='other@gmail.com',
-                                            msg_id='<1198923581.41972151344608186760.JavaMail.diff1@agrolait.com>',
-                                            subject='Re: news', extra='In-Reply-To: <12321321-openerp-%d-mail.group@example.com>\n' % frog_group.id)
+                                            to='erroneous@example.com>', subject='Re: news',
+                                            extra='In-Reply-To: <1198923581.41972151344608186760.JavaMail@agrolait.com>\n')
         # Test: no group 'Re: news' created, still only 1 Frogs group
         self.assertEqual(len(frog_groups), 0,
                             'message_process: reply on Frogs should not have created a new group with new subject')
@@ -366,28 +366,28 @@ class TestMailgateway(TestMailBase):
 
         # Do: post a new message, with a known partner -> duplicate emails -> partner
         format_and_process(MAIL_TEMPLATE, email_from='Lombrik Lubrik <test_raoul@email.com>',
-                                            to='erroneous@example.com>', subject='Re: news (2)',
-                                            msg_id='<1198923581.41972151344608186760.JavaMail.new1@agrolait.com>',
-                                            extra='In-Reply-To: <12321321-openerp-%d-mail.group@example.com>\n' % frog_group.id)
+                           subject='Re: news (2)',
+                           msg_id='<1198923581.41972151344608186760.JavaMail.new1@agrolait.com>',
+                           extra='In-Reply-To: <1198923581.41972151344608186760.JavaMail@agrolait.com>\n')
         frog_groups = self.mail_group.search(cr, uid, [('name', '=', 'Frogs')])
         frog_group = self.mail_group.browse(cr, uid, frog_groups[0])
         # Test: author is A-Raoul (only existing)
         self.assertEqual(frog_group.message_ids[0].author_id.id, extra_partner_id,
-                            'message_process: email_from -> author_id wrong')
+                         'message_process: email_from -> author_id wrong')
 
         # Do: post a new message, with a known partner -> duplicate emails -> user
         frog_group.message_unsubscribe([extra_partner_id])
         raoul_email = self.user_raoul.email
         self.res_users.write(cr, uid, self.user_raoul_id, {'email': 'test_raoul@email.com'})
         format_and_process(MAIL_TEMPLATE, email_from='Lombrik Lubrik <test_raoul@email.com>',
-                                            to='erroneous@example.com>', subject='Re: news (3)',
-                                            msg_id='<1198923581.41972151344608186760.JavaMail.new2@agrolait.com>',
-                                            extra='In-Reply-To: <12321321-openerp-%d-mail.group@example.com>\n' % frog_group.id)
+                           to='groups@example.com', subject='Re: news (3)',
+                           msg_id='<1198923581.41972151344608186760.JavaMail.new2@agrolait.com>',
+                           extra='In-Reply-To: <1198923581.41972151344608186760.JavaMail@agrolait.com>\n')
         frog_groups = self.mail_group.search(cr, uid, [('name', '=', 'Frogs')])
         frog_group = self.mail_group.browse(cr, uid, frog_groups[0])
         # Test: author is Raoul (user), not A-Raoul
         self.assertEqual(frog_group.message_ids[0].author_id.id, self.partner_raoul_id,
-                            'message_process: email_from -> author_id wrong')
+                         'message_process: email_from -> author_id wrong')
 
         # Do: post a new message, with a known partner -> duplicate emails -> partner because is follower
         frog_group.message_unsubscribe([self.partner_raoul_id])
@@ -395,14 +395,14 @@ class TestMailgateway(TestMailBase):
         raoul_email = self.user_raoul.email
         self.res_users.write(cr, uid, self.user_raoul_id, {'email': 'test_raoul@email.com'})
         format_and_process(MAIL_TEMPLATE, email_from='Lombrik Lubrik <test_raoul@email.com>',
-                                            to='erroneous@example.com>', subject='Re: news (3)',
-                                            msg_id='<1198923581.41972151344608186760.JavaMail.new3@agrolait.com>',
-                                            extra='In-Reply-To: <12321321-openerp-%d-mail.group@example.com>\n' % frog_group.id)
+                           to='groups@example.com', subject='Re: news (3)',
+                           msg_id='<1198923581.41972151344608186760.JavaMail.new3@agrolait.com>',
+                           extra='In-Reply-To: <1198923581.41972151344608186760.JavaMail@agrolait.com>\n')
         frog_groups = self.mail_group.search(cr, uid, [('name', '=', 'Frogs')])
         frog_group = self.mail_group.browse(cr, uid, frog_groups[0])
         # Test: author is Raoul (user), not A-Raoul
         self.assertEqual(frog_group.message_ids[0].author_id.id, extra_partner_id,
-                            'message_process: email_from -> author_id wrong')
+                         'message_process: email_from -> author_id wrong')
 
         self.res_users.write(cr, uid, self.user_raoul_id, {'email': raoul_email})