summaryrefslogtreecommitdiff
path: root/openerp
diff options
context:
space:
mode:
authorThibault Delavallée <tde@openerp.com>2015-12-03 12:59:19 +0100
committerThibault Delavallée <tde@openerp.com>2016-09-01 10:41:04 +0200
commitb259eb69d2085a961c1ad00d8bba3cb8a92123c7 (patch)
treefbb7d975b55f290746589f2e6b43ff5cd03f07b0 /openerp
parentea4c4a22c41ec780818ec03ff6801791bda46065 (diff)
[CLEAN] mail, tools: factorize decode email tools
mail.mail and mail.thread have methods to decode headers. This commit move them in mail tools. It is indeed simplier to have all email tools in the same file. Moreover they have been renamed to avoid confusion with standard library methods: decode and decode_header already exist. * decode in mail_message is now decode_smtp_header * decode_header in mail_thread is now decode_message_header A method to data from references is also moved into tools. A regex was already defined in tools. Now the method using it is already put in tools. Mailgateway will now simply call this method.
Diffstat (limited to 'openerp')
-rw-r--r--openerp/tools/mail.py38
1 files changed, 35 insertions, 3 deletions
diff --git a/openerp/tools/mail.py b/openerp/tools/mail.py
index 1f8e8d6f67f..3c9953677fd 100644
--- a/openerp/tools/mail.py
+++ b/openerp/tools/mail.py
@@ -1,21 +1,21 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from lxml import etree
import cgi
import logging
-import lxml.html
import lxml.html.clean as clean
import random
import re
import socket
import threading
import time
+
+from email.header import decode_header
from email.utils import getaddresses, formataddr
+from lxml import etree
import openerp
from openerp.loglevels import ustr
-from openerp.tools.translate import _
_logger = logging.getLogger(__name__)
@@ -403,7 +403,10 @@ email_re = re.compile(r"""([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63})""",
# matches a string containing only one email
single_email_re = re.compile(r"""^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$""", re.VERBOSE)
+# legacy behavior: match [ID] in the Subject, used with the fallback model of the mail gateway
res_re = re.compile(r"\[([0-9]+)\]", re.UNICODE)
+
+# update command in emails body
command_re = re.compile("^Set-([a-z]+) *: *(.+)$", re.I + re.UNICODE)
# Updated in 7.0 to match the model name as well
@@ -411,6 +414,8 @@ command_re = re.compile("^Set-([a-z]+) *: *(.+)$", re.I + re.UNICODE)
# group(1) = the record ID ; group(2) = the model (if any) ; group(3) = the domain
reference_re = re.compile("<.*-open(?:object|erp)-(\\d+)(?:-([\w.]+))?[^>]*@([^>]*)>", re.UNICODE)
+mail_header_msgid_re = re.compile('<[^<>]+>')
+
def generate_tracking_message_id(res_id):
"""Returns a string that can be used in the Message-ID RFC822 header field
@@ -487,3 +492,30 @@ def email_split_and_format(text):
# is strictly required in RFC2822's `addr-spec`.
if addr[1]
if '@' in addr[1]]
+
+def email_references(references):
+ ref_match, model, thread_id, hostname = False, False, False, False
+ if references:
+ ref_match = reference_re.search(references)
+ if ref_match:
+ model = ref_match.group(2)
+ thread_id = int(ref_match.group(1))
+ hostname = ref_match.group(3)
+ return (ref_match, model, thread_id, hostname)
+
+# was mail_message.decode()
+def decode_smtp_header(smtp_header):
+ """Returns unicode() string conversion of the given encoded smtp header
+ text. email.header decode_header method return a decoded string and its
+ charset for each decoded par of the header. This method unicodes the
+ decoded header and join them in a complete string. """
+ if smtp_header:
+ text = decode_header(smtp_header.replace('\r', ''))
+ # The joining space will not be needed as of Python 3.3
+ # See https://hg.python.org/cpython/rev/8c03fe231877
+ return ' '.join([ustr(x[0], x[1]) for x in text])
+ return u''
+
+# was mail_thread.decode_header()
+def decode_message_header(message, header, separator=' '):
+ return separator.join(map(decode_smtp_header, filter(None, message.get_all(header, []))))