[IMP]crm : improve mailgate
bzr revid: hmo@tinyerp.com-20091027122746-fn0y4ukq9kn87rrw
This commit is contained in:
parent
7dc6bbf3f5
commit
8b9eba5ea9
|
@ -25,5 +25,8 @@ import crm_segmentation
|
|||
import report
|
||||
import wizard
|
||||
|
||||
|
||||
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
||||
|
|
|
@ -32,6 +32,11 @@ import tools
|
|||
from osv import fields,osv,orm
|
||||
from osv.orm import except_orm
|
||||
|
||||
from scripts.openerp_mailgate import openerp_mailgate
|
||||
import email
|
||||
import netsvc
|
||||
from poplib import POP3, POP3_SSL
|
||||
from imaplib import IMAP4, IMAP4_SSL
|
||||
|
||||
MAX_LEVEL = 15
|
||||
AVAILABLE_STATES = [
|
||||
|
@ -203,101 +208,126 @@ class crm_case_section(osv.osv):
|
|||
return res
|
||||
crm_case_section()
|
||||
|
||||
class crm_email_gateway_server(osv.osv):
|
||||
_name = "crm.email.gateway.server"
|
||||
_description = "Email Gateway Server"
|
||||
_columns = {
|
||||
'name': fields.char('Server Address',size=64,required=True ,help="IMAP/POP Address Of Email gateway Server"),
|
||||
'login': fields.char('User',size=64,required=True,help="User Login Id of Email gateway"),
|
||||
'password': fields.char('Password',size=64,required=True,help="User Password Of Email gateway"),
|
||||
'server_type': fields.selection([("pop","POP"),("imap","Imap")],"Type of Server", required=True, help="Type of Email gateway Server"),
|
||||
'port': fields.integer("Port" , help="Port Of Email gateway Server. If port is omitted, the standard POP3 port (110) is used for POP EMail Server and the standard IMAP4 port (143) is used for IMAP Sever."),
|
||||
'ssl': fields.boolean('SSL',help ="Use Secure Authentication"),
|
||||
'active': fields.boolean('Active'),
|
||||
}
|
||||
_defaults = {
|
||||
'server_type':lambda * a:'pop',
|
||||
'active':lambda * a:True,
|
||||
}
|
||||
crm_email_gateway_server()
|
||||
|
||||
|
||||
|
||||
class crm_email_gateway(osv.osv):
|
||||
_name = "crm.email.gateway"
|
||||
_description = "Email Gateway"
|
||||
_rec_name="login"
|
||||
_columns = {
|
||||
'pop': fields.char('POP Server Name',size=64,required=True ,help="POP Server Name Of Email gateway"),
|
||||
'login': fields.char('User',size=64,required=True,help="User Login Id of Email gateway"),
|
||||
'password': fields.char('Password',size=64,required=True,help="User Password Of Email gateway"),
|
||||
'email': fields.char('Email Id',size=64,help="Default eMail in case of any trouble."),
|
||||
'mailgateway':fields.selection([("fetchmail","Using Fetchmail"),("postfix","Using Postfix")],"EMail gateway", readonly=True),
|
||||
'section_id':fields.many2one('crm.case.section',"Section"),
|
||||
'path':fields.char("Path ",size=255,required= True,help ="Path of script file of Email gateway"),
|
||||
'port':fields.integer("Port" , help="Port Number "),
|
||||
'ssl':fields.boolean('Use Secure Authentication',help ="Use Secure Authentication"),
|
||||
}
|
||||
_defaults = {
|
||||
'path': lambda *a: tools.config['addons_path']+"/crm/scripts/openerp-mailgate/openerp-mailgate.py",
|
||||
'port':lambda * a:110,
|
||||
'mailgateway':lambda * a:'fetchmail',
|
||||
}
|
||||
|
||||
def _get_fetchmail_path(self, cr):
|
||||
return os.path.join(tools.config['root_path'], '.fetchmail', cr.dbname)
|
||||
|
||||
def fetch_mail(self, cr, uid, section_ids=[], context={}):
|
||||
path = self._get_fetchmail_path(cr)
|
||||
sect_obj = self.pool.get("crm.case.section")
|
||||
if not len(section_ids):
|
||||
section_ids = sect_obj.search(cr, uid, [])
|
||||
for section in sect_obj.browse(cr, uid, section_ids):
|
||||
fetch_file = path + "/" +section.name
|
||||
if os.path.isfile(fetch_file):
|
||||
try :
|
||||
os.system("fetchmail -f %s" %(fetch_file))
|
||||
except Exception, e:
|
||||
import netsvc
|
||||
netsvc.Logger().notifyChannel('fetchmail', netsvc.LOG_ERROR, "%s" % e)
|
||||
return True
|
||||
|
||||
def make_fetchmail(self, cr, uid, section_ids=[], context={}):
|
||||
sect_obj = self.pool.get("crm.case.section")
|
||||
user_obj = self.pool.get("res.users")
|
||||
for section in sect_obj.browse(cr, uid, section_ids, context):
|
||||
user = user_obj.browse(cr,uid,uid)
|
||||
path = self._get_fetchmail_path(cr)
|
||||
if not os.path.isdir(path):
|
||||
try:
|
||||
os.makedirs(path)
|
||||
except:
|
||||
raise except_orm(_('Permission Denied !'), _('You do not permissions to write on the server side.'))
|
||||
|
||||
fmail_path = path + "/" +section.name
|
||||
fmail = open(fmail_path , 'w')
|
||||
os.chmod(fmail_path,0710)
|
||||
for mailgateway in section.gateway_ids:
|
||||
mdatext = '%s -u %d -p %s -s %d -d %s '%(mailgateway.path,uid,user.password,mailgateway.section_id.id,cr.dbname)
|
||||
if mailgateway.email:
|
||||
mdatext += ' -m %s'%(mailgateway.email)
|
||||
if section.reply_to:
|
||||
mdatext += ' -e %s'%(section.reply_to)
|
||||
text = "\npoll %s user '%s' password '%s'"%(mailgateway.pop,mailgateway.login,mailgateway.password)
|
||||
if mailgateway.port:
|
||||
text += ' port %d' % (mailgateway.port)
|
||||
if mailgateway.ssl:
|
||||
text += ' ssl'
|
||||
text += " mda '%s'"%(mdatext)
|
||||
fmail.write(text)
|
||||
fmail.close()
|
||||
|
||||
def create(self, cr, uid, vals, context=None):
|
||||
res = super(crm_email_gateway, self).create(cr, uid, vals, context=context)
|
||||
self.make_fetchmail(cr, uid, [vals['section_id']])
|
||||
return res
|
||||
|
||||
def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
|
||||
res = super(crm_email_gateway, self).write(cr, uid, ids, vals, context)
|
||||
section_ids = []
|
||||
for gateway in self.browse(cr, uid, ids, context):
|
||||
if gateway.section_id.id not in section_ids:
|
||||
section_ids.append(gateway.section_id.id)
|
||||
self.make_fetchmail(cr, uid, section_ids)
|
||||
return res
|
||||
|
||||
def unlink(self, cr, uid, ids, context={}, check=True):
|
||||
section_ids = []
|
||||
for gateway in self.browse(cr, uid, ids, context):
|
||||
if gateway.section_id.id not in section_ids:
|
||||
section_ids.append(gateway.section_id.id)
|
||||
res = super(crm_email_gateway, self).unlink(cr, uid,ids, context=context)
|
||||
|
||||
self.make_fetchmail(cr, uid, section_ids)
|
||||
return res
|
||||
_columns = {
|
||||
'name': fields.many2one('crm.email.gateway.server',"Gateway Server", required=True),
|
||||
'to_email_id': fields.char('TO', size=64, help="Email address used in the From field of outgoing messages"),
|
||||
'cc_email_id': fields.char('CC',size=64,help="Default eMail in case of any trouble."),
|
||||
'section_id': fields.many2one('crm.case.section',"Section",required=True),
|
||||
'mail_history': fields.one2many("crm.email.history","gateway_id","History", readonly=True)
|
||||
}
|
||||
|
||||
def _fetch_mails(self, cr, uid, ids=False, context={}):
|
||||
'''
|
||||
Function called by the scheduler to fetch mails
|
||||
'''
|
||||
cr.execute('select * from crm_email_gateway gateway \
|
||||
inner join crm_email_gateway_server server \
|
||||
on server.id = gateway.name where server.active = True')
|
||||
ids2 = map(lambda x: x[0], cr.fetchall() or [])
|
||||
return self.fetch_mails(cr, uid, ids=ids2, context=context)
|
||||
|
||||
def fetch_mails(self, cr, uid, ids=[], section_ids=[], context={}):
|
||||
if len(section_ids):
|
||||
casesection_obj = self.pool.get('crm.case.section')
|
||||
for section in casesection_obj.read(cr, uid, section_ids, ['gateway_ids']):
|
||||
ids += section['gateway_ids']
|
||||
log_messages = []
|
||||
user_obj = self.pool.get('res.users')
|
||||
mail_history_obj = self.pool.get('crm.email.history')
|
||||
for mailgateway in self.browse(cr, uid, ids):
|
||||
try :
|
||||
mailgate_server = mailgateway.name
|
||||
if not mailgate_server.active:
|
||||
continue
|
||||
log_messages.append("Mail Server : %s:%d" % (mailgate_server.name, mailgate_server.port))
|
||||
log_messages.append("-"*20)
|
||||
email_messages = []
|
||||
read_messages = mailgateway.mail_history and map(lambda x:x.name, mailgateway.mail_history) or []
|
||||
new_messages = []
|
||||
if mailgate_server.server_type == 'pop':
|
||||
if mailgate_server.ssl:
|
||||
pop_server = POP3_SSL(mailgate_server.name or 'localhost', mailgate_server.port or 110)
|
||||
else:
|
||||
pop_server = POP3(mailgate_server.name or 'localhost', mailgate_server.port or 110)
|
||||
pop_server.user(mailgate_server.login)
|
||||
pop_server.pass_(mailgate_server.password)
|
||||
pop_server.list()
|
||||
(numMsgs, totalSize) = pop_server.stat()
|
||||
for i in range(1, numMsgs + 1):
|
||||
if i not in read_messages:
|
||||
(header, msges, octets) = pop_server.retr(i)
|
||||
for msg in msges:
|
||||
email_messages.append((i,msg))
|
||||
new_messages.append(i)
|
||||
pop_server.quit()
|
||||
|
||||
elif mailgate_server.server_type == 'imap':
|
||||
if mailgate_server.ssl:
|
||||
imap_server = IMAP4_SSL(mailgate_server.name or 'localhost', mailgate_server.port or 143)
|
||||
else:
|
||||
imap_server = IMAP4(mailgate_server.name or 'localhost', mailgate_server.port or 143)
|
||||
imap_server.login(mailgate_server.login, mailgate_server.password)
|
||||
imap_server.select()
|
||||
typ, data = imap_server.search(None, '(UNSEEN)')
|
||||
for num in data[0].split():
|
||||
typ, data = imap_server.fetch(num, '(RFC822)')
|
||||
email_messages.append((num, data[0][1]))
|
||||
new_messages.append(num)
|
||||
imap_server.close()
|
||||
imap_server.logout()
|
||||
|
||||
except Exception, e:
|
||||
log_messages.append("Error in Fetching Mail: %s " %(str(e)))
|
||||
netsvc.Logger().notifyChannel('Emailgate:Fetching mail:[%d]%s' % (mailgate_server.id, mailgate_server.name), netsvc.LOG_ERROR, str(e))
|
||||
|
||||
if len(email_messages):
|
||||
users = user_obj.read(cr, uid, uid, ['password'])
|
||||
parser = openerp_mailgate.email_parser(uid, users['password'], mailgateway.section_id.id,
|
||||
mailgateway.to_email_id or '', mailgateway.cc_email_id or '', dbname=cr.dbname,
|
||||
host=tools.config['interface'] or 'localhost', port=tools.config['port'] or '8069')
|
||||
for msg_id, message in email_messages:
|
||||
try :
|
||||
msg_txt = email.message_from_string(message)
|
||||
res = parser.parse(msg_txt)
|
||||
mail_history_obj.create(cr, uid, {'name':msg_id, 'case_id': res[0], 'gateway_id':mailgateway.id})
|
||||
log_messages.append('Case Successfull created : %d' % res[0])
|
||||
|
||||
except Exception, e:
|
||||
log_messages.append("Error in Parsing Mail: %s " %(str(e)))
|
||||
netsvc.Logger().notifyChannel('Emailgate:Parsing mail:[%d]%s' % (mailgate_server.id, mailgate_server.name), netsvc.LOG_ERROR, str(e))
|
||||
|
||||
log_messages.append("="*20)
|
||||
log_messages.append("Total Read Mail: %d" %(len(new_messages)))
|
||||
return log_messages
|
||||
|
||||
crm_email_gateway()
|
||||
|
||||
|
||||
|
||||
class crm_case_categ(osv.osv):
|
||||
_name = "crm.case.categ"
|
||||
_description = "Category of case"
|
||||
|
@ -980,6 +1010,16 @@ class crm_case_history(osv.osv):
|
|||
}
|
||||
crm_case_history()
|
||||
|
||||
class crm_email_history(osv.osv):
|
||||
_name = "crm.email.history"
|
||||
_description = "Email History"
|
||||
_columns = {
|
||||
'name': fields.char('Message Id', size=64, help="Message Id in Email Server."),
|
||||
'case_id': fields.many2one('crm.case',"Case", required=True),
|
||||
'gateway_id': fields.many2one('crm.email.gateway',"Email Gateway", required=True),
|
||||
}
|
||||
_order = 'case_id desc'
|
||||
crm_email_history()
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@
|
|||
<field name="numbercall">-1</field>
|
||||
<field eval="False" name="doall"/>
|
||||
<field eval="'crm.email.gateway'" name="model"/>
|
||||
<field eval="'fetch_mail'" name="function"/>
|
||||
<field eval="'_fetch_mails'" name="function"/>
|
||||
<field eval="'()'" name="args"/>
|
||||
</record>
|
||||
|
||||
|
|
|
@ -40,29 +40,24 @@
|
|||
<field name="allow_unlink" select="2"/>
|
||||
<field name="reply_to" select="2"/>
|
||||
<field name="gateway_ids" widget="one2many_list" nolabel="1" colspan="4">
|
||||
<tree string="Email Gateway">
|
||||
<field name="pop" />
|
||||
<field name="login"/>
|
||||
<field name="email" />
|
||||
<field name="mailgateway"/>
|
||||
<tree string="Email Gateway" editable="bottom">
|
||||
<field name="name" />
|
||||
|
||||
</tree>
|
||||
<form string="Email Gateway">
|
||||
<field name="mailgateway"/>
|
||||
<notebook colspan="4">
|
||||
<page string="Server Setting">
|
||||
<field name="pop" />
|
||||
<field name="port" />
|
||||
<field name="login" />
|
||||
<field name="password" password="True"/>
|
||||
</page>
|
||||
<page string="Security Setting">
|
||||
<field name="ssl" />
|
||||
</page>
|
||||
<page string="Email Gateway">
|
||||
<field name="email" />
|
||||
<field name="path" />
|
||||
</page>
|
||||
</notebook>
|
||||
<form string="Email Gateway">
|
||||
<field name="name" colspan="4"/>
|
||||
<field name="to_email_id"/>
|
||||
<field name="cc_email_id" />
|
||||
<field name="mail_history" widget="one2many_list" nolabel="1" colspan="4">
|
||||
<tree string="Email History">
|
||||
<field name="name"/>
|
||||
<field name="case_id"/>
|
||||
</tree>
|
||||
<form string="Email History">
|
||||
<field name="name"/>
|
||||
<field name="case_id"/>
|
||||
</form>
|
||||
</field>
|
||||
</form>
|
||||
</field>
|
||||
</page>
|
||||
|
@ -622,56 +617,52 @@
|
|||
|
||||
<act_window domain="[('user_id', '=', active_id),('state','<>','done'),('state','<>','cancel'),('state','<>','pending')]" id="act_res_users_2_crm_case_opened" name="Open cases" res_model="crm.case" src_model="res.users" view_mode="tree,form,calendar" view_type="form"/>
|
||||
|
||||
<record id="crm_email_gateway_form" model="ir.ui.view">
|
||||
<field name="name">crm.email.gateway.form</field>
|
||||
<field name="model">crm.email.gateway</field>
|
||||
|
||||
<record id="crm_email_gateway_server_form" model="ir.ui.view">
|
||||
<field name="name">crm.email.gateway.server.form</field>
|
||||
<field name="model">crm.email.gateway.server</field>
|
||||
<field name="type">form</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Email Gateway">
|
||||
<field name="section_id" widget="selection"/>
|
||||
<field name="mailgateway"/>
|
||||
<form string="Email Gateway Server">
|
||||
<field name="server_type"/>
|
||||
<notebook colspan="4">
|
||||
<page string="Server Setting">
|
||||
<field name="pop" />
|
||||
<field name="port" />
|
||||
<page string="Server Info">
|
||||
<field name="name"/>
|
||||
<field name="port" />
|
||||
<field name="login" />
|
||||
<field name="password" password="True"/>
|
||||
</page>
|
||||
<page string="Security Setting">
|
||||
<field name="ssl" />
|
||||
</page>
|
||||
<page string="Email Gateway">
|
||||
<field name="email" />
|
||||
<field name="path" />
|
||||
</page>
|
||||
<field name="ssl" />
|
||||
<field name="active" />
|
||||
</page>
|
||||
</notebook>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="crm_email_gateway_tree" model="ir.ui.view">
|
||||
<field name="name">crm.email.gateway.tree</field>
|
||||
<field name="model">crm.email.gateway</field>
|
||||
<record id="crm_email_gateway_server_tree" model="ir.ui.view">
|
||||
<field name="name">crm.email.gateway.server.tree</field>
|
||||
<field name="model">crm.email.gateway.server</field>
|
||||
<field name="type">tree</field>
|
||||
<field name="arch" type="xml">
|
||||
<tree string="Gateway">
|
||||
<field name="section_id"/>
|
||||
<field name="mailgateway"/>
|
||||
<field name="pop" />
|
||||
<field name="login"/>
|
||||
<field name="email" />
|
||||
<field name="path" />
|
||||
<tree string="Email Gateway Server">
|
||||
<field name="name"/>
|
||||
<field name="port" />
|
||||
<field name="server_type"/>
|
||||
<field name="ssl" />
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
<record id="crm_email_gateway_act" model="ir.actions.act_window">
|
||||
<field name="name">Email Gateway</field>
|
||||
<field name="res_model">crm.email.gateway</field>
|
||||
|
||||
|
||||
<record id="crm_email_gateway_server_act" model="ir.actions.act_window">
|
||||
<field name="name">Email Gateway Server</field>
|
||||
<field name="res_model">crm.email.gateway.server</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">tree,form</field>
|
||||
<field name="view_id" ref="crm_email_gateway_tree"/>
|
||||
<field name="view_id" ref="crm_email_gateway_server_tree"/>
|
||||
</record>
|
||||
<menuitem id="crm_email_gateway_menu" name="Email Gateway" parent="next_id_51" action="crm_email_gateway_act" />
|
||||
|
||||
<menuitem id="crm_email_gateway_server_menu" name="Email Gateway Server" parent="next_id_51" action="crm_email_gateway_server_act" />
|
||||
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
||||
#
|
||||
# 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 openerp_mailgate
|
||||
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
||||
#
|
||||
# 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 openerp_mailgate
|
|
@ -49,8 +49,83 @@ priorities = {
|
|||
'5': '5 (Lowest)',
|
||||
}
|
||||
|
||||
def html2plaintext(html, body_id=None, encoding='utf-8'):
|
||||
## (c) Fry-IT, www.fry-it.com, 2007
|
||||
## <peter@fry-it.com>
|
||||
## download here: http://www.peterbe.com/plog/html2plaintext
|
||||
|
||||
|
||||
""" from an HTML text, convert the HTML to plain text.
|
||||
If @body_id is provided then this is the tag where the
|
||||
body (not necessarily <body>) starts.
|
||||
"""
|
||||
try:
|
||||
from BeautifulSoup import BeautifulSoup, SoupStrainer, Comment
|
||||
except:
|
||||
return html
|
||||
|
||||
urls = []
|
||||
if body_id is not None:
|
||||
strainer = SoupStrainer(id=body_id)
|
||||
else:
|
||||
strainer = SoupStrainer('body')
|
||||
|
||||
soup = BeautifulSoup(html, parseOnlyThese=strainer, fromEncoding=encoding)
|
||||
for link in soup.findAll('a'):
|
||||
title = link.renderContents()
|
||||
for url in [x[1] for x in link.attrs if x[0]=='href']:
|
||||
urls.append(dict(url=url, tag=str(link), title=title))
|
||||
|
||||
html = soup.__str__()
|
||||
|
||||
url_index = []
|
||||
i = 0
|
||||
for d in urls:
|
||||
if d['title'] == d['url'] or 'http://'+d['title'] == d['url']:
|
||||
html = html.replace(d['tag'], d['url'])
|
||||
else:
|
||||
i += 1
|
||||
html = html.replace(d['tag'], '%s [%s]' % (d['title'], i))
|
||||
url_index.append(d['url'])
|
||||
|
||||
html = html.replace('<strong>','*').replace('</strong>','*')
|
||||
html = html.replace('<b>','*').replace('</b>','*')
|
||||
html = html.replace('<h3>','*').replace('</h3>','*')
|
||||
html = html.replace('<h2>','**').replace('</h2>','**')
|
||||
html = html.replace('<h1>','**').replace('</h1>','**')
|
||||
html = html.replace('<em>','/').replace('</em>','/')
|
||||
|
||||
|
||||
# the only line breaks we respect is those of ending tags and
|
||||
# breaks
|
||||
|
||||
html = html.replace('\n',' ')
|
||||
html = html.replace('<br>', '\n')
|
||||
html = html.replace('<tr>', '\n')
|
||||
html = html.replace('</p>', '\n\n')
|
||||
html = re.sub('<br\s*/>', '\n', html)
|
||||
html = html.replace(' ' * 2, ' ')
|
||||
|
||||
|
||||
# for all other tags we failed to clean up, just remove then and
|
||||
# complain about them on the stderr
|
||||
def desperate_fixer(g):
|
||||
#print >>sys.stderr, "failed to clean up %s" % str(g.group())
|
||||
return ' '
|
||||
|
||||
html = re.sub('<.*?>', desperate_fixer, html)
|
||||
|
||||
# lstrip all lines
|
||||
html = '\n'.join([x.lstrip() for x in html.splitlines()])
|
||||
|
||||
for i, url in enumerate(url_index):
|
||||
if i == 0:
|
||||
html += '\n\n'
|
||||
html += '[%s] %s\n' % (i+1, url)
|
||||
return html
|
||||
|
||||
class rpc_proxy(object):
|
||||
def __init__(self, uid, passwd, host='localhost', port=8069, path='object', dbname='terp'):
|
||||
def __init__(self, uid, passwd, host='localhost', port=8069, path='object', dbname='terp'):
|
||||
self.rpc = xmlrpclib.ServerProxy('http://%s:%s/xmlrpc/%s' % (host, port, path))
|
||||
self.user_id = uid
|
||||
self.passwd = passwd
|
||||
|
@ -60,8 +135,8 @@ class rpc_proxy(object):
|
|||
return self.rpc.execute(self.dbname, self.user_id, self.passwd, *request)
|
||||
|
||||
class email_parser(object):
|
||||
def __init__(self, uid, password, section, email, email_default, dbname, host):
|
||||
self.rpc = rpc_proxy(uid, password, host=host, dbname=dbname)
|
||||
def __init__(self, uid, password, section, email, email_default, dbname, host, port):
|
||||
self.rpc = rpc_proxy(uid, password, host=host, port=port, dbname=dbname)
|
||||
try:
|
||||
self.section_id = int(section)
|
||||
except:
|
||||
|
@ -82,7 +157,7 @@ class email_parser(object):
|
|||
adr = self.rpc('res.partner.address', 'read', adr_ids, ['partner_id'])
|
||||
return {
|
||||
'partner_address_id': adr[0]['id'],
|
||||
'partner_id': adr[0]['partner_id'][0]
|
||||
'partner_id': adr[0].get('partner_id',False) and adr[0]['partner_id'][0] or False
|
||||
}
|
||||
|
||||
def _decode_header(self, s):
|
||||
|
@ -142,7 +217,7 @@ class email_parser(object):
|
|||
# #
|
||||
def msg_body_get(self, msg):
|
||||
message = {};
|
||||
message['body'] = u'';
|
||||
message['body'] = '';
|
||||
message['attachment'] = {};
|
||||
attachment = message['attachment'];
|
||||
counter = 1;
|
||||
|
@ -153,13 +228,17 @@ class email_parser(object):
|
|||
if part.get_content_maintype() == 'multipart':
|
||||
continue
|
||||
|
||||
if part.get_content_maintype()=='text' and part.get_content_subtype() in ('plain','html'):
|
||||
if part.get_content_maintype()=='text':
|
||||
buf = part.get_payload(decode=True)
|
||||
if buf:
|
||||
txt = buf.decode(part.get_charsets()[0] or 'ascii', 'replace')
|
||||
txt = re.sub("<(\w)>", replace, txt)
|
||||
txt = re.sub("<\/(\w)>", replace, txt)
|
||||
message['body'] += txt
|
||||
if txt and part.get_content_subtype() == 'plain':
|
||||
message['body'] += txt
|
||||
elif txt and part.get_content_subtype() == 'html':
|
||||
message['body'] += html2plaintext(txt)
|
||||
|
||||
elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text':
|
||||
filename = part.get_filename();
|
||||
if filename :
|
||||
|
@ -171,7 +250,7 @@ class email_parser(object):
|
|||
#end if
|
||||
#end if
|
||||
message['attachment'] = attachment
|
||||
#end for
|
||||
#end for
|
||||
return message
|
||||
#end def
|
||||
|
||||
|
@ -297,7 +376,7 @@ class email_parser(object):
|
|||
del msg['Subject']
|
||||
msg['Subject'] = '[OpenERP-CaseError] ' + a
|
||||
self.msg_send(msg, self.email_default.split(','))
|
||||
return emails
|
||||
return case_id, emails
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys, optparse
|
||||
|
@ -317,10 +396,11 @@ if __name__ == '__main__':
|
|||
parser.add_option("-m", "--default", dest="default", help="Default eMail in case of any trouble.", default=None)
|
||||
parser.add_option("-d", "--dbname", dest="dbname", help="Database name (default: terp)", default='terp')
|
||||
parser.add_option("--host", dest="host", help="Hostname of the Open ERP Server", default="localhost")
|
||||
parser.add_option("--port", dest="port", help="Port of the Open ERP Server", default="8069")
|
||||
|
||||
|
||||
(options, args) = parser.parse_args()
|
||||
parser = email_parser(options.userid, options.password, options.section, options.email, options.default, dbname=options.dbname, host=options.host)
|
||||
parser = email_parser(options.userid, options.password, options.section, options.email, options.default, dbname=options.dbname, host=options.host, port=options.port)
|
||||
|
||||
msg_txt = email.message_from_file(sys.stdin)
|
||||
|
|
@ -26,28 +26,59 @@ import tools
|
|||
import os
|
||||
_email_form = '''<?xml version="1.0"?>
|
||||
<form string="Email Gateway">
|
||||
<label string="Fetch Email from Email Gate Way" />
|
||||
<separator string="Fetching Emails from" />
|
||||
<field name="server" colspan="4" nolabel="1" />
|
||||
</form>'''
|
||||
|
||||
_email_done_form = '''<?xml version="1.0"?>
|
||||
<form string="Email Gateway">
|
||||
<separator string="Log Detail" />
|
||||
<newline/>
|
||||
<field name="message" colspan="4" nolabel="1"/>
|
||||
</form>'''
|
||||
|
||||
_email_fields = {
|
||||
'server': {'string':"Server", 'type':'text', 'readonly':True},
|
||||
}
|
||||
|
||||
_email_done_fields = {
|
||||
'message': {'string':"Log Detail", 'type':'text', 'readonly':True},
|
||||
}
|
||||
|
||||
def _default(self , cr, uid, data, context):
|
||||
pool = pooler.get_pool(cr.dbname)
|
||||
gateway_pool=pool.get('crm.case.section')
|
||||
sections = gateway_pool.browse(cr, uid, data['ids'], context=context)
|
||||
server = []
|
||||
for section in sections:
|
||||
for gateway in section.gateway_ids:
|
||||
if gateway.name.active:
|
||||
server.append('%s:%d'%(gateway.name.name, gateway.name.port) )
|
||||
data['form']['server'] = '\n'.join(server)
|
||||
return data['form']
|
||||
|
||||
def fetch_mail(self , cr, uid, data, context):
|
||||
pool = pooler.get_pool(cr.dbname)
|
||||
gateway_pool=pool.get('crm.email.gateway')
|
||||
gateway_pool.fetch_mail(cr, uid, data['ids'], context=context)
|
||||
return {}
|
||||
gateway_pool=pool.get('crm.email.gateway')
|
||||
messages = gateway_pool.fetch_mails(cr, uid, ids=[], section_ids=data['ids'], context=context)
|
||||
data['form']['message'] = '\n'.join(messages)
|
||||
return data['form']
|
||||
|
||||
class wiz_fetch_mail(wizard.interface):
|
||||
states = {
|
||||
'init': {
|
||||
'actions': [],
|
||||
'result': {'type': 'form', 'arch':_email_form, 'fields':_email_fields, 'state':[('run','Run','gtk-execute'),('end','Cancel','gtk-cancel')]}
|
||||
},
|
||||
'run': {
|
||||
'actions': [_default],
|
||||
'result': {'type': 'form', 'arch':_email_form, 'fields':_email_fields, 'state':[('end','Cancel','gtk-cancel'), ('fetch','Fetch','gtk-execute')]}
|
||||
},
|
||||
'fetch': {
|
||||
'actions': [fetch_mail],
|
||||
'result': {'type': 'state', 'state': 'end'}
|
||||
},
|
||||
'result': {'type': 'form', 'arch': _email_done_form,
|
||||
'fields': _email_done_fields,
|
||||
'state': (
|
||||
('end', 'Close'),
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
wiz_fetch_mail('crm.case.section.fetchmail')
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
|
Loading…
Reference in New Issue