|
@ -23,12 +23,11 @@ |
|
|
import base64 |
|
|
import base64 |
|
|
import simplejson |
|
|
import simplejson |
|
|
from lxml import etree |
|
|
from lxml import etree |
|
|
from openerp.osv.orm import Model, except_orm, browse_null |
|
|
|
|
|
|
|
|
from openerp.osv.orm import Model, except_orm |
|
|
from openerp.tools.translate import _ |
|
|
from openerp.tools.translate import _ |
|
|
from openerp.osv import fields |
|
|
from openerp.osv import fields |
|
|
from openerp.addons.fetchmail.fetchmail import _logger as logger |
|
|
from openerp.addons.fetchmail.fetchmail import _logger as logger |
|
|
from openerp.tools.misc import UnquoteEvalContext |
|
|
from openerp.tools.misc import UnquoteEvalContext |
|
|
from openerp.tools.safe_eval import safe_eval |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class fetchmail_server(Model): |
|
|
class fetchmail_server(Model): |
|
@ -36,7 +35,7 @@ class fetchmail_server(Model): |
|
|
|
|
|
|
|
|
_columns = { |
|
|
_columns = { |
|
|
'folder_ids': fields.one2many( |
|
|
'folder_ids': fields.one2many( |
|
|
'fetchmail.server.folder', 'server_id', 'Folders'), |
|
|
|
|
|
|
|
|
'fetchmail.server.folder', 'server_id', 'Folders'), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_defaults = { |
|
|
_defaults = { |
|
@ -95,20 +94,19 @@ class fetchmail_server(Model): |
|
|
|
|
|
|
|
|
if connection.select(folder.path)[0] != 'OK': |
|
|
if connection.select(folder.path)[0] != 'OK': |
|
|
logger.error( |
|
|
logger.error( |
|
|
'Could not open mailbox %s on %s' % ( |
|
|
|
|
|
folder.path, this.server)) |
|
|
|
|
|
|
|
|
'Could not open mailbox %s on %s' % (folder.path, this.server)) |
|
|
connection.select() |
|
|
connection.select() |
|
|
continue |
|
|
continue |
|
|
result, msgids = this.get_msgids(connection) |
|
|
result, msgids = this.get_msgids(connection) |
|
|
if result != 'OK': |
|
|
if result != 'OK': |
|
|
logger.error( |
|
|
logger.error( |
|
|
'Could not search mailbox %s on %s' % ( |
|
|
'Could not search mailbox %s on %s' % ( |
|
|
folder.path, this.server)) |
|
|
|
|
|
|
|
|
folder.path, this.server)) |
|
|
continue |
|
|
continue |
|
|
|
|
|
|
|
|
for msgid in msgids[0].split(): |
|
|
for msgid in msgids[0].split(): |
|
|
matched_object_ids += this.apply_matching( |
|
|
matched_object_ids += this.apply_matching( |
|
|
connection, folder, msgid, match_algorithm) |
|
|
|
|
|
|
|
|
connection, folder, msgid, match_algorithm) |
|
|
|
|
|
|
|
|
logger.info('finished checking for emails in %s server %s', |
|
|
logger.info('finished checking for emails in %s server %s', |
|
|
folder.path, this.name) |
|
|
folder.path, this.name) |
|
@ -130,16 +128,16 @@ class fetchmail_server(Model): |
|
|
|
|
|
|
|
|
if result != 'OK': |
|
|
if result != 'OK': |
|
|
logger.error( |
|
|
logger.error( |
|
|
'Could not fetch %s in %s on %s' % ( |
|
|
|
|
|
msgid, folder.path, this.server)) |
|
|
|
|
|
|
|
|
'Could not fetch %s in %s on %s' % (msgid, folder.path, this.server)) |
|
|
continue |
|
|
continue |
|
|
|
|
|
|
|
|
mail_message = self.pool.get('mail.thread').message_parse( |
|
|
mail_message = self.pool.get('mail.thread').message_parse( |
|
|
cr, uid, msgdata[0][1], save_original=this.original, |
|
|
|
|
|
context=context) |
|
|
|
|
|
|
|
|
cr, uid, msgdata[0][1], save_original=this.original, |
|
|
|
|
|
context=context) |
|
|
|
|
|
|
|
|
if self.pool.get('mail.message').search(cr, uid, [ |
|
|
|
|
|
('message_id', '=', mail_message['message_id'])]): |
|
|
|
|
|
|
|
|
if self.pool.get('mail.message').search( |
|
|
|
|
|
cr, uid, [ |
|
|
|
|
|
('message_id', '=', mail_message['message_id'])]): |
|
|
continue |
|
|
continue |
|
|
|
|
|
|
|
|
found_ids = match_algorithm.search_matches( |
|
|
found_ids = match_algorithm.search_matches( |
|
@ -156,7 +154,7 @@ class fetchmail_server(Model): |
|
|
msgdata[0][1], msgid, context) |
|
|
msgdata[0][1], msgid, context) |
|
|
cr.execute('release savepoint apply_matching') |
|
|
cr.execute('release savepoint apply_matching') |
|
|
matched_object_ids += found_ids[:1] |
|
|
matched_object_ids += found_ids[:1] |
|
|
except Exception, e: |
|
|
|
|
|
|
|
|
except Exception: |
|
|
cr.execute('rollback to savepoint apply_matching') |
|
|
cr.execute('rollback to savepoint apply_matching') |
|
|
logger.exception( |
|
|
logger.exception( |
|
|
"Failed to fetch mail %s from %s", |
|
|
"Failed to fetch mail %s from %s", |
|
@ -183,40 +181,40 @@ class fetchmail_server(Model): |
|
|
cr, uid, object_id, context |
|
|
cr, uid, object_id, context |
|
|
).partner_id.id |
|
|
).partner_id.id |
|
|
|
|
|
|
|
|
attachments=[] |
|
|
|
|
|
|
|
|
attachments = [] |
|
|
if this.attach and mail_message.get('attachments'): |
|
|
if this.attach and mail_message.get('attachments'): |
|
|
for attachment in mail_message['attachments']: |
|
|
for attachment in mail_message['attachments']: |
|
|
fname, fcontent = attachment |
|
|
fname, fcontent = attachment |
|
|
if isinstance(fcontent, unicode): |
|
|
if isinstance(fcontent, unicode): |
|
|
fcontent = fcontent.encode('utf-8') |
|
|
fcontent = fcontent.encode('utf-8') |
|
|
data_attach = { |
|
|
data_attach = { |
|
|
'name': fname, |
|
|
|
|
|
'datas': base64.b64encode(str(fcontent)), |
|
|
|
|
|
'datas_fname': fname, |
|
|
|
|
|
'description': _('Mail attachment'), |
|
|
|
|
|
'res_model': folder.model_id.model, |
|
|
|
|
|
'res_id': object_id, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
'name': fname, |
|
|
|
|
|
'datas': base64.b64encode(str(fcontent)), |
|
|
|
|
|
'datas_fname': fname, |
|
|
|
|
|
'description': _('Mail attachment'), |
|
|
|
|
|
'res_model': folder.model_id.model, |
|
|
|
|
|
'res_id': object_id, |
|
|
|
|
|
} |
|
|
attachments.append( |
|
|
attachments.append( |
|
|
self.pool.get('ir.attachment').create( |
|
|
self.pool.get('ir.attachment').create( |
|
|
cr, uid, data_attach, context=context)) |
|
|
cr, uid, data_attach, context=context)) |
|
|
|
|
|
|
|
|
mail_message_ids.append( |
|
|
mail_message_ids.append( |
|
|
self.pool.get('mail.message').create( |
|
|
|
|
|
cr, uid, |
|
|
|
|
|
{ |
|
|
|
|
|
'author_id': partner_id, |
|
|
|
|
|
'model': folder.model_id.model, |
|
|
|
|
|
'res_id': object_id, |
|
|
|
|
|
'type': 'email', |
|
|
|
|
|
'body': mail_message.get('body'), |
|
|
|
|
|
'subject': mail_message.get('subject'), |
|
|
|
|
|
'email_from': mail_message.get('from'), |
|
|
|
|
|
'date': mail_message.get('date'), |
|
|
|
|
|
'message_id': mail_message.get('message_id'), |
|
|
|
|
|
'attachment_ids': [(6, 0, attachments)], |
|
|
|
|
|
}, |
|
|
|
|
|
context)) |
|
|
|
|
|
|
|
|
self.pool.get('mail.message').create( |
|
|
|
|
|
cr, uid, |
|
|
|
|
|
{ |
|
|
|
|
|
'author_id': partner_id, |
|
|
|
|
|
'model': folder.model_id.model, |
|
|
|
|
|
'res_id': object_id, |
|
|
|
|
|
'type': 'email', |
|
|
|
|
|
'body': mail_message.get('body'), |
|
|
|
|
|
'subject': mail_message.get('subject'), |
|
|
|
|
|
'email_from': mail_message.get('from'), |
|
|
|
|
|
'date': mail_message.get('date'), |
|
|
|
|
|
'message_id': mail_message.get('message_id'), |
|
|
|
|
|
'attachment_ids': [(6, 0, attachments)], |
|
|
|
|
|
}, |
|
|
|
|
|
context)) |
|
|
|
|
|
|
|
|
if folder.delete_matching: |
|
|
if folder.delete_matching: |
|
|
connection.store(msgid, '+FLAGS', '\\DELETED') |
|
|
connection.store(msgid, '+FLAGS', '\\DELETED') |
|
|