2010-01-03 06:57:57 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
##############################################################################
|
|
|
|
|
#
|
|
|
|
|
# OpenERP, Open Source Management Solution
|
2010-01-12 09:18:39 +00:00
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2010-01-03 06:57:57 +00:00
|
|
|
|
#
|
|
|
|
|
# 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/>.
|
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
import re
|
|
|
|
|
import os
|
|
|
|
|
|
2010-04-22 13:29:18 +00:00
|
|
|
|
import binascii
|
2010-01-03 06:57:57 +00:00
|
|
|
|
import mx.DateTime
|
|
|
|
|
import base64
|
|
|
|
|
|
|
|
|
|
from tools.translate import _
|
|
|
|
|
|
|
|
|
|
import tools
|
|
|
|
|
from osv import fields,osv,orm
|
|
|
|
|
from osv.orm import except_orm
|
2010-06-11 13:26:24 +00:00
|
|
|
|
import collections
|
2010-01-03 06:57:57 +00:00
|
|
|
|
|
2010-04-19 20:57:01 +00:00
|
|
|
|
from tools import command_re
|
|
|
|
|
|
2010-06-11 13:26:24 +00:00
|
|
|
|
class mailgate_thread(osv.osv):
|
|
|
|
|
""" mailgate_thread """
|
2010-03-22 10:40:26 +00:00
|
|
|
|
|
2010-05-03 12:30:48 +00:00
|
|
|
|
_name = "mailgate.thread"
|
|
|
|
|
_inherit = "mailgate.thread"
|
2010-01-03 06:57:57 +00:00
|
|
|
|
|
2010-04-15 15:42:20 +00:00
|
|
|
|
def message_new(self, cr, uid, msg, context):
|
2010-04-19 20:57:01 +00:00
|
|
|
|
"""
|
2010-04-15 15:42:20 +00:00
|
|
|
|
Automatically calls when new email message arrives
|
|
|
|
|
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks
|
2010-03-22 10:40:26 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2010-04-15 15:42:20 +00:00
|
|
|
|
mailgate_pool = self.pool.get('email.server.tools')
|
|
|
|
|
|
|
|
|
|
subject = msg.get('subject')
|
|
|
|
|
body = msg.get('body')
|
|
|
|
|
msg_from = msg.get('from')
|
|
|
|
|
priority = msg.get('priority')
|
|
|
|
|
|
|
|
|
|
vals = {
|
|
|
|
|
'name': subject,
|
2010-03-31 14:14:42 +00:00
|
|
|
|
'email_from': msg_from,
|
2010-04-15 15:42:20 +00:00
|
|
|
|
'email_cc': msg.get('cc'),
|
|
|
|
|
'description': body,
|
2010-01-03 06:57:57 +00:00
|
|
|
|
'user_id': False,
|
|
|
|
|
}
|
2010-04-15 15:42:20 +00:00
|
|
|
|
if msg.get('priority', False):
|
|
|
|
|
vals['priority'] = priority
|
|
|
|
|
|
|
|
|
|
res = mailgate_pool.get_partner(cr, uid, msg.get('from'))
|
2010-01-03 06:57:57 +00:00
|
|
|
|
if res:
|
2010-04-15 15:42:20 +00:00
|
|
|
|
vals.update(res)
|
|
|
|
|
res = self.create(cr, uid, vals, context)
|
|
|
|
|
cases = self.browse(cr, uid, [res])
|
|
|
|
|
self._history(cr, uid, cases, _('Receive'), history=True, details=body, email_from=msg_from, message_id=msg.get('id'))
|
2010-04-22 12:47:54 +00:00
|
|
|
|
|
|
|
|
|
attachents = msg.get('attachments', [])
|
|
|
|
|
for attactment in attachents or []:
|
|
|
|
|
data_attach = {
|
|
|
|
|
'name': attactment,
|
|
|
|
|
'datas':binascii.b2a_base64(str(attachents.get(attactment))),
|
|
|
|
|
'datas_fname': attactment,
|
|
|
|
|
'description': 'Mail attachment',
|
|
|
|
|
'res_model': self._name,
|
|
|
|
|
'res_id': res,
|
|
|
|
|
}
|
|
|
|
|
self.pool.get('ir.attachment').create(cr, uid, data_attach)
|
|
|
|
|
|
2010-01-13 13:09:57 +00:00
|
|
|
|
return res
|
2010-03-22 10:40:26 +00:00
|
|
|
|
|
2010-04-17 21:07:21 +00:00
|
|
|
|
def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
|
2010-04-15 15:42:20 +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 ids: List of update mail’s IDs
|
|
|
|
|
"""
|
|
|
|
|
|
2010-02-15 07:54:47 +00:00
|
|
|
|
if isinstance(ids, (str, int, long)):
|
2010-04-15 15:42:20 +00:00
|
|
|
|
ids = [ids]
|
|
|
|
|
|
|
|
|
|
msg_from = msg['from']
|
|
|
|
|
vals.update({
|
|
|
|
|
'description': msg['body']
|
2010-01-03 06:57:57 +00:00
|
|
|
|
})
|
2010-04-15 15:42:20 +00:00
|
|
|
|
if msg.get('priority', False):
|
|
|
|
|
vals['priority'] = msg.get('priority')
|
|
|
|
|
|
2010-04-19 17:46:35 +00:00
|
|
|
|
maps = {
|
|
|
|
|
'cost':'planned_cost',
|
|
|
|
|
'revenue': 'planned_revenue',
|
|
|
|
|
'probability':'probability'
|
|
|
|
|
}
|
|
|
|
|
vls = { }
|
|
|
|
|
for line in msg['body'].split('\n'):
|
|
|
|
|
line = line.strip()
|
|
|
|
|
res = command_re.match(line)
|
|
|
|
|
if res and maps.get(res.group(1).lower(), False):
|
|
|
|
|
key = maps.get(res.group(1).lower())
|
|
|
|
|
vls[key] = res.group(2).lower()
|
|
|
|
|
|
|
|
|
|
vals.update(vls)
|
2010-04-15 15:42:20 +00:00
|
|
|
|
res = self.write(cr, uid, ids, vals)
|
|
|
|
|
cases = self.browse(cr, uid, ids)
|
2010-04-17 21:07:21 +00:00
|
|
|
|
message_id = context.get('references_id', False)
|
2010-04-15 15:42:20 +00:00
|
|
|
|
self._history(cr, uid, cases, _('Receive'), history=True, details=msg['body'], email_from=msg_from, message_id=message_id)
|
|
|
|
|
#getattr(self, act)(cr, uid, select)
|
2010-01-05 12:50:51 +00:00
|
|
|
|
return res
|
2010-01-03 06:57:57 +00:00
|
|
|
|
|
2010-06-11 13:26:24 +00:00
|
|
|
|
def emails_get(self, cr, uid, ids, context=None):
|
2010-03-22 10:40:26 +00:00
|
|
|
|
|
2010-04-15 15:42:20 +00:00
|
|
|
|
"""
|
|
|
|
|
Get Emails
|
|
|
|
|
@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 ids: List of email’s IDs
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
2010-03-22 10:40:26 +00:00
|
|
|
|
"""
|
2010-06-11 13:26:24 +00:00
|
|
|
|
res = {}
|
|
|
|
|
|
2010-01-03 06:57:57 +00:00
|
|
|
|
if isinstance(ids, (str, int, long)):
|
2010-06-11 13:26:24 +00:00
|
|
|
|
select = [long(ids)]
|
2010-01-03 06:57:57 +00:00
|
|
|
|
else:
|
|
|
|
|
select = ids
|
2010-06-11 13:26:24 +00:00
|
|
|
|
|
|
|
|
|
for thread in self.browse(cr, uid, select, context=context):
|
|
|
|
|
values = collections.defaultdict(set)
|
|
|
|
|
|
|
|
|
|
for message in thread.message_ids:
|
|
|
|
|
user_email = (message.user_id and message.user_id.address_id and message.user_id.address_id.email) or False
|
|
|
|
|
values['user_email'].add(user_email)
|
|
|
|
|
values['email_from'].add(message.email_from)
|
|
|
|
|
values['email_cc'].add(message.email_cc or False)
|
|
|
|
|
|
|
|
|
|
res[str(thread.id)] = dict((key,list(values[key])) for key, value in values.iteritems())
|
|
|
|
|
|
2010-01-03 06:57:57 +00:00
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
def msg_send(self, cr, uid, id, *args, **argv):
|
2010-03-22 10:40:26 +00:00
|
|
|
|
|
|
|
|
|
""" Send The Message
|
|
|
|
|
@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 ids: List of email’s IDs
|
|
|
|
|
@param *args: Return Tuple Value
|
|
|
|
|
@param **args: Return Dictionary of Keyword Value
|
|
|
|
|
"""
|
|
|
|
|
return True
|
2010-01-03 06:57:57 +00:00
|
|
|
|
|
2010-06-11 13:26:24 +00:00
|
|
|
|
mailgate_thread()
|