diff options
| author | Mantavya Gajjar <mga@tinyerp.com> | 2010-04-15 21:13:28 +0530 |
|---|---|---|
| committer | Mantavya Gajjar <mga@tinyerp.com> | 2010-04-15 21:13:28 +0530 |
| commit | bfc458e981cc3835489e4cdb93eb89fff600b947 (patch) | |
| tree | 602f337c6c6749f8e1273e6e55c34e4666a05faf /addons | |
| parent | 021087c8cebef9cf8b74ff71ef1fe524ececb65b (diff) | |
[ADD]: clean module to fetch email and process of it.
this module will replace the email_gateway
meil_gateway = fetchmail + smtpclient
smtpclient will be add sooner totrunk branch
bzr revid: mga@tinyerp.com-20100415154328-6xht1vvhgp4c5z5g
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/fetchmail/__init__.py | 24 | ||||
| -rw-r--r-- | addons/fetchmail/__terp__.py | 48 | ||||
| -rw-r--r-- | addons/fetchmail/fetchmail.py | 405 | ||||
| -rw-r--r-- | addons/fetchmail/fetchmail_data.xml | 15 | ||||
| -rw-r--r-- | addons/fetchmail/fetchmail_view.xml | 177 |
5 files changed, 669 insertions, 0 deletions
diff --git a/addons/fetchmail/__init__.py b/addons/fetchmail/__init__.py new file mode 100644 index 00000000000..0f50df83255 --- /dev/null +++ b/addons/fetchmail/__init__.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved +# mga@tinyerp.com +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +############################################################################## + +import fetchmail diff --git a/addons/fetchmail/__terp__.py b/addons/fetchmail/__terp__.py new file mode 100644 index 00000000000..fc2b245f11f --- /dev/null +++ b/addons/fetchmail/__terp__.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved +# mga@tinyerp.com +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +############################################################################## + +{ + "name" : "Fetchmail Server", + "version" : "1.0", + "depends" : ["base", "smtpclient"], + "author" : "Tiny", + "description": """Fetchail: + * Fetch email from Pop / IMAP server + * Support SSL + * Integrated with all Modules + * Automatic Email Receive + * Email based Records (Add, Update) + """, + 'author': 'Tiny', + 'website': 'http://www.openerp.com', + 'init_xml': [], + 'update_xml': [ + "fetchmail_view.xml", + "fetchmail_data.xml" + ], + 'demo_xml': [ + + ], + 'installable': True, + 'active': False, +} diff --git a/addons/fetchmail/fetchmail.py b/addons/fetchmail/fetchmail.py new file mode 100644 index 00000000000..ae09b4da438 --- /dev/null +++ b/addons/fetchmail/fetchmail.py @@ -0,0 +1,405 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved +# mga@tinyerp.com +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +############################################################################## + +import os +import re +import time + +import email +import binascii +import mimetypes + +from imaplib import IMAP4 +from imaplib import IMAP4_SSL + +from email.header import Header +from email.header import decode_header + +import netsvc +from osv import osv +from osv import fields +from tools.translate import _ + +command_re = re.compile("^Set-([a-z]+) *: *(.+)$", re.I + re.UNICODE) + +logger = netsvc.Logger() + +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 mail_server(osv.osv): + + _name = 'email.server' + _description = "POP/IMAP Server" + + _columns = { + 'name':fields.char('Name', size=256, required=True, readonly=False), + 'active':fields.boolean('Active', required=False), + 'state':fields.selection([ + ('draft','Not Confirme'), + ('wating','Waiting for Verification'), + ('done','Confirmed'), + ],'State', select=True, readonly=True), + 'server' : fields.char('SMTP Server', size=256, required=True, readonly=True, states={'draft':[('readonly',False)]}), + 'port' : fields.integer('SMTP Port', required=True, readonly=True, states={'draft':[('readonly',False)]}), + 'type':fields.selection([ + ('pop','POP Server'), + ('imap','IMAP Server'), + ],'State', select=True, readonly=True), + 'is_ssl':fields.boolean('SSL ?', required=False), + 'date': fields.date('Date'), + 'user' : fields.char('User Name', size=256, required=True, readonly=True, states={'draft':[('readonly',False)]}), + 'password' : fields.char('Password', size=1024, invisible=True, required=True, readonly=True, states={'draft':[('readonly',False)]}), + 'note': fields.text('Description'), + 'action_id':fields.many2one('ir.actions.server', 'Reply Email', required=False, domain="[('state','=','email')]"), + 'object_id': fields.many2one('ir.model',"Model", required=True), + 'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly',False)]}, help="Priority between 0 to 10, select define the order of Processing"), + } + _defaults = { + 'type': lambda *a: "imap", + 'state': lambda *a: "draft", + 'active': lambda *a: True, + 'priority': lambda *a: 5, + 'date': lambda *a: time.strftime('%Y-%m-%d'), + } + + def check_duplicate(self, cr, uid, ids): + vals = self.read(cr, uid, ids, ['user', 'password'])[0] + cr.execute("select count(id) from email_server where user='%s' and password='%s'" % (vals['user'], vals['password'])) + res = cr.fetchone() + if res: + if res[0] > 1: + return False + return True + + _constraints = [ + (check_duplicate, 'Warning! Can\'t have duplicate server configuration!', ['user', 'password']) + ] + + def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False): + port = 0 + if server_type == 'pop': + port = ssl and 995 or 110 + elif server_type == 'imap': + port = ssl and 993 or 143 + + return {'value':{'port':port}} + + def _process_email(self, cr, uid, server, message, context={}): + history_pool = self.pool.get('mail.server.history') + msg_txt = email.message_from_string(message) + message_id = msg_txt.get('Message-ID', False) + + msg = {} + if not message_id: + return False + + fields = msg_txt.keys() + + msg['id'] = message_id + msg['message-id'] = message_id + + if 'Subject' in fields: + msg['subject'] = msg_txt.get('Subject') + + if 'Content-Type' in fields: + msg['content-type'] = msg_txt.get('Content-Type') + + if 'From' in fields: + msg['from'] = msg_txt.get('From') + + if 'Delivered-To' in fields: + msg['to'] = msg_txt.get('Delivered-To') + + if 'Cc' in fields: + msg['cc'] = msg_txt.get('Cc') + + if 'Reply-To' in fields: + msg['reply'] = msg_txt.get('Reply-To') + + if 'Date' in fields: + msg['date'] = msg_txt.get('Date') + + if 'Content-Transfer-Encoding' in fields: + msg['encoding'] = msg_txt.get('Content-Transfer-Encoding') + + if 'References' in fields: + msg['references'] = msg_txt.get('References') + + if 'X-openerp-caseid' in fields: + msg['caseid'] = msg_txt.get('X-openerp-caseid') + + if 'X-Priority' in fields: + msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0] + + if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', None): + msg['body'] = msg_txt.get_payload(decode=True) + + attachents = {} + if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', None): + body = "" + counter = 1 + for part in msg_txt.walk(): + if part.get_content_maintype() == 'multipart': + continue + + if part.get_content_maintype()=='text': + content = part.get_payload(decode=True) + if part.get_content_subtype() == 'html': + body = html2plaintext(content) + elif part.get_content_subtype() == 'plain': + body = content + + filename = part.get_filename() + if filename : + attachents[filename] = part.get_payload(decode=True) + + elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text': + filename = part.get_filename(); + if filename : + attachents[filename] = part.get_payload(decode=True) + else: + body += part.get_payload(decode=True) + + msg['body'] = body + msg['attachments'] = attachents + + if msg.get('references', False): + id = False + ref = msg.get('references').split('\r\n') + if ref: + hids = history_pool.search(cr, uid, [('name','=',ref[0])]) + if hids: + id = hids[0] + history = history_pool.browse(cr, uid, id) + model_pool = self.pool.get(server.object_id.model) + context.update({ + 'message_id':ref[0] + }) + model_pool.message_update(cr, uid, [history.res_id], msg, context=context) + res_id = id + else: + model_pool = self.pool.get(server.object_id.model) + res_id = model_pool.message_new(cr, uid, msg, context) + + for attactment in attachents or []: + data_attach = { + 'name': attactment, + 'datas':binascii.b2a_base64(str(attachents.get(attactment))), + 'datas_fname': attactment, + 'description': 'Mail attachment', + 'res_model': server.object_id.model, + 'res_id': res_id, + } + self.pool.get('ir.attachment').create(cr, uid, data_attach) + + if server.action_id: + action_pool = self.pool.get('ir.actions.server') + action_pool.run(cr, uid, [server.action_id.id], {'active_id':res_id, 'active_ids':[res_id]}) + + res = { + 'name': message_id, + 'res_id': res_id, + 'server_id': server.id, + 'note': msg.get('body', msg.get('from')), + 'ref_id':msg.get('references', msg.get('id')), + 'type':server.type + } + his_id = history_pool.create(cr, uid, res) + + return res_id + + def _fetch_mails(self, cr, uid, ids=False, context={}): + if not ids: + ids = self.search(cr, uid, []) + return self.fetch_mail(cr, uid, ids, context) + + def fetch_mail(self, cr, uid, ids, context={}): + fp = os.popen('ping www.google.com -c 1 -w 5',"r") + if not fp.read(): + logger.notifyChannel('imap', netsvc.LOG_WARNING, 'No address associated with hostname !') + + for server in self.browse(cr, uid, ids, context): + logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail start checking for new emails on %s' % (server.name)) + + try: + if server.type == 'imap': + imap_server = None + if server.is_ssl: + imap_server = IMAP4_SSL(server.server, int(server.port)) + else: + imap_server = IMAP4(server.server, int(server.port)) + + imap_server.login(server.user, server.password) + imap_server.select() + result, data = imap_server.search(None, '(UNSEEN)') + count = 0 + for num in data[0].split(): + result, data = imap_server.fetch(num, '(RFC822)') + self._process_email(cr, uid, server, data[0][1], context) + imap_server.store(num, '+FLAGS', '\\Seen') + count += 1 + logger.notifyChannel('imap', netsvc.LOG_INFO, 'fetchmail fetch %s email(s) from %s' % (count, server.name)) + + imap_server.close() + imap_server.logout() + except Exception, e: + logger.notifyChannel('IMAP', netsvc.LOG_WARNING, '%s' % (e)) + + return True + +mail_server() + +class mail_server_history(osv.osv): + + _name = "mail.server.history" + _description = "Mail Server History" + + _columns = { + 'name': fields.char('Message Id', size=256, readonly=True, help="Message Id in Email Server.", select=True), + 'ref_id': fields.char('Referance Id', size=256, readonly=True, help="Message Id in Email Server.", select=True), + 'res_id': fields.integer("Resource ID", readonly=True, select=True), + 'server_id': fields.many2one('email.server',"Mail Server", readonly=True, select=True), + 'model_id':fields.related('server_id', 'object_id', type='many2one', relation='ir.model', string='Model', readonly=True, select=True), + 'note': fields.text('Notes', readonly=True), + 'create_date': fields.datetime('Created Date', readonly=True), + 'type':fields.selection([ + ('pop','POP Server'), + ('imap','IMAP Server'), + ],'State', select=True, readonly=True), + } + _order = 'id desc' + +mail_server_history() + +class fetchmail_tool(osv.osv): + """ + OpenERP Model : fetchmail_tool + """ + + _name = 'email.server.tools' + _description = "Email Tools" + _auto = False + + def to_email(self, text): + _email = re.compile(r'.*<.*@.*\..*>', re.UNICODE) + def record(path): + eml = path.group() + index = eml.index('<') + eml = eml[index:-1].replace('<','').replace('>','') + return eml + + bits = _email.sub(record, text) + return bits + + def get_partner(self, cr, uid, from_email, context=None): + """ + @param self: The object pointer + @param cr: the current row, from the database cursor, + @param uid: the current user’s ID for security checks + @param from_email: email address based on that function will search for the correct + """ + + res = { + 'partner_address_id': False, + 'partner_id': False + } + from_email = self.to_email(from_email) + address_ids = self.pool.get('res.partner.address').search(cr, uid, [('email', '=', from_email)]) + if address_ids: + address = self.pool.get('res.partner.address').browse(cr, uid, address_ids[0]) + res['partner_address_id'] = address_ids[0] + res['partner_id'] = address.partner_id.id + + return res + +fetchmail_tool() diff --git a/addons/fetchmail/fetchmail_data.xml b/addons/fetchmail/fetchmail_data.xml new file mode 100644 index 00000000000..ef40a76489f --- /dev/null +++ b/addons/fetchmail/fetchmail_data.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="utf-8"?> +<openerp> + <data noupdate="1"> + <record id="ir_cron_mail_gateway_action" model="ir.cron"> + <field name="name">Fetchmail Service</field> + <field name="interval_number">5</field> + <field name="interval_type">minutes</field> + <field name="numbercall">-1</field> + <field eval="False" name="doall"/> + <field eval="'email.server'" name="model"/> + <field eval="'_fetch_mails'" name="function"/> + <field eval="'()'" name="args"/> + </record> + </data> +</openerp> diff --git a/addons/fetchmail/fetchmail_view.xml b/addons/fetchmail/fetchmail_view.xml new file mode 100644 index 00000000000..80d0b60a74f --- /dev/null +++ b/addons/fetchmail/fetchmail_view.xml @@ -0,0 +1,177 @@ +<?xml version="1.0"?> +<openerp> + <data> + + <record model="ir.ui.view" id="view_email_server_tree"> + <field name="name">email.server.tree</field> + <field name="model">email.server</field> + <field name="type">tree</field> + <field name="arch" type="xml"> + <tree string="POP/IMAP Servers"> + <field name="name" select="1"/> + <field name="type" select="1"/> + <field name="user" select="1"/> + <field name="is_ssl" select="1"/> + <field name="state" select="1"/> + </tree> + </field> + </record> + + <record model="ir.ui.view" id="view_email_server_form"> + <field name="name">email.server.form</field> + <field name="model">email.server</field> + <field name="type">form</field> + <field name="arch" type="xml"> + <form string="POP/IMAP Server"> + <group col="6" colspan="4"> + <field name="name" select="1" colspan="4"/> + <field name="type" select="1" on_change="onchange_server_type(type, is_ssl)"/> + + <field name="date" select="1"/> + <field name="is_ssl" select="1" on_change="onchange_server_type(type, is_ssl)"/> + <field name="active" select="1"/> + </group> + <notebook colspan="4"> + <page string="Server & Login"> + <group col="2" colspan="2"> + <separator string="Server Information" colspan="2"/> + <field name="server" /> + <field name="port" /> + </group> + <group col="2" colspan="2"> + <separator string="Login Information" colspan="2"/> + <field name="user" /> + <field name="password" password="True" /> + </group> + <group col="2" colspan="2"> + <separator string="Auto Reply?" colspan="2"/> + <field name="action_id"/> + </group> + <group col="2" colspan="2"> + <separator string="Process Parameter" colspan="2"/> + <field name="object_id"/> + <field name="priority"/> + </group> + <separator string="Description" colspan="4"/> + <field name="note" colspan="4" nolabel="1"/> + </page> + </notebook> + <group col="6" colspan="4"> + <field name="state" select="1"/> + <button string="Verify Server" type="object" name="fetch_mail"/> + <button string="Schedule"/> + </group> + </form> + </field> + </record> + + <record model="ir.actions.act_window" id="action_email_server_tree"> + <field name="name">POP Servers</field> + <field name="res_model">email.server</field> + <field name="view_type">form</field> + <field name="view_mode">tree,form</field> + <field name="view_id" ref="view_email_server_tree"/> + <field name="context">{'type':'pop'}</field> + <field name="domain">[('type','=','pop')]</field> + </record> + + <menuitem + parent="smtpclient.menu_smtpclient_administration_server" + id="menu_action_email_server_tree" + action="action_email_server_tree" + /> + + <record model="ir.actions.act_window" id="action_email_server_tree_imap"> + <field name="name">IMAP Servers</field> + <field name="res_model">email.server</field> + <field name="view_type">form</field> + <field name="view_mode">tree,form</field> + <field name="view_id" ref="view_email_server_tree"/> + <field name="context">{'type':'imap'}</field> + <field name="domain">[('type','=','imap')]</field> + </record> + + <menuitem + parent="smtpclient.menu_smtpclient_administration_server" + id="menu_action_email_server_tree_imap" + action="action_email_server_tree_imap" + /> + + <record model="ir.ui.view" id="view_mail_server_history_tree"> + <field name="name">mail.server.history.tree</field> + <field name="model">mail.server.history</field> + <field name="type">tree</field> + <field name="arch" type="xml"> + <tree string="Received Mail History"> + <field name="server_id" select="1"/> + <field name="create_date" select="1"/> + <field name="model_id"/> + <field name="name" select="1"/> + <field name="ref_id"/> + </tree> + </field> + </record> + <record model="ir.ui.view" id="view_email_server_history_form"> + <field name="name">mail.server.history.form</field> + <field name="model">mail.server.history</field> + <field name="type">form</field> + <field name="arch" type="xml"> + <form string="Email History"> + <group col="6" colspan="4"> + <field name="server_id" select="1"/> + <field name="create_date" select="1"/> + </group> + <group col="2" colspan="2"> + <separator string="Resource Information" colspan="2"/> + <field name="model_id" select="1"/> + <field name="res_id"/> + </group> + <group col="2" colspan="2"> + <separator string="Meta Information" colspan="2"/> + <field name="name" select="1"/> + <field name="ref_id"/> + </group> + <newline/> + <separator string="Description" colspan="4"/> + <field name="note" colspan="4" nolabel="1"/> + </form> + </field> + </record> + + <act_window + context="{'server_id': active_id}" + domain="[('server_id', '=', active_id)]" + id="act_server_history" name="Email History" + res_model="mail.server.history" src_model="email.server"/> + + <record model="ir.actions.act_window" id="action_email_server_history_tree"> + <field name="name">Receive Email History</field> + <field name="res_model">mail.server.history</field> + <field name="view_type">form</field> + <field name="view_mode">tree,form</field> + <field name="view_id" ref="view_mail_server_history_tree"/> + <field name="context">{'type':'imap'}</field> + <field name="domain">[('type','=','imap')]</field> + </record> + + <menuitem + parent="menu_action_email_server_tree_imap" + id="menu_action_email_server_history_tree" + action="action_email_server_history_tree"/> + + <record model="ir.actions.act_window" id="action_email_server_history_tree_pop"> + <field name="name">Receive Email History</field> + <field name="res_model">mail.server.history</field> + <field name="view_type">form</field> + <field name="view_mode">tree,form</field> + <field name="view_id" ref="view_mail_server_history_tree"/> + <field name="context">{'type':'pop'}</field> + <field name="domain">[('type','=','pop')]</field> + </record> + + <menuitem + parent="menu_action_email_server_tree" + id="menu_action_email_server_history_tree_pop" + action="action_email_server_history_tree_pop"/> + </data> +</openerp> |
