From bf1d94076ddf79df7787f89ff373666d6fbcf30f Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Fri, 14 Sep 2012 08:25:59 -0400
Subject: [PATCH] Use IMAP BINARY (RFC3516) extension to fetch message/part bodies (experimental)

---
 CHANGELOG                              |    1 +
 program/include/rcube_imap_generic.php |   15 +++++++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 36e80f3..babd053 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Use IMAP BINARY (RFC3516) extension to fetch message/part bodies
 - Fix invalid error message on deleting mail from read only folder (#1488694)
 - Fix error where session wasn't updated after folder rename/delete (#1488692)
 - Replace data URIs of images (pasted in HTML editor) with inline attachments (#1488502)
diff --git a/program/include/rcube_imap_generic.php b/program/include/rcube_imap_generic.php
index 25e6fc4..cce53ae 100644
--- a/program/include/rcube_imap_generic.php
+++ b/program/include/rcube_imap_generic.php
@@ -2402,15 +2402,22 @@
             $mode = 0;
         }
 
+        // Use BINARY extension when possible (and safe)
+        $binary     = $mode && preg_match('/^[0-9.]+$/', $part) && $this->hasCapability('BINARY');
+        $fetch_mode = $binary ? 'BINARY' : 'BODY';
+
         // format request
-        $reply_key = '* ' . $id;
         $key       = $this->nextTag();
-        $request   = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])";
+        $request   = $key . ($is_uid ? ' UID' : '') . " FETCH $id ($fetch_mode.PEEK[$part])";
 
         // send request
         if (!$this->putLine($request)) {
             $this->setError(self::ERROR_COMMAND, "Unable to send command: $request");
             return false;
+        }
+
+        if ($binary) {
+            $mode = -1;
         }
 
         // receive reply line
@@ -2457,13 +2464,13 @@
             $prev     = '';
 
             while ($bytes > 0) {
-                $line = $this->readLine(4096);
+                $line = $this->readLine(8192);
 
                 if ($line === NULL) {
                     break;
                 }
 
-                $len  = strlen($line);
+                $len = strlen($line);
 
                 if ($len > $bytes) {
                     $line = substr($line, 0, $bytes);

--
Gitblit v1.9.1