diff --git a/Mailnag/backends/base.py b/Mailnag/backends/base.py index 2a90ad1..0a55ecd 100644 --- a/Mailnag/backends/base.py +++ b/Mailnag/backends/base.py @@ -67,6 +67,11 @@ class MailboxBackend(object): """ raise NotImplementedError + def supports_notifications(self): + """Returns True if mailbox supports notifications.""" + # Default implementation + return False + @abstractmethod def notify_next_change(self, callback=None, timeout=None): """Asks mailbox to notify next change. diff --git a/Mailnag/backends/imap.py b/Mailnag/backends/imap.py index dacecc2..2bd0875 100644 --- a/Mailnag/backends/imap.py +++ b/Mailnag/backends/imap.py @@ -40,7 +40,7 @@ class IMAPMailboxBackend(MailboxBackend): """Implementation of IMAP mail boxes.""" def __init__(self, name = '', user = '', password = '', oauth2string = '', - server = '', port = '', ssl = True, folders = [], **kw): + server = '', port = '', ssl = True, folders = [], idle=True, **kw): self.name = name self.user = user self.password = password @@ -49,6 +49,7 @@ class IMAPMailboxBackend(MailboxBackend): self.port = port self.ssl = ssl # bool self.folders = [encode_mutf7(folder) for folder in folders] + self.idle = idle self._conn = None @@ -130,6 +131,11 @@ class IMAPMailboxBackend(MailboxBackend): return lst + def supports_notifications(self): + """Returns True if mailbox supports notifications. + IMAP mailbox supports notifications if idle parameter is True""" + return self.idle + def notify_next_change(self, callback=None, timeout=None): self._ensure_open() diff --git a/Mailnag/common/accounts.py b/Mailnag/common/accounts.py index f36b813..98e6b90 100644 --- a/Mailnag/common/accounts.py +++ b/Mailnag/common/accounts.py @@ -116,6 +116,11 @@ class Account: return self._get_backend().list_messages() + def supports_notifications(self): + """Returns True if account supports notifications.""" + return self._get_backend().supports_notifications() + + def notify_next_change(self, callback=None, timeout=None): """Asks mailbox to notify next change. Callback is called when new mail arrives or removed. diff --git a/tests/test_account.py b/tests/test_account.py index 9c08e17..482e130 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -22,6 +22,8 @@ """Test cases for Account.""" +import pytest + from Mailnag.common.accounts import Account @@ -109,3 +111,18 @@ def test_account_should_configurable_with_any_parameters(): assert config['weird'] == 'odd' assert config['odd'] == 'weird' + +@pytest.mark.parametrize("config,should_support", [ + ({'mailbox_type': 'imap', 'idle': True}, True), + ({'mailbox_type': 'imap', 'idle': False}, False), + ({'mailbox_type': 'pop3'}, False), + ({'mailbox_type': 'mbox'}, False), + ({'mailbox_type': 'maildir'}, False), +]) +def test_account_supports_notifications(config, should_support): + account = Account(**config) + if should_support: + assert account.supports_notifications() + else: + assert not account.supports_notifications() +