from __future__ import unicode_literals
import imaplib
+import itertools
import select
import socket
import sys
imaplib.Commands['IDLE'] = imaplib.Commands['APPEND']
+# and ID. RFC2971 says that this command is valid in all states, but not that
+# some servers (*cough* FastMail *cough*) don't seem to accept it in state
+# NONAUTH.
+if 'ID' not in imaplib.Commands:
+ imaplib.Commands['ID'] = ('NONAUTH', 'AUTH', 'SELECTED')
+
+
# System flags
DELETED = r'\Deleted'
SEEN = r'\Seen'
"""Authenticate using the OAUTH2 method.
Gmail and Yahoo both support the 'XOAUTH2' mechanism, but Yahoo requires
- the 'vendor' portion in the payload.
+ the 'vendor' portion in the payload.
"""
auth_string = 'user=%s\1auth=Bearer %s\1' % (user, access_token)
if vendor:
self._check_resp('BYE', 'logout', typ, data)
return data[0]
+ def id_(self, parameters=None):
+ """Issue an ID command.
+
+ *parameters* should be specified as a dictionary of field/value pairs,
+ for example ```{"name": "IMAPClient", "version": "0.12"}``
+ """
+ if not self.has_capability('ID'):
+ raise ValueError('server does not support IMAP ID extension')
+ if parameters is None:
+ args = 'NIL'
+ else:
+ if not isinstance(parameters, dict):
+ raise TypeError("'parameters' should be a dictionary")
+ args = seq_to_parenstr(
+ _quote(v) for v in
+ itertools.chain.from_iterable(parameters.items()))
+
+ typ, data = self._imap._simple_command('ID', args)
+ self._checkok('id', typ, data)
+ typ, data = self._imap._untagged_response(typ, data, 'ID')
+ return parse_response(data)
+
def capabilities(self):
"""Returns the server capability list.
self.client.thread(algorithm='FOO', criteria='STUFF', charset='ASCII')
self.client._imap.thread.assert_called_once_with(b'FOO', b'ASCII', '(STUFF)')
+
+class TestId(IMAPClientTest):
+
+ def test_id(self):
+ self.client._cached_capabilities = (b'ID',)
+ self.client._imap._simple_command.return_value = ('OK', [b'Success'])
+ self.client._imap._untagged_response.return_value = (
+ b'OK', [b'("name" "GImap" "vendor" "Google, Inc.")'])
+
+ id_response = self.client.id_({'name': 'IMAPClient'})
+ self.client._imap._simple_command.assert_called_with(
+ 'ID', u'("name" "IMAPClient")')
+
+ assert id_response == (('name', 'GImap', 'vendor', 'Google, Inc.'),)
+
+ def test_no_support(self):
+ self.client._cached_capabilities = (b'IMAP4rev1',)
+ self.assertRaises(ValueError, self.client.id_)
+
+ def test_invalid_parameters(self):
+ self.assertRaises(TypeError, self.client.id_, 'bananarama')