]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Make a MODSEQ returned as parts of a SEARCH response available to the caller
authorMenno Smits <menno@freshfoo.com>
Fri, 2 Jan 2015 12:16:15 +0000 (01:16 +1300)
committerMenno Smits <menno@freshfoo.com>
Fri, 2 Jan 2015 12:16:15 +0000 (01:16 +1300)
NEWS.rst
imapclient/imapclient.py
imapclient/livetest.py
imapclient/response_types.py
imapclient/test/test_search.py

index 9aed907b5a834f05151278548d62768e8eee1d6e..0c29b03e6b133da778be03573548c81447fe97fc 100644 (file)
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -33,6 +33,13 @@ version of Python being used).
 
 Thanks to Chris Arndt for this change.
 
+MODSEQ parts in SEARCH responses are now handled
+------------------------------------------------
+If the CONDSTORE extension is supported by a server and a MODSEQ
+criteria was used with search(), a TypeError could occur. This has now
+been fixed and the MODSEQ value returned by the server is now
+available via an attribute on the returned list of ids.
+
 Minor Changes
 -------------
 The deprecated get_folder_delimiter() method has been removed.
index 5e56dd1dd1e5382c160b9fcfa96b16903882be07..63cb46ac209a0a5b7899c00743e8d6d08aa476f0 100644 (file)
@@ -12,16 +12,16 @@ import re
 from datetime import datetime
 from operator import itemgetter
 
-from . import response_lexer
-
 # Confusingly, this module is for OAUTH v1, not v2
 try:
     import oauth2 as oauth_module
 except ImportError:
     oauth_module = None
 
+from . import response_lexer
 from .imap_utf7 import encode as encode_utf7, decode as decode_utf7
 from .fixed_offset import FixedOffset
+from .response_types import SearchIds
 from .six import moves, iteritems, text_type, integer_types, PY3, binary_type, string_types
 xrange = moves.xrange
 
@@ -575,6 +575,11 @@ class IMAPClient(object):
         criteria. It defaults to US-ASCII.
 
         See :rfc:`3501#section-6.4.4` for more details.
+
+        The returned list of message ids will have its *modseq*
+        attribute set if the server appended a MODSEQ value to the
+        search response (i.e. if a MODSEQ criteria was included in the
+        search).
         """
         return self._search(normalise_search_criteria(criteria), charset)
 
@@ -607,10 +612,14 @@ class IMAPClient(object):
             typ, data = self._imap.search(charset, *criteria)
 
         self._checkok('search', typ, data)
-        return [
-            item for item in parse_response(data)
-            if isinstance(item, int)
-        ]
+
+        ids = SearchIds()
+        for item in parse_response(data):
+            if isinstance(item, int):
+                ids.append(item)
+            elif isinstance(item, tuple) and len(item) == 2 and item[0].lower() == b'modseq':
+                ids.modseq = item[1]
+        return ids
 
     def thread(self, algorithm='REFERENCES', criteria='ALL', charset='UTF-8'):
         """Return a list of messages threads matching *criteria*.
index f5d8b65752a785e3d92fd87733919a20b91c4037..4797d619e1ae250df4aa6da8cd9370ec79d660da 100644 (file)
@@ -574,6 +574,35 @@ def createUidTestClass(conf, use_uid):
             self.assertEqual(len(self.client.search(['NOT DELETED', 'SUBJECT "a"'])), 1)
             self.assertEqual(len(self.client.search(['NOT DELETED', 'SUBJECT "c"'])), 0)
 
+        def test_search_with_modseq(self):
+            # CONDSTORE (RFC 4551) means that the server supports the
+            # MODSEQ search criteria and response.
+            if not self.client.has_capability('CONDSTORE'):
+                return self.skipTest("Server doesn't support CONDSTORE")
+
+            if self.is_gmail():
+                return self.skipTest("Gmail doesn't seem to return MODSEQ parts in SEARCH responses")
+
+            # A little dance to ensure MODSEQ tracking is turned on.
+            # TODO: use ENABLE for this instead
+            self.client.select_folder(self.base_folder)
+            self.append_msg(SIMPLE_MESSAGE)
+            msg_id = self.client.search()[0]
+            self.client.fetch(msg_id, ["MODSEQ"])
+            self.client.close_folder()
+            self.clear_folder(self.base_folder)
+
+            # Remember the initial MODSEQ
+            initial_modseq = self.client.select_folder(self.base_folder)[b'HIGHESTMODSEQ']
+
+            # Add a message so that the MODSEQ increases
+            self.append_msg(SIMPLE_MESSAGE)
+
+            # Ensure the message is seen and the new MODSEQ value is returned
+            ids = self.client.search(['MODSEQ %d' % initial_modseq])
+            self.assertEqual(len(ids), 1)
+            self.assertGreater(ids.modseq, initial_modseq)
+
         def test_gmail_search(self):
             self.skip_unless_capable('X-GM-EXT-1', 'Gmail search')
 
index fb00552bd76b9fed8e01e81dc5fd142fdcd5d043..78365da96148e35311b4f71e8e2e44e275f4f464 100644 (file)
@@ -49,3 +49,16 @@ class Address(namedtuple("Address", "name route mailbox host")):
 
     def __str__(self):
         return formataddr((self.name, self.mailbox + '@' + self.host))
+
+class SearchIds(list):
+    """
+    Contains a list of message ids as returned by IMAPClient.search().
+
+    The *modseq* attribute will contain the MODSEQ value returned by
+    the server (only if the SEARCH command sent involved the MODSEQ
+    criteria). See :rfc:`4551` for more details.
+    """
+
+    def __init__(self, *args):
+        list.__init__(self, *args)
+        self.modseq = None
index b805d17465060920911d5d8cf7b31982c595b4aa..362a94435f798b675aa6095ea3bd4b7dd5b2cb75 100644 (file)
@@ -14,6 +14,7 @@ class TestSearch(IMAPClientTest):
 
         self.client._imap.uid.assert_called_once_with('SEARCH', '(FOO)')
         self.assertEqual(result, [1, 2, 44])
+        self.assertEqual(result.modseq, None)
 
     def test_without_uid(self):
         self.client.use_uid = False
@@ -23,6 +24,7 @@ class TestSearch(IMAPClientTest):
 
         self.client._imap.search.assert_called_once_with(None, '(FOO)')
         self.assertEqual(result, [1, 2, 44])
+        self.assertEqual(result.modseq, None)
 
     def test_with_uid_with_charset(self):
         self.client.use_uid = True
@@ -61,6 +63,7 @@ class TestSearch(IMAPClientTest):
 
         self.client._imap.uid.assert_called_once_with('SEARCH', '(MODSEQ 40000)')
         self.assertEqual(result, [1, 2])
+        self.assertEqual(result.modseq, 51101)
 
     def test_error_from_server(self):
         self.client._imap.uid.return_value = ('NO', [b'bad karma'])