Skip to content

Commit

Permalink
[IMP] fetchmail_.._folder: optionally archive messages automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
NL66278 committed May 11, 2024
1 parent 6a24dec commit bb015b6
Show file tree
Hide file tree
Showing 5 changed files with 519 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fetchmail_attach_from_folder/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Email gateway - folders
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:95f6645118da34dd962fa794f5fca9c8797579a9a585d80228407f4997e9ba91
!! source digest: sha256:907101d997473fcc276317ee6d3bf95f2f005eab22b585dd59644a757959ca10
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -91,6 +91,10 @@ Odoo and attached to the partner in question. After some testing, you
might want to check ``Delete matches`` in your folder configuration so
that this folder doesn't grow indefinitely.

Another way to prevent having to process ever more messages from the
folder to read is to automatically move all processed messages to an
archive folder that can be specified.

Bug Tracker
===========

Expand Down
27 changes: 27 additions & 0 deletions fetchmail_attach_from_folder/models/fetchmail_server_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class FetchmailServerFolder(models.Model):
help="The path to your mail folder."
" Typically would be something like 'INBOX.myfolder'",
)
archive_path = fields.Char(
help="The path where successfully retrieved messages will be stored.",
)
model_id = fields.Many2one(
comodel_name="ir.model",
required=True,
Expand Down Expand Up @@ -123,6 +126,7 @@ def fetch_mail(self):
try:
# New connection per folder
connection = this.server_id.connect()
this.check_imap_archive_folder(connection)
this.retrieve_imap_folder(connection)
connection.close()
except Exception:
Expand All @@ -141,6 +145,20 @@ def fetch_mail(self):
if connection:
connection.logout()

def check_imap_archive_folder(self, connection):
"""If archive folder specified, check existance and create when needed."""
self.ensure_one()
server = self.server_id
if not self.archive_path:
return
if connection.select(self.archive_path)[0] != "OK":
connection.create(self.archive_path)
if connection.select(self.archive_path)[0] != "OK":
raise UserError(
_("Could not create archive folder %(folder)s on server %(server)s")
% {"folder": self.archive_path, "server": server.name}
)

def retrieve_imap_folder(self, connection):
"""Retrieve all mails for one IMAP folder."""
self.ensure_one()
Expand Down Expand Up @@ -204,6 +222,8 @@ def apply_matching(self, connection, msgid):
)
matched = True if thread_id else False
self.update_msg(connection, msgid, matched=matched)
if self.archive_path:
self._archive_msg(connection, msgid)

def fetch_msg(self, connection, msgid):
"""Select a single message from a folder."""
Expand All @@ -227,3 +247,10 @@ def update_msg(self, connection, msgid, matched=True, flagged=False):
else:
if self.flag_nonmatching:
connection.store(msgid, "+FLAGS", "\\FLAGGED")

def _archive_msg(self, connection, msgid):
"""Archive message. Folder should already have been created."""
self.ensure_one()
connection.copy(msgid, self.archive_path)
connection.store(msgid, "+FLAGS", "\\Deleted")
connection.expunge()
4 changes: 4 additions & 0 deletions fetchmail_attach_from_folder/readme/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ Now when your users drop mails into this folder, they will be fetched by Odoo
and attached to the partner in question. After some testing, you might want to
check `Delete matches` in your folder configuration so that this folder doesn't
grow indefinitely.

Another way to prevent having to process ever more messages from the folder
to read is to automatically move all processed messages to an archive folder
that can be specified.
Loading

0 comments on commit bb015b6

Please sign in to comment.