2 # -*- coding: utf-8 -*-
3 ##############################################################################
5 # OpenERP, Open Source Management Solution
6 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # Copyright (C) 2010-TODAY OpenERP S.A. (http://www.openerp.com)
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as
11 # published by the Free Software Foundation, either version 3 of the
12 # License, or (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU Affero General Public License for more details.
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>
22 ###########################################################################################
30 class rpc_proxy(object):
31 def __init__(self, uid, passwd, host='localhost', port=8069, path='object', dbname='terp'):
32 self.rpc = xmlrpclib.ServerProxy('http://%s:%s/xmlrpc/%s' % (host, port, path), allow_none=True)
37 def __call__(self, *request, **kwargs):
38 return self.rpc.execute(self.dbname, self.user_id, self.passwd, *request, **kwargs)
40 class email_parser(object):
41 def __init__(self, uid, password, model, email_default, dbname, host, port):
42 self.rpc = rpc_proxy(uid, password, host=host, port=port, dbname=dbname)
44 self.model_id = int(model)
45 self.model = str(model)
47 self.model_id = self.rpc('ir.model', 'search', [('model', '=', model)])[0]
48 self.model = str(model)
49 self.email_default = email_default
52 def parse(self, message):
54 # pass message as bytes because we don't know its encoding until we parse its headers
55 # and hence can't convert it to utf-8 for transport
56 res_id = self.rpc('email.server.tools', 'process_email', self.model, xmlrpclib.Binary(message))
58 logger = logging.getLogger('mail-gateway')
59 logger.warning('Failed to process incoming email. Source of the failed mail is available at debug level.', exc_info=True)
60 logger.debug('Source of the mail that failed to parse:', message)
63 if __name__ == '__main__':
64 parser = optparse.OptionParser(usage='usage: %prog [options]', version='%prog v1.0')
65 group = optparse.OptionGroup(parser, "Note",
66 "This program parse a mail from standard input and communicate "
67 "with the Open ERP server for case management in the CRM module.")
68 parser.add_option_group(group)
69 parser.add_option("-u", "--user", dest="userid", help="ID of the user in Open ERP", default=1, type='int')
70 parser.add_option("-p", "--password", dest="password", help="Password of the user in Open ERP", default='admin')
71 parser.add_option("-o", "--model", dest="model", help="Name or ID of crm model", default="crm.lead")
72 parser.add_option("-m", "--default", dest="default", help="Default eMail in case of any trouble.", default=None)
73 parser.add_option("-d", "--dbname", dest="dbname", help="Database name (default: terp)", default='terp')
74 parser.add_option("--host", dest="host", help="Hostname of the Open ERP Server", default="localhost")
75 parser.add_option("--port", dest="port", help="Port of the Open ERP Server", default="8069")
77 (options, args) = parser.parse_args()
79 logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")
81 parser = email_parser(options.userid, options.password, options.model, options.default, dbname=options.dbname, host=options.host, port=options.port)
83 msg_txt = sys.stdin.read()
87 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: