Skip to content

Commit a086bcc

Browse files
committed
Ignore untagged lines after IDLE and DONE
1 parent 4fc5da6 commit a086bcc

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/Connection/ImapConnection.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,17 +335,17 @@ public function readResponse(Response $response, string $tag, bool $parse = true
335335
*/
336336
public function sendRequest(string $command, array $tokens = [], ?string &$tag = null): Response
337337
{
338-
$imapCommand = new ImapCommand($command, $tokens);
338+
$command = new ImapCommand($command, $tokens);
339339

340340
if (! $tag) {
341341
$this->sequence++;
342342

343343
$tag = 'TAG'.$this->sequence;
344344
}
345345

346-
$imapCommand->setTag($tag);
346+
$command->setTag($tag);
347347

348-
return $this->sendCommand($imapCommand);
348+
return $this->sendCommand($command);
349349
}
350350

351351
/**
@@ -976,14 +976,17 @@ public function idle(): void
976976
while (true) {
977977
$line = $this->nextLine($response);
978978

979+
// Server indicates it's ready for IDLE.
979980
if (str_starts_with($line, '+ ')) {
980981
return;
981982
}
982983

983-
if (preg_match('/^\* OK/i', $line) || preg_match('/^TAG\d+ OK/i', $line)) {
984+
// Typical untagged or tagged "OK" lines.
985+
if (preg_match('/^\* /i', $line) || preg_match('/^TAG\d+ OK/i', $line)) {
984986
continue;
985987
}
986988

989+
// Unexpected response.
987990
throw new RuntimeException('Idle failed. Unexpected response: '.trim($line));
988991
}
989992
}
@@ -997,8 +1000,21 @@ public function done(): void
9971000

9981001
$this->write($response, 'DONE');
9991002

1000-
if (! $this->assumedNextTaggedLine($response, 'OK', $tags)) {
1001-
throw new RuntimeException('Done failed');
1003+
while (true) {
1004+
$line = $this->nextLine($response);
1005+
1006+
// Typical tagged "OK" line.
1007+
if (preg_match('/^TAG\d+ OK/i', $line)) {
1008+
break;
1009+
}
1010+
1011+
// Handle untagged notifications (e.g. "* 4 EXISTS").
1012+
if (preg_match('/^\* /i', $line)) {
1013+
continue;
1014+
}
1015+
1016+
// Unexpected response.
1017+
throw new RuntimeException('Done failed. Unexpected response: '.trim($line));
10021018
}
10031019
}
10041020

@@ -1064,14 +1080,6 @@ public function setDebug(bool $enabled): void
10641080
$this->debug = $enabled;
10651081
}
10661082

1067-
/**
1068-
* Disable the debug mode.
1069-
*/
1070-
public function disableDebug(): void
1071-
{
1072-
$this->debug = false;
1073-
}
1074-
10751083
/**
10761084
* Build a valid UID number set.
10771085
*/

0 commit comments

Comments
 (0)