1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>)
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>
20 ##############################################################################
24 from openerp import tools
25 from openerp.addons.mail.mail_message import decode
26 from openerp.addons.mail.mail_thread import decode_header
27 from openerp.osv import osv
29 _logger = logging.getLogger(__name__)
32 class MailThread(osv.Model):
33 """ Update MailThread to add the feature of bounced emails and replied emails
34 in message_process. """
36 _inherit = ['mail.thread']
38 def message_route_check_bounce(self, cr, uid, message, context=None):
39 """ Override to verify that the email_to is the bounce alias. If it is the
40 case, log the bounce, set the parent and related document as bounced and
41 return False to end the routing process. """
42 bounce_alias = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.bounce.alias", context=context)
43 message_id = message.get('Message-Id')
44 email_from = decode_header(message, 'From')
45 email_to = decode_header(message, 'To')
47 # 0. Verify whether this is a bounced email (wrong destination,...) -> use it to collect data, such as dead leads
48 if bounce_alias in email_to:
49 bounce_match = tools.bounce_re.search(email_to)
51 bounced_mail_id = bounce_match.group(1)
52 stat_ids = self.pool['mail.mail.statistics'].set_bounced(cr, uid, mail_mail_ids=[bounced_mail_id], context=context)
53 for stat in self.pool['mail.mail.statistics'].browse(cr, uid, stat_ids, context=context):
54 bounced_model = stat.model
55 bounced_thread_id = stat.res_id
56 _logger.info('Routing mail from %s to %s with Message-Id %s: bounced mail from mail %s, model: %s, thread_id: %s',
57 email_from, email_to, message_id, bounced_mail_id, bounced_model, bounced_thread_id)
58 if bounced_model and bounced_model in self.pool and hasattr(self.pool[bounced_model], 'message_receive_bounce'):
59 self.pool[bounced_model].message_receive_bounce(cr, uid, [bounced_thread_id], mail_id=bounced_mail_id, context=context)
64 def message_route(self, cr, uid, message, message_dict, model=None, thread_id=None,
65 custom_values=None, context=None):
66 if not self.message_route_check_bounce(cr, uid, message, context=context):
68 return super(MailThread, self).message_route(cr, uid, message, message_dict, model, thread_id, custom_values, context)
70 def message_receive_bounce(self, cr, uid, ids, mail_id=None, context=None):
71 """Called by ``message_process`` when a bounce email (such as Undelivered
72 Mail Returned to Sender) is received for an existing thread. The default
73 behavior is to check is an integer ``message_bounce`` column exists.
74 If it is the case, its content is incremented. """
75 if self._all_columns.get('message_bounce'):
76 for obj in self.browse(cr, uid, ids, context=context):
77 self.write(cr, uid, [obj.id], {'message_bounce': obj.message_bounce + 1}, context=context)
79 def message_route_process(self, cr, uid, message, message_dict, routes, context=None):
80 """ Override to update the parent mail statistics. The parent is found
81 by using the References header of the incoming message and looking for
82 matching message_id in mail.mail.statistics. """
83 if message.get('References'):
84 message_ids = [x.strip() for x in decode(message['References']).split()]
85 self.pool['mail.mail.statistics'].set_replied(cr, uid, mail_message_ids=message_ids, context=context)
86 return super(MailThread, self).message_route_process(cr, uid, message, message_dict, routes, context=context)