166e1e8781fdb4670c5567aec0f987233e17eef8
[odoo/odoo.git] / addons / mail_gateway / scripts / openerp_mailgate / openerp_mailgate.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 #
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)
8 #
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.
13 #
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.
18 #
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/>
21 #
22 ###########################################################################################
23
24 import logging
25 import optparse
26 import sys
27 import xmlrpclib
28 import email
29
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)
33         self.user_id = uid
34         self.passwd = passwd
35         self.dbname = dbname
36
37     def __call__(self, *request, **kwargs):
38         return self.rpc.execute(self.dbname, self.user_id, self.passwd, *request, **kwargs)
39
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)
43         try:
44             self.model_id = int(model)
45             self.model = str(model)
46         except:
47             self.model_id = self.rpc('ir.model', 'search', [('model', '=', model)])[0]
48             self.model = str(model)
49         self.email_default = email_default
50
51
52     def parse(self, message):
53         try:
54             res_id = self.rpc('email.server.tools', 'process_email', self.model, message)
55         except Exception, e:
56             logger = logging.getLogger('mail-gateway')
57             logger.warning('Failed to process incoming email. Source of the failed mail is available at debug level.', exc_info=True)
58             logger.debug('Source of the mail that failed to parse:', message)
59             res_id = False
60
61 if __name__ == '__main__':
62     parser = optparse.OptionParser(usage='usage: %prog [options]', version='%prog v1.0')
63     group = optparse.OptionGroup(parser, "Note",
64         "This program parse a mail from standard input and communicate "
65         "with the Open ERP server for case management in the CRM module.")
66     parser.add_option_group(group)
67     parser.add_option("-u", "--user", dest="userid", help="ID of the user in Open ERP", default=1, type='int')
68     parser.add_option("-p", "--password", dest="password", help="Password of the user in Open ERP", default='admin')
69     parser.add_option("-o", "--model", dest="model", help="Name or ID of crm model", default="crm.lead")
70     parser.add_option("-m", "--default", dest="default", help="Default eMail in case of any trouble.", default=None)
71     parser.add_option("-d", "--dbname", dest="dbname", help="Database name (default: terp)", default='terp')
72     parser.add_option("--host", dest="host", help="Hostname of the Open ERP Server", default="localhost")
73     parser.add_option("--port", dest="port", help="Port of the Open ERP Server", default="8069")
74
75     (options, args) = parser.parse_args()
76
77     logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s")
78
79     parser = email_parser(options.userid, options.password, options.model, options.default, dbname=options.dbname, host=options.host, port=options.port)
80
81     msg_txt = sys.stdin.read()
82
83     parser.parse(msg_txt)
84
85 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: