2012-06-29 10:40:21 +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
|
|
|
#
|
2011-02-25 13:56:54 +00:00
|
|
|
##############################################################################
|
|
|
|
"""
|
|
|
|
openerp_mailgate.py
|
|
|
|
"""
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2011-02-25 13:56:54 +00:00
|
|
|
import cgitb
|
|
|
|
import time
|
2010-06-11 13:07:16 +00:00
|
|
|
import optparse
|
2010-06-24 19:53:32 +00:00
|
|
|
import sys
|
|
|
|
import xmlrpclib
|
2011-02-25 13:56:54 +00:00
|
|
|
import smtplib
|
|
|
|
from email.MIMEMultipart import MIMEMultipart
|
|
|
|
from email.MIMEBase import MIMEBase
|
|
|
|
from email.MIMEText import MIMEText
|
|
|
|
from email.Utils import COMMASPACE, formatdate
|
|
|
|
from email import Encoders
|
|
|
|
|
|
|
|
class DefaultConfig(object):
|
|
|
|
"""
|
|
|
|
Default configuration
|
|
|
|
"""
|
|
|
|
OPENERP_DEFAULT_USER_ID = 1
|
|
|
|
OPENERP_DEFAULT_PASSWORD = 'admin'
|
|
|
|
OPENERP_HOSTNAME = 'localhost'
|
|
|
|
OPENERP_PORT = 8069
|
|
|
|
OPENERP_DEFAULT_DATABASE = 'openerp'
|
|
|
|
MAIL_ERROR = 'error@example.com'
|
2011-09-07 15:13:48 +00:00
|
|
|
MAIL_SERVER = 'smtp.example.com'
|
|
|
|
MAIL_SERVER_PORT = 25
|
|
|
|
MAIL_ADMINS = ('info@example.com',)
|
2011-02-25 13:56:54 +00:00
|
|
|
|
|
|
|
config = DefaultConfig()
|
|
|
|
|
|
|
|
|
2011-09-07 15:13:48 +00:00
|
|
|
def send_mail(_from_, to_, subject, text, files=None, server=config.MAIL_SERVER, port=config.MAIL_SERVER_PORT):
|
2011-02-25 13:56:54 +00:00
|
|
|
assert isinstance(to_, (list, tuple))
|
|
|
|
|
|
|
|
if files is None:
|
|
|
|
files = []
|
|
|
|
|
|
|
|
msg = MIMEMultipart()
|
|
|
|
msg['From'] = _from_
|
|
|
|
msg['To'] = COMMASPACE.join(to_)
|
|
|
|
msg['Date'] = formatdate(localtime=True)
|
|
|
|
msg['Subject'] = subject
|
|
|
|
|
|
|
|
msg.attach( MIMEText(text) )
|
|
|
|
|
|
|
|
for file_name, file_content in files:
|
|
|
|
part = MIMEBase('application', "octet-stream")
|
|
|
|
part.set_payload( file_content )
|
|
|
|
Encoders.encode_base64(part)
|
|
|
|
part.add_header('Content-Disposition', 'attachment; filename="%s"'
|
|
|
|
% file_name)
|
|
|
|
msg.attach(part)
|
2010-03-17 04:30:34 +00:00
|
|
|
|
2011-09-07 15:13:48 +00:00
|
|
|
smtp = smtplib.SMTP(server, port=port)
|
2011-02-25 13:56:54 +00:00
|
|
|
smtp.sendmail(_from_, to_, msg.as_string() )
|
|
|
|
smtp.close()
|
|
|
|
|
|
|
|
class RPCProxy(object):
|
|
|
|
def __init__(self, uid, passwd,
|
|
|
|
host=config.OPENERP_HOSTNAME,
|
|
|
|
port=config.OPENERP_PORT,
|
|
|
|
path='object',
|
|
|
|
dbname=config.OPENERP_DEFAULT_DATABASE):
|
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
|
|
|
|
2011-02-25 13:56:54 +00:00
|
|
|
class EmailParser(object):
|
2012-06-29 10:22:18 +00:00
|
|
|
def __init__(self, uid, password, dbname, host, port, model=False, email_default=False):
|
2011-02-25 13:56:54 +00:00
|
|
|
self.rpc = RPCProxy(uid, password, host=host, port=port, dbname=dbname)
|
2012-06-29 10:22:18 +00:00
|
|
|
if model:
|
|
|
|
try:
|
|
|
|
self.model_id = int(model)
|
|
|
|
self.model = str(model)
|
|
|
|
except:
|
|
|
|
self.model_id = self.rpc('ir.model', 'search', [('model', '=', model)])[0]
|
|
|
|
self.model = str(model)
|
|
|
|
self.email_default = email_default
|
2010-06-25 12:18:47 +00:00
|
|
|
|
2008-07-22 15:11:28 +00:00
|
|
|
|
2012-08-07 18:04:12 +00:00
|
|
|
def parse(self, message, custom_values=None, save_original=None):
|
2011-02-25 13:56:54 +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
|
2012-08-07 18:04:12 +00:00
|
|
|
return self.rpc('mail.thread',
|
|
|
|
'message_process',
|
|
|
|
self.model,
|
|
|
|
xmlrpclib.Binary(message),
|
|
|
|
custom_values or {},
|
|
|
|
save_original or False)
|
2010-06-24 19:53:32 +00:00
|
|
|
|
2011-02-25 13:56:54 +00:00
|
|
|
def configure_parser():
|
|
|
|
parser = optparse.OptionParser(usage='usage: %prog [options]', version='%prog v1.1')
|
2010-06-25 12:18:47 +00:00
|
|
|
group = optparse.OptionGroup(parser, "Note",
|
2012-06-29 10:22:18 +00:00
|
|
|
"This program parse a mail from standard input and communicate "
|
|
|
|
"with the OpenERP server for case management in the CRM module.")
|
2008-07-22 15:11:28 +00:00
|
|
|
parser.add_option_group(group)
|
2011-02-25 13:56:54 +00:00
|
|
|
parser.add_option("-u", "--user", dest="userid",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="OpenERP user id to connect with",
|
2011-02-25 13:56:54 +00:00
|
|
|
default=config.OPENERP_DEFAULT_USER_ID, type='int')
|
|
|
|
parser.add_option("-p", "--password", dest="password",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="OpenERP user password",
|
2011-02-25 13:56:54 +00:00
|
|
|
default=config.OPENERP_DEFAULT_PASSWORD)
|
2012-06-29 10:22:18 +00:00
|
|
|
parser.add_option("-o", "--model", dest="model",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="Name or ID of destination model",
|
2012-06-29 10:22:18 +00:00
|
|
|
default="crm.lead")
|
|
|
|
parser.add_option("-m", "--default", dest="default",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="Admin email for error notifications.",
|
2012-06-29 10:22:18 +00:00
|
|
|
default=None)
|
2011-02-25 13:56:54 +00:00
|
|
|
parser.add_option("-d", "--dbname", dest="dbname",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="OpenERP database name (default: %default)",
|
2011-02-25 13:56:54 +00:00
|
|
|
default=config.OPENERP_DEFAULT_DATABASE)
|
|
|
|
parser.add_option("--host", dest="host",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="OpenERP Server hostname",
|
2011-02-25 13:56:54 +00:00
|
|
|
default=config.OPENERP_HOSTNAME)
|
|
|
|
parser.add_option("--port", dest="port",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="OpenERP Server XML-RPC port number",
|
2011-02-25 13:56:54 +00:00
|
|
|
default=config.OPENERP_PORT)
|
2012-06-29 10:22:18 +00:00
|
|
|
parser.add_option("--custom-values", dest="custom_values",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="Dictionary of extra values to pass when creating records",
|
2012-06-29 10:22:18 +00:00
|
|
|
default=None)
|
|
|
|
parser.add_option("-s", dest="save_original",
|
|
|
|
action="store_true",
|
2012-08-07 18:04:12 +00:00
|
|
|
help="Keep a full copy of the email source attached to each message",
|
2012-06-29 10:22:18 +00:00
|
|
|
default=False)
|
2009-10-08 13:40:36 +00:00
|
|
|
|
2011-02-25 13:56:54 +00:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Receive the email via the stdin and send it to the OpenERP Server
|
|
|
|
"""
|
2012-06-29 10:40:21 +00:00
|
|
|
|
2011-02-25 13:56:54 +00:00
|
|
|
parser = configure_parser()
|
2008-07-22 15:11:28 +00:00
|
|
|
(options, args) = parser.parse_args()
|
2011-02-25 13:56:54 +00:00
|
|
|
email_parser = EmailParser(options.userid,
|
|
|
|
options.password,
|
2012-06-29 10:40:21 +00:00
|
|
|
options.dbname,
|
|
|
|
options.host,
|
|
|
|
options.port,
|
|
|
|
model=options.model,
|
|
|
|
email_default= options.default)
|
2010-06-24 19:53:32 +00:00
|
|
|
msg_txt = sys.stdin.read()
|
2012-06-29 10:22:18 +00:00
|
|
|
custom_values = {}
|
|
|
|
try:
|
2012-06-29 10:40:21 +00:00
|
|
|
custom_values = dict(eval(options.custom_values or "{}" ))
|
2012-06-29 10:22:18 +00:00
|
|
|
except:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2011-02-25 13:56:54 +00:00
|
|
|
try:
|
2012-08-07 18:04:12 +00:00
|
|
|
email_parser.parse(msg_txt, custom_values, options.save_original or False)
|
2011-02-25 13:56:54 +00:00
|
|
|
except Exception:
|
|
|
|
msg = '\n'.join([
|
|
|
|
'parameters',
|
|
|
|
'==========',
|
|
|
|
'%r' % (options,),
|
|
|
|
'traceback',
|
|
|
|
'=========',
|
|
|
|
'%s' % (cgitb.text(sys.exc_info())),
|
|
|
|
])
|
|
|
|
|
|
|
|
subject = '[OPENERP]:ERROR: Mailgateway - %s' % time.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
send_mail(
|
|
|
|
config.MAIL_ERROR,
|
|
|
|
config.MAIL_ADMINS,
|
|
|
|
subject, msg, files=[('message.txt', msg_txt)]
|
|
|
|
)
|
2011-09-07 15:13:48 +00:00
|
|
|
sys.stderr.write("Failed to deliver email to OpenERP Server, sending error notification to %s\n" % config.MAIL_ADMINS)
|
2011-02-25 13:56:54 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
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:
|