2010-04-29 13:49:19 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
##############################################################################
|
|
|
|
|
#
|
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>)
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Affero General Public License for more details
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
|
|
from osv import osv, fields
|
2010-05-03 12:30:48 +00:00
|
|
|
|
import time
|
2010-06-14 08:47:02 +00:00
|
|
|
|
import tools
|
2010-06-24 12:57:47 +00:00
|
|
|
|
import binascii
|
|
|
|
|
import email
|
|
|
|
|
from email.header import decode_header
|
2010-06-11 15:53:51 +00:00
|
|
|
|
import base64
|
2010-06-24 12:57:47 +00:00
|
|
|
|
import re
|
2010-06-24 19:53:32 +00:00
|
|
|
|
from tools.translate import _
|
2010-06-25 12:18:47 +00:00
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
_logger = logging.getLogger('mailgate')
|
2010-04-29 13:49:19 +00:00
|
|
|
|
|
|
|
|
|
class mailgate_thread(osv.osv):
|
|
|
|
|
'''
|
|
|
|
|
Mailgateway Thread
|
|
|
|
|
'''
|
|
|
|
|
_name = 'mailgate.thread'
|
|
|
|
|
_description = 'Mailgateway Thread'
|
2010-06-24 12:57:47 +00:00
|
|
|
|
|
2010-04-29 13:49:19 +00:00
|
|
|
|
_columns = {
|
2010-06-24 19:53:32 +00:00
|
|
|
|
'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('history', '=', True)]),
|
|
|
|
|
'log_ids': fields.one2many('mailgate.message', 'res_id', 'Logs', domain=[('history', '=', False)]),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def message_new(self, cr, uid, msg, context):
|
|
|
|
|
raise Exception, _('Method is not implemented')
|
|
|
|
|
|
|
|
|
|
def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
|
2010-06-25 12:18:47 +00:00
|
|
|
|
raise Exception, _('Method is not implemented')
|
2010-06-24 19:53:32 +00:00
|
|
|
|
|
2010-06-25 22:02:47 +00:00
|
|
|
|
def message_followers(self, cr, uid, ids, context=None):
|
|
|
|
|
""" Get a list of emails of the people following this thread
|
|
|
|
|
"""
|
|
|
|
|
res = {}
|
|
|
|
|
if isinstance(ids, (str, int, long)):
|
|
|
|
|
ids = [long(ids)]
|
|
|
|
|
for thread in self.browse(cr, uid, ids, context=context):
|
|
|
|
|
l=[]
|
|
|
|
|
for message in thread.message_ids:
|
|
|
|
|
l.append((message.user_id and message.user_id.email) or '')
|
|
|
|
|
l.append(message.email_from or '')
|
|
|
|
|
l.append(message.email_cc or '')
|
|
|
|
|
res[thread.id] = l
|
|
|
|
|
return res
|
2010-06-24 19:53:32 +00:00
|
|
|
|
|
2010-06-25 12:18:47 +00:00
|
|
|
|
def msg_send(self, cr, uid, id, *args, **argv):
|
2010-06-24 19:53:32 +00:00
|
|
|
|
raise Exception, _('Method is not implemented')
|
|
|
|
|
|
|
|
|
|
def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, \
|
|
|
|
|
email_from=False, message_id=False, references=None, attach=None, context=None):
|
2010-05-12 10:12:56 +00:00
|
|
|
|
"""
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param cases: a browse record list
|
|
|
|
|
@param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
|
|
|
|
|
@param history: Value True/False, If True it makes entry in case History otherwise in Case Log
|
|
|
|
|
@param email: Email address if any
|
|
|
|
|
@param details: Details of case history if any
|
2010-05-25 06:35:30 +00:00
|
|
|
|
@param atach: Attachment sent in email
|
2010-05-12 10:12:56 +00:00
|
|
|
|
@param context: A standard dictionary for contextual values"""
|
2010-05-31 12:46:48 +00:00
|
|
|
|
if context is None:
|
2010-05-12 10:12:56 +00:00
|
|
|
|
context = {}
|
2010-06-11 13:26:24 +00:00
|
|
|
|
if attach is None:
|
|
|
|
|
attach = []
|
|
|
|
|
|
2010-05-12 10:12:56 +00:00
|
|
|
|
# The mailgate sends the ids of the cases and not the object list
|
2010-06-11 13:26:24 +00:00
|
|
|
|
|
|
|
|
|
if all(isinstance(case_id, (int, long)) for case_id in cases):
|
|
|
|
|
cases = self.browse(cr, uid, cases, context=context)
|
2010-05-12 10:12:56 +00:00
|
|
|
|
|
2010-05-25 06:35:30 +00:00
|
|
|
|
att_obj = self.pool.get('ir.attachment')
|
2010-05-12 10:12:56 +00:00
|
|
|
|
obj = self.pool.get('mailgate.message')
|
2010-05-25 06:35:30 +00:00
|
|
|
|
|
2010-05-12 10:12:56 +00:00
|
|
|
|
for case in cases:
|
|
|
|
|
data = {
|
2010-05-24 13:35:02 +00:00
|
|
|
|
'name': keyword,
|
|
|
|
|
'user_id': uid,
|
2010-06-24 12:57:47 +00:00
|
|
|
|
'model' : case._name,
|
|
|
|
|
'res_id': case.id,
|
2010-05-24 13:35:02 +00:00
|
|
|
|
'date': time.strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
|
'message_id': message_id,
|
2010-05-12 10:12:56 +00:00
|
|
|
|
}
|
2010-05-25 06:35:30 +00:00
|
|
|
|
attachments = []
|
2010-05-12 10:12:56 +00:00
|
|
|
|
if history:
|
2010-05-25 06:35:30 +00:00
|
|
|
|
for att in attach:
|
|
|
|
|
attachments.append(att_obj.create(cr, uid, {'name': att[0], 'datas': base64.encodestring(att[1])}))
|
2010-06-25 12:18:47 +00:00
|
|
|
|
|
2010-05-24 13:35:02 +00:00
|
|
|
|
data = {
|
2010-06-11 13:26:24 +00:00
|
|
|
|
'name': subject or 'History',
|
|
|
|
|
'history': True,
|
|
|
|
|
'user_id': uid,
|
2010-06-24 12:57:47 +00:00
|
|
|
|
'model' : case._name,
|
2010-06-11 13:26:24 +00:00
|
|
|
|
'res_id': case.id,
|
|
|
|
|
'date': time.strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
|
'description': details or (hasattr(case, 'description') and case.description or False),
|
|
|
|
|
'email_to': email or \
|
|
|
|
|
(hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
|
|
|
|
|
case.user_id.address_id.email) or tools.config.get('email_from', False),
|
|
|
|
|
'email_from': email_from or \
|
|
|
|
|
(hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
|
|
|
|
|
case.user_id.address_id.email) or tools.config.get('email_from', False),
|
|
|
|
|
'partner_id': hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False,
|
2010-06-24 19:53:32 +00:00
|
|
|
|
'references': references,
|
2010-06-11 13:26:24 +00:00
|
|
|
|
'message_id': message_id,
|
|
|
|
|
'attachment_ids': [(6, 0, attachments)]
|
|
|
|
|
}
|
2010-06-29 09:10:06 +00:00
|
|
|
|
obj.create(cr, uid, data, context)
|
2010-05-12 10:12:56 +00:00
|
|
|
|
return True
|
2010-04-29 13:49:19 +00:00
|
|
|
|
mailgate_thread()
|
|
|
|
|
|
|
|
|
|
class mailgate_message(osv.osv):
|
|
|
|
|
'''
|
|
|
|
|
Mailgateway Message
|
|
|
|
|
'''
|
|
|
|
|
_name = 'mailgate.message'
|
|
|
|
|
_description = 'Mailgateway Message'
|
2010-06-23 20:29:29 +00:00
|
|
|
|
_order = 'id desc'
|
2010-04-29 13:49:19 +00:00
|
|
|
|
_columns = {
|
2010-06-29 09:10:06 +00:00
|
|
|
|
'name':fields.char('Subject', size=128),
|
|
|
|
|
'model': fields.char('Object Name', size=128, select=1),
|
|
|
|
|
'res_id': fields.integer('Resource ID', select=1),
|
2010-06-24 12:57:47 +00:00
|
|
|
|
'ref_id': fields.char('Reference Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
|
2010-06-29 09:10:06 +00:00
|
|
|
|
'date': fields.datetime('Date'),
|
2010-06-23 20:29:29 +00:00
|
|
|
|
'history': fields.boolean('Is History?'),
|
2010-06-29 09:10:06 +00:00
|
|
|
|
'user_id': fields.many2one('res.users', 'User Responsible', readonly=True),
|
|
|
|
|
'message': fields.text('Description'),
|
|
|
|
|
'email_from': fields.char('Email From', size=84),
|
|
|
|
|
'email_to': fields.char('Email To', size=84),
|
|
|
|
|
'email_cc': fields.char('Email CC', size=84),
|
|
|
|
|
'email_bcc': fields.char('Email BCC', size=84),
|
2010-06-24 19:53:32 +00:00
|
|
|
|
'message_id': fields.char('Message Id', size=1024, readonly=True, help="Message Id on Email.", select=True),
|
|
|
|
|
'references': fields.text('References', readonly=True, help="Referencess emails."),
|
2010-06-29 09:10:06 +00:00
|
|
|
|
'description': fields.text('Description'),
|
|
|
|
|
'partner_id': fields.many2one('res.partner', 'Partner', required=False),
|
|
|
|
|
'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments'),
|
2010-04-29 13:49:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-06-29 09:10:06 +00:00
|
|
|
|
def init(self, cr):
|
|
|
|
|
cr.execute("""SELECT indexname
|
|
|
|
|
FROM pg_indexes
|
|
|
|
|
WHERE indexname = 'mailgate_message_res_id_model_idx'""")
|
|
|
|
|
if not cr.fetchone():
|
|
|
|
|
cr.execute("""CREATE INDEX mailgate_message_res_id_model_idx
|
|
|
|
|
ON mailgate_message (model, res_id)""")
|
|
|
|
|
|
2010-04-29 13:49:19 +00:00
|
|
|
|
mailgate_message()
|
|
|
|
|
|
2010-06-24 19:53:32 +00:00
|
|
|
|
class mailgate_tool(osv.osv_memory):
|
|
|
|
|
|
|
|
|
|
_name = 'email.server.tools'
|
|
|
|
|
_description = "Email Server Tools"
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-06-24 19:53:32 +00:00
|
|
|
|
def _to_decode(self, s, charsets):
|
2010-06-25 14:12:51 +00:00
|
|
|
|
if not s:
|
|
|
|
|
return s
|
2010-06-24 19:53:32 +00:00
|
|
|
|
for charset in charsets:
|
|
|
|
|
if charset:
|
|
|
|
|
try:
|
|
|
|
|
return s.decode(charset)
|
|
|
|
|
except UnicodeError:
|
|
|
|
|
pass
|
|
|
|
|
return s.decode('latin1')
|
|
|
|
|
|
|
|
|
|
def _decode_header(self, text):
|
|
|
|
|
if text:
|
|
|
|
|
text = decode_header(text.replace('\r', ''))
|
|
|
|
|
return ''.join(map(lambda x:self._to_decode(x[0], [x[1]]), text or []))
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-06-25 22:02:47 +00:00
|
|
|
|
def to_email(self,text):
|
|
|
|
|
return re.findall(r'([^ ,<@]+@[^> ,]+)',text)
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-06-25 09:57:41 +00:00
|
|
|
|
def history(self, cr, uid, model, res_ids, msg, attach, context=None):
|
2010-06-24 19:53:32 +00:00
|
|
|
|
"""This function creates history for mails fetched
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param model: OpenObject Model
|
|
|
|
|
@param res_ids: Ids of the record of OpenObject model created
|
|
|
|
|
@param msg: Email details
|
|
|
|
|
@param attach: Email attachments
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(res_ids, (int, long)):
|
|
|
|
|
res_ids = [res_ids]
|
|
|
|
|
|
|
|
|
|
msg_pool = self.pool.get('mailgate.message')
|
|
|
|
|
for res_id in res_ids:
|
|
|
|
|
msg_data = {
|
|
|
|
|
'name': msg.get('subject', 'No subject'),
|
|
|
|
|
'date': msg.get('date') ,
|
|
|
|
|
'description': msg.get('body', msg.get('from')),
|
|
|
|
|
'history': True,
|
|
|
|
|
'res_model': model,
|
|
|
|
|
'email_cc': msg.get('cc'),
|
|
|
|
|
'email_from': msg.get('from'),
|
|
|
|
|
'email_to': msg.get('to'),
|
|
|
|
|
'message_id': msg.get('message-id'),
|
|
|
|
|
'references': msg.get('references'),
|
|
|
|
|
'res_id': res_id,
|
|
|
|
|
'user_id': uid,
|
|
|
|
|
'attachment_ids': [(6, 0, attach)]
|
|
|
|
|
}
|
2010-06-29 09:10:06 +00:00
|
|
|
|
msg_pool.create(cr, uid, msg_data, context=context)
|
2010-06-24 19:53:32 +00:00
|
|
|
|
return True
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-06-28 10:38:32 +00:00
|
|
|
|
def email_forward(self, cr, uid, model, res_ids, msg, email_error=False, context=None):
|
2010-06-25 22:02:47 +00:00
|
|
|
|
"""Sends an email to all people following the thread
|
2010-06-28 10:38:32 +00:00
|
|
|
|
@param res_id: Id of the record of OpenObject model created from the email message
|
|
|
|
|
@param msg: email.message.Message to forward
|
2010-06-25 22:02:47 +00:00
|
|
|
|
@param email_error: Default Email address in case of any Problem
|
2010-06-24 19:53:32 +00:00
|
|
|
|
"""
|
2010-06-28 10:38:32 +00:00
|
|
|
|
model_pool = self.pool.get(model)
|
2010-06-25 22:02:47 +00:00
|
|
|
|
|
2010-06-28 10:38:32 +00:00
|
|
|
|
for res in model_pool.browse(cr, uid, res_ids, context=context):
|
|
|
|
|
message_followers = model_pool.message_followers(cr, uid, [res.id])[res.id]
|
2010-06-25 22:02:47 +00:00
|
|
|
|
message_followers_emails = self.to_email(','.join(message_followers))
|
2010-06-28 10:38:32 +00:00
|
|
|
|
message_recipients = self.to_email(','.join([self._decode_header(msg['from']),
|
|
|
|
|
self._decode_header(msg['to']),
|
|
|
|
|
self._decode_header(msg['cc'])]))
|
2010-06-25 22:02:47 +00:00
|
|
|
|
message_forward = [i for i in message_followers_emails if (i and (i not in message_recipients))]
|
|
|
|
|
|
2010-06-28 10:38:32 +00:00
|
|
|
|
if message_forward:
|
|
|
|
|
# TODO: we need an interface for this for all types of objects, not just leads
|
2010-06-28 14:47:26 +00:00
|
|
|
|
if hasattr(res, 'section_id'):
|
2010-06-28 10:38:32 +00:00
|
|
|
|
del msg['reply-to']
|
2010-06-28 14:47:26 +00:00
|
|
|
|
msg['reply-to'] = res.section_id.reply_to
|
2010-06-28 10:38:32 +00:00
|
|
|
|
|
2010-06-28 14:47:26 +00:00
|
|
|
|
smtp_from = self.to_email(msg['from'])
|
|
|
|
|
if not tools.misc._email_send(smtp_from, message_forward, msg, openobject_id=res.id) and email_error:
|
2010-06-28 10:38:32 +00:00
|
|
|
|
subj = msg['subject']
|
|
|
|
|
del msg['subject'], msg['to'], msg['cc'], msg['bcc']
|
|
|
|
|
msg['subject'] = '[OpenERP-Forward-Failed] %s' % subj
|
|
|
|
|
msg['to'] = email_error
|
2010-06-28 14:47:26 +00:00
|
|
|
|
tools.misc._email_send(smtp_from, self.to_email(email_error), msg, openobject_id=res.id)
|
2010-06-24 19:53:32 +00:00
|
|
|
|
|
|
|
|
|
def process_email(self, cr, uid, model, message, attach=True, context=None):
|
2010-06-28 14:47:26 +00:00
|
|
|
|
"""This function Processes email and create record for given OpenERP model
|
2010-06-24 19:53:32 +00:00
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param model: OpenObject Model
|
|
|
|
|
@param message: Email details
|
2010-06-25 12:18:47 +00:00
|
|
|
|
@param attach: Email attachments
|
2010-06-24 19:53:32 +00:00
|
|
|
|
@param context: A standard dictionary for contextual values"""
|
|
|
|
|
model_pool = self.pool.get(model)
|
|
|
|
|
if not context:
|
|
|
|
|
context = {}
|
|
|
|
|
res_id = False
|
|
|
|
|
# Create New Record into particular model
|
2010-06-25 12:18:47 +00:00
|
|
|
|
def create_record(msg):
|
2010-06-24 19:53:32 +00:00
|
|
|
|
if hasattr(model_pool, 'message_new'):
|
|
|
|
|
res_id = model_pool.message_new(cr, uid, msg, context)
|
|
|
|
|
else:
|
|
|
|
|
data = {
|
2010-06-25 12:18:47 +00:00
|
|
|
|
'name': msg.get('subject'),
|
|
|
|
|
'email_from': msg.get('from'),
|
|
|
|
|
'email_cc': msg.get('cc'),
|
|
|
|
|
'user_id': False,
|
|
|
|
|
'description': msg.get('body'),
|
|
|
|
|
'state' : 'draft',
|
2010-06-24 19:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
data.update(self.get_partner(cr, uid, msg.get('from'), context=context))
|
|
|
|
|
res_id = model_pool.create(cr, uid, data, context=context)
|
|
|
|
|
|
|
|
|
|
att_ids = []
|
|
|
|
|
if attach:
|
|
|
|
|
for attachment in msg.get('attachments', []):
|
|
|
|
|
data_attach = {
|
2010-06-25 12:18:47 +00:00
|
|
|
|
'name': attachment,
|
|
|
|
|
'datas': binascii.b2a_base64(str(attachments.get(attachment))),
|
|
|
|
|
'datas_fname': attachment,
|
|
|
|
|
'description': 'Mail attachment',
|
|
|
|
|
'res_model': model,
|
|
|
|
|
'res_id': res_id,
|
2010-06-24 19:53:32 +00:00
|
|
|
|
}
|
2010-06-25 12:18:47 +00:00
|
|
|
|
att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
|
|
|
|
|
|
2010-06-29 09:10:06 +00:00
|
|
|
|
return res_id, att_ids
|
2010-06-24 19:53:32 +00:00
|
|
|
|
|
2010-06-25 12:18:47 +00:00
|
|
|
|
# Warning: message_from_string doesn't always work correctly on unicode,
|
|
|
|
|
# we must use utf-8 strings here :-(
|
|
|
|
|
msg_txt = email.message_from_string(tools.ustr(message).encode('utf-8'))
|
2010-06-24 19:53:32 +00:00
|
|
|
|
message_id = msg_txt.get('Message-ID', False)
|
|
|
|
|
msg = {}
|
2010-06-25 12:18:47 +00:00
|
|
|
|
|
2010-06-24 19:53:32 +00:00
|
|
|
|
if not message_id:
|
2010-06-25 12:18:47 +00:00
|
|
|
|
# Very unusual situation, be we should be fault-tolerant here
|
|
|
|
|
message_id = time.time()
|
|
|
|
|
msg_txt['Message-ID'] = message_id
|
|
|
|
|
_logger.info('Message without message-id, generating a random one: %s', message_id)
|
2010-06-24 19:53:32 +00:00
|
|
|
|
|
|
|
|
|
fields = msg_txt.keys()
|
|
|
|
|
msg['id'] = message_id
|
2010-06-25 12:18:47 +00:00
|
|
|
|
msg['message-id'] = message_id
|
|
|
|
|
|
2010-06-24 19:53:32 +00:00
|
|
|
|
if 'Subject' in fields:
|
|
|
|
|
msg['subject'] = self._decode_header(msg_txt.get('Subject'))
|
|
|
|
|
|
|
|
|
|
if 'Content-Type' in fields:
|
|
|
|
|
msg['content-type'] = msg_txt.get('Content-Type')
|
|
|
|
|
|
|
|
|
|
if 'From' in fields:
|
|
|
|
|
msg['from'] = self._decode_header(msg_txt.get('From'))
|
|
|
|
|
|
|
|
|
|
if 'Delivered-To' in fields:
|
|
|
|
|
msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
|
|
|
|
|
|
|
|
|
|
if 'Cc' in fields:
|
|
|
|
|
msg['cc'] = self._decode_header(msg_txt.get('Cc'))
|
|
|
|
|
|
|
|
|
|
if 'Reply-To' in fields:
|
|
|
|
|
msg['reply'] = self._decode_header(msg_txt.get('Reply-To'))
|
|
|
|
|
|
|
|
|
|
if 'Date' in fields:
|
|
|
|
|
msg['date'] = msg_txt.get('Date')
|
|
|
|
|
|
|
|
|
|
if 'Content-Transfer-Encoding' in fields:
|
|
|
|
|
msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
|
|
|
|
|
|
|
|
|
|
if 'References' in fields:
|
2010-06-25 12:18:47 +00:00
|
|
|
|
msg['references'] = msg_txt.get('References')
|
2010-06-24 19:53:32 +00:00
|
|
|
|
|
|
|
|
|
if 'X-Priority' in fields:
|
|
|
|
|
msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0]
|
|
|
|
|
|
2010-06-25 12:18:47 +00:00
|
|
|
|
if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', ''):
|
2010-06-24 19:53:32 +00:00
|
|
|
|
encoding = msg_txt.get_content_charset()
|
|
|
|
|
msg['body'] = msg_txt.get_payload(decode=True)
|
|
|
|
|
if encoding:
|
|
|
|
|
msg['body'] = tools.ustr(msg['body'])
|
|
|
|
|
|
|
|
|
|
attachments = {}
|
2010-06-25 12:18:47 +00:00
|
|
|
|
if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
|
2010-06-24 19:53:32 +00:00
|
|
|
|
body = ""
|
|
|
|
|
for part in msg_txt.walk():
|
|
|
|
|
if part.get_content_maintype() == 'multipart':
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
encoding = part.get_content_charset()
|
|
|
|
|
|
|
|
|
|
if part.get_content_maintype()=='text':
|
|
|
|
|
content = part.get_payload(decode=True)
|
|
|
|
|
filename = part.get_filename()
|
|
|
|
|
if filename :
|
|
|
|
|
attachments[filename] = content
|
|
|
|
|
else:
|
|
|
|
|
if encoding:
|
|
|
|
|
content = unicode(content, encoding)
|
|
|
|
|
if part.get_content_subtype() == 'html':
|
|
|
|
|
body = tools.html2plaintext(content)
|
|
|
|
|
elif part.get_content_subtype() == 'plain':
|
|
|
|
|
body = content
|
2010-06-29 09:10:06 +00:00
|
|
|
|
elif part.get_content_maintype() in ('application', 'image', 'text'):
|
2010-06-24 19:53:32 +00:00
|
|
|
|
filename = part.get_filename();
|
|
|
|
|
if filename :
|
|
|
|
|
attachments[filename] = part.get_payload(decode=True)
|
|
|
|
|
else:
|
|
|
|
|
res = part.get_payload(decode=True)
|
|
|
|
|
if encoding:
|
|
|
|
|
res = tools.ustr(res)
|
|
|
|
|
body += res
|
|
|
|
|
|
|
|
|
|
msg['body'] = body
|
|
|
|
|
msg['attachments'] = attachments
|
|
|
|
|
res_ids = []
|
2010-06-29 09:10:06 +00:00
|
|
|
|
attachment_ids = []
|
2010-06-24 19:53:32 +00:00
|
|
|
|
new_res_id = False
|
2010-06-25 12:18:47 +00:00
|
|
|
|
if msg.get('references'):
|
2010-06-24 19:53:32 +00:00
|
|
|
|
references = msg.get('references')
|
|
|
|
|
if '\r\n' in references:
|
|
|
|
|
references = msg.get('references').split('\r\n')
|
|
|
|
|
else:
|
|
|
|
|
references = msg.get('references').split(' ')
|
|
|
|
|
for ref in references:
|
|
|
|
|
ref = ref.strip()
|
|
|
|
|
res_id = tools.misc.reference_re.search(ref)
|
|
|
|
|
if res_id:
|
|
|
|
|
res_id = res_id.group(1)
|
|
|
|
|
else:
|
|
|
|
|
res_id = tools.misc.res_re.search(msg['subject'])
|
|
|
|
|
if res_id:
|
|
|
|
|
res_id = res_id.group(1)
|
|
|
|
|
if res_id:
|
|
|
|
|
res_id = int(res_id)
|
|
|
|
|
res_ids.append(res_id)
|
|
|
|
|
model_pool = self.pool.get(model)
|
2010-06-25 12:18:47 +00:00
|
|
|
|
|
2010-06-24 19:53:32 +00:00
|
|
|
|
vals = {}
|
|
|
|
|
if hasattr(model_pool, 'message_update'):
|
|
|
|
|
model_pool.message_update(cr, uid, [res_id], vals, msg, context=context)
|
2010-06-25 12:18:47 +00:00
|
|
|
|
|
2010-06-24 19:53:32 +00:00
|
|
|
|
if not len(res_ids):
|
2010-06-29 09:10:06 +00:00
|
|
|
|
new_res_id, attachment_ids = create_record(msg)
|
2010-06-24 19:53:32 +00:00
|
|
|
|
res_ids = [new_res_id]
|
2010-06-25 09:57:41 +00:00
|
|
|
|
# Store messages
|
2010-06-25 12:18:47 +00:00
|
|
|
|
context.update({'model' : model})
|
2010-06-24 19:53:32 +00:00
|
|
|
|
if hasattr(model_pool, '_history'):
|
2010-06-25 17:41:41 +00:00
|
|
|
|
model_pool._history(cr, uid, res_ids, _('Receive'), history=True,
|
|
|
|
|
subject = msg.get('subject'),
|
|
|
|
|
email = msg.get('to'),
|
|
|
|
|
details = msg.get('body'),
|
|
|
|
|
email_from = msg.get('from'),
|
|
|
|
|
message_id = msg.get('message-id'),
|
2010-06-24 19:53:32 +00:00
|
|
|
|
references = msg.get('references', False),
|
2010-06-29 09:10:06 +00:00
|
|
|
|
attach = attachments.items(),
|
2010-06-25 09:57:41 +00:00
|
|
|
|
context = context)
|
2010-06-24 19:53:32 +00:00
|
|
|
|
else:
|
2010-06-29 09:10:06 +00:00
|
|
|
|
self.history(cr, uid, model, res_ids, msg, attachment_ids, context=context)
|
2010-06-28 10:38:32 +00:00
|
|
|
|
self.email_forward(cr, uid, model, res_ids, msg_txt)
|
2010-06-24 19:53:32 +00:00
|
|
|
|
return new_res_id
|
|
|
|
|
|
|
|
|
|
def get_partner(self, cr, uid, from_email, context=None):
|
|
|
|
|
"""This function returns partner Id based on email passed
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks
|
|
|
|
|
@param from_email: email address based on that function will search for the correct
|
|
|
|
|
"""
|
|
|
|
|
address_pool = self.pool.get('res.partner.address')
|
|
|
|
|
res = {
|
2010-06-25 12:18:47 +00:00
|
|
|
|
'partner_address_id': False,
|
2010-06-24 19:53:32 +00:00
|
|
|
|
'partner_id': False
|
|
|
|
|
}
|
2010-06-25 22:02:47 +00:00
|
|
|
|
from_email = self.to_email(from_email)[0]
|
2010-06-24 19:53:32 +00:00
|
|
|
|
address_ids = address_pool.search(cr, uid, [('email', '=', from_email)])
|
|
|
|
|
if address_ids:
|
|
|
|
|
address = address_pool.browse(cr, uid, address_ids[0])
|
|
|
|
|
res['partner_address_id'] = address_ids[0]
|
|
|
|
|
res['partner_id'] = address.partner_id.id
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
mailgate_tool()
|