<commit_before>Index: twisted/mail/imap4.py
===================================================================
--- twisted/mail/imap4.py	(revision 19773)
+++ twisted/mail/imap4.py	(working copy)
@@ -363,16 +363,11 @@
         for L in self.lines:
             names = parseNestedParens(L)
             N = len(names)
+            # This section is patched as described in http://twistedmatrix.com/trac/ticket/1105
             if (N >= 1 and names[0] in self._1_RESPONSES or
+                N >= 2 and names[1] in self._2_RESPONSES or
                 N >= 2 and names[0] == 'OK' and isinstance(names[1], types.ListType) and names[1][0] in self._OK_RESPONSES):
                 send.append(L)
-            elif N >= 3 and names[1] in self._2_RESPONSES:
-                if isinstance(names[2], list) and len(names[2]) >= 1 and names[2][0] == 'FLAGS' and 'FLAGS' not in self.args:
-                    unuse.append(L)
-                else:
-                    send.append(L)
-            elif N >= 2 and names[1] in self._2_RESPONSES:
-                send.append(L)
             else:
                 unuse.append(L)
         d, self.defer = self.defer, None
@@ -2247,8 +2242,8 @@
                     flags.setdefault(mId, []).extend(sum)
             else:
                 log.msg('Unhandled unsolicited response: ' + repr(L))
-        if flags:
-            self.flagsChanged(flags)
+        #if flags:
+        #    self.flagsChanged(flags)
         if recent is not None or exists is not None:
             self.newMessages(exists, recent)
 
@@ -3336,6 +3331,8 @@
                             if len(data) < 2:
                                 raise IllegalServerResponse("Not enough arguments", data)
                             flags.setdefault(id, {})[data[0]] = data[1]
+                            if data[0] == 'FLAGS':
+                                self.flagsChanged({id: data[1]})
                             del data[:2]
                 else:
                     print '(2)Ignoring ', parts
@@ -3431,7 +3428,16 @@
                     except ValueError:
                         raise IllegalServerResponse, line
                     else:
-                        info[id] = parseNestedParens(parts[2])
+                        data = parseNestedParens(parts[2])[0]
+                        # This section is patched as described in http://twistedmatrix.com/trac/ticket/1105
+                        # XXX this will fail if 'FLAGS' is a retrieved part
+                        for i in range(len(data) -1):
+                            if data[i] == 'FLAGS':
+                                self.flagsChanged({id: data[i+1]})
+                                del data[i:i+2]
+                                break
+                        if data:
+                            info.setdefault(id, []).append(data)
         return info
 
     def _fetch(self, messages, useUID=0, **terms):<commit_msg>Ignore 'BYE LOGOUT' response.<commit_after>Index: twisted/mail/imap4.py
===================================================================
--- twisted/mail/imap4.py	(revision 19773)
+++ twisted/mail/imap4.py	(working copy)
@@ -363,16 +363,11 @@
         for L in self.lines:
             names = parseNestedParens(L)
             N = len(names)
+            # This section is patched as described in http://twistedmatrix.com/trac/ticket/1105
             if (N >= 1 and names[0] in self._1_RESPONSES or
+                N >= 2 and names[1] in self._2_RESPONSES or
                 N >= 2 and names[0] == 'OK' and isinstance(names[1], types.ListType) and names[1][0] in self._OK_RESPONSES):
                 send.append(L)
-            elif N >= 3 and names[1] in self._2_RESPONSES:
-                if isinstance(names[2], list) and len(names[2]) >= 1 and names[2][0] == 'FLAGS' and 'FLAGS' not in self.args:
-                    unuse.append(L)
-                else:
-                    send.append(L)
-            elif N >= 2 and names[1] in self._2_RESPONSES:
-                send.append(L)
             else:
                 unuse.append(L)
         d, self.defer = self.defer, None
@@ -2245,10 +2240,12 @@
                     for f in fetched.get('FLAGS', []):
                         sum.append(f)
                     flags.setdefault(mId, []).extend(sum)
+            elif L.find('BYE LOGOUT') != -1:
+                pass
             else:
                 log.msg('Unhandled unsolicited response: ' + repr(L))
-        if flags:
-            self.flagsChanged(flags)
+        #if flags:
+        #    self.flagsChanged(flags)
         if recent is not None or exists is not None:
             self.newMessages(exists, recent)
 
@@ -3336,6 +3333,8 @@
                             if len(data) < 2:
                                 raise IllegalServerResponse("Not enough arguments", data)
                             flags.setdefault(id, {})[data[0]] = data[1]
+                            if data[0] == 'FLAGS':
+                                self.flagsChanged({id: data[1]})
                             del data[:2]
                 else:
                     print '(2)Ignoring ', parts
@@ -3431,7 +3430,16 @@
                     except ValueError:
                         raise IllegalServerResponse, line
                     else:
-                        info[id] = parseNestedParens(parts[2])
+                        data = parseNestedParens(parts[2])[0]
+                        # This section is patched as described in http://twistedmatrix.com/trac/ticket/1105
+                        # XXX this will fail if 'FLAGS' is a retrieved part
+                        for i in range(len(data) -1):
+                            if data[i] == 'FLAGS':
+                                self.flagsChanged({id: data[i+1]})
+                                del data[i:i+2]
+                                break
+                        if data:
+                            info.setdefault(id, []).append(data)
         return info
 
     def _fetch(self, messages, useUID=0, **terms):
{'fraction_non_alphanumeric': 0.13876469614174533, 'fraction_numerical': 0.03129657228017884, 'mean_word_length': 1.2957050551121247, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1551405', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1669, 'n_words': 406}