From 281ca0ea2a368a5ecd62344bffaa4b4a5986db9e Mon Sep 17 00:00:00 2001
From: Bartlomiej Nogas <bart.nogas@gmail.com>
Date: Fri, 24 Oct 2014 07:01:54 -0400
Subject: [PATCH] Fix of really fix UNKNOWN-CTE handling

---
 program/lib/Roundcube/rcube_imap_generic.php |   30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/program/lib/Roundcube/rcube_imap_generic.php b/program/lib/Roundcube/rcube_imap_generic.php
index 298d44c..efdf087 100644
--- a/program/lib/Roundcube/rcube_imap_generic.php
+++ b/program/lib/Roundcube/rcube_imap_generic.php
@@ -865,7 +865,7 @@
 
         if (!$this->fp) {
             $this->setError(self::ERROR_BAD, sprintf("Could not connect to %s:%d: %s",
-                $host, $this->prefs['port'], $errstr ?: "Unknown reason"));
+                $host, $this->prefs['port'], $errstr ? $errstr : "Unknown reason"));
 
             return false;
         }
@@ -2530,7 +2530,7 @@
             return false;
         }
 
-        $initiated = false;
+        $binary    = true;
 
         do {
             if (!$initiated) {
@@ -2552,16 +2552,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)) {
@@ -2577,14 +2577,14 @@
 
             $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)
@@ -2692,7 +2692,7 @@
                     }
                 }
             }
-        } while (!$this->startsWith($line, $key, true));
+        } while (!$this->startsWith($line, $key, true) || !$initiated);
 
         if ($result !== false) {
             if ($file) {

--
Gitblit v1.9.1