From bf9c9b3e38f984b5cca82d869252900963466b98 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 23 Oct 2014 12:55:05 -0400
Subject: [PATCH] Really fix UNKNOWN-CTE handling

---
 program/lib/Roundcube/rcube_imap_generic.php |   25 +++++++++++++------------
 1 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/program/lib/Roundcube/rcube_imap_generic.php b/program/lib/Roundcube/rcube_imap_generic.php
index a3a76ab..63d4f06 100644
--- a/program/lib/Roundcube/rcube_imap_generic.php
+++ b/program/lib/Roundcube/rcube_imap_generic.php
@@ -2570,6 +2570,7 @@
         }
 
         $initiated = false;
+        $binary    = true;
 
         do {
             if (!$initiated) {
@@ -2591,16 +2592,16 @@
                 }
 
                 // Use BINARY extension when possible (and safe)
-                $binary     = !$binary_err && $mode && preg_match('/^[0-9.]+$/', $part) && $this->hasCapability('BINARY');
+                $binary     = $binary && $mode && preg_match('/^[0-9.]+$/', $part) && $this->hasCapability('BINARY');
                 $fetch_mode = $binary ? 'BINARY' : 'BODY';
                 $partial    = $max_bytes ? sprintf('<0.%d>', $max_bytes) : '';
 
                 // format request
-                $key        = $this->nextTag();
-                $request    = $key . ($is_uid ? ' UID' : '') . " FETCH $id ($fetch_mode.PEEK[$part]$partial)";
-                $result     = false;
-                $found      = false;
-                $initiated  = true;
+                $key       = $this->nextTag();
+                $request   = $key . ($is_uid ? ' UID' : '') . " FETCH $id ($fetch_mode.PEEK[$part]$partial)";
+                $result    = false;
+                $found     = false;
+                $initiated = true;
 
                 // send request
                 if (!$this->putLine($request)) {
@@ -2616,16 +2617,16 @@
 
             $line = trim($this->readLine(1024));
 
-            // handle UNKNOWN-CTE response - RFC 3516, try standard BODY request instead of BINARY
-            if ($binary && preg_match('/^' . $key . ' NO \[UNKNOWN-CTE\]/i', $line)) {
-                $initiated = false;
-                continue;
-            }
-
             if (!$line) {
                 break;
             }
 
+            // handle UNKNOWN-CTE response - RFC 3516, try again with standard BODY request
+            if ($binary && !$found && preg_match('/^' . $key . ' NO \[UNKNOWN-CTE\]/i', $line)) {
+                $binary = $initiated = false;
+                continue;
+            }
+
             // skip irrelevant untagged responses (we have a result already)
             if ($found || !preg_match('/^\* ([0-9]+) FETCH (.*)$/', $line, $m)) {
                 continue;

--
Gitblit v1.9.1