]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Add support for the ID command (RFC2971)
authorEben Freeman <eben@inboxapp.com>
Tue, 2 Jun 2015 12:24:07 +0000 (00:24 +1200)
committerEben Freeman <eben@inboxapp.com>
Tue, 2 Jun 2015 12:24:07 +0000 (00:24 +1200)
imapclient/imapclient.py
imapclient/test/test_imapclient.py

index ee2bc7fd968a8e35bb3fa88eb5062cc14e53ac4f..d6c051d51444319f18c00c2cb58a28595bd41960 100644 (file)
@@ -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.
 
index 45003c3c2338007b9ab3223b6268d7c0fe3e13f1..2a2ab9634c1d2903769ed9b9496536d4f3e1dca1 100644 (file)
@@ -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')