From b8ae99a6b38838045257c7dac6f52c66a0f6ef39 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Fri, 28 Aug 2009 04:29:46 -0400
Subject: [PATCH] - Fix endless loop on error response for APPEND command (#1486060)

---
 CHANGELOG            |    1 +
 INSTALL              |    2 +-
 program/lib/imap.inc |   56 ++++++++++++++++++++++++++++++++++----------------------
 3 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 95ac432..4997ea9 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG RoundCube Webmail
 ===========================
 
+- Fix endless loop on error response for APPEND command (#1486060)
 - Don't require date.timezone setting in installer (#1485989)
 - Fix date sorting problem with Courier IMAP server (#1486065)
 - Unselect pressed buttons on mouse up (#1485987)
diff --git a/INSTALL b/INSTALL
index cf46faf..67fbce9 100644
--- a/INSTALL
+++ b/INSTALL
@@ -38,7 +38,7 @@
    - /logs
 3. Create a new database and a database user for RoundCube (see DATABASE SETUP)
 4. Point your browser to http://url-to-roundcube/installer/
-5. Follow the instructions of the install script (or see MANUAL CONFINGURATION)
+5. Follow the instructions of the install script (or see MANUAL CONFIGURATION)
 6. After creating and testing the configuration, remove the installer directory
 7. Done!
 
diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index ce778b2..48828c0 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -301,8 +301,8 @@
 }
 
 function iil_ParseResult($string) {
-	$a = explode(' ', $string);
-	if (count($a) > 2) {
+	$a = explode(' ', trim($string));
+	if (count($a) >= 2) {
 		if (strcasecmp($a[1], 'OK') == 0) {
 			return 0;
 		} else if (strcasecmp($a[1], 'NO') == 0) {
@@ -311,7 +311,7 @@
 			return -2;
 		} else if (strcasecmp($a[1], 'BYE') == 0) {
 			return -3;
-    		}
+		}
 	}
 	return -4;
 }
@@ -2125,8 +2125,8 @@
 	$query = 'sub1 SUBSCRIBE "' . iil_Escape($folder). '"';
 	iil_PutLine($fp, $query);
 
-	$line = trim(iil_ReadLine($fp, 10000));
-	return iil_ParseResult($line);
+	$line = trim(iil_ReadLine($fp, 512));
+	return (iil_ParseResult($line) == 0);
 }
 
 function iil_C_UnSubscribe(&$conn, $folder) {
@@ -2135,8 +2135,8 @@
 	$query = 'usub1 UNSUBSCRIBE "' . iil_Escape($folder) . '"';
 	iil_PutLine($fp, $query);
     
-	$line = trim(iil_ReadLine($fp, 10000));
-	return iil_ParseResult($line);
+	$line = trim(iil_ReadLine($fp, 512));
+	return (iil_ParseResult($line) == 0);
 }
 
 function iil_C_FetchMIMEHeaders(&$conn, $mailbox, $id, $parts) {
@@ -2371,31 +2371,39 @@
 
 function iil_C_Append(&$conn, $folder, &$message) {
 	if (!$folder) {
-    		return false;
+		return false;
 	}
 	$fp = $conn->fp;
 
 	$message = str_replace("\r", '', $message);
-	$message = str_replace("\n", "\r\n", $message);		
+	$message = str_replace("\n", "\r\n", $message);
 
 	$len = strlen($message);
 	if (!$len) {
-    		return false;
+		return false;
 	}
 
 	$request = 'a APPEND "' . iil_Escape($folder) .'" (\\Seen) {' . $len . '}';
-    
+
 	if (iil_PutLine($fp, $request)) {
-		$line = iil_ReadLine($fp, 100);		
-		$sent = iil_PutLine($fp, $message);
+		$line = iil_ReadLine($fp, 512);
+
+		$result = (iil_ParseResult($line) == 0);
+		if (!$result) {
+			$conn->error .= "Cannot write to folder: $line\n";
+			return false;
+		}
+
+		iil_PutLine($fp, $message);
+
 		do {
-			$line=iil_ReadLine($fp);
+			$line = iil_ReadLine($fp);
 		} while (!iil_StartsWith($line, 'a ', true));
 	
 		$result = (iil_ParseResult($line) == 0);
 		if (!$result) {
 		    $conn->error .= $line . "\n";
-    		}
+		}
 		return $result;
 	}
 
@@ -2409,7 +2417,7 @@
 	}
     
 	//open message file
-	$in_fp = false;				
+	$in_fp = false;
 	if (file_exists(realpath($path))) {
 		$in_fp = fopen($path, 'r');
 	}
@@ -2426,14 +2434,18 @@
     
 	//send APPEND command
 	$request    = 'a APPEND "' . iil_Escape($folder) . '" (\\Seen) {' . $len . '}';
-	$bytes_sent = 0;
 	if (iil_PutLine($fp, $request)) {
-		$line = iil_ReadLine($fp, 100);
-				
+		$line = iil_ReadLine($fp, 512);
+
+		$result = (iil_ParseResult($line) == 0);
+		if (!$result) {
+			$conn->error .= "Cannot write to folder: $line\n";
+			return false;
+		}
+
 		//send file
 		while (!feof($in_fp)) {
 			$buffer      = fgets($in_fp, 4096);
-			$bytes_sent += strlen($buffer);
 			iil_PutLine($fp, $buffer, false);
 		}
 		fclose($in_fp);
@@ -2444,12 +2456,12 @@
 		do {
 			$line = iil_ReadLine($fp);
 		} while (!iil_StartsWith($line, 'a ', true));
-			
+
 		$result = (iil_ParseResult($line) == 0);
 		if (!$result) {
 		    $conn->error .= $line . "\n";
 		}
-        
+
 		return $result;
 	}
 	

--
Gitblit v1.9.1