Skip to content
Snippets Groups Projects
Commit 0028c421 authored by Thibault Delavallée's avatar Thibault Delavallée
Browse files

[CLEAN] mail: clean mail gateway code

The purpose of this commit is to clean a bit the code related to email
routing in the mailgateway: message_route and message_route_verify.

message_route is now clearly separeted into two categories. First one
handles emails considered as answers to existing threads. Second one
handles emails that have to create new threads, either through alias or
default behavior of the mailgateway.

message_route_verify has been a bit facotrized to simplify the various
overrides that appeared lately in the codebase.

This commit only contains code cleaning. No functional change should occur
due to this commit.
parent b259eb69
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
import logging
from odoo import api, models
from odoo import api, models, _
from odoo.tools import decode_message_header, email_split
_logger = logging.getLogger(__name__)
......@@ -13,35 +13,24 @@ class MailThread(models.AbstractModel):
_inherit = "mail.thread"
@api.model
def message_route_verify(self, message, message_dict, route, update_author=True, assert_model=True, create_fallback=True, allow_private=False):
res = super(MailThread, self).message_route_verify(message, message_dict, route, update_author, assert_model, create_fallback, allow_private)
def message_route_verify(self, message, message_dict, route,
update_author=True, assert_model=True,
create_fallback=True, allow_private=False,
drop_alias=False):
res = super(MailThread, self).message_route_verify(
message, message_dict, route,
update_author=update_author,
assert_model=assert_model,
create_fallback=create_fallback,
allow_private=allow_private,
drop_alias=drop_alias)
if res:
alias = route[4]
email_from = decode_message_header(message, 'From')
message_id = message.get('Message-Id')
# Identically equal to the definition in mail module because sub methods are local
# variables and cannot be called with super
def _create_bounce_email(body_html):
bounce_to = decode_message_header(message, 'Return-Path') or email_from
bounce_mail_values = {
'body_html': body_html,
'subject': 'Re: %s' % message.get('subject'),
'email_to': bounce_to,
'auto_delete': True,
}
bounce_from = self.env['ir.mail_server']._get_default_bounce_address()
if bounce_from:
bounce_mail_values['email_from'] = 'MAILER-DAEMON <%s>' % bounce_from
self.env['mail.mail'].create(bounce_mail_values).send()
def _warn(message):
_logger.info('Routing mail with Message-Id %s: route %s: %s',
message_id, route, message)
# Alias: check alias_contact settings for employees
if alias and alias.alias_contact == 'employees':
email_address = email_split(email_from)[0]
employee = self.env['hr.employee'].search([('work_email', 'ilike', email_address)], limit=1)
......@@ -49,7 +38,7 @@ class MailThread(models.AbstractModel):
employee = self.env['hr.employee'].search([('user_id.email', 'ilike', email_address)], limit=1)
if not employee:
mail_template = self.env.ref('hr.mail_template_data_unknown_employee_email_address')
_warn('alias %s does not accept unknown employees, skipping' % alias.alias_name)
_create_bounce_email(mail_template.body_html)
self._routing_warn(_('alias %s does not accept unknown employees') % alias.alias_name, _('skipping'), message_id, route, False)
self._routing_create_bounce_email(email_from, mail_template.body_html, message)
return False
return res
This diff is collapsed.
......@@ -413,6 +413,7 @@ command_re = re.compile("^Set-([a-z]+) *: *(.+)$", re.I + re.UNICODE)
# Typical form of references is <timestamp-openerp-record_id-model_name@domain>
# 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)
discussion_re = re.compile("<.*-open(?:object|erp)-private[^>]*@([^>]*)>", re.UNICODE)
mail_header_msgid_re = re.compile('<[^<>]+>')
......@@ -494,14 +495,18 @@ def email_split_and_format(text):
if '@' in addr[1]]
def email_references(references):
ref_match, model, thread_id, hostname = False, False, False, False
ref_match, model, thread_id, hostname, is_private = False, 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)
else:
ref_match = discussion_re.search(references)
if ref_match:
is_private = True
return (ref_match, model, thread_id, hostname, is_private)
# was mail_message.decode()
def decode_smtp_header(smtp_header):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment