From c9211b824271044e8d3cecb538a18ebed839f94c Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Wed, 27 Mar 2013 20:31:48 +0000 Subject: [PATCH] Enhanced/fixed CAPABILITY handling --- NEWS | 15 +++++++++++++ imapclient/imapclient.py | 35 ++++++++++++++++++++++++++---- imapclient/test/test_imapclient.py | 32 +++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 3e1af41..7efd67b 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,19 @@ .. -*-Mode: rst; -*- +======= + 0.9.2 +======= + +Enhanced capability querying [NEW] +---------------------------------- +Previously only the pre-authentication server capabilities were +returned by the capabilities() method. Now, if the connection is +authenticated, the post-authentication capabilities will be returned. +If the server sent an untagged CAPABILITY response after authentication, +that will be used, avoiding an unnecessary CAPABILITY command call. + +All this ensures that the client sees all available server +capabilities. + ======= 0.9.1 ======= diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 9426235..49947c2 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -111,6 +111,7 @@ class IMAPClient(object): self.log_file = sys.stderr self.normalise_times = True + self._cached_capabilities = None self._imap = self._create_IMAP4() self._imap._mesg = self._log # patch in custom debug log method self._idle_tag = None @@ -159,9 +160,38 @@ class IMAPClient(object): def capabilities(self): """Returns the server capability list. + + If the session is authenticated and the server has returned a + CAPABILITY response at authentication time, this response + will be returned. Otherwise, the CAPABILITY command will be + issued to the server, with the results cached for future calls. + + If the session is not yet authenticated, the cached + capabilities determined at connection time will be returned. """ + # if a capability response has been cached, use that + if self._cached_capabilities: + return self._cached_capabilities + + # If server returned an untagged CAPABILITY response (during + # authentication), cache it and return that. + response = self._imap.untagged_responses.pop('CAPABILITY', None) + if response: + return self._save_capabilities(response[0]) + + # if authenticated, but don't have a capability reponse, ask for one + if self._imap.state in ('SELECTED', 'AUTH'): + response = self._command_and_check('capability', unpack=True) + return self._save_capabilities(response) + + # Just return capabilities that imaplib grabbed at connection + # time (pre-auth) return self._imap.capabilities + def _save_capabilities(self, raw_response): + self._cached_capabilities = tuple(raw_response.upper().split()) + return self._cached_capabilities + def has_capability(self, capability): """Return ``True`` if the IMAP server has the given *capability*. """ @@ -170,10 +200,7 @@ class IMAPClient(object): # capabilities may in the future be named SORT2 which is # still compatible with the current standard and will not # be detected by this method. - if capability.upper() in self._imap.capabilities: - return True - else: - return False + return capability.upper() in self.capabilities() def namespace(self): """Return the namespace for the account as a (personal, other, diff --git a/imapclient/test/test_imapclient.py b/imapclient/test/test_imapclient.py index 096bf0d..1ff7371 100644 --- a/imapclient/test/test_imapclient.py +++ b/imapclient/test/test_imapclient.py @@ -390,6 +390,38 @@ class TestNamespace(IMAPClientTest): (("#shared/", "/"), ("#public/", "/"), ("#ftp/", "/"), ("#news.", ".")), )) +class TestCapabilities(IMAPClientTest): + + def test_preauth(self): + self.client._imap.capabilities = ('FOO', 'BAR') + self.client._imap.untagged_responses = {} + + self.assertEquals(self.client.capabilities(), ('FOO', 'BAR')) + + def test_server_returned_capability_after_auth(self): + self.client._imap.capabilities = ('FOO',) + self.client._imap.untagged_responses = {'CAPABILITY': ['FOO MORE']} + + self.assertEquals(self.client._cached_capabilities, None) + self.assertEquals(self.client.capabilities(), ('FOO', 'MORE')) + self.assertEquals(self.client._cached_capabilities, ('FOO', 'MORE')) + + def test_caching(self): + self.client._imap.capabilities = ('FOO',) + self.client._imap.untagged_responses = {} + self.client._cached_capabilities = ('FOO', 'MORE') + + self.assertEquals(self.client.capabilities(), ('FOO', 'MORE')) + + def test_post_auth_request(self): + self.client._imap.capabilities = ('FOO',) + self.client._imap.untagged_responses = {} + self.client._imap.state = 'SELECTED' + self.client._imap.capability.return_value = ('OK', ['FOO BAR']) + + self.assertEquals(self.client.capabilities(), ('FOO', 'BAR')) + self.assertEquals(self.client._cached_capabilities, ('FOO', 'BAR')) + if __name__ == '__main__': unittest.main() -- 2.43.0