When using Courier IMAP, the response from a search that matched a message
looks like:
* SEARCH 50
A006 OK SEARCH done.
But from a search that didn't match anything has an extra line:
* SEARCH
* 1 FETCH (FLAGS (\Deleted))
A006 OK SEARCH done.
This is odd, but appears to be permitted by the IMAP spec.
The SquirrelMail code is assuming it always returns exactly two lines and
looking for the response in the second. This causes strange problems (in
particular, it doesn't read the last line of the response, causing the
next IMAP command to read the result of this previous one, and so on).
The following patch against the latest development snapshot makes it read
as many untagged lines as necessary, remembering the "* SEARCH" one and
ignoring any others, until we see the final response.
--- filters.php.orig 2007-07-20 10:13:05.000000000 -0700
+++ filters.php 2009-01-02 14:53:15.000000000 -0800
@@ -368,10 +368,18 @@
#echo "SS $what
";
$read2 = sqimap_fgets($imap_stream);
#echo "RR2 $read2
";
- $read[]=$read2;
- $read3 = sqimap_fgets($imap_stream);
- #echo "RR3 $read3
";
- list($rtag,$response,$message)=explode(' ',$read3,3);
+
+ # The server may return more untagged results than just "* SEARCH",
+ # so keep reading all of them until we get to the actual response
+ while (preg_match("/^\*/", $read2)) {
+ if (preg_match("/^\* SEARCH/", $read2)) {
+ $read[]=$read2;
+ }
+ $read2 = sqimap_fgets($imap_stream);
+ #echo "RR2 $read2
";
+ }
+
+ list($rtag,$response,$message)=explode(' ',$read2,3);
## $read2 = sqimap_retrieve_imap_response($imap_stream, $sid, true,
## $response, $message, $search_str, false, true, false);
#echo "RR2 $read2 / RESPONSE $response
";