From bcab395a25f2a77b3271fd847c52f5d0c3595148 Mon Sep 17 00:00:00 2001 From: John Louis Del Rosario Date: Fri, 6 Sep 2013 17:37:57 +0800 Subject: [PATCH] Add gm_search() method which leverages Gmail's search syntax using X-GM-RAW attribute. --- imapclient/imapclient.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 785d073..d72000e 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -573,6 +573,43 @@ class IMAPClient(object): return [] return [long(i) for i in data.split()] + def gm_search(self, query, charset=None): + """Do a search using Gmail's X-GM-RAW attribute. + + *query* should be a valid Gmail search query string. Example values + include:: + + 'has:attachment in:unread' + + See https://developers.google.com/gmail/imap_extensions#extension_of_the_search_command_x-gm-raw + for more info. + + *charset* specifies the character set of the strings in the + criteria. It defaults to US-ASCII. + """ + # we use the literal feature to allow querying for non-ascii subjects + if charset: + query = query.encode(charset) + else: + query = query.encode('us-ascii') + self._imap.literal = query + if self.use_uid: + args = [] + if charset: + args.extend(['CHARSET', charset]) + args.append('X-GM-RAW') + typ, data = self._imap.uid('SEARCH', *args) + else: + typ, data = self._imap.search(charset, 'X-GM-RAW') + + data = from_bytes(data) + + self._checkok('search', typ, data) + data = data[0] + if data is None: # no untagged responses... + return [] + return [long(i) for i in data.split()] + def thread(self, algorithm='REFERENCES', criteria='ALL', charset='UTF-8'): """Return a list of messages threads matching *criteria*. -- 2.43.0