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 _
33 _logger = logging.getLogger('mailgate')
35 class mailgate_thread(osv.osv):
39 _name = 'mailgate.thread'
40 _description = 'Mailgateway Thread'
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)]),
47 def message_new(self, cr, uid, msg, context):
48 raise Exception, _('Method is not implemented')
50 def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
51 raise Exception, _('Method is not implemented')
53 def emails_get(self, cr, uid, ids, context=None):
54 raise Exception, _('Method is not implemented')
56 def msg_send(self, cr, uid, id, *args, **argv):
57 raise Exception, _('Method is not implemented')
59 def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, \
60 email_from=False, message_id=False, references=None, attach=None, context=None):
62 @param self: The object pointer
63 @param cr: the current row, from the database cursor,
64 @param uid: the current user’s ID for security checks,
65 @param cases: a browse record list
66 @param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
67 @param history: Value True/False, If True it makes entry in case History otherwise in Case Log
68 @param email: Email address if any
69 @param details: Details of case history if any
70 @param atach: Attachment sent in email
71 @param context: A standard dictionary for contextual values"""
77 # The mailgate sends the ids of the cases and not the object list
79 if all(isinstance(case_id, (int, long)) for case_id in cases):
80 cases = self.browse(cr, uid, cases, context=context)
82 att_obj = self.pool.get('ir.attachment')
83 obj = self.pool.get('mailgate.message')
91 'date': time.strftime('%Y-%m-%d %H:%M:%S'),
92 'message_id': message_id,
97 attachments.append(att_obj.create(cr, uid, {'name': att[0], 'datas': base64.encodestring(att[1])}))
100 'name': subject or 'History',
103 'model' : case._name,
105 'date': time.strftime('%Y-%m-%d %H:%M:%S'),
106 'description': details or (hasattr(case, 'description') and case.description or False),
107 'email_to': email or \
108 (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
109 case.user_id.address_id.email) or tools.config.get('email_from', False),
110 'email_from': email_from or \
111 (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
112 case.user_id.address_id.email) or tools.config.get('email_from', False),
113 'partner_id': hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False,
114 'references': references,
115 'message_id': message_id,
116 'attachment_ids': [(6, 0, attachments)]
118 res = obj.create(cr, uid, data, context)
122 class mailgate_message(osv.osv):
126 _name = 'mailgate.message'
127 _description = 'Mailgateway Message'
130 'name':fields.char('Message', size=64),
131 'model': fields.char('Object Name', size=128),
132 'res_id': fields.integer('Resource ID'),
133 'ref_id': fields.char('Reference Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
134 'date': fields.datetime('Date'),
135 'history': fields.boolean('Is History?'),
136 'user_id': fields.many2one('res.users', 'User Responsible', readonly=True),
137 'message': fields.text('Description'),
138 'email_from': fields.char('Email From', size=84),
139 'email_to': fields.char('Email To', size=84),
140 'email_cc': fields.char('Email CC', size=84),
141 'email_bcc': fields.char('Email BCC', size=84),
142 'message_id': fields.char('Message Id', size=1024, readonly=True, help="Message Id on Email.", select=True),
143 'references': fields.text('References', readonly=True, help="Referencess emails."),
144 'description': fields.text('Description'),
145 'partner_id': fields.many2one('res.partner', 'Partner', required=False),
146 'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments'),
151 class mailgate_tool(osv.osv_memory):
153 _name = 'email.server.tools'
154 _description = "Email Server Tools"
156 def _to_decode(self, s, charsets):
159 for charset in charsets:
162 return s.decode(charset)
165 return s.decode('latin1')
167 def _decode_header(self, text):
169 text = decode_header(text.replace('\r', ''))
170 return ''.join(map(lambda x:self._to_decode(x[0], [x[1]]), text or []))
172 def to_email(self, text):
173 _email = re.compile(r'.*<.*@.*\..*>', re.UNICODE)
176 index = eml.index('<')
177 eml = eml[index:-1].replace('<', '').replace('>', '')
180 bits = _email.sub(record, text)
183 def history(self, cr, uid, model, res_ids, msg, attach, context=None):
184 """This function creates history for mails fetched
185 @param self: The object pointer
186 @param cr: the current row, from the database cursor,
187 @param uid: the current user’s ID for security checks,
188 @param model: OpenObject Model
189 @param res_ids: Ids of the record of OpenObject model created
190 @param msg: Email details
191 @param attach: Email attachments
193 if isinstance(res_ids, (int, long)):
196 msg_pool = self.pool.get('mailgate.message')
197 for res_id in res_ids:
199 'name': msg.get('subject', 'No subject'),
200 'date': msg.get('date') ,
201 'description': msg.get('body', msg.get('from')),
204 'email_cc': msg.get('cc'),
205 'email_from': msg.get('from'),
206 'email_to': msg.get('to'),
207 'message_id': msg.get('message-id'),
208 'references': msg.get('references'),
211 'attachment_ids': [(6, 0, attach)]
213 msg_id = msg_pool.create(cr, uid, msg_data, context=context)
216 def email_send(self, cr, uid, model, res_id, msg, from_email=False, email_default=False):
217 """This function Sends return email on submission of Fetched email in OpenERP database
218 @param self: The object pointer
219 @param cr: the current row, from the database cursor,
220 @param uid: the current user’s ID for security checks,
221 @param model: OpenObject Model
222 @param res_id: Id of the record of OpenObject model created from the Email details
223 @param msg: Email details
224 @param email_default: Default Email address in case of any Problem
226 history_pool = self.pool.get('mailgate.message')
227 model_pool = self.pool.get(model)
228 from_email = from_email or tools.config.get('email_from', None)
229 message = email.message_from_string(tools.ustr(msg).encode('utf-8'))
230 subject = "[%s] %s" %(res_id, message['Subject'])
232 #mails = [self._decode_header(message['From']), self._decode_header(message['To'])]
233 #mails += self._decode_header(message.get('Cc', '')).split(',')
236 if hasattr(model_pool, 'emails_get'):
237 values = model_pool.emails_get(cr, uid, [res_id])
238 emails = values.get(res_id, {})
240 priority = emails.get('priority', [3])[0]
241 em = emails['user_email'] + emails['email_from'] + emails['email_cc']
242 msg_mails = map(self.to_email, filter(None, em))
244 #mm = [self._decode_header(message['From']), self._decode_header(message['To'])]
245 #mm += self._decode_header(message.get('Cc', '')).split(',')
247 #msg_mails = map(self.to_email, filter(None, mm))
249 encoding = message.get_content_charset()
250 message['body'] = message.get_payload(decode=True)
252 message['body'] = self._to_decode(message['body'], [encoding])
254 from_mail = self._decode_header(message['From'])
256 Hello %s,""" % (from_mail))
259 Your Request ID: %s""") % (res_id)
264 -------- Original Message --------
266 """) % (self._to_decode(message['body'], [encoding]))
269 res = tools.email_send(from_email, msg_mails, subject, body, openobject_id=res_id)
272 temp_msg = '[%s] %s'%(res_id, message['Subject'])
273 del message['Subject']
274 message['Subject'] = '[OpenERP-FetchError] %s' %(temp_msg)
275 tools.email_send(from_email, email_default, message.get('Subject'), message.get('body'), openobject_id=res_id)
278 def process_email(self, cr, uid, model, message, attach=True, context=None):
279 """This function Processes email and create record for given OpenERP model
280 @param self: The object pointer
281 @param cr: the current row, from the database cursor,
282 @param uid: the current user’s ID for security checks,
283 @param model: OpenObject Model
284 @param message: Email details
285 @param attach: Email attachments
286 @param context: A standard dictionary for contextual values"""
288 model_pool = self.pool.get(model)
292 # Create New Record into particular model
293 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)
310 for attachment in msg.get('attachments', []):
313 'datas': binascii.b2a_base64(str(attachments.get(attachment))),
314 'datas_fname': attachment,
315 'description': 'Mail attachment',
319 att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
323 history_pool = self.pool.get('mailgate.message')
325 # Warning: message_from_string doesn't always work correctly on unicode,
326 # we must use utf-8 strings here :-(
327 msg_txt = email.message_from_string(tools.ustr(message).encode('utf-8'))
328 message_id = msg_txt.get('Message-ID', False)
332 # Very unusual situation, be we should be fault-tolerant here
333 message_id = time.time()
334 msg_txt['Message-ID'] = message_id
335 _logger.info('Message without message-id, generating a random one: %s', message_id)
337 fields = msg_txt.keys()
338 msg['id'] = message_id
339 msg['message-id'] = message_id
341 if 'Subject' in fields:
342 msg['subject'] = self._decode_header(msg_txt.get('Subject'))
344 if 'Content-Type' in fields:
345 msg['content-type'] = msg_txt.get('Content-Type')
348 msg['from'] = self._decode_header(msg_txt.get('From'))
350 if 'Delivered-To' in fields:
351 msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
354 msg['cc'] = self._decode_header(msg_txt.get('Cc'))
356 if 'Reply-To' in fields:
357 msg['reply'] = self._decode_header(msg_txt.get('Reply-To'))
360 msg['date'] = msg_txt.get('Date')
362 if 'Content-Transfer-Encoding' in fields:
363 msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
365 if 'References' in fields:
366 msg['references'] = msg_txt.get('References')
368 if 'X-Priority' in fields:
369 msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0]
371 if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', ''):
372 encoding = msg_txt.get_content_charset()
373 msg['body'] = msg_txt.get_payload(decode=True)
375 msg['body'] = tools.ustr(msg['body'])
378 if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
381 for part in msg_txt.walk():
382 if part.get_content_maintype() == 'multipart':
385 encoding = part.get_content_charset()
387 if part.get_content_maintype()=='text':
388 content = part.get_payload(decode=True)
389 filename = part.get_filename()
391 attachments[filename] = content
394 content = unicode(content, encoding)
395 if part.get_content_subtype() == 'html':
396 body = tools.html2plaintext(content)
397 elif part.get_content_subtype() == 'plain':
399 elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text':
400 filename = part.get_filename();
402 attachments[filename] = part.get_payload(decode=True)
404 res = part.get_payload(decode=True)
406 res = tools.ustr(res)
411 msg['attachments'] = attachments
414 if msg.get('references'):
415 references = msg.get('references')
416 if '\r\n' in references:
417 references = msg.get('references').split('\r\n')
419 references = msg.get('references').split(' ')
420 for ref in references:
422 res_id = tools.misc.reference_re.search(ref)
424 res_id = res_id.group(1)
426 res_id = tools.misc.res_re.search(msg['subject'])
428 res_id = res_id.group(1)
431 res_ids.append(res_id)
432 model_pool = self.pool.get(model)
435 if hasattr(model_pool, 'message_update'):
436 model_pool.message_update(cr, uid, [res_id], vals, msg, context=context)
439 new_res_id = create_record(msg)
440 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 message_id = msg.get('message-id'),
450 references = msg.get('references', False),
451 attach = msg.get('attachments', {}).items(),
454 self.history(cr, uid, model, res_ids, msg, att_ids, context=context)
457 def get_partner(self, cr, uid, from_email, context=None):
458 """This function returns partner Id based on email passed
459 @param self: The object pointer
460 @param cr: the current row, from the database cursor,
461 @param uid: the current user’s ID for security checks
462 @param from_email: email address based on that function will search for the correct
464 address_pool = self.pool.get('res.partner.address')
466 'partner_address_id': False,
469 from_email = self.to_email(from_email)
470 address_ids = address_pool.search(cr, uid, [('email', '=', from_email)])
472 address = address_pool.browse(cr, uid, address_ids[0])
473 res['partner_address_id'] = address_ids[0]
474 res['partner_id'] = address.partner_id.id