[FIX] mail: more robust parsing of In-Reply-To/References (OPW 608919)

When parsing incoming messages, ignore white-space around
In-Reply-To headers, and extract message-id items inside
the References header using a regex.
This actually serves as a workaround for broken MTAs
mangling References (such as outlook.com nesting past ones
with commas, violating RFC2822).

Closes #516 as a manual rebase.
This commit is contained in:
Anael Closson
2014-06-18 14:39:37 +02:00
committed by Olivier Dony
parent 481c64e077
commit 4bad513d29
+6 -3
View File
@@ -33,6 +33,7 @@ import pytz
import socket import socket
import time import time
import xmlrpclib import xmlrpclib
import re
from email.message import Message from email.message import Message
from openerp import tools from openerp import tools
@@ -46,6 +47,8 @@ from openerp.tools.translate import _
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
mail_header_msgid_re = re.compile('<[^<>]+>')
def decode_header(message, header, separator=' '): def decode_header(message, header, separator=' '):
return separator.join(map(decode, filter(None, message.get_all(header, [])))) return separator.join(map(decode, filter(None, message.get_all(header, []))))
@@ -1259,13 +1262,13 @@ class mail_thread(osv.AbstractModel):
msg_dict['date'] = stored_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT) msg_dict['date'] = stored_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
if message.get('In-Reply-To'): if message.get('In-Reply-To'):
parent_ids = self.pool.get('mail.message').search(cr, uid, [('message_id', '=', decode(message['In-Reply-To']))]) parent_ids = self.pool.get('mail.message').search(cr, uid, [('message_id', '=', decode(message['In-Reply-To'].strip()))])
if parent_ids: if parent_ids:
msg_dict['parent_id'] = parent_ids[0] msg_dict['parent_id'] = parent_ids[0]
if message.get('References') and 'parent_id' not in msg_dict: if message.get('References') and 'parent_id' not in msg_dict:
parent_ids = self.pool.get('mail.message').search(cr, uid, [('message_id', 'in', msg_list = mail_header_msgid_re.findall(decode(message['References']))
[x.strip() for x in decode(message['References']).split()])]) parent_ids = self.pool.get('mail.message').search(cr, uid, [('message_id', 'in', [x.strip() for x in msg_list])])
if parent_ids: if parent_ids:
msg_dict['parent_id'] = parent_ids[0] msg_dict['parent_id'] = parent_ids[0]