From a2e8cb3642a27cf3b151c60d2adfbdf7ac4a1dff Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Tue, 19 Oct 2010 15:32:56 -0400
Subject: [PATCH] - Simplify/improve LSUB/LIST/SELECT commands handling - Store UIDNEXT, UIDVALIDITY, UNSEEN values of selected folder

---
 program/include/rcube_imap.php         |    2 
 program/include/rcube_imap_generic.php |   83 ++++++++++++++---------------------------
 2 files changed, 29 insertions(+), 56 deletions(-)

diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index de54003..d94f4ae 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -449,7 +449,7 @@
     {
         $flag = strtoupper($flag);
         $imap_flag = $this->conn->flags[$flag];
-        return (in_array_nocase($imap_flag, $this->conn->permanentflags));
+        return (in_array_nocase($imap_flag, $this->conn->data['PERMANENTFLAGS']));
     }
 
     
diff --git a/program/include/rcube_imap_generic.php b/program/include/rcube_imap_generic.php
index 367c018..e7e858f 100644
--- a/program/include/rcube_imap_generic.php
+++ b/program/include/rcube_imap_generic.php
@@ -88,7 +88,7 @@
 	public $message;
 	public $rootdir;
 	public $delimiter;
-	public $permanentflags = array();
+    public $data = array();
     public $flags = array(
         'SEEN'     => '\\Seen',
         'DELETED'  => '\\Deleted',
@@ -101,8 +101,6 @@
         '*'        => '\\*',
     );
 
-	private $exists;
-	private $recent;
     private $selected;
 	private $fp;
 	private $host;
@@ -707,31 +705,27 @@
 	    if (empty($mailbox)) {
 		    return false;
 	    }
+
 	    if ($this->selected == $mailbox) {
 		    return true;
 	    }
 
-        $key = $this->next_tag();
-        $command = "$key SELECT " . $this->escape($mailbox);
+        list($code, $response) = $this->execute('SELECT', array($this->escape($mailbox)));
 
-	    if (!$this->putLine($command)) {
-            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $command");
-            return false;
-        }
+        if ($code == self::ERROR_OK) {
+            $response = explode("\r\n", $response);
+            foreach ($response as $line) {
+    			if (preg_match('/^\* ([0-9]+) (EXISTS|RECENT)$/i', $line, $m)) {
+	    		    $this->data[strtoupper($m[2])] = (int) $m[1];
+		    	}
+			    else if (preg_match('/^\* OK \[(UIDNEXT|UIDVALIDITY|UNSEEN) ([0-9]+)\]/i', $line, $match)) {
+			        $this->data[strtoupper($match[1])] = (int) $match[2];
+			    }
+			    else if (preg_match('/^\* OK \[PERMANENTFLAGS \(([^\)]+)\)\]/iU', $line, $match)) {
+			        $this->data['PERMANENTFLAGS'] = explode(' ', $match[1]);
+			    }
+            }
 
-		do {
-			$line = rtrim($this->readLine(512));
-
-			if (preg_match('/^\* ([0-9]+) (EXISTS|RECENT)$/', $line, $m)) {
-			    $token = strtolower($m[2]);
-			    $this->$token = (int) $m[1];
-			}
-			else if (preg_match('/\[?PERMANENTFLAGS\s+\(([^\)]+)\)\]/U', $line, $match)) {
-			    $this->permanentflags = explode(' ', $match[1]);
-			}
-		} while (!$this->startsWith($line, $key, true, true));
-
-        if ($this->parseResult($line, 'SELECT: ') == self::ERROR_OK) {
 		    $this->selected = $mailbox;
 			return true;
 		}
@@ -747,7 +741,7 @@
 
 	    $this->select($mailbox);
 	    if ($this->selected == $mailbox) {
-		    return $this->recent;
+		    return $this->data['RECENT'];
 	    }
 
 	    return false;
@@ -761,7 +755,7 @@
 
 	    $this->select($mailbox);
 	    if ($this->selected == $mailbox) {
-		    return $this->exists;
+		    return $this->data['EXISTS'];
 	    }
 
         return false;
@@ -781,7 +775,6 @@
 	        return false;
 	    }
 
-	    /*  Do "SELECT" command */
 	    if (!$this->select($mailbox)) {
 	        return false;
 	    }
@@ -1488,7 +1481,7 @@
 	    }
 
         // return empty result when folder is empty and we're just after SELECT
-        if ($old_sel != $folder && !$this->exists) {
+        if ($old_sel != $folder && !$this->data['EXISTS']) {
             return array(array(), array(), array());
 	    }
 
@@ -1524,7 +1517,7 @@
 	    }
 
         // return empty result when folder is empty and we're just after SELECT
-        if ($old_sel != $folder && !$this->exists) {
+        if ($old_sel != $folder && !$this->data['EXISTS']) {
             return array();
 	    }
 
@@ -1574,37 +1567,17 @@
 	        $ref = $this->rootdir;
 	    }
 
-        $command = $subscribed ? 'LSUB' : 'LIST';
-        $key     = $this->next_tag();
-        $query   = sprintf("%s %s %s %s", $key, $command,
-            $this->escape($ref), $this->escape($mailbox));
+        list($code, $response) = $this->execute($subscribed ? 'LSUB' : 'LIST',
+            array($this->escape($ref), $this->escape($mailbox)));
 
-    	// send command
-	    if (!$this->putLine($query)) {
-            $this->set_error(self::ERROR_COMMAND, "Unable to send command: $query");
-	        return false;
-	    }
-
-	    // get folder list
-	    do {
-		    $line = $this->readLine(500);
-		    $line = $this->multLine($line, true);
-		    $line = trim($line);
-
-		    if (preg_match('/^\* '.$command.' \(([^\)]*)\) "*([^"]+)"* (.*)$/', $line, $m)) {
+        if ($code == self::ERROR_OK) {
+            $folders = array();
+            while ($this->tokenizeResponse($response, 1) == '*') {
+                list (,$opts, $delim, $folder) = $this->tokenizeResponse($response, 4);
         		// folder name
-   			    $folders[] = preg_replace(array('/^"/', '/"$/'), '', $this->unEscape($m[3]));
-		        // attributes
-//        		$attrib = explode(' ', $this->unEscape($m[1]));
-		        // delimiter
-//        		$delim = $this->unEscape($m[2]);
+   			    $folders[] = $folder;
 		    }
-	    } while (!$this->startsWith($line, $key, true));
-
-	    if (is_array($folders)) {
-    	    return $folders;
-	    } else if ($this->parseResult($line, $command.': ') == self::ERROR_OK) {
-            return array();
+            return $folders;
         }
 
     	return false;

--
Gitblit v1.9.1