From 43cac375f34913641db08137ef8a970ce0d0b519 Mon Sep 17 00:00:00 2001 From: Patrick Ulbrich Date: Sat, 17 Sep 2016 12:23:45 +0200 Subject: [PATCH] Don't allow to re-open accounts (hack, not needed anymore) --- Mailnag/backends/base.py | 6 ++---- Mailnag/backends/imap.py | 12 ++++++------ Mailnag/backends/pop3.py | 12 ++++++------ Mailnag/common/accounts.py | 9 ++------- Mailnag/daemon/idlers.py | 6 +++--- Mailnag/daemon/mails.py | 5 +++-- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/Mailnag/backends/base.py b/Mailnag/backends/base.py index aefd6eb..14af852 100644 --- a/Mailnag/backends/base.py +++ b/Mailnag/backends/base.py @@ -39,10 +39,8 @@ class MailboxBackend(object): """ @abstractmethod - def open(self, reopen): - """Opens the mailbox. - If reopen is true, mailbox should first be closed and then opened. - """ + def open(self): + """Opens the mailbox.""" raise NotImplementedError @abstractmethod diff --git a/Mailnag/backends/imap.py b/Mailnag/backends/imap.py index 1a32aee..74dc0c8 100644 --- a/Mailnag/backends/imap.py +++ b/Mailnag/backends/imap.py @@ -32,6 +32,7 @@ import re from Mailnag.backends.base import MailboxBackend import Mailnag.common.imaplib2 as imaplib from Mailnag.common.imaplib2 import AUTH +from Mailnag.common.exceptions import InvalidOperationException class IMAPMailboxBackend(MailboxBackend): @@ -51,12 +52,11 @@ class IMAPMailboxBackend(MailboxBackend): self._conn_closed = True - def open(self, reopen): - # try to reuse existing connection - if not reopen and self.is_open(): - return + def open(self): + if self._conn != None: + raise InvalidOperationException("Account is aready open") - self._conn = conn = None + conn = None try: if self.ssl: @@ -149,7 +149,7 @@ class IMAPMailboxBackend(MailboxBackend): # Always create a new connection as an existing one may # be used for IMAP IDLE. - self.open(reopen = True) + self.open() try: status, data = self._conn.list('', '*') diff --git a/Mailnag/backends/pop3.py b/Mailnag/backends/pop3.py index e6ee33d..4a8c0ba 100644 --- a/Mailnag/backends/pop3.py +++ b/Mailnag/backends/pop3.py @@ -30,6 +30,7 @@ import logging import poplib from Mailnag.backends.base import MailboxBackend +from Mailnag.common.exceptions import InvalidOperationException class POP3MailboxBackend(MailboxBackend): @@ -47,12 +48,11 @@ class POP3MailboxBackend(MailboxBackend): self._conn = None - def open(self, reopen): - # try to reuse existing connection - if not reopen and self.is_open(): - return - - self._conn = conn = None + def open(self): + if self._conn != None: + raise InvalidOperationException("Account is aready open") + + conn = None try: if self.ssl: diff --git a/Mailnag/common/accounts.py b/Mailnag/common/accounts.py index 73a7768..61798ee 100644 --- a/Mailnag/common/accounts.py +++ b/Mailnag/common/accounts.py @@ -67,9 +67,9 @@ class Account: self.backend = backend - def open(self, reopen = True): + def open(self): """Open mailbox for the account.""" - self.backend.open(reopen = reopen) + self.backend.open() def close(self): @@ -79,11 +79,6 @@ class Account: # Indicates whether the account # holds an active existing connection. - # Note: this method only indicates if the - # account *holds* (caches) an existing connection. - # There may be further, but no longer - # associated connections if open() - # was called multiple times (with reopen set to False). def is_open(self): """Returns true if the mailbox is opened.""" return self.backend.is_open() diff --git a/Mailnag/daemon/idlers.py b/Mailnag/daemon/idlers.py index 5504127..df4eb38 100644 --- a/Mailnag/daemon/idlers.py +++ b/Mailnag/daemon/idlers.py @@ -62,9 +62,9 @@ class Idler(object): # idle thread def _idle(self): - # reopen = False: # mailbox has been opened in mailnagdaemon.py already (immediate check) - self._account.open(reopen = False) + if not self._account.is_open(): + self._account.open() while True: # if the event is set here, @@ -110,7 +110,7 @@ class Idler(object): while (not self._account.is_open()) and (not self._event.isSet()): logging.info("Trying to reconnect Idler thread for account '%s'." % self._account.name) try: - self._account.open(reopen = True) + self._account.open() logging.info("Successfully reconnected Idler thread for account '%s'." % self._account.name) except Exception as ex: logging.error("Failed to reconnect Idler thread for account '%s' (%s)." % (self._account.name, ex)) diff --git a/Mailnag/daemon/mails.py b/Mailnag/daemon/mails.py index 4054757..e3f89f4 100644 --- a/Mailnag/daemon/mails.py +++ b/Mailnag/daemon/mails.py @@ -62,9 +62,10 @@ class MailCollector: mail_ids = {} for acc in self._accounts: - # open mailbox for this account + # open mailbox for this account try: - acc.open(reopen = False) + if not acc.is_open(): + acc.open() except Exception as ex: logging.error("Failed to open mailbox for account '%s' (%s)." % (acc.name, ex)) continue