From 13c22ee955c59a207ead7594dd0020511affe775 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Sat, 3 Jan 2015 01:16:15 +1300 Subject: [PATCH] Make a MODSEQ returned as parts of a SEARCH response available to the caller --- NEWS.rst | 7 +++++++ imapclient/imapclient.py | 21 +++++++++++++++------ imapclient/livetest.py | 29 +++++++++++++++++++++++++++++ imapclient/response_types.py | 13 +++++++++++++ imapclient/test/test_search.py | 3 +++ 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 9aed907..0c29b03 100644 --- 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. diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 5e56dd1..63cb46a 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -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*. diff --git a/imapclient/livetest.py b/imapclient/livetest.py index f5d8b65..4797d61 100644 --- a/imapclient/livetest.py +++ b/imapclient/livetest.py @@ -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') diff --git a/imapclient/response_types.py b/imapclient/response_types.py index fb00552..78365da 100644 --- a/imapclient/response_types.py +++ b/imapclient/response_types.py @@ -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 diff --git a/imapclient/test/test_search.py b/imapclient/test/test_search.py index b805d17..362a944 100644 --- a/imapclient/test/test_search.py +++ b/imapclient/test/test_search.py @@ -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']) -- 2.43.0