[MIGR] fetchmail : migrate to new api

This commit is contained in:
Keyur Gajjar
2016-04-14 13:23:17 +02:00
committed by Jérome Maes
parent 17b7a2045f
commit 20a7ba1b14
7 changed files with 178 additions and 213 deletions
+3 -3
View File
@@ -2,9 +2,9 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name' : 'Email Gateway',
'version' : '1.0',
'depends' : ['mail'],
'name': 'Email Gateway',
'version': '1.0',
'depends': ['mail'],
'category': 'Tools',
'description': """
Retrieve incoming email on POP/IMAP servers.
+2 -2
View File
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<odoo>
<data noupdate="1">
<record id="ir_cron_mail_gateway_action" model="ir.cron">
<field name="name">Fetchmail Service</field>
@@ -14,4 +14,4 @@
<field name="active" eval="False"/>
</record>
</data>
</openerp>
</odoo>
+2
View File
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import fetchmail
import res_config
import mail_mail
+140 -169
View File
@@ -3,20 +3,12 @@
import logging
import poplib
import time
from imaplib import IMAP4
from imaplib import IMAP4_SSL
from poplib import POP3
from poplib import POP3_SSL
try:
import cStringIO as StringIO
except ImportError:
import StringIO
from imaplib import IMAP4, IMAP4_SSL
from poplib import POP3, POP3_SSL
from odoo import api, fields, models, tools, _
from odoo.exceptions import UserError
from openerp.osv import fields, osv
from openerp import tools, api, SUPERUSER_ID
from openerp.tools.translate import _
from openerp.exceptions import UserError
_logger = logging.getLogger(__name__)
MAX_POP_MESSAGES = 50
@@ -25,129 +17,124 @@ MAIL_TIMEOUT = 60
# Workaround for Python 2.7.8 bug https://bugs.python.org/issue23906
poplib._MAXLINE = 65536
class fetchmail_server(osv.osv):
class FetchmailServer(models.Model):
"""Incoming POP/IMAP mail server account"""
_name = 'fetchmail.server'
_description = "POP/IMAP Server"
_order = 'priority'
_columns = {
'name':fields.char('Name', required=True, readonly=False),
'active':fields.boolean('Active', required=False),
'state':fields.selection([
('draft', 'Not Confirmed'),
('done', 'Confirmed'),
], 'Status', select=True, readonly=True, copy=False),
'server' : fields.char('Server Name', readonly=True, help="Hostname or IP of the mail server", states={'draft':[('readonly', False)]}),
'port' : fields.integer('Port', readonly=True, states={'draft':[('readonly', False)]}),
'type':fields.selection([
('pop', 'POP Server'),
('imap', 'IMAP Server'),
('local', 'Local Server'),
], 'Server Type', select=True, required=True, readonly=False),
'is_ssl':fields.boolean('SSL/TLS', help="Connections are encrypted with SSL/TLS through a dedicated port (default: IMAPS=993, POP3S=995)"),
'attach':fields.boolean('Keep Attachments', help="Whether attachments should be downloaded. "
"If not enabled, incoming emails will be stripped of any attachments before being processed"),
'original':fields.boolean('Keep Original', help="Whether a full original copy of each email should be kept for reference "
"and attached to each processed message. This will usually double the size of your message database."),
'date': fields.datetime('Last Fetch Date', readonly=True),
'user' : fields.char('Username', readonly=True, states={'draft':[('readonly', False)]}),
'password' : fields.char('Password', readonly=True, states={'draft':[('readonly', False)]}),
'action_id':fields.many2one('ir.actions.server', 'Server Action', help="Optional custom server action to trigger for each incoming mail, "
"on the record that was created or updated by this mail"),
'object_id': fields.many2one('ir.model', "Create a New Record", help="Process each incoming mail as part of a conversation "
"corresponding to this document type. This will create "
"new documents for new conversations, or attach follow-up "
"emails to the existing conversations (documents)."),
'priority': fields.integer('Server Priority', readonly=True, states={'draft':[('readonly', False)]}, help="Defines the order of processing, "
"lower values mean higher priority"),
'message_ids': fields.one2many('mail.mail', 'fetchmail_server_id', 'Messages', readonly=True),
'configuration' : fields.text('Configuration', readonly=True),
'script' : fields.char('Script', readonly=True),
}
_defaults = {
'state': "draft",
'type': "pop",
'active': True,
'priority': 5,
'attach': True,
'script': '/mail/static/scripts/openerp_mailgate.py',
}
name = fields.Char('Name', required=True)
active = fields.Boolean('Active', default=True)
state = fields.Selection([
('draft', 'Not Confirmed'),
('done', 'Confirmed'),
], string='Status', index=True, readonly=True, copy=False, default='draft')
server = fields.Char(string='Server Name', readonly=True, help="Hostname or IP of the mail server", states={'draft': [('readonly', False)]})
port = fields.Integer(readonly=True, states={'draft': [('readonly', False)]})
type = fields.Selection([
('pop', 'POP Server'),
('imap', 'IMAP Server'),
('local', 'Local Server'),
], 'Server Type', index=True, required=True, default='pop')
is_ssl = fields.Boolean('SSL/TLS', help="Connections are encrypted with SSL/TLS through a dedicated port (default: IMAPS=993, POP3S=995)")
attach = fields.Boolean('Keep Attachments', help="Whether attachments should be downloaded. "
"If not enabled, incoming emails will be stripped of any attachments before being processed", default=True)
original = fields.Boolean('Keep Original', help="Whether a full original copy of each email should be kept for reference "
"and attached to each processed message. This will usually double the size of your message database.")
date = fields.Datetime(string='Last Fetch Date', readonly=True)
user = fields.Char(string='Username', readonly=True, states={'draft': [('readonly', False)]})
password = fields.Char(readonly=True, states={'draft': [('readonly', False)]})
action_id = fields.Many2one('ir.actions.server', string='Server Action', help="Optional custom server action to trigger for each incoming mail, on the record that was created or updated by this mail")
object_id = fields.Many2one('ir.model', string="Create a New Record", help="Process each incoming mail as part of a conversation "
"corresponding to this document type. This will create "
"new documents for new conversations, or attach follow-up "
"emails to the existing conversations (documents).")
priority = fields.Integer(string='Server Priority', readonly=True, states={'draft': [('readonly', False)]}, help="Defines the order of processing, lower values mean higher priority", default=5)
message_ids = fields.One2many('mail.mail', 'fetchmail_server_id', string='Messages', readonly=True)
configuration = fields.Text('Configuration', readonly=True)
script = fields.Char(readonly=True, default='/mail/static/scripts/openerp_mailgate.py')
def onchange_server_type(self, cr, uid, ids, server_type=False, ssl=False, object_id=False):
port = 0
values = {}
if server_type == 'pop':
port = ssl and 995 or 110
elif server_type == 'imap':
port = ssl and 993 or 143
@api.onchange('type', 'is_ssl', 'object_id')
def onchange_server_type(self):
self.port = 0
if self.type == 'pop':
self.port = self.is_ssl and 995 or 110
elif self.type == 'imap':
self.port = self.is_ssl and 993 or 143
else:
values['server'] = ''
values['port'] = port
self.server = ''
conf = {
'dbname' : cr.dbname,
'uid' : uid,
'model' : 'MODELNAME',
'dbname': self.env.cr.dbname,
'uid': self.env.uid,
'model': self.object_id.model if self.object_id else 'MODELNAME'
}
if object_id:
m = self.pool.get('ir.model')
r = m.read(cr,uid,[object_id],['model'])
conf['model']=r[0]['model']
values['configuration'] = """Use the below script with the following command line options with your Mail Transport Agent (MTA)
self.configuration = """
Use the below script with the following command line options with your Mail Transport Agent (MTA)
openerp_mailgate.py --host=HOSTNAME --port=PORT -u %(uid)d -p PASSWORD -d %(dbname)s
Example configuration for the postfix mta running locally:
/etc/postfix/virtual_aliases:
@youdomain openerp_mailgate@localhost
/etc/aliases:
openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p PASSWORD -d %(dbname)s"
""" % conf
openerp_mailgate.py --host=HOSTNAME --port=PORT -u %(uid)d -p PASSWORD -d %(dbname)s
@api.model
def create(self, values):
res = super(FetchmailServer, self).create(values)
self._update_cron()
return res
Example configuration for the postfix mta running locally:
@api.multi
def write(self, values):
res = super(FetchmailServer, self).write(values)
self._update_cron()
return res
/etc/postfix/virtual_aliases:
@youdomain openerp_mailgate@localhost
@api.multi
def unlink(self):
res = super(FetchmailServer, self).unlink()
self._update_cron()
return res
/etc/aliases:
openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p PASSWORD -d %(dbname)s"
""" % conf
return {'value':values}
def set_draft(self, cr, uid, ids, context=None):
self.write(cr, uid, ids , {'state':'draft'})
@api.multi
def set_draft(self):
self.write({'state': 'draft'})
return True
@api.cr_uid_ids_context
def connect(self, cr, uid, server_id, context=None):
if isinstance(server_id, (list,tuple)):
server_id = server_id[0]
server = self.browse(cr, uid, server_id, context)
if server.type == 'imap':
if server.is_ssl:
connection = IMAP4_SSL(server.server, int(server.port))
@api.multi
def connect(self):
self.ensure_one()
if self.type == 'imap':
if self.is_ssl:
connection = IMAP4_SSL(self.server, int(self.port))
else:
connection = IMAP4(server.server, int(server.port))
connection.login(server.user, server.password)
elif server.type == 'pop':
if server.is_ssl:
connection = POP3_SSL(server.server, int(server.port))
connection = IMAP4(self.server, int(self.port))
connection.login(self.user, self.password)
elif self.type == 'pop':
if self.is_ssl:
connection = POP3_SSL(self.server, int(self.port))
else:
connection = POP3(server.server, int(server.port))
connection = POP3(self.server, int(self.port))
#TODO: use this to remove only unread messages
#connection.user("recent:"+server.user)
connection.user(server.user)
connection.pass_(server.password)
connection.user(self.user)
connection.pass_(self.password)
# Add timeout on socket
connection.sock.settimeout(MAIL_TIMEOUT)
return connection
def button_confirm_login(self, cr, uid, ids, context=None):
if context is None:
context = {}
for server in self.browse(cr, uid, ids, context=context):
@api.multi
def button_confirm_login(self):
for server in self:
try:
connection = server.connect()
server.write({'state':'done'})
except Exception, e:
server.write({'state': 'done'})
except Exception, err:
_logger.info("Failed to connect to %s server %s.", server.type, server.name, exc_info=True)
raise UserError(_("Connection test failed: %s") % tools.ustr(e))
raise UserError(_("Connection test failed: %s") % tools.ustr(err))
finally:
try:
if connection:
@@ -160,23 +147,25 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p
pass
return True
def _fetch_mails(self, cr, uid, ids=False, context=None):
if not ids:
ids = self.search(cr, uid, [('state','=','done'),('type','in',['pop','imap'])])
return self.fetch_mail(cr, uid, ids, context=context)
@api.model
def _fetch_mails(self):
""" Method called by cron to fetch mails from servers """
return self.search([('state', '=', 'done'), ('type', 'in', ['pop', 'imap'])]).fetch_mail()
def fetch_mail(self, cr, uid, ids, context=None):
"""WARNING: meant for cron usage only - will commit() after each email!"""
context = dict(context or {})
context['fetchmail_cron_running'] = True
mail_thread = self.pool.get('mail.thread')
action_pool = self.pool.get('ir.actions.server')
for server in self.browse(cr, uid, ids, context=context):
@api.multi
def fetch_mail(self):
""" WARNING: meant for cron usage only - will commit() after each email! """
additionnal_context = {
'fetchmail_cron_running': True
}
MailThread = self.env['mail.thread']
for server in self:
_logger.info('start checking for new emails on %s server %s', server.type, server.name)
context.update({'fetchmail_server_id': server.id, 'server_type': server.type})
additionnal_context['fetchmail_server_id'] = server.id
additionnal_context['server_type'] = server.type
count, failed = 0, 0
imap_server = False
pop_server = False
imap_server = None
pop_server = None
if server.type == 'imap':
try:
imap_server = server.connect()
@@ -187,18 +176,18 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p
result, data = imap_server.fetch(num, '(RFC822)')
imap_server.store(num, '-FLAGS', '\\Seen')
try:
res_id = mail_thread.message_process(cr, uid, server.object_id.model,
data[0][1],
save_original=server.original,
strip_attachments=(not server.attach),
context=context)
res_id = MailThread.with_context(**additionnal_context).message_process(server.object_id.model, data[0][1], save_original=server.original, strip_attachments=(not server.attach))
except Exception:
_logger.info('Failed to process mail from %s server %s.', server.type, server.name, exc_info=True)
failed += 1
if res_id and server.action_id:
action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids': [res_id], 'active_model': context.get("thread_model", server.object_id.model)})
server.action_id.with_context({
'active_id': res_id,
'active_ids': [res_id],
'active_model': self.env.context.get("thread_model", server.object_id.model)
}).run()
imap_server.store(num, '+FLAGS', '\\Seen')
cr.commit()
self._cr.commit()
count += 1
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", count, server.type, server.name, (count - failed), failed)
except Exception:
@@ -211,62 +200,44 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p
try:
while True:
pop_server = server.connect()
(numMsgs, totalSize) = pop_server.stat()
(num_messages, total_size) = pop_server.stat()
pop_server.list()
for num in range(1, min(MAX_POP_MESSAGES, numMsgs) + 1):
(header, msges, octets) = pop_server.retr(num)
msg = '\n'.join(msges)
for num in range(1, min(MAX_POP_MESSAGES, num_messages) + 1):
(header, messages, octets) = pop_server.retr(num)
message = '\n'.join(messages)
res_id = None
try:
res_id = mail_thread.message_process(cr, uid, server.object_id.model,
msg,
save_original=server.original,
strip_attachments=(not server.attach),
context=context)
res_id = MailThread.with_context(**additionnal_context).message_process(server.object_id.model, message, save_original=server.original, strip_attachments=(not server.attach))
pop_server.dele(num)
except Exception:
_logger.info('Failed to process mail from %s server %s.', server.type, server.name, exc_info=True)
failed += 1
if res_id and server.action_id:
action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids': [res_id], 'active_model': context.get("thread_model", server.object_id.model)})
cr.commit()
if numMsgs < MAX_POP_MESSAGES:
server.action_id.with_context({
'active_id': res_id,
'active_ids': [res_id],
'active_model': self.env.context.get("thread_model", server.object_id.model)
}).run()
self.env.cr.commit()
if num_messages < MAX_POP_MESSAGES:
break
pop_server.quit()
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", numMsgs, server.type, server.name, (numMsgs - failed), failed)
_logger.info("Fetched %d email(s) on %s server %s; %d succeeded, %d failed.", num_messages, server.type, server.name, (num_messages - failed), failed)
except Exception:
_logger.info("General failure when trying to fetch mail from %s server %s.", server.type, server.name, exc_info=True)
finally:
if pop_server:
pop_server.quit()
server.write({'date': time.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)})
server.write({'date': fields.Datetime.now()})
return True
def _update_cron(self, cr, uid, context=None):
if context and context.get('fetchmail_cron_running'):
@api.model
def _update_cron(self):
if self.env.context.get('fetchmail_cron_running'):
return
try:
cron = self.pool['ir.model.data'].get_object(
cr, SUPERUSER_ID, 'fetchmail', 'ir_cron_mail_gateway_action', context=context)
# Enabled/Disable cron based on the number of 'done' server of type pop or imap
cron = self.env.ref('fetchmail.ir_cron_mail_gateway_action')
cron.toggle(model=self._name, domain=[('state', '=', 'done'), ('type', 'in', ['pop', 'imap'])])
except ValueError:
# Nevermind if default cron cannot be found
return
# Enabled/Disable cron based on the number of 'done' server of type pop or imap
cron.toggle(model=self._name, domain=[('state','=','done'), ('type','in',['pop','imap'])])
def create(self, cr, uid, values, context=None):
res = super(fetchmail_server, self).create(cr, uid, values, context=context)
self._update_cron(cr, uid, context=context)
return res
def write(self, cr, uid, ids, values, context=None):
res = super(fetchmail_server, self).write(cr, uid, ids, values, context=context)
self._update_cron(cr, uid, context=context)
return res
def unlink(self, cr, uid, ids, context=None):
res = super(fetchmail_server, self).unlink(cr, uid, ids, context=context)
self._update_cron(cr, uid, context=context)
return res
pass
+16 -20
View File
@@ -1,29 +1,25 @@
# -*- coding: utf-8 -*-
from openerp.osv import fields, osv
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class MailMail(models.Model):
class mail_mail(osv.osv):
_inherit = "mail.mail"
_columns = {
'fetchmail_server_id': fields.many2one('fetchmail.server', "Inbound Mail Server",
readonly=True,
select=True,
oldname='server_id'),
}
def create(self, cr, uid, values, context=None):
if context is None:
context = {}
fetchmail_server_id = context.get('fetchmail_server_id')
fetchmail_server_id = fields.Many2one('fetchmail.server', "Inbound Mail Server", readonly=True, index=True, oldname='server_id')
@api.model
def create(self, values):
fetchmail_server_id = self.env.context.get('fetchmail_server_id')
if fetchmail_server_id:
values['fetchmail_server_id'] = fetchmail_server_id
res = super(mail_mail, self).create(cr, uid, values, context=context)
return res
return super(MailMail, self).create(values)
def write(self, cr, uid, ids, values, context=None):
if context is None:
context = {}
fetchmail_server_id = context.get('fetchmail_server_id')
@api.multi
def write(self, values):
fetchmail_server_id = self.env.context.get('fetchmail_server_id')
if fetchmail_server_id:
values['fetchmail_server_id'] = fetchmail_server_id
res = super(mail_mail, self).write(cr, uid, ids, values, context=context)
return res
return super(MailMail, self).write(values)
+5 -7
View File
@@ -1,22 +1,20 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from openerp.osv import fields, osv
from odoo import api, fields, models
class fetchmail_config_settings(osv.osv_memory):
class FetchmailConfigSettings(models.TransientModel):
""" This wizard can be inherited in conjunction with 'res.config.settings', in order to
define fields that configure a fetchmail server.
It relies on the following convention on the object::
class my_config_settings(osv.osv_memory):
class MyConfigSettings(models.TransientModel):
_name = 'my.settings'
_inherit = ['res.config.settings', 'fetchmail.config.settings']
_columns = {
'fetchmail_stuff': fields.boolean(...,
fetchmail_model='my.stuff', fetchmail_name='Incoming Stuff'),
}
fetchmail_stuff = fields.Boolean(..., fetchmail_model='my.stuff', fetchmail_name='Incoming Stuff'),
def configure_fetchmail_stuff(self, cr, uid, ids, context=None):
return self.configure_fetchmail(cr, uid, 'fetchmail_stuff', context)
+10 -12
View File
@@ -1,8 +1,7 @@
<?xml version="1.0"?>
<openerp>
<data>
<odoo>
<record model="ir.ui.view" id="view_email_server_tree">
<record id="view_email_server_tree" model="ir.ui.view">
<field name="name">fetchmail.server.list</field>
<field name="model">fetchmail.server</field>
<field name="arch" type="xml">
@@ -18,7 +17,7 @@
</field>
</record>
<record model="ir.ui.view" id="view_email_server_form">
<record id="view_email_server_form" model="ir.ui.view">
<field name="name">fetchmail.server.form</field>
<field name="model">fetchmail.server</field>
<field name="arch" type="xml">
@@ -32,7 +31,7 @@
<sheet>
<group col="4">
<field name="name"/>
<field name="type" on_change="onchange_server_type(type, is_ssl, object_id)"/>
<field name="type"/>
<field name="date"/>
</group>
<notebook>
@@ -41,14 +40,14 @@
<group attrs="{'invisible' : [('type', '=', 'local')]}" string="Server Information">
<field name="server" colspan="2" attrs="{'required' : [('type', '!=', 'local')]}" />
<field name="port" required="1" attrs="{'required' : [('type', '!=', 'local')]}" />
<field name="is_ssl" on_change="onchange_server_type(type, is_ssl)"/>
<field name="is_ssl"/>
</group>
<group attrs="{'invisible' : [('type', '=', 'local')]}" string="Login Information">
<field name="user" attrs="{'required' : [('type', '!=', 'local')]}"/>
<field name="password" password="True" attrs="{'required' : [('type', '!=', 'local')]}"/>
</group>
<group string="Actions to Perform on Incoming Mails">
<field name="object_id" on_change="onchange_server_type(type, is_ssl, object_id)"/>
<field name="object_id"/>
<field name="action_id" groups="base.group_no_one"/>
</group>
<group attrs="{'invisible' : [('type', '!=', 'local')]}" string="Configuration">
@@ -71,7 +70,7 @@
</field>
</record>
<record model="ir.ui.view" id="view_email_server_search">
<record id="view_email_server_search" model="ir.ui.view">
<field name="name">fetchmail.server.search</field>
<field name="model">fetchmail.server</field>
<field name="arch" type="xml">
@@ -85,7 +84,7 @@
</field>
</record>
<record model="ir.actions.act_window" id="action_email_server_tree">
<record id="action_email_server_tree" model="ir.actions.act_window">
<field name="name">Incoming Mail Servers</field>
<field name="res_model">fetchmail.server</field>
<field name="view_type">form</field>
@@ -93,7 +92,7 @@
<field name="view_id" ref="view_email_server_tree"/>
<field name="search_view_id" ref="view_email_server_search"/>
</record>
<record id="inherit_view_general_configuration" model="ir.ui.view">
<field name="name">General Settings</field>
<field name="model">base.config.settings</field>
@@ -119,5 +118,4 @@
groups="base.group_no_one"
/>
</data>
</openerp>
</odoo>