2010-07-29 13:26:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-01-11 10:43:23 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
2010-07-06 12:01:55 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2010-07-29 13:26:11 +00:00
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2010-01-11 10:43:23 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2010-07-29 13:26:11 +00:00
|
|
|
# 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.
|
2010-01-11 10:43:23 +00:00
|
|
|
#
|
|
|
|
# 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
|
2010-07-29 13:26:11 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2010-01-11 10:43:23 +00:00
|
|
|
#
|
2010-07-29 13:26:11 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2010-01-11 10:43:23 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
import time
|
|
|
|
import ir
|
|
|
|
from osv import osv,fields
|
|
|
|
import base64
|
|
|
|
import netsvc
|
|
|
|
from tools.translate import _
|
2010-08-11 14:07:26 +00:00
|
|
|
import email
|
|
|
|
import tools
|
|
|
|
import binascii
|
|
|
|
class email_server_tools(osv.osv_memory):
|
|
|
|
_inherit = "email.server.tools"
|
2010-09-11 11:34:51 +00:00
|
|
|
def history_message(self, cr, uid, model, res_id, message, context=None):
|
2010-08-11 14:07:26 +00:00
|
|
|
#@param message: string of mail which is read from EML File
|
|
|
|
attachment_pool = self.pool.get('ir.attachment')
|
|
|
|
msg = self.parse_message(message)
|
|
|
|
attachments = msg.get('attachments', [])
|
|
|
|
att_ids = []
|
|
|
|
for attachment in attachments:
|
|
|
|
data_attach = {
|
|
|
|
'name': attachment,
|
|
|
|
'datas': binascii.b2a_base64(str(attachments.get(attachment))),
|
|
|
|
'datas_fname': attachment,
|
|
|
|
'description': 'Mail attachment From Thunderbird msg_id: %s' %(msg.get('message_id', '')),
|
|
|
|
'res_model': model,
|
|
|
|
'res_id': res_id,
|
|
|
|
}
|
|
|
|
att_ids.append(attachment_pool.create(cr, uid, data_attach))
|
|
|
|
return self.history(cr, uid, model, res_id, msg, att_ids)
|
|
|
|
|
|
|
|
def parse_message(self, message):
|
2010-09-11 11:34:51 +00:00
|
|
|
#TOCHECK: put this function in mailgateway module
|
2010-09-20 09:42:08 +00:00
|
|
|
if isinstance(message, unicode):
|
|
|
|
message = message.encode('utf-8')
|
2010-08-11 14:07:26 +00:00
|
|
|
msg_txt = email.message_from_string(message)
|
|
|
|
message_id = msg_txt.get('message-id', False)
|
|
|
|
msg = {}
|
|
|
|
fields = msg_txt.keys()
|
|
|
|
msg['id'] = message_id
|
|
|
|
msg['message-id'] = message_id
|
|
|
|
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'] = self._decode_header(msg_txt.get('Date'))
|
|
|
|
|
|
|
|
if 'Content-Transfer-Encoding' in fields:
|
|
|
|
msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
|
|
|
|
|
|
|
|
if 'References' in fields:
|
|
|
|
msg['references'] = msg_txt.get('References')
|
|
|
|
|
|
|
|
if 'In-Reply-To' in fields:
|
|
|
|
msg['in-reply-to'] = msg_txt.get('In-Reply-To')
|
|
|
|
|
|
|
|
if 'X-Priority' in fields:
|
|
|
|
msg['priority'] = msg_txt.get('X-Priority', '3 (Normal)').split(' ')[0]
|
|
|
|
|
|
|
|
if not msg_txt.is_multipart() or 'text/plain' in msg.get('Content-Type', ''):
|
|
|
|
encoding = msg_txt.get_content_charset()
|
|
|
|
body = msg_txt.get_payload(decode=True)
|
|
|
|
msg['body'] = tools.ustr(body, encoding)
|
|
|
|
|
|
|
|
attachments = {}
|
|
|
|
has_plain_text = False
|
|
|
|
if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
|
|
|
|
body = ""
|
|
|
|
for part in msg_txt.walk():
|
|
|
|
if part.get_content_maintype() == 'multipart':
|
|
|
|
continue
|
|
|
|
|
|
|
|
encoding = part.get_content_charset()
|
|
|
|
filename = part.get_filename()
|
|
|
|
if part.get_content_maintype()=='text':
|
|
|
|
content = part.get_payload(decode=True)
|
|
|
|
if filename:
|
|
|
|
attachments[filename] = content
|
|
|
|
elif not has_plain_text:
|
|
|
|
# main content parts should have 'text' maintype
|
|
|
|
# and no filename. we ignore the html part if
|
|
|
|
# there is already a plaintext part without filename,
|
|
|
|
# because presumably these are alternatives.
|
|
|
|
content = tools.ustr(content, encoding)
|
|
|
|
if part.get_content_subtype() == 'html':
|
|
|
|
body = tools.ustr(tools.html2plaintext(content))
|
|
|
|
elif part.get_content_subtype() == 'plain':
|
|
|
|
body = content
|
|
|
|
has_plain_text = True
|
|
|
|
elif part.get_content_maintype() in ('application', 'image'):
|
|
|
|
if filename :
|
|
|
|
attachments[filename] = part.get_payload(decode=True)
|
|
|
|
else:
|
|
|
|
res = part.get_payload(decode=True)
|
|
|
|
body += tools.ustr(res, encoding)
|
|
|
|
|
|
|
|
msg['body'] = body
|
|
|
|
msg['attachments'] = attachments
|
|
|
|
return msg
|
|
|
|
email_server_tools()
|
2010-01-11 10:43:23 +00:00
|
|
|
|
2010-08-03 11:12:14 +00:00
|
|
|
class thunderbird_partner(osv.osv_memory):
|
|
|
|
_name = "thunderbird.partner"
|
2010-01-11 10:43:23 +00:00
|
|
|
_description="Thunderbid mails"
|
|
|
|
|
2010-08-03 09:36:44 +00:00
|
|
|
def create_contact(self,cr,user,vals):
|
2010-01-11 10:43:23 +00:00
|
|
|
dictcreate = dict(vals)
|
2010-08-16 10:12:17 +00:00
|
|
|
# Set False value if 'undefined'. Thunerbird set 'undefined' if user did not set any value.
|
|
|
|
for key in dictcreate:
|
|
|
|
if dictcreate[key] == 'undefined':
|
|
|
|
dictcreate[key] = False
|
2010-08-03 13:50:54 +00:00
|
|
|
if not eval(dictcreate.get('partner_id')):
|
|
|
|
dictcreate.update({'partner_id': False})
|
2010-01-11 10:43:23 +00:00
|
|
|
create_id = self.pool.get('res.partner.address').create(cr, user, dictcreate)
|
|
|
|
return create_id
|
2010-07-21 13:32:00 +00:00
|
|
|
|
2010-08-17 10:56:30 +00:00
|
|
|
def history_message(self, cr, uid, vals):
|
2010-08-11 14:07:26 +00:00
|
|
|
dictcreate = dict(vals)
|
2010-08-17 10:56:30 +00:00
|
|
|
ref_ids = str(dictcreate.get('ref_ids')).split(';')
|
|
|
|
msg = dictcreate.get('message')
|
2010-09-14 13:41:41 +00:00
|
|
|
msg = self.pool.get('email.server.tools').parse_message(msg)
|
2010-08-11 14:07:26 +00:00
|
|
|
server_tools_pool = self.pool.get('email.server.tools')
|
2010-09-14 13:41:41 +00:00
|
|
|
message_id = msg.get('message-id', False)
|
|
|
|
msg_pool = self.pool.get('mailgate.message')
|
2010-09-20 09:42:08 +00:00
|
|
|
msg_ids = []
|
2010-09-14 13:41:41 +00:00
|
|
|
res = {}
|
2010-09-20 07:58:13 +00:00
|
|
|
res_ids = []
|
2010-11-10 10:53:05 +00:00
|
|
|
obj_list= ['crm.lead','project.issue','hr.applicant','res.partner']
|
2010-08-17 10:56:30 +00:00
|
|
|
for ref_id in ref_ids:
|
2010-09-14 13:41:41 +00:00
|
|
|
msg_new = dictcreate.get('message')
|
2010-08-17 10:56:30 +00:00
|
|
|
ref = ref_id.split(',')
|
|
|
|
model = ref[0]
|
2010-09-20 10:15:33 +00:00
|
|
|
res_id = int(ref[1])
|
|
|
|
if message_id:
|
|
|
|
msg_ids = msg_pool.search(cr, uid, [('message_id','=',message_id),('res_id','=',res_id),('model','=',model)])
|
|
|
|
if msg_ids and len(msg_ids):
|
|
|
|
continue
|
2010-11-10 10:53:05 +00:00
|
|
|
if model not in obj_list:
|
|
|
|
res={}
|
|
|
|
obj_attch = self.pool.get('ir.attachment')
|
|
|
|
ls = ['*', '/', '\\', '<', '>', ':', '?', '"', '|', '\t', '\n',':','~']
|
|
|
|
sub = (email.Subject).replace(' ','')
|
|
|
|
if sub.strip() == '':
|
|
|
|
sub = 'NO SBUJECT'
|
|
|
|
fn = sub
|
|
|
|
for c in ls:
|
|
|
|
fn = fn.replace(c,'')
|
|
|
|
if len(fn) > 64:
|
|
|
|
l = 64 - len(fn)
|
|
|
|
f = fn.split('-')
|
|
|
|
fn = '-'.join(f[1:])
|
|
|
|
if len(fn) > 64:
|
|
|
|
l = 64 - len(fn)
|
|
|
|
f = fn.split('.')
|
|
|
|
fn = f[0][0:l] + '.' + f[-1]
|
|
|
|
fn = fn[:-4]+'.txt'
|
|
|
|
res['res_model'] = model
|
|
|
|
res['name'] = msg.get('subject','NO-SUBJECT')
|
|
|
|
res['datas_fname'] = fn
|
|
|
|
res['datas'] = base64.b64encode(msg.get('body'))
|
|
|
|
res['res_id'] = res_id
|
|
|
|
obj_attch.create(cr, uid, res)
|
2010-09-20 10:15:33 +00:00
|
|
|
server_tools_pool.history_message(cr, uid, model, res_id, msg_new)
|
|
|
|
res_ids.append(res_id)
|
2010-09-20 07:58:13 +00:00
|
|
|
return len(res_ids)
|
2010-08-11 14:07:26 +00:00
|
|
|
|
2010-09-11 11:34:51 +00:00
|
|
|
def process_email(self, cr, uid, vals):
|
2010-08-11 14:07:26 +00:00
|
|
|
dictcreate = dict(vals)
|
|
|
|
model = str(dictcreate.get('model'))
|
2010-08-17 08:32:47 +00:00
|
|
|
message = dictcreate.get('message')
|
2010-08-11 14:07:26 +00:00
|
|
|
return self.pool.get('email.server.tools').process_email(cr, uid, model, message, attach=True, context=None)
|
|
|
|
|
2010-09-24 12:35:03 +00:00
|
|
|
def search_message(self, cr, uid, message, context=None):
|
|
|
|
#@param message: string of mail which is read from EML File
|
|
|
|
#@return model,res_id
|
2010-09-24 13:58:23 +00:00
|
|
|
dictcreate = dict(message)
|
2010-10-07 07:43:37 +00:00
|
|
|
message_id = dictcreate.get('message_id')
|
2010-09-24 12:35:03 +00:00
|
|
|
msg_pool = self.pool.get('mailgate.message')
|
2010-10-07 07:43:37 +00:00
|
|
|
model = ''
|
|
|
|
res_id = 0
|
2010-09-24 12:35:03 +00:00
|
|
|
if message_id:
|
|
|
|
msg_ids = msg_pool.search(cr, uid, [('message_id','=',message_id)])
|
|
|
|
if msg_ids and len(msg_ids):
|
|
|
|
msg = msg_pool.browse(cr, uid, msg_ids[0])
|
|
|
|
model = msg.model
|
|
|
|
res_id = msg.res_id
|
|
|
|
return (model,res_id)
|
|
|
|
|
2010-10-07 07:43:37 +00:00
|
|
|
|
2010-09-11 11:34:51 +00:00
|
|
|
def search_contact(self, cr, user, email):
|
|
|
|
address_pool = self.pool.get('res.partner.address')
|
|
|
|
address_ids = address_pool.search(cr, user, [('email','=',email)])
|
2010-07-21 13:32:00 +00:00
|
|
|
res = {}
|
2010-08-10 11:26:58 +00:00
|
|
|
|
2010-09-11 11:34:51 +00:00
|
|
|
if not address_ids:
|
|
|
|
res = {
|
2010-08-10 09:23:57 +00:00
|
|
|
'email': '',
|
2010-09-11 11:34:51 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
address_id = address_ids[0]
|
|
|
|
address = address_pool.browse(cr, user, address_id)
|
2010-07-21 13:32:00 +00:00
|
|
|
res = {
|
2010-09-11 11:34:51 +00:00
|
|
|
'partner_name': address.partner_id and address.partner_id.name or '',
|
|
|
|
'contactname': address.name,
|
|
|
|
'street': address.street or '',
|
|
|
|
'street2': address.street2 or '',
|
|
|
|
'zip': address.zip or '',
|
|
|
|
'city': address.city or '',
|
|
|
|
'country': address.country_id and address.country_id.name or '',
|
|
|
|
'state': address.state_id and address.state_id.name or '',
|
|
|
|
'email': address.email or '',
|
|
|
|
'phone': address.phone or '',
|
|
|
|
'mobile': address.mobile or '',
|
|
|
|
'fax': address.fax or '',
|
2010-09-13 13:16:39 +00:00
|
|
|
'partner_id': address.partner_id and str(address.partner_id.id) or '',
|
2010-09-11 11:34:51 +00:00
|
|
|
'res_id': str(address.id),
|
2010-07-21 13:32:00 +00:00
|
|
|
}
|
|
|
|
return res.items()
|
2010-07-22 13:43:18 +00:00
|
|
|
|
2010-09-11 11:34:51 +00:00
|
|
|
def update_contact(self, cr, user, vals):
|
2010-07-21 13:32:00 +00:00
|
|
|
dictcreate = dict(vals)
|
2010-07-28 13:34:59 +00:00
|
|
|
res_id = dictcreate.get('res_id',False)
|
2010-09-11 11:34:51 +00:00
|
|
|
result = {}
|
|
|
|
address_pool = self.pool.get('res.partner.address')
|
|
|
|
if not (dictcreate.get('partner_id')): # TOCHECK: It should be check res_id or not
|
2010-08-10 11:26:58 +00:00
|
|
|
dictcreate.update({'partner_id': False})
|
2010-09-11 11:34:51 +00:00
|
|
|
create_id = address_pool.create(cr, user, dictcreate)
|
2010-08-10 11:33:52 +00:00
|
|
|
return create_id
|
2010-08-10 11:26:58 +00:00
|
|
|
|
2010-07-28 13:34:59 +00:00
|
|
|
if res_id:
|
2010-09-11 11:34:51 +00:00
|
|
|
address_data = address_pool.read(cr, user, int(res_id), [])
|
2010-08-13 12:20:05 +00:00
|
|
|
result = {
|
2010-09-11 11:34:51 +00:00
|
|
|
'partner_id': address_data['partner_id'] and address_data['partner_id'][0] or False, #TOFIX: parter_id should take from address_data
|
2010-08-13 12:20:05 +00:00
|
|
|
'country_id': dictcreate['country_id'] and int(dictcreate['country_id'][0]) or False,
|
|
|
|
'state_id': dictcreate['state_id'] and int(dictcreate['state_id'][0]) or False,
|
|
|
|
'name': dictcreate['name'],
|
|
|
|
'street': dictcreate['street'],
|
|
|
|
'street2': dictcreate['street2'],
|
|
|
|
'zip': dictcreate['zip'],
|
|
|
|
'city': dictcreate['city'],
|
|
|
|
'phone': dictcreate['phone'],
|
|
|
|
'fax': dictcreate['fax'],
|
|
|
|
'mobile': dictcreate['mobile'],
|
|
|
|
'email': dictcreate['email'],
|
|
|
|
}
|
2010-09-11 11:34:51 +00:00
|
|
|
address_pool.write(cr, user, int(res_id), result )
|
2010-07-28 13:34:59 +00:00
|
|
|
return True
|
2010-01-11 10:43:23 +00:00
|
|
|
|
2010-08-03 09:36:44 +00:00
|
|
|
def create_partner(self,cr,user,vals):
|
2010-01-11 10:43:23 +00:00
|
|
|
dictcreate = dict(vals)
|
2010-08-03 09:36:44 +00:00
|
|
|
partner_obj = self.pool.get('res.partner')
|
|
|
|
search_id = partner_obj.search(cr, user,[('name','=',dictcreate['name'])])
|
2010-01-11 10:43:23 +00:00
|
|
|
if search_id:
|
|
|
|
return 0
|
2010-08-03 09:36:44 +00:00
|
|
|
create_id = partner_obj.create(cr, user, dictcreate)
|
2010-08-02 13:44:02 +00:00
|
|
|
return create_id
|
2010-07-14 13:43:21 +00:00
|
|
|
|
2010-08-03 09:36:44 +00:00
|
|
|
def search_document(self,cr,user,vals):
|
2010-01-11 10:43:23 +00:00
|
|
|
dictcreate = dict(vals)
|
|
|
|
search_id = self.pool.get('ir.model').search(cr, user,[('model','=',dictcreate['model'])])
|
|
|
|
return (search_id and search_id[0]) or 0
|
|
|
|
|
2010-08-03 09:36:44 +00:00
|
|
|
def search_checkbox(self,cr,user,vals):
|
2010-01-11 10:43:23 +00:00
|
|
|
if vals[0]:
|
|
|
|
value = vals[0][0]
|
|
|
|
if vals[1]:
|
|
|
|
obj = vals[1];
|
|
|
|
name_get=[]
|
|
|
|
er_val=[]
|
|
|
|
for object in obj:
|
2010-05-12 13:13:17 +00:00
|
|
|
dyn_object = self.pool.get(object)
|
2010-01-11 10:43:23 +00:00
|
|
|
if object == 'res.partner.address':
|
2010-05-12 13:13:17 +00:00
|
|
|
search_id1 = dyn_object.search(cr,user,[('name','ilike',value)])
|
|
|
|
search_id2 = dyn_object.search(cr,user,[('email','=',value)])
|
2010-01-11 10:43:23 +00:00
|
|
|
if search_id1:
|
|
|
|
name_get.append(object)
|
2010-05-12 13:13:17 +00:00
|
|
|
name_get.append(dyn_object.name_get(cr, user, search_id1))
|
2010-01-11 10:43:23 +00:00
|
|
|
elif search_id2:
|
|
|
|
name_get.append(object)
|
2010-05-12 13:13:17 +00:00
|
|
|
name_get.append(dyn_object.name_get(cr, user, search_id2))
|
2010-01-11 10:43:23 +00:00
|
|
|
else:
|
|
|
|
try:
|
2010-05-12 13:13:17 +00:00
|
|
|
search_id1 = dyn_object.search(cr,user,[('name','ilike',value)])
|
2010-01-11 10:43:23 +00:00
|
|
|
if search_id1:
|
|
|
|
name_get.append(object)
|
2010-05-12 13:13:17 +00:00
|
|
|
name_get.append(dyn_object.name_get(cr, user, search_id1))
|
2010-01-11 10:43:23 +00:00
|
|
|
except:
|
|
|
|
er_val.append(object)
|
|
|
|
continue
|
|
|
|
if len(er_val) > 0:
|
|
|
|
name_get.append('error')
|
|
|
|
name_get.append(er_val)
|
|
|
|
return name_get
|
2010-08-03 09:36:44 +00:00
|
|
|
def list_alldocument(self,cr,user,vals):
|
2010-11-03 13:23:09 +00:00
|
|
|
obj_list= [('crm.lead','CRM Lead'),('project.issue','Project Issue'), ('hr.applicant','HR Applicant')]
|
2010-04-27 12:10:09 +00:00
|
|
|
object=[]
|
|
|
|
model_obj = self.pool.get('ir.model')
|
|
|
|
for obj in obj_list:
|
|
|
|
if model_obj.search(cr, user, [('model', '=', obj[0])]):
|
|
|
|
object.append(obj)
|
|
|
|
return object
|
|
|
|
|
2010-08-03 09:36:44 +00:00
|
|
|
def list_allcountry(self,cr,user,vals):
|
2010-07-28 13:34:59 +00:00
|
|
|
country_list = []
|
2010-10-08 06:32:36 +00:00
|
|
|
cr.execute("SELECT id, name from res_country order by name")
|
2010-07-28 13:34:59 +00:00
|
|
|
country_list = cr.fetchall()
|
|
|
|
return country_list
|
|
|
|
|
2010-10-07 11:30:18 +00:00
|
|
|
|
2010-08-03 09:36:44 +00:00
|
|
|
def list_allstate(self,cr,user,vals):
|
2010-10-08 06:32:36 +00:00
|
|
|
cr.execute("select id, name from res_country_state where country_id = %s order by name",(vals,) )
|
2010-10-07 11:30:18 +00:00
|
|
|
state_country_list = cr.fetchall()
|
|
|
|
return state_country_list
|
|
|
|
|
2010-07-28 13:34:59 +00:00
|
|
|
|
2010-08-03 09:36:44 +00:00
|
|
|
def search_document_attachment(self,cr,user,vals):
|
2010-05-04 10:24:37 +00:00
|
|
|
model_obj = self.pool.get('ir.model')
|
|
|
|
object=''
|
|
|
|
for obj in vals[0][1].split(','):
|
|
|
|
if model_obj.search(cr, user, [('model', '=', obj)]):
|
|
|
|
object += obj + ","
|
|
|
|
else:
|
|
|
|
object += "null,"
|
|
|
|
return object
|
|
|
|
|
2010-08-03 11:12:14 +00:00
|
|
|
thunderbird_partner()
|