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