1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>)
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 ##############################################################################
22 from osv import osv, fields
27 from email.header import decode_header
30 from tools.translate import _
34 _logger = logging.getLogger('mailgate')
36 class mailgate_thread(osv.osv):
40 _name = 'mailgate.thread'
41 _description = 'Mailgateway Thread'
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)]),
48 def message_new(self, cr, uid, msg, context):
49 raise Exception, _('Method is not implemented')
51 def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
52 raise Exception, _('Method is not implemented')
54 def message_followers(self, cr, uid, ids, context=None):
55 """ Get a list of emails of the people following this thread
58 if isinstance(ids, (str, int, long)):
60 for thread in self.browse(cr, uid, ids, context=context):
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 '')
69 def msg_send(self, cr, uid, id, *args, **argv):
70 raise Exception, _('Method is not implemented')
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):
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"""
95 # The mailgate sends the ids of the cases and not the object list
97 if all(isinstance(case_id, (int, long)) for case_id in cases):
98 cases = self.browse(cr, uid, cases, context=context)
100 att_obj = self.pool.get('ir.attachment')
101 obj = self.pool.get('mailgate.message')
107 'model' : case._name,
109 'date': time.strftime('%Y-%m-%d %H:%M:%S'),
110 'message_id': message_id,
115 attachments.append(att_obj.create(cr, uid, {'name': att[0], 'datas': base64.encodestring(att[1])}))
117 for param in (email, email_cc, email_bcc):
118 if isinstance(param, list):
119 param = ", ".join(param)
122 'name': subject or 'History',
125 'model' : case._name,
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),
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)]
140 obj.create(cr, uid, data, context=context)
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)
154 class mailgate_message(osv.osv):
158 _name = 'mailgate.message'
159 _description = 'Mailgateway Message'
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'),
182 cr.execute("""SELECT indexname
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)""")
191 class mailgate_tool(osv.osv_memory):
193 _name = 'email.server.tools'
194 _description = "Email Server Tools"
196 def _decode_header(self, text):
197 """Returns unicode() string conversion of the the given encoded smtp header"""
199 text = decode_header(text.replace('\r', ''))
200 return ''.join([tools.ustr(x[0], x[1]) for x in text])
202 def to_email(self,text):
203 return re.findall(r'([^ ,<@]+@[^> ,]+)',text)
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
215 if isinstance(res_ids, (int, long)):
218 msg_pool = self.pool.get('mailgate.message')
219 for res_id in res_ids:
221 'name': msg.get('subject', 'No subject'),
222 'date': msg.get('date') ,
223 'description': msg.get('body', msg.get('from')),
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'),
233 'attachment_ids': [(6, 0, attach)]
235 msg_pool.create(cr, uid, msg_data, context=context)
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
244 model_pool = self.pool.get(model)
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))]
256 # TODO: we need an interface for this for all types of objects, not just leads
257 if hasattr(res, 'section_id'):
259 msg['reply-to'] = res.section_id.reply_to
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)
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"""
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)
288 model_pool = self.pool.get(model)
291 # Create New Record into particular model
292 def create_record(msg):
294 if hasattr(model_pool, 'message_new'):
295 res_id = model_pool.message_new(cr, uid, msg, context)
298 'name': msg.get('subject'),
299 'email_from': msg.get('from'),
300 'email_cc': msg.get('cc'),
302 'description': msg.get('body'),
305 data.update(self.get_partner(cr, uid, msg.get('from'), context=context))
306 res_id = model_pool.create(cr, uid, data, context=context)
309 for attachment in msg.get('attachments', []):
312 'datas': binascii.b2a_base64(str(attachments.get(attachment))),
313 'datas_fname': attachment,
314 'description': 'Mail attachment',
318 att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
320 return res_id, att_ids
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)
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)
336 fields = msg_txt.keys()
337 msg['id'] = message_id
338 msg['message-id'] = message_id
340 if 'Subject' in fields:
341 msg['subject'] = self._decode_header(msg_txt.get('Subject'))
343 if 'Content-Type' in fields:
344 msg['content-type'] = msg_txt.get('Content-Type')
347 msg['from'] = self._decode_header(msg_txt.get('From'))
349 if 'Delivered-To' in fields:
350 msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
353 msg['cc'] = self._decode_header(msg_txt.get('CC'))
355 if 'Reply-to' in fields:
356 msg['reply'] = self._decode_header(msg_txt.get('Reply-To'))
359 msg['date'] = self._decode_header(msg_txt.get('Date'))
361 if 'Content-Transfer-Encoding' in fields:
362 msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
364 if 'References' in fields:
365 msg['references'] = msg_txt.get('References')
367 if 'X-Priority' in fields:
368 msg['priority'] = msg_txt.get('X-Priority', '3 (Normal)').split(' ')[0]
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)
376 has_plain_text = False
377 if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
379 for part in msg_txt.walk():
380 if part.get_content_maintype() == 'multipart':
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)
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':
399 has_plain_text = True
400 elif part.get_content_maintype() in ('application', 'image'):
402 attachments[filename] = part.get_payload(decode=True)
404 res = part.get_payload(decode=True)
405 body += tools.ustr(res, encoding)
408 msg['attachments'] = attachments
412 if msg.get('references'):
413 references = msg.get('references')
414 if '\r\n' in references:
415 references = msg.get('references').split('\r\n')
417 references = msg.get('references').split(' ')
418 for ref in references:
420 res_id = tools.misc.reference_re.search(ref)
422 res_id = res_id.group(1)
424 res_id = tools.misc.res_re.search(msg['subject'])
426 res_id = res_id.group(1)
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)
435 raise NotImplementedError('model %s does not support updating records, mailgate API method message_update() is missing'%model)
438 new_res_id, attachment_ids = create_record(msg)
439 res_ids = [new_res_id]
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(),
455 self.history(cr, uid, model, res_ids, msg, attachment_ids, context=context)
456 self.email_forward(cr, uid, model, res_ids, msg_txt)
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
466 address_pool = self.pool.get('res.partner.address')
468 'partner_address_id': False,
471 from_email = self.to_email(from_email)[0]
472 address_ids = address_pool.search(cr, uid, [('email', '=', from_email)])
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