]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Handle the same message appearing multiple times in a single FETCH response
authorMenno Smits <menno@freshfoo.com>
Wed, 29 May 2013 22:23:38 +0000 (23:23 +0100)
committerMenno Smits <menno@freshfoo.com>
Wed, 29 May 2013 22:23:38 +0000 (23:23 +0100)
This can occur when server sends unsolicited FETCH responses.

imapclient/response_parser.py
imapclient/test/test_response_parser.py

index cd2fdd7ce451b50cb591b30217c0ede973fa4aaa..0adfc0937bbab59e065a3f8c467d3ccf9a74f053 100644 (file)
@@ -14,6 +14,7 @@ Initially inspired by http://effbot.org/zone/simple-iterator-parser.htm
 from __future__ import unicode_literals
 
 import sys
+from collections import defaultdict
 from datetime import datetime
 
 from . import six
@@ -69,7 +70,7 @@ def parse_fetch_response(text, normalise_times=True, uid_is_key=True):
         return {}
     response = gen_parsed_response(text)
 
-    parsed_response = {}
+    parsed_response = defaultdict(dict)
     while True:
         try:
             msg_id = seq = _int_or_error(six.next(response),
@@ -107,7 +108,7 @@ def parse_fetch_response(text, normalise_times=True, uid_is_key=True):
             else:
                 msg_data[word] = value
 
-        parsed_response[msg_id] = msg_data
+        parsed_response[msg_id].update(msg_data)
 
     return parsed_response
 
index b3e4d4baf6819fd3c8b6dad8976490343ab37fec..84c249c9ac4bebf34690453f2c6b307bb9719868 100644 (file)
@@ -224,6 +224,13 @@ class TestParseFetchResponse(unittest.TestCase):
                             7: {'FLAGS': ('Baz', 'Sneeve'), 'SEQ': 7},
                          })
 
+    def test_same_message_appearing_multiple_times(self):
+        # This can occur when server sends unsolicited FETCH responses
+        # (e.g. RFC 4551)
+        self.assertEqual(parse_fetch_response(
+                                    ["2 (FLAGS (Foo Bar)) ",
+                                     "2 (MODSEQ 4)"]),
+                         {2: {'FLAGS': ('Foo', 'Bar'), 'SEQ': 2, 'MODSEQ': 4}})
 
     def test_literals(self):
         self.assertEqual(parse_fetch_response([('1 (RFC822.TEXT {4}', 'body'),