]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Enhanced/fixed CAPABILITY handling
authorMenno Smits <menno@freshfoo.com>
Wed, 27 Mar 2013 20:31:48 +0000 (20:31 +0000)
committerMenno Smits <menno@freshfoo.com>
Wed, 27 Mar 2013 20:31:48 +0000 (20:31 +0000)
NEWS
imapclient/imapclient.py
imapclient/test/test_imapclient.py

diff --git a/NEWS b/NEWS
index 3e1af41fae940a510908efead363e249c092a38c..7efd67bfb4b7a0612a7ac66d9d73b51329bec91f 100644 (file)
--- 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
 =======
index 9426235bcb4d24054e25e2260402bedc7e6546c6..49947c25ee558be8a8bda23a01ab03d150383791 100644 (file)
@@ -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,
index 096bf0d11367d032285a5e1c4d436ccdd82101e3..1ff737136a68d5dc10a9a8fc700426a3d42c07f3 100644 (file)
@@ -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()