From d1d5b8ca1bebafe05d70b5cfb7e91895a4c0a38a Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Thu, 29 Apr 2010 22:10:51 +0100 Subject: [PATCH] #42: Folder names with double quotes around them have the double quotes stripped Work around an imaplib misfeature which prevents quoting when folder names start and end with double quotes. --- imapclient/imapclient.py | 18 ++++++++++++++++-- imapclient/test/test_IMAPClient.py | 6 +++--- livetest.py | 4 +++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 7fa048e..23ba467 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -328,6 +328,7 @@ class IMAPClient(object): """ typ, data = self._imap.list('', self._encode_folder_name(folder)) self._checkok('list', typ, data) + data = [x for x in data if x] return len(data) == 1 and data[0] != None @@ -628,8 +629,17 @@ class IMAPClient(object): def _encode_folder_name(self, name): if self.folder_encode: - return imap_utf7.encode(name) - return name + name = imap_utf7.encode(name) + # imaplib assumes that if a command argument (in this case a + # folder name) has double quotes around it, then it doesn't + # need quoting. This "feature" prevents creation of folders + # with names that start and end with double quotes. + # + # To work around this IMAPClient performs the quoting + # itself. This adds start and end double quotes which also + # serves to fool IMAP4._checkquote into not attempting further + # quoting. A hack but it works. + return _quote_arg(name) def messages_to_str(messages): @@ -670,3 +680,7 @@ def datetime_to_imap(dt): return dt.strftime("%d-%b-%Y %H:%M:%S %z") +def _quote_arg(arg): + arg = arg.replace('\\', '\\\\') + arg = arg.replace('"', '\\"') + return '"%s"' % arg diff --git a/imapclient/test/test_IMAPClient.py b/imapclient/test/test_IMAPClient.py index c1546b6..9ef2e52 100644 --- a/imapclient/test/test_IMAPClient.py +++ b/imapclient/test/test_IMAPClient.py @@ -116,9 +116,9 @@ class TestAppend(IMAPClientTest): self.client._imap.append.return_value = ('OK', ['Good']) self.client.append('foobar', sentinel.msg, ['FLAG', 'WAVE'], None) - + self.assert_(self.client._imap.method_calls == - [('append', ('foobar', + [('append', ('"foobar"', '(FLAG WAVE)', None, sentinel.msg), @@ -135,7 +135,7 @@ class TestAppend(IMAPClientTest): self.assert_(datetime_to_imap.called) self.assert_(self.client._imap.method_calls == - [('append', ('foobar', + [('append', ('"foobar"', '(FLAG WAVE)', '"somedate"', sentinel.msg), diff --git a/livetest.py b/livetest.py index 3d2d87c..deb53a4 100755 --- a/livetest.py +++ b/livetest.py @@ -143,6 +143,8 @@ def test_folders(client): assert not client.folder_exists('this is very unlikely to exist') test_folders = ['foobar', + '"foobar"', + 'foo "bar"', 'stuff & things', u'test & \u2622'] @@ -152,7 +154,7 @@ def test_folders(client): client.create_folder(folder) assert client.folder_exists(folder) - assert folder in extract_normal_folders(client.list_folders()) + assert folder in extract_normal_folders(client.list_folders()), repr(folder) client.select_folder(folder) client.close_folder() -- 2.43.0