]> khamidou — GitHub mirrors - imapclient.git/commitdiff
NOOP support (#74)
authorMenno Smits <menno@freshfoo.com>
Tue, 8 Nov 2011 20:32:59 +0000 (20:32 +0000)
committerMenno Smits <menno@freshfoo.com>
Tue, 8 Nov 2011 20:32:59 +0000 (20:32 +0000)
Untagged response handling shared with IDLE.

imapclient/imapclient.py
imapclient/test/test_IMAPClient.py
livetest.py

index 664b7e7ead45a96f568372193ed8cdbdf3baa905..d65c688ac98cfdf4c0b51e9306568ffdfabb39b5 100644 (file)
@@ -316,6 +316,25 @@ class IMAPClient(object):
             out[key] = value
         return out
 
+    def noop(self):
+        """Execute the NOOP command.
+
+        This command returns immediately, returning any server side
+        status updates. It can also be used to reset any auto-logout
+        timers.
+
+        The return value is the server command response message
+        followed by a list of status responses. For example::
+
+            ('NOOP completed.',
+             [(4, 'EXISTS'),
+              (3, 'FETCH', ('FLAGS', ('bar', 'sne'))),
+              (6, 'FETCH', ('FLAGS', ('sne',)))])
+
+        """
+        tag = self._imap._command('NOOP')
+        return self._consume_until_tagged_response(tag, 'NOOP')
+
     def idle(self):
         """Put the server into IDLE mode.
 
@@ -372,7 +391,7 @@ class IMAPClient(object):
                     except (socket.timeout, socket.error):
                         break
                     else:
-                        resps.append(_parse_idle_response(line))
+                        resps.append(_parse_untagged_response(line))
             return resps
         finally:
             sock.setblocking(1)
@@ -392,19 +411,7 @@ class IMAPClient(object):
         ``idle_check()``.
         """
         self._imap.send('DONE\r\n')
-        # Slurp up any remaining IDLE responses until the IDLE is done
-        tag = self._idle_tag
-        tagged_commands = self._imap.tagged_commands
-        resps = []
-        while True:
-            line = self._imap._get_response()
-            if tagged_commands[tag]:
-                break
-            resps.append(_parse_idle_response(line))
-        self._idle_tag = None
-        typ, data = tagged_commands.pop(tag)
-        self._checkok('idle', typ, data)
-        return data[0], resps
+        return self._consume_until_tagged_response(self._idle_tag, 'IDLE')
 
     def folder_status(self, folder, what=None):
         """Return the status of *folder*.
@@ -753,6 +760,18 @@ class IMAPClient(object):
         if typ != expected:
             raise self.Error('%s failed: %r' % (command, data[0]))
 
+    def _consume_until_tagged_response(self, tag, command):
+        tagged_commands = self._imap.tagged_commands
+        resps = []
+        while True:
+            line = self._imap._get_response()
+            if tagged_commands[tag]:
+                break
+            resps.append(_parse_untagged_response(line))
+        typ, data = tagged_commands.pop(tag)
+        self._checkok(command, typ, data)
+        return data[0], resps
+
     def _checkok(self, command, typ, data):
         self._check_resp('OK', command, typ, data)
 
@@ -858,7 +877,7 @@ def _quote_arg(arg):
   return '"%s"' % arg
 
 
-def _parse_idle_response(text):
+def _parse_untagged_response(text):
     assert text.startswith('* ')
     text = text[2:]
     if text.startswith(('OK ', 'NO ')):
index da6fbf7ecc7da2590b6a49bba86c576f7b431c76..1367437603429d9628cd026d002c594c498ebba7 100644 (file)
@@ -168,7 +168,7 @@ class TestAclMethods(IMAPClientTest):
         self.assertSequenceEqual(acl, [('Fred', 'rwipslda'), ('Sally', 'rwip')])
 
 
-class TestIdle(IMAPClientTest):
+class TestIdleAndNoop(IMAPClientTest):
 
     def test_idle(self):
         self.client._imap._command.return_value = sentinel.tag
@@ -241,23 +241,50 @@ class TestIdle(IMAPClientTest):
 
     def test_idle_done(self):
         self.client._idle_tag = sentinel.tag
-        self.client._imap.tagged_commands = {sentinel.tag: None}
+
+        mockSend = Mock()
+        self.client._imap.send = mockSend
+        mockConsume = Mock(return_value=sentinel.out)
+        self.client._consume_until_tagged_response = mockConsume
+
+        result = self.client.idle_done()
+
+        mockSend.assert_called_with('DONE\r\n')
+        mockConsume.assert_called_with(sentinel.tag, 'IDLE')
+        self.assertEquals(result, sentinel.out)
+
+    def test_noop(self):
+        mockCommand = Mock(return_value=sentinel.tag)
+        self.client._imap._command = mockCommand
+        mockConsume = Mock(return_value=sentinel.out)
+        self.client._consume_until_tagged_response = mockConsume
+
+        result = self.client.noop()
+
+        mockCommand.assert_called_with('NOOP')
+        mockConsume.assert_called_with(sentinel.tag, 'NOOP')
+        self.assertEquals(result, sentinel.out)
+
+    def test_consume_until_tagged_response(self):
+        client = self.client
+        client._imap.tagged_commands = {sentinel.tag: None}
 
         counter = itertools.count()
         def fake_get_response():
             count = counter.next()
             if count == 0:
                 return '* 99 EXISTS'
-            self.client._imap.tagged_commands[sentinel.tag] = ('OK', ['Idle done'])
-        self.client._imap._get_response = fake_get_response
+            client._imap.tagged_commands[sentinel.tag] = ('OK', ['Idle done'])
+        client._imap._get_response = fake_get_response
             
-        text, responses = self.client.idle_done()
-
-        self.assertEqual(self.client._imap.tagged_commands, {})
+        text, responses = client._consume_until_tagged_response(sentinel.tag,
+                                                                'IDLE')
+        self.assertEqual(client._imap.tagged_commands, {})
         self.assertEqual(text, 'Idle done')
         self.assertListEqual([(99, 'EXISTS')], responses)
 
 
+                         
 class TestDebugLogging(IMAPClientTest):
 
     def test_default_is_stderr(self):
index 43ce3cdea1d8c751e1a795278e4e5e3c50e4d3bc..852a1f72870077e87882a6c33fde2d7875a4d52c 100755 (executable)
@@ -506,6 +506,26 @@ def createLiveTestClass(conf, use_uid):
             self.assertTrue(isinstance(text, str))
             self.assertGreater(len(text), 0)
 
+        def test_noop(self):
+            self.client.select_folder('INBOX')
+
+            # Initially there should be no responses
+            text, resps = self.client.noop()
+            self.assertTrue(isinstance(text, str))
+            self.assertGreater(len(text), 0)
+            self.assertEquals(resps, [])
+
+            # Start a new connection and upload a new message
+            client2 = create_client_from_config(conf)
+            client2.select_folder('INBOX')
+            client2.append('INBOX', SIMPLE_MESSAGE)
+
+            # Check for this addition in the NOOP data
+            msg, resps = self.client.noop()
+            self.assertTrue(isinstance(text, str))
+            self.assertGreater(len(text), 0)
+            self.assertTrue(isinstance(resps, list))
+            self.assertIn((1, 'EXISTS'), resps)
 
     return LiveTest