self._starttls_done = True
- # Clear cached capabilities as per
- # https://tools.ietf.org/html/rfc2595#section-3.1
- self._cached_capabilities = None
-
self._imap.sock = tls.wrap_socket(self._imap.sock, ssl_context, self.host)
self._imap.file = self._imap.sock.makefile()
return data[0]
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 authenticated and the server has returned an
+ untagged 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 the session is not yet authenticated, the capabilities
+ requested at connection time will be returned.
"""
+ # Ensure cached capabilities aren't used post-STARTTLS. As per
+ # https://tools.ietf.org/html/rfc2595#section-3.1
+ if self._starttls_done and self._imap.state == 'NONAUTH':
+ self._cached_capabilities = None
+ return self._do_capabilites()
+
# If a capability response has been cached, use that.
if self._cached_capabilities:
return self._cached_capabilities
untagged = _dict_bytes_normaliser(self._imap.untagged_responses)
response = untagged.pop(b'CAPABILITY', None)
if response:
- return self._save_capabilities(response[0])
+ self._cached_capabilities = self._normalise_capabilites(response[0])
+ return self._cached_capabilities
# 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)
+ self._cached_capabilities = self._do_capabilites()
+ return self._cached_capabilities
- # Just return capabilities that imaplib grabbed at connection
+ # Return capabilities that imaplib requested at connection
# time (pre-auth)
return tuple(to_bytes(c) for c in self._imap.capabilities)
- def _save_capabilities(self, raw_response):
+ def _do_capabilites(self):
+ raw_response = self._command_and_check('capability', unpack=True)
+ return self._normalise_capabilites(raw_response)
+
+ def _normalise_capabilites(self, raw_response):
raw_response = to_bytes(raw_response)
- self._cached_capabilities = tuple(raw_response.upper().split())
- return self._cached_capabilities
+ return tuple(raw_response.upper().split())
def has_capability(self, capability):
"""Return ``True`` if the IMAP server has the given *capability*.
self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR'))
self.assertEqual(self.client._cached_capabilities, (b'FOO', b'BAR'))
+ def test_with_starttls(self):
+ # Initial connection
+ self.client._imap.capabilities = ('FOO',)
+ self.client._imap.untagged_responses = {}
+ self.client._imap.state = 'NONAUTH'
+ self.assertEqual(self.client.capabilities(), (b'FOO',))
+
+ # Now do STARTTLS; capabilities change and should be reported.
+ self.client._starttls_done = True
+ self.client._imap.capability.return_value = ('OK', [b'FOO BAR'])
+ self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR'))
+
+ # Login done; capabilities change again.
+ self.client._imap.state = 'AUTH'
+ self.client._imap.capability.return_value = ('OK', [b'FOO BAR QUX'])
+ self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR', b'QUX'))
+
def test_has_capability(self):
self.client._cached_capabilities = (b'FOO', b'MORE')
self.assertEqual(self.client._imap.file, sentinel.file)
self.assertEqual(resp, b'start TLS negotiation')
- def test_clears_cached_capabilities(self):
- # As per https://tools.ietf.org/html/rfc2595#section-3.1
-
- self.client._save_capabilities(b"FOO BAR")
- self.assertEqual(self.client.capabilities(), (b"FOO", b"BAR"))
-
- self.client.starttls(sentinel.ssl_context)
-
- # Now verify that the cache has been cleared and new
- # capabilities are read.
- self.client._imap.untagged_responses = {'CAPABILITY': [b'META']}
- self.assertEqual(self.client.capabilities(), (b"META",))
-
def test_command_fails(self):
self.client._imap._simple_command.return_value = "NO", [b'sorry']