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.
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
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)
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*.
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')
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
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
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
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'])