Browse Source

[IMP] refactored fetch_mail

[IMP] also log when we're done with one folder
pull/78/head
Holger Brunn 12 years ago
parent
commit
600727b2c2
  1. 36
      fetchmail_attach_from_folder/model/fetchmail_server.py

36
fetchmail_attach_from_folder/model/fetchmail_server.py

@ -74,9 +74,19 @@ class fetchmail_server(Model):
connection = this.connect() connection = this.connect()
for folder in this.folder_ids: for folder in this.folder_ids:
this.handle_folder(connection, folder)
connection.close()
return super(fetchmail_server, self).fetch_mail(
cr, uid, check_original, context)
def handle_folder(self, cr, uid, ids, connection, folder, context=None):
for this in self.browse(cr, uid, ids, context=context):
logger.info('start checking for emails in %s server %s', logger.info('start checking for emails in %s server %s',
folder.path, this.name) folder.path, this.name)
matcher = folder.get_algorithm()
match_algorithm = folder.get_algorithm()
if connection.select(folder.path)[0] != 'OK': if connection.select(folder.path)[0] != 'OK':
logger.error( logger.error(
@ -84,14 +94,28 @@ class fetchmail_server(Model):
folder.path, this.server)) folder.path, this.server))
connection.select() connection.select()
continue continue
result, msgids = connection.search(None, 'UNDELETED')
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():
this.apply_matching(connection, folder, msgid, match_algorithm)
logger.info('finished checking for emails in %s server %s',
folder.path, this.name)
def get_msgids(self, cr, uid, ids, connection, context=None):
return connection.search(None, 'UNDELETED')
def apply_matching(self, cr, uid, ids, connection, folder, msgid,
match_algorithm, context=None):
for this in self.browse(cr, uid, ids, context=context):
result, msgdata = connection.fetch(msgid, '(RFC822)') result, msgdata = connection.fetch(msgid, '(RFC822)')
if result != 'OK': if result != 'OK':
logger.error( logger.error(
'Could not fetch %s in %s on %s' % ( 'Could not fetch %s in %s on %s' % (
@ -105,14 +129,14 @@ class fetchmail_server(Model):
('message_id', '=', mail_message['message-id'])]): ('message_id', '=', mail_message['message-id'])]):
continue continue
found_ids = matcher.search_matches(
found_ids = match_algorithm.search_matches(
cr, uid, folder, cr, uid, folder,
mail_message, msgdata[0][1]) mail_message, msgdata[0][1])
if found_ids and (len(found_ids) == 1 or if found_ids and (len(found_ids) == 1 or
folder.match_first): folder.match_first):
try: try:
matcher.handle_match(
match_algorithm.handle_match(
cr, uid, connection, cr, uid, connection,
found_ids[0], folder, mail_message, found_ids[0], folder, mail_message,
msgdata[0][1], msgid, context) msgdata[0][1], msgid, context)
@ -124,10 +148,6 @@ class fetchmail_server(Model):
msgid, this.name) msgid, this.name)
elif folder.flag_nonmatching: elif folder.flag_nonmatching:
connection.store(msgid, '+FLAGS', '\\FLAGGED') connection.store(msgid, '+FLAGS', '\\FLAGGED')
connection.close()
return super(fetchmail_server, self).fetch_mail(
cr, uid, check_original, context)
def attach_mail( def attach_mail(
self, cr, uid, ids, connection, object_id, folder, self, cr, uid, ids, connection, object_id, folder,

Loading…
Cancel
Save