[FIX] OPW 18442 : View location don't consolidate child, actualy any location with...
[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 from email.utils import parsedate
29 import base64
30 import re
31 from tools.translate import _
32 import logging
33 import xmlrpclib
34
35 _logger = logging.getLogger('mailgate')
36
37 class mailgate_thread(osv.osv):
38     '''
39     Mailgateway Thread
40     '''
41     _name = 'mailgate.thread'
42     _description = 'Mailgateway Thread'
43
44     _columns = {
45         'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', readonly=True),
46     }
47
48     def copy(self, cr, uid, id, default=None, context=None):
49         """
50         Overrides orm copy method.
51         @param self: the object pointer
52         @param cr: the current row, from the database cursor,
53         @param uid: the current user’s ID for security checks,
54         @param id: Id of mailgate thread
55         @param default: Dictionary of default values for copy.
56         @param context: A standard dictionary for contextual values
57         """
58         if default is None:
59             default = {}
60
61         default.update({
62             'message_ids': [],
63             'date_closed': False,
64             'date_open': False
65         })
66         return super(mailgate_thread, self).copy(cr, uid, id, default, context=context)
67
68     def message_new(self, cr, uid, msg, context):
69         raise Exception, _('Method is not implemented')
70
71     def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
72         raise Exception, _('Method is not implemented')
73
74     def message_followers(self, cr, uid, ids, context=None):
75         """ Get a list of emails of the people following this thread
76         """
77         res = {}
78         if isinstance(ids, (str, int, long)):
79             ids = [long(ids)]
80         for thread in self.browse(cr, uid, ids, context=context):
81             l=[]
82             for message in thread.message_ids:
83                 l.append((message.user_id and message.user_id.email) or '')
84                 l.append(message.email_from or '')
85                 l.append(message.email_cc or '')
86             res[thread.id] = l
87         return res
88
89     def msg_send(self, cr, uid, id, *args, **argv):
90         raise Exception, _('Method is not implemented')
91
92     def history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, \
93                     email_from=False, message_id=False, references=None, attach=None, email_cc=None, \
94                     email_bcc=None, email_date=None, context=None):
95         """
96         @param self: The object pointer
97         @param cr: the current row, from the database cursor,
98         @param uid: the current user’s ID for security checks,
99         @param cases: a browse record list
100         @param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
101         @param history: Value True/False, If True it makes entry in case History otherwise in Case Log
102         @param email: Email-To / Recipient address
103         @param email_from: Email From / Sender address if any
104         @param email_cc: Comma-Separated list of Carbon Copy Emails To addresse if any
105         @param email_bcc: Comma-Separated list of Blind Carbon Copy Emails To addresses if any
106         @param email_date: Email Date string if different from now, in server Timezone
107         @param details: Description, Details of case history if any
108         @param atach: Attachment sent in email
109         @param context: A standard dictionary for contextual values"""
110         if context is None:
111             context = {}
112         if attach is None:
113             attach = []
114
115         if email_date:
116             edate = parsedate(email_date)
117             if edate is not None:
118                 email_date = time.strftime('%Y-%m-%d %H:%M:%S', edate)
119
120         # The mailgate sends the ids of the cases and not the object list
121
122         if all(isinstance(case_id, (int, long)) for case_id in cases):
123             cases = self.browse(cr, uid, cases, context=context)
124
125         att_obj = self.pool.get('ir.attachment')
126         obj = self.pool.get('mailgate.message')
127
128         for case in cases:
129             attachments = []
130             for att in attach:
131                 if isinstance(att,(int,long)):
132                     attachments.append(att)
133                 elif isinstance(att,dict):
134                     domain = [
135                         ('name', '=', att[0]),
136                         ('res_id', '=', case.id),
137                         ('res_model', '=', case._name)
138                     ]
139                     att_ids = att_obj.search(cr, uid, domain, context=context)
140
141                     if att_ids:
142                         attachments.extend(att_ids)
143                     else:
144                         values = {
145                             'res_model' : case._name,
146                             'res_id' : case.id,
147                             'name' : att[0],
148                             'datas' : base64.encodestring(att[1])
149                         }
150                         attachment_id = att_obj.create(cr, uid, values, context=context)
151                         attachments.append(attachment_id)
152
153             partner_id = hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False
154             if not partner_id and case._name == 'res.partner':
155                 partner_id = case.id
156             data = {
157                 'name': keyword,
158                 'user_id': uid,
159                 'model' : case._name,
160                 'partner_id': partner_id,
161                 'res_id': case.id,
162                 'date': time.strftime('%Y-%m-%d %H:%M:%S'),
163                 'message_id': message_id,
164                 'description': details or (hasattr(case, 'description') and case.description or False),
165                 'attachment_ids': [(6, 0, attachments)]
166             }
167
168             if history:
169                 for param in (email, email_cc, email_bcc):
170                     if isinstance(param, list):
171                         param = ", ".join(param)
172
173                 data.update({
174                     'name': subject or _('History'),
175                     'history': True,
176                     'date': email_date or time.strftime('%Y-%m-%d %H:%M:%S'),
177                     'email_to': email,
178                     'email_from': email_from or \
179                         (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
180                          case.user_id.address_id.email),
181                     'email_cc': email_cc,
182                     'email_bcc': email_bcc,
183                     'references': references,
184                 })
185             obj.create(cr, uid, data, context=context)
186         return True
187 mailgate_thread()
188
189 def format_date_tz(date, tz=None):
190     if not date:
191         return 'n/a'
192     format = tools.DEFAULT_SERVER_DATETIME_FORMAT
193     return tools.server_to_local_timestamp(date, format, format, tz)
194
195 class mailgate_message(osv.osv):
196     '''
197     Mailgateway Message
198     '''
199     def open_document(self, cr, uid, ids, context=None):
200         """ To Open Document
201         @param self: The object pointer.
202         @param cr: A database cursor
203         @param uid: ID of the user currently logged in
204         @param ids: the ID of messages
205         @param context: A standard dictionary
206         """
207         action_data = False
208         if ids:
209             message_id = ids[0]
210             mailgate_data = self.browse(cr, uid, message_id, context=context)
211             model = mailgate_data.model
212             res_id = mailgate_data.res_id
213
214             action_pool = self.pool.get('ir.actions.act_window')
215             action_ids = action_pool.search(cr, uid, [('res_model', '=', model)])
216             if action_ids:
217                 action_data = action_pool.read(cr, uid, action_ids[0], context=context)
218                 action_data.update({
219                     'domain' : "[('id','=',%d)]"%(res_id),
220                     'nodestroy': True,
221                     'context': {}
222                     })
223         return action_data
224
225     def open_attachment(self, cr, uid, ids, context=None):
226         """ To Open attachments
227         @param self: The object pointer.
228         @param cr: A database cursor
229         @param uid: ID of the user currently logged in
230         @param ids: the ID of messages
231         @param context: A standard dictionary
232         """
233         action_data = False
234         action_pool = self.pool.get('ir.actions.act_window')
235         message_pool = self.browse(cr ,uid, ids, context=context)[0]
236         att_ids = [x.id for x in message_pool.attachment_ids] 
237         action_ids = action_pool.search(cr, uid, [('res_model', '=', 'ir.attachment')])
238         if action_ids:
239             action_data = action_pool.read(cr, uid, action_ids[0], context=context)
240             action_data.update({
241                 'domain': [('id','in',att_ids)],
242                 'nodestroy': True
243                 })
244         return action_data
245
246     def truncate_data(self, cr, uid, data, context=None):
247         data_list = data and data.split('\n') or []
248         if len(data_list) > 3:
249             res = '\n\t'.join(data_list[:3]) + '...'
250         else:
251             res = '\n\t'.join(data_list)
252         return res
253
254     def _get_display_text(self, cr, uid, ids, name, arg, context=None):
255         if context is None:
256             context = {}
257         tz = context.get('tz')
258         result = {}
259         for message in self.browse(cr, uid, ids, context=context):
260             msg_txt = ''
261             msg_name = message.name
262             if message.history:
263                 msg_txt += (message.email_from or '/') + _(' wrote on ') + format_date_tz(message.date, tz) + ':\n\t'
264                 if message.description:
265                     msg_txt += self.truncate_data(cr, uid, message.description, context=context)
266             else:
267                 msg_txt = (message.user_id.name or '/') + _(' on ') + format_date_tz(message.date, tz) + ':\n\t'
268                 if msg_name == _('Opportunity'):
269                     msg_txt += _("Converted to Opportunity")
270                 elif msg_name == _('Note'):
271                     msg_txt = (message.user_id.name or '/') + _(' added note on ') + format_date_tz(message.date, tz) + ':\n\t'
272                     msg_txt += self.truncate_data(cr, uid, message.description, context=context)
273                 elif msg_name == _('Stage'):
274                     msg_txt += _("Changed Stage to: ") + message.description
275                 elif msg_name:
276                     msg_txt += _("Changed Status to: ") + msg_name
277             result[message.id] = msg_txt
278         return result
279
280     _name = 'mailgate.message'
281     _description = 'Mailgateway Message'
282     _order = 'date desc'
283     _columns = {
284         'name':fields.text('Subject', readonly=True),
285         'model': fields.char('Object Name', size=128, select=1, readonly=True),
286         'res_id': fields.integer('Resource ID', select=1, readonly=True),
287         'ref_id': fields.char('Reference Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
288         'date': fields.datetime('Date', readonly=True),
289         'history': fields.boolean('Is History?', readonly=True),
290         'user_id': fields.many2one('res.users', 'User Responsible', readonly=True),
291         'message': fields.text('Description', readonly=True),
292         'email_from': fields.char('From', size=128, help="Email From", readonly=True),
293         'email_to': fields.char('To', help="Email Recipients", size=256, readonly=True),
294         'email_cc': fields.char('Cc', help="Carbon Copy Email Recipients", size=256, readonly=True),
295         'email_bcc': fields.char('Bcc', help='Blind Carbon Copy Email Recipients', size=256, readonly=True),
296         'message_id': fields.char('Message Id', size=1024, readonly=True, help="Message Id on Email.", select=True),
297         'references': fields.text('References', readonly=True, help="References emails."),
298         'description': fields.text('Description', readonly=True),
299         'partner_id': fields.many2one('res.partner', 'Partner', required=False),
300         'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments', readonly=True),
301         'display_text': fields.function(_get_display_text, method=True, type='text', size="512", string='Display Text'),
302     }
303
304     def init(self, cr):
305         cr.execute("""SELECT indexname
306                       FROM pg_indexes
307                       WHERE indexname = 'mailgate_message_res_id_model_idx'""")
308         if not cr.fetchone():
309             cr.execute("""CREATE INDEX mailgate_message_res_id_model_idx
310                           ON mailgate_message (model, res_id)""")
311
312 mailgate_message()
313
314 class mailgate_tool(osv.osv_memory):
315
316     _name = 'email.server.tools'
317     _description = "Email Server Tools"
318
319     def _decode_header(self, text):
320         """Returns unicode() string conversion of the the given encoded smtp header"""
321         if text:
322             text = decode_header(text.replace('\r', ''))
323             return ''.join([tools.ustr(x[0], x[1]) for x in text])
324
325     def to_email(self,text):
326         return re.findall(r'([^ ,<@]+@[^> ,]+)',text)
327
328     def history(self, cr, uid, model, res_ids, msg, attach, context=None):
329         """This function creates history for mails fetched
330         @param self: The object pointer
331         @param cr: the current row, from the database cursor,
332         @param uid: the current user’s ID for security checks,
333         @param model: OpenObject Model
334         @param res_ids: Ids of the record of OpenObject model created
335         @param msg: Email details
336         @param attach: Email attachments
337         """
338         if isinstance(res_ids, (int, long)):
339             res_ids = [res_ids]
340
341         msg_pool = self.pool.get('mailgate.message')
342         for res_id in res_ids:
343             case = self.pool.get(model).browse(cr, uid, res_id, context=context)
344             partner_id = hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False
345             if not partner_id and model == 'res.partner':
346                 partner_id = res_id
347             msg_data = {
348                 'name': msg.get('subject', 'No subject'),
349                 'date': msg.get('date'),
350                 'description': msg.get('body', msg.get('from')),
351                 'history': True,
352                 'partner_id': partner_id,
353                 'model': model,
354                 'email_cc': msg.get('cc'),
355                 'email_from': msg.get('from'),
356                 'email_to': msg.get('to'),
357                 'message_id': msg.get('message-id'),
358                 'references': msg.get('references') or msg.get('in-reply-to'),
359                 'res_id': res_id,
360                 'user_id': uid,
361                 'attachment_ids': [(6, 0, attach)]
362             }
363             msg_pool.create(cr, uid, msg_data, context=context)
364         return True
365
366     def email_forward(self, cr, uid, model, res_ids, msg, email_error=False, context=None):
367         """Sends an email to all people following the thread
368         @param res_id: Id of the record of OpenObject model created from the email message
369         @param msg: email.message.Message to forward
370         @param email_error: Default Email address in case of any Problem
371         """
372
373         model_pool = self.pool.get(model)
374         for res in model_pool.browse(cr, uid, res_ids, context=context):
375             message_followers = model_pool.message_followers(cr, uid, [res.id])[res.id]
376             message_followers_emails = self.to_email(','.join(filter(None, message_followers)))
377             message_recipients = self.to_email(','.join(filter(None,
378                                                          [self._decode_header(msg['from']),
379                                                          self._decode_header(msg['to']),
380                                                          self._decode_header(msg['cc'])])))
381             message_forward = [i for i in message_followers_emails if (i and (i not in message_recipients))]
382
383             if message_forward:
384                 # TODO: we need an interface for this for all types of objects, not just leads
385                 if hasattr(res, 'section_id'):
386                     del msg['reply-to']
387                     msg['reply-to'] = res.section_id.reply_to
388
389                 smtp_from = self.to_email(msg['from'])
390                 if not tools.misc._email_send(smtp_from, message_forward, msg, openobject_id=res.id) and email_error:
391                     subj = msg['subject']
392                     del msg['subject'], msg['to'], msg['cc'], msg['bcc']
393                     msg['subject'] = '[OpenERP-Forward-Failed] %s' % subj
394                     msg['to'] = email_error
395                     tools.misc._email_send(smtp_from, self.to_email(email_error), msg, openobject_id=res.id)
396
397     def process_email(self, cr, uid, model, message, custom_values=None, attach=True, context=None):
398         """This function Processes email and create record for given OpenERP model
399         @param self: The object pointer
400         @param cr: the current row, from the database cursor,
401         @param uid: the current user’s ID for security checks,
402         @param model: OpenObject Model
403         @param message: Email details, passed as a string or an xmlrpclib.Binary
404         @param attach: Email attachments
405         @param context: A standard dictionary for contextual values"""
406
407         # extract message bytes, we are forced to pass the message as binary because
408         # we don't know its encoding until we parse its headers and hence can't
409         # convert it to utf-8 for transport between the mailgate script and here.
410         if isinstance(message, xmlrpclib.Binary):
411             message = str(message.data)
412
413         if context is None:
414             context = {}
415
416         if custom_values is None or not isinstance(custom_values, dict):
417             custom_values = {}
418
419         model_pool = self.pool.get(model)
420         res_id = False
421
422         # Create New Record into particular model
423         def create_record(msg):
424             att_ids = []
425             if hasattr(model_pool, 'message_new'):
426                 res_id,att_ids = model_pool.message_new(cr, uid, msg, context=context)
427                 if custom_values:
428                     model_pool.write(cr, uid, [res_id], custom_values, context=context)
429             else:
430                 data = {
431                     'name': msg.get('subject'),
432                     'email_from': msg.get('from'),
433                     'email_cc': msg.get('cc'),
434                     'user_id': False,
435                     'description': msg.get('body'),
436                     'state' : 'draft',
437                 }
438                 data.update(self.get_partner(cr, uid, msg.get('from'), context=context))
439                 res_id = model_pool.create(cr, uid, data, context=context)
440
441                 if attach:
442                     for attachment in msg.get('attachments', []):
443                         data_attach = {
444                             'name': attachment,
445                             'datas': binascii.b2a_base64(str(attachments.get(attachment))),
446                             'datas_fname': attachment,
447                             'description': 'Mail attachment',
448                             'res_model': model,
449                             'res_id': res_id,
450                         }
451                         att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
452
453             return res_id, att_ids
454
455         # Warning: message_from_string doesn't always work correctly on unicode,
456         # we must use utf-8 strings here :-(
457         if isinstance(message, unicode):
458             message = message.encode('utf-8')
459         msg_txt = email.message_from_string(message)
460         message_id = msg_txt.get('message-id', False)
461         msg = {}
462
463         if not message_id:
464             # Very unusual situation, be we should be fault-tolerant here
465             message_id = time.time()
466             msg_txt['message-id'] = message_id
467             _logger.info('Message without message-id, generating a random one: %s', message_id)
468
469         fields = msg_txt.keys()
470         msg['id'] = message_id
471         msg['message-id'] = message_id
472
473         if 'Subject' in fields:
474             msg['subject'] = self._decode_header(msg_txt.get('Subject'))
475
476         if 'Content-Type' in fields:
477             msg['content-type'] = msg_txt.get('Content-Type')
478
479         if 'From' in fields:
480             msg['from'] = self._decode_header(msg_txt.get('From'))
481
482         if 'Delivered-To' in fields:
483             msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
484
485         if 'CC' in fields:
486             msg['cc'] = self._decode_header(msg_txt.get('CC'))
487
488         if 'Reply-to' in fields:
489             msg['reply'] = self._decode_header(msg_txt.get('Reply-To'))
490
491         if 'Date' in fields:
492             msg['date'] = self._decode_header(msg_txt.get('Date'))
493
494         if 'Content-Transfer-Encoding' in fields:
495             msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
496
497         if 'References' in fields:
498             msg['references'] = msg_txt.get('References')
499
500         if 'In-Reply-To' in fields:
501             msg['in-reply-to'] = msg_txt.get('In-Reply-To')
502
503         if 'X-Priority' in fields:
504             msg['priority'] = msg_txt.get('X-Priority', '3 (Normal)').split(' ')[0]
505
506         if not msg_txt.is_multipart() or 'text/plain' in msg.get('Content-Type', ''):
507             encoding = msg_txt.get_content_charset()
508             body = msg_txt.get_payload(decode=True)
509             if 'text/html' in msg_txt.get('Content-Type', ''):
510                 body = tools.html2plaintext(body)
511             msg['body'] = tools.ustr(body, encoding)
512
513         attachments = {}
514         has_plain_text = False
515         if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
516             body = ""
517             for part in msg_txt.walk():
518                 if part.get_content_maintype() == 'multipart':
519                     continue
520
521                 encoding = part.get_content_charset()
522                 filename = part.get_filename()
523                 if part.get_content_maintype()=='text':
524                     content = part.get_payload(decode=True)
525                     if filename:
526                         attachments[filename] = content
527                     elif not has_plain_text:
528                         # main content parts should have 'text' maintype
529                         # and no filename. we ignore the html part if
530                         # there is already a plaintext part without filename,
531                         # because presumably these are alternatives.
532                         content = tools.ustr(content, encoding)
533                         if part.get_content_subtype() == 'html':
534                             body = tools.ustr(tools.html2plaintext(content))
535                         elif part.get_content_subtype() == 'plain':
536                             body = content
537                             has_plain_text = True
538                 elif part.get_content_maintype() in ('application', 'image'):
539                     if filename :
540                         attachments[filename] = part.get_payload(decode=True)
541                     else:
542                         res = part.get_payload(decode=True)
543                         body += tools.ustr(res, encoding)
544
545             msg['body'] = body
546             msg['attachments'] = attachments
547         res_ids = []
548         attachment_ids = []
549         new_res_id = False
550         if msg.get('references') or msg.get('in-reply-to'):
551             references = msg.get('references') or msg.get('in-reply-to')
552             if '\r\n' in references:
553                 references = references.split('\r\n')
554             else:
555                 references = references.split(' ')
556             for ref in references:
557                 ref = ref.strip()
558                 res_id = tools.misc.reference_re.search(ref)
559                 if res_id:
560                     res_id = res_id.group(1)
561                 else:
562                     res_id = tools.misc.res_re.search(msg['subject'])
563                     if res_id:
564                         res_id = res_id.group(1)
565                 if res_id:
566                     res_id = int(res_id)
567                     model_pool = self.pool.get(model)
568                     if model_pool.exists(cr, uid, res_id):
569                         res_ids.append(res_id)
570                         if hasattr(model_pool, 'message_update'):
571                             model_pool.message_update(cr, uid, [res_id], {}, msg, context=context)
572                         else:
573                             raise NotImplementedError('model %s does not support updating records, mailgate API method message_update() is missing'%model)
574
575         if not len(res_ids):
576             new_res_id, attachment_ids = create_record(msg)
577             res_ids = [new_res_id]
578
579         # Store messages
580         context.update({'model' : model})
581         if hasattr(model_pool, 'history'):
582             model_pool.history(cr, uid, res_ids, _('receive'), history=True,
583                             subject = msg.get('subject'),
584                             email = msg.get('to'),
585                             details = msg.get('body'),
586                             email_from = msg.get('from'),
587                             email_cc = msg.get('cc'),
588                             message_id = msg.get('message-id'),
589                             references = msg.get('references', False) or msg.get('in-reply-to', False),
590                             attach = attachment_ids or attachments.items(),
591                             email_date = msg.get('date'),
592                             context = context)
593         else:
594             self.history(cr, uid, model, res_ids, msg, attachment_ids, context=context)
595         self.email_forward(cr, uid, model, res_ids, msg_txt)
596         return new_res_id
597
598     def get_partner(self, cr, uid, from_email, context=None):
599         """This function returns partner Id based on email passed
600         @param self: The object pointer
601         @param cr: the current row, from the database cursor,
602         @param uid: the current user’s ID for security checks
603         @param from_email: email address based on that function will search for the correct
604         """
605         address_pool = self.pool.get('res.partner.address')
606         res = {
607             'partner_address_id': False,
608             'partner_id': False
609         }
610         from_email = self.to_email(from_email)[0]
611         address_ids = address_pool.search(cr, uid, [('email', 'like', from_email)])
612         if address_ids:
613             address = address_pool.browse(cr, uid, address_ids[0])
614             res['partner_address_id'] = address_ids[0]
615             res['partner_id'] = address.partner_id.id
616
617         return res
618
619 mailgate_tool()
620
621 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: