]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Allow for handling of FETCH parts with sections, like "BODY[HEADER.FIELDS
authormsmits <none@none>
Mon, 15 Oct 2007 18:55:48 +0000 (18:55 +0000)
committermsmits <none@none>
Mon, 15 Oct 2007 18:55:48 +0000 (18:55 +0000)
(FROM)]". Thanks to Brian Jackson (iggy _AT_ theiggy DOT com) for the bug
report.

imapclient/imapclient.py
imapclient/test_FetchParser.py

index 6f19927266a8047d637ba9e0ea9dbddac456b920..8b15e4329f1d6102fb02819259459393a8d199f1 100644 (file)
@@ -455,9 +455,9 @@ class FetchTokeniser(object):
     PAREN_LIST = '(?:\(.*?\))'
 
     PAIR_RE = re.compile((
-        '([\w\.]+)\s+' +        # name
-        '((?:\d+)' +            # bare integer 
-        '|(?:{\d+?})' +         # IMAP literal
+        '([\w\.]+(?:\[[^\]]+\]+)?)\s+' +    # name (matches "FOO", "FOO.BAR" & "BODY[SECTION STUFF]")
+        '((?:\d+)' +                        # bare integer 
+        '|(?:{\d+?})' +                     # IMAP literal
         '|' + QUOTED_STRING +
         '|' + PAREN_LIST +
         ')\s*'))
@@ -477,7 +477,7 @@ class FetchTokeniser(object):
         out = []
         for m in strict_finditer(self.PAIR_RE, s):
             name, data = m.groups()
-            out.append((name, self._convert(data)))
+            out.append((name, self.nativefy(data)))
         return out
 
     def process_list(self, s):
@@ -490,10 +490,10 @@ class FetchTokeniser(object):
             return []
         out = []
         for m in strict_finditer(self.DATA_RE, s):
-            out.append(self._convert(m.group(1)))
+            out.append(self.nativefy(m.group(1)))
         return out
 
-    def _convert(self, s):
+    def nativefy(self, s):
         if s.startswith('"'):
             return s[1:-1]      # Debracket
         elif s.startswith('{'):
index e802490ecc71618acebac4c7e160037a9e275b0e..8de1ea0a8f41dcaad2592061a84d26ba72d61041 100644 (file)
@@ -178,6 +178,22 @@ class TestFetchParser(unittest.TestCase):
                 }
             )
 
+    def testLiteralsWithSections(self):
+        self._parse_test(
+            [('1 (BODY[TEXT] {11}', 'Hi there.\r\n'), ')'],
+            { 1: {'BODY[TEXT]': 'Hi there.\r\n',} }
+            )
+
+    def testLiteralsWithSectionsAndOtherParts(self):
+        self._parse_test(
+            [('1 (FLAGS (\\Seen) UID 2 BODY[HEADER.FIELDS (FROM)] {21}',
+              'From: foo@foo.com\r\n'), ')'],
+            {2: { 'BODY[HEADER.FIELDS (FROM)]': 'From: foo@foo.com\r\n',
+                  'FLAGS': ['\\Seen'],
+                   }
+                }
+            )
+
     def testUID(self):
         '''Test UID handling. The UID is returned instead of the given message
         ID if present.