.. -*-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
=======
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
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*.
"""
# 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,
(("#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()