[IMP] mail_gateway: forward emails to followers with attachments and everything,...
[odoo/odoo.git] / addons / mail_gateway / mail_gateway.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>)
6 #
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
11 #
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
16 #
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/>
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 import time
24 import tools
25 import binascii
26 import email
27 from email.header import decode_header
28 import base64
29 import re
30 from tools.translate import _
31 import logging
32
33 _logger = logging.getLogger('mailgate')
34
35 class mailgate_thread(osv.osv):
36     '''
37     Mailgateway Thread
38     '''
39     _name = 'mailgate.thread'
40     _description = 'Mailgateway Thread'
41
42     _columns = {
43         'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('history', '=', True)]),
44         'log_ids': fields.one2many('mailgate.message', 'res_id', 'Logs', domain=[('history', '=', False)]),
45     }
46
47     def message_new(self, cr, uid, msg, context):
48         raise Exception, _('Method is not implemented')
49
50     def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
51         raise Exception, _('Method is not implemented')
52
53     def message_followers(self, cr, uid, ids, context=None):
54         """ Get a list of emails of the people following this thread
55         """
56         res = {}
57         if isinstance(ids, (str, int, long)):
58             ids = [long(ids)]
59         for thread in self.browse(cr, uid, ids, context=context):
60             l=[]
61             for message in thread.message_ids:
62                 l.append((message.user_id and message.user_id.email) or '')
63                 l.append(message.email_from or '')
64                 l.append(message.email_cc or '')
65             res[thread.id] = l
66         return res
67
68     def msg_send(self, cr, uid, id, *args, **argv):
69         raise Exception, _('Method is not implemented')
70
71     def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, \
72                     email_from=False, message_id=False, references=None, attach=None, context=None):
73         """
74         @param self: The object pointer
75         @param cr: the current row, from the database cursor,
76         @param uid: the current user’s ID for security checks,
77         @param cases: a browse record list
78         @param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
79         @param history: Value True/False, If True it makes entry in case History otherwise in Case Log
80         @param email: Email address if any
81         @param details: Details of case history if any 
82         @param atach: Attachment sent in email
83         @param context: A standard dictionary for contextual values"""
84         if context is None:
85             context = {}
86         if attach is None:
87             attach = []
88
89         # The mailgate sends the ids of the cases and not the object list
90
91         if all(isinstance(case_id, (int, long)) for case_id in cases):
92             cases = self.browse(cr, uid, cases, context=context)
93
94         att_obj = self.pool.get('ir.attachment')
95         obj = self.pool.get('mailgate.message')
96
97         for case in cases:
98             data = {
99                 'name': keyword, 
100                 'user_id': uid, 
101                 'model' : case._name, 
102                 'res_id': case.id, 
103                 'date': time.strftime('%Y-%m-%d %H:%M:%S'), 
104                 'message_id': message_id, 
105             }
106             attachments = []
107             if history:
108                 for att in attach:
109                     attachments.append(att_obj.create(cr, uid, {'name': att[0], 'datas': base64.encodestring(att[1])}))
110
111                 data = {
112                     'name': subject or 'History', 
113                     'history': True, 
114                     'user_id': uid, 
115                     'model' : case._name, 
116                     'res_id': case.id,
117                     'date': time.strftime('%Y-%m-%d %H:%M:%S'), 
118                     'description': details or (hasattr(case, 'description') and case.description or False), 
119                     'email_to': email or \
120                         (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
121                          case.user_id.address_id.email) or tools.config.get('email_from', False), 
122                     'email_from': email_from or \
123                         (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
124                          case.user_id.address_id.email) or tools.config.get('email_from', False), 
125                     'partner_id': hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False, 
126                     'references': references, 
127                     'message_id': message_id, 
128                     'attachment_ids': [(6, 0, attachments)]
129                 }
130             res = obj.create(cr, uid, data, context)
131         return True
132 mailgate_thread()
133
134 class mailgate_message(osv.osv):
135     '''
136     Mailgateway Message
137     '''
138     _name = 'mailgate.message'
139     _description = 'Mailgateway Message'
140     _order = 'id desc'
141     _columns = {
142         'name':fields.char('Subject', size=128), 
143         'model': fields.char('Object Name', size=128), 
144         'res_id': fields.integer('Resource ID'),
145         'ref_id': fields.char('Reference Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
146         'date': fields.datetime('Date'), 
147         'history': fields.boolean('Is History?'),
148         'user_id': fields.many2one('res.users', 'User Responsible', readonly=True), 
149         'message': fields.text('Description'), 
150         'email_from': fields.char('Email From', size=84), 
151         'email_to': fields.char('Email To', size=84), 
152         'email_cc': fields.char('Email CC', size=84), 
153         'email_bcc': fields.char('Email BCC', size=84), 
154         'message_id': fields.char('Message Id', size=1024, readonly=True, help="Message Id on Email.", select=True),
155         'references': fields.text('References', readonly=True, help="Referencess emails."),
156         'description': fields.text('Description'), 
157         'partner_id': fields.many2one('res.partner', 'Partner', required=False), 
158         'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments'), 
159     }
160
161 mailgate_message()
162
163 class mailgate_tool(osv.osv_memory):
164
165     _name = 'email.server.tools'
166     _description = "Email Server Tools"
167
168     def _to_decode(self, s, charsets):
169         if not s:
170             return s
171         for charset in charsets:
172             if charset:
173                 try:
174                     return s.decode(charset)
175                 except UnicodeError:
176                     pass
177         return s.decode('latin1')
178
179     def _decode_header(self, text):
180         if text:
181             text = decode_header(text.replace('\r', '')) 
182         return ''.join(map(lambda x:self._to_decode(x[0], [x[1]]), text or []))
183
184     def to_email(self,text):
185         return re.findall(r'([^ ,<@]+@[^> ,]+)',text)
186
187     def history(self, cr, uid, model, res_ids, msg, attach, context=None):
188         """This function creates history for mails fetched
189         @param self: The object pointer
190         @param cr: the current row, from the database cursor,
191         @param uid: the current user’s ID for security checks,
192         @param model: OpenObject Model
193         @param res_ids: Ids of the record of OpenObject model created 
194         @param msg: Email details
195         @param attach: Email attachments
196         """
197         if isinstance(res_ids, (int, long)):
198             res_ids = [res_ids]
199
200         msg_pool = self.pool.get('mailgate.message')
201         for res_id in res_ids:
202             msg_data = {
203                         'name': msg.get('subject', 'No subject'), 
204                         'date': msg.get('date') , 
205                         'description': msg.get('body', msg.get('from')), 
206                         'history': True,
207                         'res_model': model, 
208                         'email_cc': msg.get('cc'), 
209                         'email_from': msg.get('from'), 
210                         'email_to': msg.get('to'), 
211                         'message_id': msg.get('message-id'), 
212                         'references': msg.get('references'), 
213                         'res_id': res_id,
214                         'user_id': uid, 
215                         'attachment_ids': [(6, 0, attach)]
216             }
217             msg_id = msg_pool.create(cr, uid, msg_data, context=context)
218         return True
219
220     def email_forward(self, cr, uid, model, res_ids, msg, email_error=False, context=None):
221         """Sends an email to all people following the thread
222         @param res_id: Id of the record of OpenObject model created from the email message
223         @param msg: email.message.Message to forward
224         @param email_error: Default Email address in case of any Problem
225         """
226         model_pool = self.pool.get(model)
227
228         for res in model_pool.browse(cr, uid, res_ids, context=context):
229             message_followers = model_pool.message_followers(cr, uid, [res.id])[res.id]
230             message_followers_emails = self.to_email(','.join(message_followers))
231             message_recipients = self.to_email(','.join([self._decode_header(msg['from']),
232                                                          self._decode_header(msg['to']),
233                                                          self._decode_header(msg['cc'])]))
234             message_forward = [i for i in message_followers_emails if (i and (i not in message_recipients))]
235
236             if message_forward:
237                 # TODO: we need an interface for this for all types of objects, not just leads
238                 if hasattr(res, 'section'):
239                     del msg['reply-to']
240                     msg['reply-to'] = res.section.email_from
241
242                 if not tools._email_send(msg, openobject_id=res_id) and email_error:
243                     subj = msg['subject']
244                     del msg['subject'], msg['to'], msg['cc'], msg['bcc']
245                     msg['subject'] = '[OpenERP-Forward-Failed] %s' % subj
246                     msg['to'] = email_error
247                     tools._email_send(msg, openobject_id=res_id)
248
249     def process_email(self, cr, uid, model, message, attach=True, context=None):
250         """This function Processes email and create record for given OpenERP model 
251         @param self: The object pointer
252         @param cr: the current row, from the database cursor,
253         @param uid: the current user’s ID for security checks,
254         @param model: OpenObject Model
255         @param message: Email details
256         @param attach: Email attachments
257         @param context: A standard dictionary for contextual values"""
258         model_pool = self.pool.get(model)
259         if not context:
260             context = {}
261         res_id = False
262         # Create New Record into particular model
263         def create_record(msg):
264             if hasattr(model_pool, 'message_new'):
265                 res_id = model_pool.message_new(cr, uid, msg, context)
266             else:
267                 data = {
268                     'name': msg.get('subject'),
269                     'email_from': msg.get('from'),
270                     'email_cc': msg.get('cc'),
271                     'user_id': False,
272                     'description': msg.get('body'),
273                     'state' : 'draft',
274                 }
275                 data.update(self.get_partner(cr, uid, msg.get('from'), context=context))
276                 res_id = model_pool.create(cr, uid, data, context=context)
277
278                 att_ids = []
279                 if attach:
280                     for attachment in msg.get('attachments', []):
281                         data_attach = {
282                             'name': attachment,
283                             'datas': binascii.b2a_base64(str(attachments.get(attachment))),
284                             'datas_fname': attachment,
285                             'description': 'Mail attachment',
286                             'res_model': model,
287                             'res_id': res_id,
288                         }
289                         att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
290
291             return res_id
292
293         # Warning: message_from_string doesn't always work correctly on unicode,
294         # we must use utf-8 strings here :-(
295         msg_txt = email.message_from_string(tools.ustr(message).encode('utf-8'))
296         message_id = msg_txt.get('Message-ID', False)
297         msg = {}
298
299         if not message_id:
300             # Very unusual situation, be we should be fault-tolerant here
301             message_id = time.time()
302             msg_txt['Message-ID'] = message_id
303             _logger.info('Message without message-id, generating a random one: %s', message_id)
304
305         fields = msg_txt.keys()
306         msg['id'] = message_id
307         msg['message-id'] = message_id
308
309         if 'Subject' in fields:
310             msg['subject'] = self._decode_header(msg_txt.get('Subject'))
311
312         if 'Content-Type' in fields:
313             msg['content-type'] = msg_txt.get('Content-Type')
314
315         if 'From' in fields:
316             msg['from'] = self._decode_header(msg_txt.get('From'))
317
318         if 'Delivered-To' in fields:
319             msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
320
321         if 'Cc' in fields:
322             msg['cc'] = self._decode_header(msg_txt.get('Cc'))
323
324         if 'Reply-To' in fields:
325             msg['reply'] = self._decode_header(msg_txt.get('Reply-To'))
326
327         if 'Date' in fields:
328             msg['date'] = msg_txt.get('Date')
329
330         if 'Content-Transfer-Encoding' in fields:
331             msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
332
333         if 'References' in fields:
334             msg['references'] = msg_txt.get('References')
335
336         if 'X-Priority' in fields:
337             msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0]
338
339         if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', ''):
340             encoding = msg_txt.get_content_charset()
341             msg['body'] = msg_txt.get_payload(decode=True)
342             if encoding:
343                 msg['body'] = tools.ustr(msg['body'])
344
345         attachments = {}
346         if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
347             body = ""
348             counter = 1
349             for part in msg_txt.walk():
350                 if part.get_content_maintype() == 'multipart':
351                     continue
352
353                 encoding = part.get_content_charset()
354
355                 if part.get_content_maintype()=='text':
356                     content = part.get_payload(decode=True)
357                     filename = part.get_filename()
358                     if filename :
359                         attachments[filename] = content
360                     else:
361                         if encoding:
362                             content = unicode(content, encoding)
363                         if part.get_content_subtype() == 'html':
364                             body = tools.html2plaintext(content)
365                         elif part.get_content_subtype() == 'plain':
366                             body = content
367                 elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text':
368                     filename = part.get_filename();
369                     if filename :
370                         attachments[filename] = part.get_payload(decode=True)
371                     else:
372                         res = part.get_payload(decode=True)
373                         if encoding:
374                             res = tools.ustr(res)
375                         body += res
376
377             msg['body'] = body
378             msg['attachments'] = attachments
379         res_ids = []
380         new_res_id = False
381         if msg.get('references'):
382             references = msg.get('references')
383             if '\r\n' in references:
384                 references = msg.get('references').split('\r\n')
385             else:
386                 references = msg.get('references').split(' ')
387             for ref in references:
388                 ref = ref.strip()
389                 res_id = tools.misc.reference_re.search(ref)
390                 if res_id:
391                     res_id = res_id.group(1)
392                 else:
393                     res_id = tools.misc.res_re.search(msg['subject'])
394                     if res_id:
395                         res_id = res_id.group(1)
396                 if res_id:
397                     res_id = int(res_id)
398                     res_ids.append(res_id)
399                     model_pool = self.pool.get(model)
400
401                     vals = {}
402                     if hasattr(model_pool, 'message_update'):
403                         model_pool.message_update(cr, uid, [res_id], vals, msg, context=context)
404
405         if not len(res_ids):
406             new_res_id = create_record(msg)
407             res_ids = [new_res_id]
408         # Store messages
409         context.update({'model' : model})
410         if hasattr(model_pool, '_history'):
411             model_pool._history(cr, uid, res_ids, _('Receive'), history=True,
412                             subject = msg.get('subject'),
413                             email = msg.get('to'),
414                             details = msg.get('body'),
415                             email_from = msg.get('from'),
416                             message_id = msg.get('message-id'),
417                             references = msg.get('references', False),
418                             attach = msg.get('attachments', {}).items(),
419                             context = context)
420         else:
421             self.history(cr, uid, model, res_ids, msg, att_ids, context=context)
422         self.email_forward(cr, uid, model, res_ids, msg_txt)
423         return new_res_id
424
425     def get_partner(self, cr, uid, from_email, context=None):
426         """This function returns partner Id based on email passed
427         @param self: The object pointer
428         @param cr: the current row, from the database cursor,
429         @param uid: the current user’s ID for security checks
430         @param from_email: email address based on that function will search for the correct
431         """
432         address_pool = self.pool.get('res.partner.address')
433         res = {
434             'partner_address_id': False,
435             'partner_id': False
436         }
437         from_email = self.to_email(from_email)[0]
438         address_ids = address_pool.search(cr, uid, [('email', '=', from_email)])
439         if address_ids:
440             address = address_pool.browse(cr, uid, address_ids[0])
441             res['partner_address_id'] = address_ids[0]
442             res['partner_id'] = address.partner_id.id
443
444         return res
445
446 mailgate_tool()