]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Add gm_search() method which leverages Gmail's search syntax using X-GM-RAW
authorJohn Louis Del Rosario <john2x@gmail.com>
Fri, 6 Sep 2013 09:37:57 +0000 (17:37 +0800)
committerJohn Louis Del Rosario <john2x@gmail.com>
Fri, 6 Sep 2013 09:37:57 +0000 (17:37 +0800)
attribute.

imapclient/imapclient.py

index 785d0736b16dab038b07d58187ea6ff41dbac1e5..d72000e2e0b1dfeb8b008892b00ff80e53fb8165 100644 (file)
@@ -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*.