2010-03-17 04:30:34 +00:00
|
|
|
#!/usr/bin/env python
|
2009-10-13 05:58:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-06-16 11:00:21 +00:00
|
|
|
##############################################################################
|
2010-06-25 12:18:47 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2010-06-11 13:07:16 +00:00
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
|
|
|
# Copyright (C) 2010-TODAY OpenERP S.A. (http://www.openerp.com)
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2008-11-03 19:18:56 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 11:15:34 +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.
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2008-11-03 19:18:56 +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
|
2009-10-14 11:15:34 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2010-06-25 12:18:47 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
###########################################################################################
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2010-06-11 13:07:16 +00:00
|
|
|
import logging
|
|
|
|
import optparse
|
2010-06-24 19:53:32 +00:00
|
|
|
import sys
|
|
|
|
import xmlrpclib
|
|
|
|
import email
|
2010-03-17 04:30:34 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
class rpc_proxy(object):
|
2010-09-30 15:30:03 +00:00
|
|
|
def __init__(self, uid, passwd, host='localhost', port=8069, path='object', dbname='openerp'):
|
2010-06-24 19:53:32 +00:00
|
|
|
self.rpc = xmlrpclib.ServerProxy('http://%s:%s/xmlrpc/%s' % (host, port, path), allow_none=True)
|
2008-07-22 15:11:28 +00:00
|
|
|
self.user_id = uid
|
|
|
|
self.passwd = passwd
|
|
|
|
self.dbname = dbname
|
2007-04-02 09:40:45 +00:00
|
|
|
|
2010-03-09 14:13:53 +00:00
|
|
|
def __call__(self, *request, **kwargs):
|
|
|
|
return self.rpc.execute(self.dbname, self.user_id, self.passwd, *request, **kwargs)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
class email_parser(object):
|
2010-06-25 22:02:47 +00:00
|
|
|
def __init__(self, uid, password, model, email_default, dbname, host, port):
|
2009-10-27 12:27:46 +00:00
|
|
|
self.rpc = rpc_proxy(uid, password, host=host, port=port, dbname=dbname)
|
2008-07-22 15:11:28 +00:00
|
|
|
try:
|
2010-06-24 19:53:32 +00:00
|
|
|
self.model_id = int(model)
|
2010-03-08 13:49:55 +00:00
|
|
|
self.model = str(model)
|
2008-07-22 15:11:28 +00:00
|
|
|
except:
|
2010-06-24 19:53:32 +00:00
|
|
|
self.model_id = self.rpc('ir.model', 'search', [('model', '=', model)])[0]
|
2010-03-08 13:49:55 +00:00
|
|
|
self.model = str(model)
|
2008-07-22 15:11:28 +00:00
|
|
|
self.email_default = email_default
|
2010-06-25 12:18:47 +00:00
|
|
|
|
2008-07-22 15:11:28 +00:00
|
|
|
|
2010-09-30 15:30:03 +00:00
|
|
|
def parse(self, message, custom_values=None):
|
|
|
|
if custom_values is None:
|
|
|
|
custom_values = {}
|
2008-07-22 15:11:28 +00:00
|
|
|
try:
|
2010-07-02 13:44:45 +00:00
|
|
|
# pass message as bytes because we don't know its encoding until we parse its headers
|
|
|
|
# and hence can't convert it to utf-8 for transport
|
2010-09-30 15:30:03 +00:00
|
|
|
res_id = self.rpc('email.server.tools', 'process_email', self.model, xmlrpclib.Binary(message), custom_values)
|
2010-06-24 19:53:32 +00:00
|
|
|
except Exception, e:
|
2010-06-25 12:18:47 +00:00
|
|
|
logger = logging.getLogger('mail-gateway')
|
|
|
|
logger.warning('Failed to process incoming email. Source of the failed mail is available at debug level.', exc_info=True)
|
|
|
|
logger.debug('Source of the mail that failed to parse:', message)
|
2010-06-24 19:53:32 +00:00
|
|
|
res_id = False
|
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
if __name__ == '__main__':
|
2010-03-19 10:51:46 +00:00
|
|
|
parser = optparse.OptionParser(usage='usage: %prog [options]', version='%prog v1.0')
|
2010-06-25 12:18:47 +00:00
|
|
|
group = optparse.OptionGroup(parser, "Note",
|
2008-07-22 15:11:28 +00:00
|
|
|
"This program parse a mail from standard input and communicate "
|
2010-08-10 11:35:06 +00:00
|
|
|
"with the OpenERP server for case management in the CRM module.")
|
2008-07-22 15:11:28 +00:00
|
|
|
parser.add_option_group(group)
|
2010-08-10 11:35:06 +00:00
|
|
|
parser.add_option("-u", "--user", dest="userid", help="ID of the user in OpenERP", default=1, type='int')
|
|
|
|
parser.add_option("-p", "--password", dest="password", help="Password of the user in OpenERP", default='admin')
|
2010-03-08 13:49:55 +00:00
|
|
|
parser.add_option("-o", "--model", dest="model", help="Name or ID of crm model", default="crm.lead")
|
2008-07-22 15:11:28 +00:00
|
|
|
parser.add_option("-m", "--default", dest="default", help="Default eMail in case of any trouble.", default=None)
|
2010-09-30 15:30:03 +00:00
|
|
|
parser.add_option("-d", "--dbname", dest="dbname", help="Database name (default: openerp)", default='openerp')
|
2010-08-10 11:35:06 +00:00
|
|
|
parser.add_option("--host", dest="host", help="Hostname of the OpenERP Server", default="localhost")
|
|
|
|
parser.add_option("--port", dest="port", help="Port of the OpenERP Server", default="8069")
|
2010-09-30 15:30:03 +00:00
|
|
|
parser.add_option("--custom-values", dest="custom_values", help="Add Custom Values to the object", default=None)
|
2009-10-08 13:40:36 +00:00
|
|
|
|
2008-07-22 15:11:28 +00:00
|
|
|
(options, args) = parser.parse_args()
|
2010-06-11 13:07:16 +00:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")
|
|
|
|
|
2010-06-25 22:02:47 +00:00
|
|
|
parser = email_parser(options.userid, options.password, options.model, options.default, dbname=options.dbname, host=options.host, port=options.port)
|
2007-04-13 10:43:49 +00:00
|
|
|
|
2010-06-24 19:53:32 +00:00
|
|
|
msg_txt = sys.stdin.read()
|
2007-04-13 10:45:40 +00:00
|
|
|
|
2010-09-30 15:30:03 +00:00
|
|
|
custom_values = {}
|
|
|
|
try:
|
|
|
|
custom_values = dict(eval(options.custom_values))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
parser.parse(msg_txt, custom_values)
|
2010-06-25 12:18:47 +00:00
|
|
|
|
2010-03-22 05:10:19 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|