From e7d5ff77ae779ea58e12472c814e8f48611ffb5a Mon Sep 17 00:00:00 2001 From: Eben Freeman Date: Wed, 3 Jun 2015 00:24:07 +1200 Subject: [PATCH] Add support for the ID command (RFC2971) --- imapclient/imapclient.py | 32 +++++++++++++++++++++++++++++- imapclient/test/test_imapclient.py | 21 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index ee2bc7f..d6c051d 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import imaplib +import itertools import select import socket import sys @@ -42,6 +43,13 @@ if 'IDLE' not in imaplib.Commands: 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' @@ -169,7 +177,7 @@ class IMAPClient(object): """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: @@ -184,6 +192,28 @@ class IMAPClient(object): 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. diff --git a/imapclient/test/test_imapclient.py b/imapclient/test/test_imapclient.py index 45003c3..2a2ab96 100644 --- a/imapclient/test/test_imapclient.py +++ b/imapclient/test/test_imapclient.py @@ -529,3 +529,24 @@ class TestThread(IMAPClientTest): 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') -- 2.43.0