From c149220debe48049df8a113053fa4424f0a62253 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Wed, 14 Aug 2013 00:26:36 +0100 Subject: [PATCH] Handle square brackets in flags returned in SELECT response 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 | 19 +++++++++++---- imapclient/test/test_imapclient.py | 38 +++++++++++++++++------------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 785d073..e402b1e 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -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[A-Z-]+)( \((?P.*)\))?\]', 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 diff --git a/imapclient/test/test_imapclient.py b/imapclient/test/test_imapclient.py index 61e8f7c..1566280 100644 --- a/imapclient/test/test_imapclient.py +++ b/imapclient/test/test_imapclient.py @@ -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'] }) -- 2.43.0