]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Commited Helder's patchs for LSUB, STATUS, capabilites etc with fixes and enhancements.
authormenno <none@none>
Tue, 4 Dec 2007 23:13:53 +0000 (23:13 +0000)
committermenno <none@none>
Tue, 4 Dec 2007 23:13:53 +0000 (23:13 +0000)
IMAPClient/AUTHORS
IMAPClient/THANKS
IMAPClient/imapclient/imapclient.py

index 600961fa3b7b0e76563fe81e3cfd58f79b84f584..2cee8336c482d87e6e8183814b8c9b474eced480 100644 (file)
@@ -2,3 +2,6 @@ IMAPClient was created and is maintained by Menno Smits <menno@freshfoo.com>.
 
 The project was started while the author was working at NetBox Blue
 (http://netboxblue.com/).
+
+Patches received with many thanks from:
+    Helder Guerreiro (helder _AT_ paxjulia DOT com) 
index 1f4feab869d69361addd350d2836f523ad5fe205..99e6275745d921952caa29d1fd7fe9a24742cd57 100644 (file)
@@ -1,3 +1,8 @@
 Thanks go to the following people for their help with this project.
 
-Brian Jackson (iggy _AT_ theiggy DOT com) - bug report.
+Helder Guerreiro (helder _AT_ paxjulia DOT com) 
+    - patches for capability checks, logout command, status command, lsub
+      command
+
+Brian Jackson (iggy _AT_ theiggy DOT com) 
+    - bug report
index 5ad2dbbe06c4849532d5b85e12f43b00a57ac667..0e96a5cb68d6f79762bee421749936dba9e72876 100644 (file)
@@ -64,6 +64,8 @@ class IMAPClient:
 
     re_sep = re.compile('^\(\("[^"]*" "([^"]+)"\)\)')
     re_folder = re.compile('\([^)]*\) "[^"]+" "([^"]+)"')
+    re_status = re.compile(r'^\s*(?P<folder>[ \w%&+."\-]+)\s+'
+                           r'\((?P<status_items>.*)\)$')
 
     def __init__(self, host, port=143, use_uid=True):
         '''Initialise object instance and connect to the remote IMAP server.
@@ -82,6 +84,33 @@ class IMAPClient:
         self._checkok('login', typ, data)
         return data[0]
 
+    def logout(self):
+        '''Perform a logout
+        '''
+        typ, data = self._imap.logout()
+        self._checkbye('logout', typ, data)
+        return data[0]
+
+    def capabilities(self):
+        '''Returns the server capability list
+        '''
+        return self._imap.capabilities
+
+    def has_capability(self, capability):
+        '''Checks if the server has the given capability.
+
+        @param capability: capability to test (eg 'SORT')
+        '''
+        # FIXME: this will not detect capabilities that are backwards
+        # compatible with the current level. For instance the SORT
+        # capabilities may in the future be named SORT2 which is
+        # still compatible with the current standard and will not
+        # be detected by this method.
+        if capability.upper() in self._imap.capabilities:
+            return True
+        else:
+            return False
+
     def get_folder_delimiter(self):
         '''Determine the folder separator used by the IMAP server.
 
@@ -119,6 +148,28 @@ class IMAPClient:
 
         return folders
 
+    def list_sub_folders(self, directory="", pattern="*"):
+        '''Get a listing of subscribed folders on the server.
+
+        The default behaviour (no args) will list all subscribed folders for the
+        logged in user.
+
+        @param directory: The base directory to look for folders from.
+        @param pattern: A pattern to match against folder names. Only folder
+            names matching this pattern will be returned. Wildcards accepted.
+        @return: A list of folder names.
+        '''
+        typ, data = self._imap.lsub(directory, pattern)
+        self._checkok('lsub', typ, data)
+
+        folders = []
+        for line in data:
+            m = self.re_folder.match(line)
+            if m:
+                folders.append(m.group(1))
+
+        return folders
+
     def select_folder(self, folder):
         '''Select the current folder on the server. Future calls to methods
         such as search and fetch will act on the selected folder.
@@ -131,6 +182,31 @@ class IMAPClient:
         self._checkok('select', typ, data)
         return long(data[0])
 
+    def folder_status(self, folder, what=None):
+        '''Requests the status from folder.
+
+        @param folder: The folder name.
+        @param what: A sequence of status items to query. Defaults to
+            ('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN').
+        @return: Dictionary of the status items for the folder. The keys match
+            the items specified in the what parameter.
+        @rtype: dict
+        '''
+        if what is None:
+            what = ('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN')
+        elif isinstance(what, basestring):
+            what = (what,)
+        what_ = '(%s)' % (' '.join(what))
+
+        typ, data = self._imap.status(folder, what_)
+        self._checkok('status', typ, data)
+
+        match = self.re_status.match(data[0])
+        if not match:
+            raise self.Error('Could not get the folder status')
+        items = iter(match.group('status_items').strip().split())
+        return dict(zip(items, items))
+
     def close_folder(self):
         '''Close the currently selected folder.
 
@@ -191,6 +267,34 @@ class IMAPClient:
 
         return [ long(i) for i in data[0].split() ]
 
+    def sort(self, sort_criteria, criteria='ALL', charset='UTF-8' ):
+        '''Returns a list of messages sorted by sort_criteria.
+
+        Note that this is an extension to the IMAP4:
+        http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-19.txt
+        '''
+        if not criteria:
+            raise ValueError('no criteria specified')
+
+        if not self.has_capability('SORT'):
+            raise self.Error('The server does not support the SORT extension')
+
+        if isinstance(criteria, basestring):
+            criteria = (criteria,)
+        crit_list = ['(%s)' % c for c in criteria]
+
+        sort_criteria = seq_to_parenlist([ s.upper() for s in sort_criteria])
+
+        if self.use_uid:
+            typ, data = self._imap.uid('SORT', sort_criteria, charset,
+                *crit_list)
+        else:
+            typ, data = self._imap.sort(sort_criteria, charset, *crit_list)
+
+        self._checkok('sort', typ, data)
+
+        return [ long(i) for i in data[0].split() ]
+
     def get_flags(self, messages):
         '''Return the flags set for messages
 
@@ -319,14 +423,20 @@ class IMAPClient:
         self._checkok('setacl', typ, data)
         return data[0]
 
-    def _checkok(self, command, typ, data):
+    def _check_resp(self, expected, command, typ, data):
         '''Check command responses for errors.
 
-        @raise: IMAPClient.Error if a command failed.
+        @raise: Error if a command failed.
         '''
-        if typ != 'OK':
+        if typ != expected:
             raise self.Error('%s failed: %r' % (command, data[0]))
 
+    def _checkok(self, command, typ, data):
+        self._check_resp('OK', command, typ, data)
+
+    def _checkbye(self, command, typ, data):
+        self._check_resp('BYE', command, typ, data)
+
     def _store(self, cmd, messages, flags):
         '''Worker functions for flag manipulation functions
 
@@ -356,6 +466,7 @@ class IMAPClient:
             for msgid, data in fetch_dict.iteritems()
             ])
 
+
 class FetchParser(object):
     '''
     Parse an IMAP FETCH response and convert the return values to useful Python
@@ -453,7 +564,7 @@ class FetchTokeniser(object):
 
     PAIR_RE = re.compile((
         '([\w\.]+(?:\[[^\]]+\]+)?)\s+' +    # name (matches "FOO", "FOO.BAR" & "BODY[SECTION STUFF]")
-        '((?:\d+)' +                        # bare integer 
+        '((?:\d+)' +                        # bare integer
         '|(?:{\d+?})' +                     # IMAP literal
         '|' + QUOTED_STRING +
         '|' + PAREN_LIST +