]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Handle square brackets in flags returned in SELECT response
authorMenno Smits <menno@freshfoo.com>
Tue, 13 Aug 2013 23:26:36 +0000 (00:26 +0100)
committerMenno Smits <menno@freshfoo.com>
Tue, 13 Aug 2013 23:26:36 +0000 (00:26 +0100)
Works around incorrect regex in imaplib as well as dealing with
potentially illegal characters being returned by some servers.

--HG--
branch : stable

imapclient/imapclient.py
imapclient/test/test_imapclient.py

index 785d0736b16dab038b07d58187ea6ff41dbac1e5..e402b1ed230298f0d1d0bbbbc593850a5244bdaf 100644 (file)
@@ -8,6 +8,7 @@ import imaplib
 import select
 import socket
 import sys
+import re
 import warnings
 from datetime import datetime
 from operator import itemgetter
@@ -359,16 +360,26 @@ class IMAPClient(object):
 
     def _process_select_response(self, resp):
         out = {}
+
+        # imaplib doesn't parse these correctly (broken regex) so replace
+        # with the raw values out of the OK section
+        for line in resp.get('OK', []):
+            match = re.match(r'\[(?P<key>[A-Z-]+)( \((?P<data>.*)\))?\]', line)
+            if match:
+                key = match.group('key')
+                if key == 'PERMANENTFLAGS':
+                    out[key] = tuple(match.group('data').split())
+
         for key, value in iteritems(resp):
             key = key.upper()
-            if key == 'OK':
-                continue
+            if key in ('OK', 'PERMANENTFLAGS'):
+                continue  # already handled above
             elif key in ('EXISTS', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'HIGHESTMODSEQ'):
                 value = int(value[0])
-            elif key in ('FLAGS', 'PERMANENTFLAGS'):
-                value = parse_response(value)[0]
             elif key == 'READ-WRITE':
                 value = True
+            elif key == 'FLAGS':
+                value = tuple(value[0][1:-1].split())
             out[key] = value
         return out
 
index 61e8f7cc400b5c8efc7925924e09ea005c16abdc..15662809fdced58d9167bcaf20266cd2189714d5 100644 (file)
@@ -155,16 +155,19 @@ class TestSelectFolder(IMAPClientTest):
     def test_normal(self):
         self.client._command_and_check = Mock()
         self.client._imap.untagged_responses = {
-            'OK': [],
-            'exists': ['1'],
-            'RECENT': ['2'],
-            'UIDNEXT': ['3'],
-            'UIDVALIDITY': ['4'],
-            'HIGHESTMODSEQ': ['5'],
-            'FLAGS': ['(ABC DEF)'],
-            'PERMANENTFLAGS': ['(XXX ZZZ)'],
-            'READ-WRITE': [''],
-            'OTHER': ['blah']
+            b'exists': [b'3'],
+            b'FLAGS': [br"(\Flagged \Deleted abc [foo]/bar def)"],
+            b'HIGHESTMODSEQ': [b'127110'],
+            b'OK': [br"[PERMANENTFLAGS (\Flagged \Deleted abc [foo]/bar def \*)] Flags permitted.",
+                    b'[UIDVALIDITY 631062293] UIDs valid.',
+                    b'[UIDNEXT 1281] Predicted next UID.',
+                    b'[HIGHESTMODSEQ 127110]'],
+            b'PERMANENTFLAGS': [br'(\Flagged \Deleted abc [foo'],
+            b'READ-WRITE': [b''],
+            b'RECENT': [b'0'],
+            b'UIDNEXT': [b'1281'],
+            b'UIDVALIDITY': [b'631062293'],
+            b'OTHER': [b'blah']
         }
 
         result = self.client.select_folder(b'folder_name', sentinel.readonly)
@@ -172,14 +175,15 @@ class TestSelectFolder(IMAPClientTest):
         self.client._command_and_check.assert_called_once_with('select',
                                                                '"folder_name"',
                                                                sentinel.readonly)
+        self.maxDiff = 99999
         self.assertEqual(result, {
-            'EXISTS': 1,
-            'RECENT': 2,
-            'UIDNEXT': 3,
-            'UIDVALIDITY': 4,
-            'HIGHESTMODSEQ': 5,
-            'FLAGS': ('ABC', 'DEF'),
-            'PERMANENTFLAGS': ('XXX', 'ZZZ'),
+            'EXISTS': 3,
+            'RECENT': 0,
+            'UIDNEXT': 1281,
+            'UIDVALIDITY': 631062293,
+            'HIGHESTMODSEQ': 127110,
+            'FLAGS': (r'\Flagged', r'\Deleted', 'abc', '[foo]/bar', 'def'),
+            'PERMANENTFLAGS': (r'\Flagged', r'\Deleted', 'abc', '[foo]/bar', 'def', r'\*'),
             'READ-WRITE': True,
             'OTHER': ['blah']
         })