Skip to content

Commit f0f6d61

Browse files
committed
Idle command reworked and several issues fixed #170 #229 #237 #249 #258
1 parent 8a50a2d commit f0f6d61

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/Connection/Protocols/ImapProtocol.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,9 @@ public function idle() {
10501050
* @throws RuntimeException
10511051
*/
10521052
public function done(): bool {
1053-
if (fwrite($this->stream, "DONE\r\n") === false) {
1054-
throw new RuntimeException('failed to write - connection closed?');
1053+
$this->write("DONE");
1054+
if (!$this->assumedNextTaggedLine('OK', $tags)) {
1055+
throw new RuntimeException('done failed');
10551056
}
10561057
return true;
10571058
}

src/Folder.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ public function unsubscribe(): bool {
351351
/**
352352
* Idle the current connection
353353
* @param callable $callback
354-
* @param integer $timeout max 1740 seconds - recommended by rfc2177 §3
355-
* @param boolean $auto_reconnect try to reconnect on connection close
354+
* @param integer $timeout max 1740 seconds - recommended by rfc2177 §3. Should not be lower than the servers "* OK Still here" message interval
355+
* @param boolean $auto_reconnect try to reconnect on connection close (@deprecated is no longer required)
356356
*
357357
* @throws ConnectionFailedException
358358
* @throws Exceptions\InvalidMessageDateException
@@ -364,25 +364,24 @@ public function unsubscribe(): bool {
364364
* @throws Exceptions\MessageNotFoundException
365365
* @throws Exceptions\NotSupportedCapabilityException
366366
*/
367-
public function idle(callable $callback, int $timeout = 1200, bool $auto_reconnect = false) {
368-
$this->client->getConnection()->setConnectionTimeout($timeout);
369-
370-
$this->client->reconnect();
367+
public function idle(callable $callback, int $timeout = 300, bool $auto_reconnect = false) {
368+
$this->client->setTimeout($timeout);
371369
if (!in_array("IDLE", $this->client->getConnection()->getCapabilities())) {
372370
throw new NotSupportedCapabilityException("IMAP server does not support IDLE");
373371
}
374372
$this->client->openFolder($this->path, true);
375373
$connection = $this->client->getConnection();
374+
$connection->idle();
376375

377376
$sequence = ClientManager::get('options.sequence', IMAP::ST_MSGN);
378-
$connection->idle();
379377

380378
while (true) {
381379
try {
382380
$line = $connection->nextLine();
381+
383382
if (($pos = strpos($line, "EXISTS")) !== false) {
384-
$msgn = (int) substr($line, 2, $pos -2);
385383
$connection->done();
384+
$msgn = (int) substr($line, 2, $pos -2);
386385

387386
$this->client->openFolder($this->path, true);
388387
$message = $this->query()->getMessageByMsgn($msgn);
@@ -391,20 +390,26 @@ public function idle(callable $callback, int $timeout = 1200, bool $auto_reconne
391390

392391
$event = $this->getEvent("message", "new");
393392
$event::dispatch($message);
394-
393+
$connection->idle();
394+
} elseif (strpos($line, "OK") === false) {
395+
$connection->done();
395396
$connection->idle();
396397
}
397398
}catch (Exceptions\RuntimeException $e) {
399+
if(strpos($e->getMessage(), "empty response") >= 0 && $connection->connected()) {
400+
$connection->done();
401+
$connection->idle();
402+
continue;
403+
}
398404
if(strpos($e->getMessage(), "connection closed") === false) {
399405
throw $e;
400406
}
401-
if ($auto_reconnect === true) {
402-
$this->client->reconnect();
403-
$this->client->openFolder($this->path, true);
404407

405-
$connection = $this->client->getConnection();
406-
$connection->idle();
407-
}
408+
$this->client->reconnect();
409+
$this->client->openFolder($this->path, true);
410+
411+
$connection = $this->client->getConnection();
412+
$connection->idle();
408413
}
409414
}
410415
}

0 commit comments

Comments
 (0)