]> khamidou — GitHub mirrors - imapclient.git/commitdiff
#42: Folder names with double quotes around them have the double quotes stripped
authorMenno Smits <menno@freshfoo.com>
Thu, 29 Apr 2010 21:10:51 +0000 (22:10 +0100)
committerMenno Smits <menno@freshfoo.com>
Thu, 29 Apr 2010 21:10:51 +0000 (22:10 +0100)
Work around an imaplib misfeature which prevents quoting when folder names
start and end with double quotes.

imapclient/imapclient.py
imapclient/test/test_IMAPClient.py
livetest.py

index 7fa048e238b0c09f7e34a11146d7d0f3eba96616..23ba467b5114b819e4d863b82c6dba397a93bddb 100644 (file)
@@ -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
index c1546b643e811e905f57a4b9d50b1445e7d9ca36..9ef2e5271677e82a727655374d426498923ddfc1 100644 (file)
@@ -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),
index 3d2d87c5e2c9a728699f2e1cbb88e35618407401..deb53a4c51df726dd5024c08c097ba7496010cad 100755 (executable)
@@ -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()