From a507bc89b7b6b905ef0b2bfa457eafa0739075bc Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 1 Nov 2019 21:21:24 -0400 Subject: [PATCH] Add extra logging to the IMAP email source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GitLab issue #682. Thanks to Patrick Hütter (@PatrickHuetter) for the report. Signed-off-by: Roberto Rosario --- HISTORY.rst | 2 + mayan/apps/sources/models/email_sources.py | 47 +++++++++++++++++----- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 2c868e820b..99d5c63666 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -36,6 +36,8 @@ - Add a version attribute to setting namespace. These are dumped as SMART_SETTINGS_NAMESPACES. - Add savesettings command. +- Add extra logging to the IMAP email source. GitLab issue #682. + Thanks to Patrick Hütter (@PatrickHuetter) for the report. 3.2.8 (2019-10-01) ================== diff --git a/mayan/apps/sources/models/email_sources.py b/mayan/apps/sources/models/email_sources.py index 4c07219088..d74347f206 100644 --- a/mayan/apps/sources/models/email_sources.py +++ b/mayan/apps/sources/models/email_sources.py @@ -20,6 +20,7 @@ from mayan.apps.documents.models import Document from mayan.apps.metadata.api import set_bulk_metadata from mayan.apps.metadata.models import MetadataType +from ..exceptions import SourceException from ..literals import ( DEFAULT_IMAP_MAILBOX, DEFAULT_METADATA_ATTACHMENT_NAME, DEFAULT_POP3_TIMEOUT, SOURCE_CHOICE_EMAIL_IMAP, SOURCE_CHOICE_EMAIL_POP3, @@ -238,9 +239,22 @@ class IMAPEmail(EmailBaseModel): mailbox = imaplib.IMAP4(host=self.host, port=self.port) mailbox.login(user=self.username, password=self.password) - mailbox.select(mailbox=self.mailbox) + try: + mailbox.select(mailbox=self.mailbox) + except Exception as exception: + raise SourceException( + 'Error selecting mailbox: {}; {}'.format( + self.mailbox, exception + ) + ) + + try: + status, data = mailbox.search(None, 'NOT', 'DELETED') + except Exception as exception: + raise SourceException( + 'Error executing search command; {}'.format(exception) + ) - status, data = mailbox.search(None, 'NOT', 'DELETED') if data: messages_info = data[0].split() logger.debug('messages count: %s', len(messages_info)) @@ -250,15 +264,30 @@ class IMAPEmail(EmailBaseModel): status, data = mailbox.fetch( message_set=message_number, message_parts='(RFC822)' ) - EmailBaseModel.process_message( - source=self, message_text=data[0][1] - ) + try: + EmailBaseModel.process_message( + source=self, message_text=data[0][1] + ) + except Exception as exception: + raise SourceException( + 'Error processing message number: {}; {}'.format( + message_number, exception + ) + ) if not test: - mailbox.store( - message_set=message_number, command='+FLAGS', - flags=r'\Deleted' - ) + try: + mailbox.store( + message_set=message_number, command='+FLAGS', + flags=r'\Deleted' + ) + except Exception as exception: + raise SourceException( + 'Error executing IMAP store command ' + 'on message number {}; {}'.format( + message_number, exception + ) + ) mailbox.expunge() mailbox.close()