generated from Nodarx/template
feat: initial version
Signed-off-by: Sebastian Krupinski <root@LAPTOP-7DVOR6NC>
This commit was merged in pull request #1.
This commit is contained in:
39
lib/Client/Protocol/Command/AppendCommand.php
Normal file
39
lib/Client/Protocol/Command/AppendCommand.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Gricob\IMAP\Protocol\Command\Argument\DateTime;
|
||||
use Gricob\IMAP\Protocol\Command\Argument\QuotedString;
|
||||
use Gricob\IMAP\Protocol\Command\Argument\SynchronizingLiteral;
|
||||
use Gricob\IMAP\Protocol\Command\Argument\ParenthesizedList;
|
||||
|
||||
final readonly class AppendCommand extends Command implements Continuable
|
||||
{
|
||||
/**
|
||||
* @param list<string>|null $flags
|
||||
*/
|
||||
public function __construct(
|
||||
string $mailboxName,
|
||||
private string $message,
|
||||
?array $flags,
|
||||
?DateTimeInterface $internalDate
|
||||
) {
|
||||
parent::__construct(
|
||||
'APPEND',
|
||||
...array_filter([
|
||||
new QuotedString($mailboxName),
|
||||
ParenthesizedList::tryFrom($flags),
|
||||
DateTime::tryFrom($internalDate),
|
||||
new SynchronizingLiteral($this->message),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
public function continue(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
}
|
||||
10
lib/Client/Protocol/Command/Argument/Argument.php
Normal file
10
lib/Client/Protocol/Command/Argument/Argument.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument;
|
||||
|
||||
interface Argument
|
||||
{
|
||||
public function __toString(): string;
|
||||
}
|
||||
24
lib/Client/Protocol/Command/Argument/Date.php
Normal file
24
lib/Client/Protocol/Command/Argument/Date.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument;
|
||||
|
||||
use DateTimeInterface;
|
||||
|
||||
readonly class Date implements Argument
|
||||
{
|
||||
public function __construct(private DateTimeInterface $value)
|
||||
{
|
||||
}
|
||||
|
||||
public static function tryFrom(?DateTimeInterface $value): ?self
|
||||
{
|
||||
return is_null($value) ? null : new self($value);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value->format('d-M-Y');
|
||||
}
|
||||
}
|
||||
24
lib/Client/Protocol/Command/Argument/DateTime.php
Normal file
24
lib/Client/Protocol/Command/Argument/DateTime.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument;
|
||||
|
||||
use DateTimeInterface;
|
||||
|
||||
readonly class DateTime implements Argument
|
||||
{
|
||||
public function __construct(private DateTimeInterface $value)
|
||||
{
|
||||
}
|
||||
|
||||
public static function tryFrom(?DateTimeInterface $value): ?self
|
||||
{
|
||||
return is_null($value) ? null : new self($value);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return '"'.$this->value->format('d-M-Y H:i:s O').'"';
|
||||
}
|
||||
}
|
||||
28
lib/Client/Protocol/Command/Argument/ParenthesizedList.php
Normal file
28
lib/Client/Protocol/Command/Argument/ParenthesizedList.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument;
|
||||
|
||||
final readonly class ParenthesizedList implements Argument
|
||||
{
|
||||
/**
|
||||
* @param list<string> $items
|
||||
*/
|
||||
public function __construct(public array $items)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $items
|
||||
*/
|
||||
public static function tryFrom(?array $items): ?self
|
||||
{
|
||||
return empty($items) ? null : new self($items);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('(%s)', implode(' ', $this->items));
|
||||
}
|
||||
}
|
||||
17
lib/Client/Protocol/Command/Argument/QuotedString.php
Normal file
17
lib/Client/Protocol/Command/Argument/QuotedString.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument;
|
||||
|
||||
final readonly class QuotedString implements Argument
|
||||
{
|
||||
public function __construct(private string $value)
|
||||
{
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('"%s"', $this->value);
|
||||
}
|
||||
}
|
||||
13
lib/Client/Protocol/Command/Argument/Search/All.php
Normal file
13
lib/Client/Protocol/Command/Argument/Search/All.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument\Search;
|
||||
|
||||
class All implements Criteria
|
||||
{
|
||||
public function __toString(): string
|
||||
{
|
||||
return 'ALL';
|
||||
}
|
||||
}
|
||||
15
lib/Client/Protocol/Command/Argument/Search/Before.php
Normal file
15
lib/Client/Protocol/Command/Argument/Search/Before.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument\Search;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\Date;
|
||||
|
||||
readonly class Before extends Date implements Criteria
|
||||
{
|
||||
public function __toString(): string
|
||||
{
|
||||
return 'BEFORE '.parent::__toString();
|
||||
}
|
||||
}
|
||||
11
lib/Client/Protocol/Command/Argument/Search/Criteria.php
Normal file
11
lib/Client/Protocol/Command/Argument/Search/Criteria.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument\Search;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\Argument;
|
||||
|
||||
interface Criteria extends Argument
|
||||
{
|
||||
}
|
||||
19
lib/Client/Protocol/Command/Argument/Search/Header.php
Normal file
19
lib/Client/Protocol/Command/Argument/Search/Header.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument\Search;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\QuotedString;
|
||||
|
||||
class Header implements Criteria
|
||||
{
|
||||
public function __construct(
|
||||
private string $fieldName,
|
||||
private string $value,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('HEADER %s %s', $this->fieldName, new QuotedString($this->value));
|
||||
}
|
||||
}
|
||||
17
lib/Client/Protocol/Command/Argument/Search/Not.php
Normal file
17
lib/Client/Protocol/Command/Argument/Search/Not.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument\Search;
|
||||
|
||||
final readonly class Not implements Criteria
|
||||
{
|
||||
public function __construct(private Criteria $criteria)
|
||||
{
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return 'NOT ('.$this->criteria.')';
|
||||
}
|
||||
}
|
||||
15
lib/Client/Protocol/Command/Argument/Search/Since.php
Normal file
15
lib/Client/Protocol/Command/Argument/Search/Since.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument\Search;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\Date;
|
||||
|
||||
readonly class Since extends Date implements Criteria
|
||||
{
|
||||
public function __toString(): string
|
||||
{
|
||||
return 'SINCE '.parent::__toString();
|
||||
}
|
||||
}
|
||||
36
lib/Client/Protocol/Command/Argument/SequenceSet.php
Normal file
36
lib/Client/Protocol/Command/Argument/SequenceSet.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument;
|
||||
|
||||
final class SequenceSet implements Argument
|
||||
{
|
||||
/**
|
||||
* @var array<int>
|
||||
*/
|
||||
private array $numbers;
|
||||
private ?string $range;
|
||||
|
||||
public function __construct(int ...$numbers)
|
||||
{
|
||||
$this->numbers = $numbers;
|
||||
$this->range = null;
|
||||
}
|
||||
|
||||
public static function range(int $from, int $to): self
|
||||
{
|
||||
$set = new self();
|
||||
$set->range = $from . ':' . $to;
|
||||
return $set;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
if ($this->range !== null) {
|
||||
return $this->range;
|
||||
}
|
||||
|
||||
return implode(',', $this->numbers);
|
||||
}
|
||||
}
|
||||
30
lib/Client/Protocol/Command/Argument/Store/Flags.php
Normal file
30
lib/Client/Protocol/Command/Argument/Store/Flags.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument\Store;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\Argument;
|
||||
|
||||
final readonly class Flags implements Argument
|
||||
{
|
||||
/**
|
||||
* @param list<string> $flags
|
||||
*/
|
||||
public function __construct(
|
||||
private array $flags,
|
||||
private string $modifier = '',
|
||||
private bool $silent = true,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'%sFLAGS%s (%s)',
|
||||
$this->modifier,
|
||||
$this->silent ? '.SILENT' : '',
|
||||
implode(' ', $this->flags),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Argument;
|
||||
|
||||
final readonly class SynchronizingLiteral implements Argument
|
||||
{
|
||||
public function __construct(private string $value)
|
||||
{
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'{%s}',
|
||||
strlen($this->value)
|
||||
);
|
||||
}
|
||||
}
|
||||
12
lib/Client/Protocol/Command/Authenticate/SASLMechanism.php
Normal file
12
lib/Client/Protocol/Command/Authenticate/SASLMechanism.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Authenticate;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\Argument;
|
||||
use Gricob\IMAP\Protocol\Command\Continuable;
|
||||
|
||||
interface SASLMechanism extends Argument, Continuable
|
||||
{
|
||||
}
|
||||
26
lib/Client/Protocol/Command/Authenticate/XOAuth2.php
Normal file
26
lib/Client/Protocol/Command/Authenticate/XOAuth2.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command\Authenticate;
|
||||
|
||||
final readonly class XOAuth2 implements SASLMechanism
|
||||
{
|
||||
public function __construct(
|
||||
private string $user,
|
||||
private string $accessToken
|
||||
) {
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return 'XOAUTH2';
|
||||
}
|
||||
|
||||
public function continue(): string
|
||||
{
|
||||
return base64_encode(
|
||||
sprintf("user=%s\1auth=Bearer %s\1\1", $this->user, $this->accessToken)
|
||||
);
|
||||
}
|
||||
}
|
||||
20
lib/Client/Protocol/Command/AuthenticateCommand.php
Normal file
20
lib/Client/Protocol/Command/AuthenticateCommand.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Authenticate\SASLMechanism;
|
||||
|
||||
readonly class AuthenticateCommand extends Command implements Continuable
|
||||
{
|
||||
public function __construct(private SASLMechanism $mechanism)
|
||||
{
|
||||
parent::__construct('AUTHENTICATE', $mechanism);
|
||||
}
|
||||
|
||||
public function continue(): string
|
||||
{
|
||||
return $this->mechanism->continue();
|
||||
}
|
||||
}
|
||||
40
lib/Client/Protocol/Command/Command.php
Normal file
40
lib/Client/Protocol/Command/Command.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\Argument;
|
||||
use Stringable;
|
||||
|
||||
abstract readonly class Command implements Stringable
|
||||
{
|
||||
private string $command;
|
||||
|
||||
/**
|
||||
* @var Argument[]
|
||||
*/
|
||||
private array $arguments;
|
||||
|
||||
public function __construct(
|
||||
string $command,
|
||||
Argument ...$arguments,
|
||||
) {
|
||||
$this->command = $command;
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function command(): string
|
||||
{
|
||||
return $this->command;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'%s %s',
|
||||
$this->command,
|
||||
implode(' ', $this->arguments)
|
||||
);
|
||||
}
|
||||
}
|
||||
10
lib/Client/Protocol/Command/Continuable.php
Normal file
10
lib/Client/Protocol/Command/Continuable.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
interface Continuable
|
||||
{
|
||||
public function continue(): string;
|
||||
}
|
||||
15
lib/Client/Protocol/Command/CreateCommand.php
Normal file
15
lib/Client/Protocol/Command/CreateCommand.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\QuotedString;
|
||||
|
||||
final readonly class CreateCommand extends Command
|
||||
{
|
||||
public function __construct(string $mailboxName)
|
||||
{
|
||||
parent::__construct('CREATE', new QuotedString($mailboxName));
|
||||
}
|
||||
}
|
||||
13
lib/Client/Protocol/Command/ExpungeCommand.php
Normal file
13
lib/Client/Protocol/Command/ExpungeCommand.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
final readonly class ExpungeCommand extends Command
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('EXPUNGE');
|
||||
}
|
||||
}
|
||||
28
lib/Client/Protocol/Command/FetchCommand.php
Normal file
28
lib/Client/Protocol/Command/FetchCommand.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\ParenthesizedList;
|
||||
use Gricob\IMAP\Protocol\Command\Argument\SequenceSet;
|
||||
|
||||
final readonly class FetchCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @param bool $uid
|
||||
* @param SequenceSet $sequenceSet
|
||||
* @param list<string> $items
|
||||
*/
|
||||
public function __construct(
|
||||
bool $uid,
|
||||
SequenceSet $sequenceSet,
|
||||
array $items,
|
||||
) {
|
||||
parent::__construct(
|
||||
$uid ? 'UID FETCH' : 'FETCH',
|
||||
$sequenceSet,
|
||||
new ParenthesizedList($items),
|
||||
);
|
||||
}
|
||||
}
|
||||
19
lib/Client/Protocol/Command/ListCommand.php
Normal file
19
lib/Client/Protocol/Command/ListCommand.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\QuotedString;
|
||||
|
||||
readonly class ListCommand extends Command
|
||||
{
|
||||
public function __construct(string $referenceName, string $pattern)
|
||||
{
|
||||
parent::__construct(
|
||||
'LIST',
|
||||
new QuotedString($referenceName),
|
||||
new QuotedString($pattern)
|
||||
);
|
||||
}
|
||||
}
|
||||
19
lib/Client/Protocol/Command/LogInCommand.php
Normal file
19
lib/Client/Protocol/Command/LogInCommand.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\QuotedString;
|
||||
|
||||
final readonly class LogInCommand extends Command
|
||||
{
|
||||
public function __construct(string $user, string $password)
|
||||
{
|
||||
parent::__construct(
|
||||
'LOGIN',
|
||||
new QuotedString($user),
|
||||
new QuotedString($password)
|
||||
);
|
||||
}
|
||||
}
|
||||
20
lib/Client/Protocol/Command/SearchCommand.php
Normal file
20
lib/Client/Protocol/Command/SearchCommand.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\Search\Criteria;
|
||||
|
||||
final readonly class SearchCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
bool $uid,
|
||||
Criteria ...$criteria,
|
||||
) {
|
||||
parent::__construct(
|
||||
$uid ? 'UID SEARCH' : 'SEARCH',
|
||||
...$criteria,
|
||||
);
|
||||
}
|
||||
}
|
||||
15
lib/Client/Protocol/Command/SelectCommand.php
Normal file
15
lib/Client/Protocol/Command/SelectCommand.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\QuotedString;
|
||||
|
||||
readonly class SelectCommand extends Command
|
||||
{
|
||||
public function __construct(string $mailbox)
|
||||
{
|
||||
parent::__construct('SELECT', new QuotedString($mailbox));
|
||||
}
|
||||
}
|
||||
19
lib/Client/Protocol/Command/StartTlsCommand.php
Normal file
19
lib/Client/Protocol/Command/StartTlsCommand.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
/**
|
||||
* STARTTLS command (RFC 3501 §6.2.1) — patched into gricob/imap.
|
||||
*
|
||||
* After the server responds OK, upgradeTls() must be called on the underlying
|
||||
* SocketConnection to complete the TLS handshake.
|
||||
*/
|
||||
final readonly class StartTlsCommand extends Command
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('STARTTLS');
|
||||
}
|
||||
}
|
||||
23
lib/Client/Protocol/Command/StoreCommand.php
Normal file
23
lib/Client/Protocol/Command/StoreCommand.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Command;
|
||||
|
||||
use Gricob\IMAP\Protocol\Command\Argument\SequenceSet;
|
||||
use Gricob\IMAP\Protocol\Command\Argument\Store\Flags;
|
||||
|
||||
final readonly class StoreCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
bool $uid,
|
||||
SequenceSet $sequenceSet,
|
||||
Flags $dataItem
|
||||
) {
|
||||
parent::__construct(
|
||||
$uid ? 'UID STORE' : 'STORE',
|
||||
$sequenceSet,
|
||||
$dataItem,
|
||||
);
|
||||
}
|
||||
}
|
||||
16
lib/Client/Protocol/CommandFailed.php
Normal file
16
lib/Client/Protocol/CommandFailed.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol;
|
||||
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\Status;
|
||||
use RuntimeException;
|
||||
|
||||
class CommandFailed extends RuntimeException
|
||||
{
|
||||
public static function withStatus(Status $status): self
|
||||
{
|
||||
return new self($status->message);
|
||||
}
|
||||
}
|
||||
70
lib/Client/Protocol/CommandInteraction.php
Normal file
70
lib/Client/Protocol/CommandInteraction.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol;
|
||||
|
||||
use Generator;
|
||||
use Gricob\IMAP\Protocol\Command\Command;
|
||||
use Gricob\IMAP\Protocol\Command\Continuable;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Line;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\Status;
|
||||
use Gricob\IMAP\Protocol\Response\Response;
|
||||
use Gricob\IMAP\Transport\Connection;
|
||||
use RuntimeException;
|
||||
|
||||
final readonly class CommandInteraction implements ContinuationHandler
|
||||
{
|
||||
public function __construct(
|
||||
private Connection $connection,
|
||||
private ResponseHandler $responseHandler,
|
||||
private string $tag,
|
||||
private Command $command,
|
||||
) {
|
||||
}
|
||||
|
||||
public function interact(): Response
|
||||
{
|
||||
$request = sprintf(
|
||||
"%s %s\r\n",
|
||||
$this->tag,
|
||||
$this->command,
|
||||
);
|
||||
|
||||
$this->connection->send($request);
|
||||
$streamResponse = $this->connection->receive();
|
||||
|
||||
return $this->responseHandler->handle($this->tag, $streamResponse, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like interact() but yields each untagged Line immediately as it arrives.
|
||||
* The terminal Status is the generator's return value.
|
||||
*
|
||||
* @return Generator<int, Line, mixed, Status>
|
||||
*/
|
||||
public function streamInteract(): Generator
|
||||
{
|
||||
$request = sprintf(
|
||||
"%s %s\r\n",
|
||||
$this->tag,
|
||||
$this->command,
|
||||
);
|
||||
|
||||
$this->connection->send($request);
|
||||
$streamResponse = $this->connection->receive();
|
||||
|
||||
yield from $this->responseHandler->stream($this->tag, $streamResponse, $this);
|
||||
}
|
||||
|
||||
public function continue(): void
|
||||
{
|
||||
if (!$this->command instanceof Continuable) {
|
||||
throw new RuntimeException(
|
||||
sprintf('Command %s does not support continuable interaction', $this->command->command())
|
||||
);
|
||||
}
|
||||
|
||||
$this->connection->send($this->command->continue()."\r\n");
|
||||
}
|
||||
}
|
||||
11
lib/Client/Protocol/ConnectionRejected.php
Normal file
11
lib/Client/Protocol/ConnectionRejected.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class ConnectionRejected extends RuntimeException
|
||||
{
|
||||
}
|
||||
10
lib/Client/Protocol/ContinuationHandler.php
Normal file
10
lib/Client/Protocol/ContinuationHandler.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol;
|
||||
|
||||
interface ContinuationHandler
|
||||
{
|
||||
public function continue(): void;
|
||||
}
|
||||
129
lib/Client/Protocol/Imap.php
Normal file
129
lib/Client/Protocol/Imap.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol;
|
||||
|
||||
use Generator;
|
||||
use Gricob\IMAP\Protocol\Command\Command;
|
||||
use Gricob\IMAP\Protocol\Command\StartTlsCommand;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Line;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\Status;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\StatusType;
|
||||
use Gricob\IMAP\Protocol\Response\Parser\Parser;
|
||||
use Gricob\IMAP\Protocol\Response\Response;
|
||||
use Gricob\IMAP\Transport\Connection;
|
||||
use RuntimeException;
|
||||
|
||||
class Imap
|
||||
{
|
||||
protected Connection $connection;
|
||||
private TagGenerator $tagGenerator;
|
||||
private ResponseHandler $responseHandler;
|
||||
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->tagGenerator = new TagGenerator();
|
||||
$this->responseHandler = new ResponseHandler(new Parser());
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->disconnect();
|
||||
}
|
||||
|
||||
public function connect(): void
|
||||
{
|
||||
if ($this->connection->isOpen()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->connection->open();
|
||||
|
||||
$responseStream = $this->connection->receive();
|
||||
|
||||
$greeting = $this->responseHandler->handle('*', $responseStream, new UnexpectedContinuationHandler());
|
||||
|
||||
match ($greeting->status->type) {
|
||||
StatusType::OK => null, // Do nothing
|
||||
StatusType::PREAUTH => throw new RuntimeException('pre-auth is not supported'),
|
||||
StatusType::BAD,
|
||||
StatusType::NO,
|
||||
StatusType::BYE => throw new ConnectionRejected($greeting->status->message),
|
||||
};
|
||||
}
|
||||
|
||||
public function disconnect(): void
|
||||
{
|
||||
$this->connection->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform STARTTLS negotiation (patch).
|
||||
*
|
||||
* Sends the STARTTLS command and upgrades the underlying socket to TLS.
|
||||
* The connection must be a SocketConnection (or any Connection that
|
||||
* implements upgradeTls()). Call this after connect() but before logIn().
|
||||
*
|
||||
* @throws \RuntimeException if the server rejects STARTTLS
|
||||
* @throws \BadMethodCallException if the connection does not support TLS upgrade
|
||||
*/
|
||||
public function startTls(): void
|
||||
{
|
||||
if (!method_exists($this->connection, 'upgradeTls')) {
|
||||
throw new \BadMethodCallException(
|
||||
'The current Connection implementation does not support STARTTLS upgrade'
|
||||
);
|
||||
}
|
||||
|
||||
$response = $this->send(new StartTlsCommand());
|
||||
|
||||
if ($response->status->type !== StatusType::OK) {
|
||||
throw new \RuntimeException(
|
||||
'Server rejected STARTTLS: ' . $response->status->message
|
||||
);
|
||||
}
|
||||
|
||||
$this->connection->upgradeTls();
|
||||
}
|
||||
|
||||
public function send(Command $command): Response
|
||||
{
|
||||
$interaction = new CommandInteraction(
|
||||
$this->connection,
|
||||
$this->responseHandler,
|
||||
$this->tagGenerator->next(),
|
||||
$command,
|
||||
);
|
||||
|
||||
$response = $interaction->interact();
|
||||
|
||||
if ($response->status->type != StatusType::OK) {
|
||||
throw CommandFailed::withStatus($response->status);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends $command and returns a Generator that yields each untagged Line as
|
||||
* it arrives from the socket. CommandFailed is thrown (inside the generator)
|
||||
* if the server responds with NO or BAD.
|
||||
*
|
||||
* @return Generator<int, Line, mixed, Status>
|
||||
*/
|
||||
public function sendStreaming(Command $command): Generator
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$interaction = new CommandInteraction(
|
||||
$this->connection,
|
||||
$this->responseHandler,
|
||||
$this->tagGenerator->next(),
|
||||
$command,
|
||||
);
|
||||
|
||||
yield from $interaction->streamInteract();
|
||||
}
|
||||
}
|
||||
13
lib/Client/Protocol/Response/Line/CommandContinuation.php
Normal file
13
lib/Client/Protocol/Response/Line/CommandContinuation.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line;
|
||||
|
||||
final readonly class CommandContinuation implements Line
|
||||
{
|
||||
public function __construct(
|
||||
public string $message,
|
||||
) {
|
||||
}
|
||||
}
|
||||
15
lib/Client/Protocol/Response/Line/Data/CapabilityData.php
Normal file
15
lib/Client/Protocol/Response/Line/Data/CapabilityData.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
final readonly class CapabilityData implements Data
|
||||
{
|
||||
/**
|
||||
* @param list<string> $capabilities
|
||||
*/
|
||||
public function __construct(public array $capabilities)
|
||||
{
|
||||
}
|
||||
}
|
||||
11
lib/Client/Protocol/Response/Line/Data/Data.php
Normal file
11
lib/Client/Protocol/Response/Line/Data/Data.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
use Gricob\IMAP\Protocol\Response\Line\Line;
|
||||
|
||||
interface Data extends Line
|
||||
{
|
||||
}
|
||||
12
lib/Client/Protocol/Response/Line/Data/ExistsData.php
Normal file
12
lib/Client/Protocol/Response/Line/Data/ExistsData.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
final readonly class ExistsData implements Data
|
||||
{
|
||||
public function __construct(public int $numberOfMessages)
|
||||
{
|
||||
}
|
||||
}
|
||||
12
lib/Client/Protocol/Response/Line/Data/ExpungeData.php
Normal file
12
lib/Client/Protocol/Response/Line/Data/ExpungeData.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
final readonly class ExpungeData implements Data
|
||||
{
|
||||
public function __construct(public int $id)
|
||||
{
|
||||
}
|
||||
}
|
||||
16
lib/Client/Protocol/Response/Line/Data/Fetch/Address.php
Normal file
16
lib/Client/Protocol/Response/Line/Data/Fetch/Address.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch;
|
||||
|
||||
final readonly class Address
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $displayName,
|
||||
public ?string $atDomainList,
|
||||
public ?string $mailboxName,
|
||||
public ?string $hostName,
|
||||
) {
|
||||
}
|
||||
}
|
||||
12
lib/Client/Protocol/Response/Line/Data/Fetch/BodySection.php
Normal file
12
lib/Client/Protocol/Response/Line/Data/Fetch/BodySection.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch;
|
||||
|
||||
final readonly class BodySection
|
||||
{
|
||||
public function __construct(public string $section, public string $text)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch;
|
||||
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure\Part;
|
||||
|
||||
class BodyStructure
|
||||
{
|
||||
public function __construct(
|
||||
public Part $part,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure;
|
||||
|
||||
final readonly class Disposition
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $attributes
|
||||
*/
|
||||
public function __construct(
|
||||
public string $type,
|
||||
public array $attributes,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure;
|
||||
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\Envelope;
|
||||
|
||||
readonly class MessagePart extends SinglePart
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $attributes
|
||||
* @param string[]|null $language
|
||||
*/
|
||||
public function __construct(
|
||||
array $attributes,
|
||||
?string $id,
|
||||
?string $description,
|
||||
string $encoding,
|
||||
int $size,
|
||||
public Envelope $envelope,
|
||||
public BodyStructure $bodyStructure,
|
||||
public int $textLines,
|
||||
?string $md5,
|
||||
?Disposition $disposition,
|
||||
?array $language,
|
||||
?string $location,
|
||||
) {
|
||||
parent::__construct(
|
||||
'MESSAGE',
|
||||
'RFC822',
|
||||
$attributes,
|
||||
$id,
|
||||
$description,
|
||||
$encoding,
|
||||
$size,
|
||||
$md5,
|
||||
$disposition,
|
||||
$language,
|
||||
$location,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure;
|
||||
|
||||
final readonly class MultiPart extends Part
|
||||
{
|
||||
/**
|
||||
* @param array<string,string> $attributes
|
||||
* @param string[] $language
|
||||
* @param list<Part> $parts
|
||||
*/
|
||||
public function __construct(
|
||||
string $subtype,
|
||||
array $attributes,
|
||||
public array $parts,
|
||||
public ?Disposition $disposition,
|
||||
public ?array $language,
|
||||
public ?string $location,
|
||||
) {
|
||||
parent::__construct('MULTIPART', $subtype, $attributes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure;
|
||||
|
||||
abstract readonly class Part
|
||||
{
|
||||
/**
|
||||
* @param array<string,string> $attributes
|
||||
*/
|
||||
public function __construct(
|
||||
public string $type,
|
||||
public string $subtype,
|
||||
public array $attributes,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure;
|
||||
|
||||
readonly class SinglePart extends Part
|
||||
{
|
||||
/**
|
||||
* @param array<string,string> $attributes
|
||||
* @param string[]|null $language
|
||||
*/
|
||||
public function __construct(
|
||||
string $type,
|
||||
string $subtype,
|
||||
array $attributes,
|
||||
public ?string $id,
|
||||
public ?string $description,
|
||||
public string $encoding,
|
||||
public int $size,
|
||||
public ?string $md5,
|
||||
public ?Disposition $disposition,
|
||||
public ?array $language,
|
||||
public ?string $location,
|
||||
) {
|
||||
parent::__construct($type, $subtype, $attributes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure;
|
||||
|
||||
final readonly class TextPart extends SinglePart
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $attributes
|
||||
* @param string[]|null $language
|
||||
*/
|
||||
public function __construct(
|
||||
string $subtype,
|
||||
array $attributes,
|
||||
?string $id,
|
||||
?string $description,
|
||||
string $encoding,
|
||||
int $size,
|
||||
public int $textLines,
|
||||
?string $md5,
|
||||
?Disposition $disposition,
|
||||
?array $language,
|
||||
?string $location,
|
||||
) {
|
||||
parent::__construct(
|
||||
'TEXT',
|
||||
$subtype,
|
||||
$attributes,
|
||||
$id,
|
||||
$description,
|
||||
$encoding,
|
||||
$size,
|
||||
$md5,
|
||||
$disposition,
|
||||
$language,
|
||||
$location,
|
||||
);
|
||||
}
|
||||
}
|
||||
32
lib/Client/Protocol/Response/Line/Data/Fetch/Envelope.php
Normal file
32
lib/Client/Protocol/Response/Line/Data/Fetch/Envelope.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data\Fetch;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
final readonly class Envelope
|
||||
{
|
||||
/**
|
||||
* @param Address[]|null $from
|
||||
* @param Address[]|null $sender
|
||||
* @param Address[]|null $replyTo
|
||||
* @param Address[]|null $to
|
||||
* @param Address[]|null $cc
|
||||
* @param Address[]|null $bcc
|
||||
*/
|
||||
public function __construct(
|
||||
public ?DateTimeImmutable $date,
|
||||
public ?string $subject,
|
||||
public ?array $from,
|
||||
public ?array $sender,
|
||||
public ?array $replyTo,
|
||||
public ?array $to,
|
||||
public ?array $cc,
|
||||
public ?array $bcc,
|
||||
public ?string $inReplyTo,
|
||||
public ?string $messageId,
|
||||
) {
|
||||
}
|
||||
}
|
||||
40
lib/Client/Protocol/Response/Line/Data/FetchData.php
Normal file
40
lib/Client/Protocol/Response/Line/Data/FetchData.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodySection;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\Envelope;
|
||||
|
||||
final readonly class FetchData implements Data
|
||||
{
|
||||
/**
|
||||
* @param array<string>|null $flags
|
||||
* @param BodySection[] $bodySections
|
||||
*/
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public ?array $flags = null,
|
||||
public ?\DateTimeImmutable $internalDate = null,
|
||||
public ?Envelope $envelope = null,
|
||||
public ?int $rfc822Size = null,
|
||||
public ?string $rfc822 = null,
|
||||
public ?int $uid = null,
|
||||
public ?BodyStructure $bodyStructure = null,
|
||||
public array $bodySections = [],
|
||||
) {
|
||||
}
|
||||
|
||||
public function getBodySection(string $name): ?BodySection
|
||||
{
|
||||
foreach (($this->bodySections ?? []) as $bodySection) {
|
||||
if ($bodySection->section == $name) {
|
||||
return $bodySection;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
15
lib/Client/Protocol/Response/Line/Data/FlagsData.php
Normal file
15
lib/Client/Protocol/Response/Line/Data/FlagsData.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
final readonly class FlagsData implements Data
|
||||
{
|
||||
/**
|
||||
* @param list<string> $flags
|
||||
*/
|
||||
public function __construct(public array $flags)
|
||||
{
|
||||
}
|
||||
}
|
||||
18
lib/Client/Protocol/Response/Line/Data/ListData.php
Normal file
18
lib/Client/Protocol/Response/Line/Data/ListData.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
final class ListData implements Data
|
||||
{
|
||||
/**
|
||||
* @param list<string> $nameAttributes
|
||||
*/
|
||||
public function __construct(
|
||||
public array $nameAttributes,
|
||||
public string $hierarchyDelimiter,
|
||||
public string $name
|
||||
) {
|
||||
}
|
||||
}
|
||||
12
lib/Client/Protocol/Response/Line/Data/RecentData.php
Normal file
12
lib/Client/Protocol/Response/Line/Data/RecentData.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
final class RecentData implements Data
|
||||
{
|
||||
public function __construct(public int $numberOfMessages)
|
||||
{
|
||||
}
|
||||
}
|
||||
15
lib/Client/Protocol/Response/Line/Data/SearchData.php
Normal file
15
lib/Client/Protocol/Response/Line/Data/SearchData.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Data;
|
||||
|
||||
final readonly class SearchData implements Data
|
||||
{
|
||||
/**
|
||||
* @param list<int> $numbers
|
||||
*/
|
||||
public function __construct(public array $numbers)
|
||||
{
|
||||
}
|
||||
}
|
||||
9
lib/Client/Protocol/Response/Line/Line.php
Normal file
9
lib/Client/Protocol/Response/Line/Line.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line;
|
||||
|
||||
interface Line
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status\Code;
|
||||
|
||||
final readonly class AppendUidCode implements Code
|
||||
{
|
||||
public function __construct(
|
||||
public int $uidValidity,
|
||||
public int $uid,
|
||||
) {
|
||||
}
|
||||
}
|
||||
9
lib/Client/Protocol/Response/Line/Status/Code/Code.php
Normal file
9
lib/Client/Protocol/Response/Line/Status/Code/Code.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status\Code;
|
||||
|
||||
interface Code
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status\Code;
|
||||
|
||||
final readonly class PermanentFlagsCode implements Code
|
||||
{
|
||||
/**
|
||||
* @param string[] $flags
|
||||
*/
|
||||
public function __construct(
|
||||
public array $flags,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status\Code;
|
||||
|
||||
final readonly class ReadOnlyCode implements Code
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status\Code;
|
||||
|
||||
final readonly class ReadWriteCode implements Code
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status\Code;
|
||||
|
||||
final readonly class UidNextCode implements Code
|
||||
{
|
||||
public function __construct(
|
||||
public int $value,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status\Code;
|
||||
|
||||
final readonly class UidValidityCode implements Code
|
||||
{
|
||||
public function __construct(
|
||||
public int $value,
|
||||
) {
|
||||
}
|
||||
}
|
||||
13
lib/Client/Protocol/Response/Line/Status/Code/UnseenCode.php
Normal file
13
lib/Client/Protocol/Response/Line/Status/Code/UnseenCode.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status\Code;
|
||||
|
||||
final readonly class UnseenCode implements Code
|
||||
{
|
||||
public function __construct(
|
||||
public int $seq,
|
||||
) {
|
||||
}
|
||||
}
|
||||
19
lib/Client/Protocol/Response/Line/Status/Status.php
Normal file
19
lib/Client/Protocol/Response/Line/Status/Status.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status;
|
||||
|
||||
use Gricob\IMAP\Protocol\Response\Line\Line;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\Code\Code;
|
||||
|
||||
final readonly class Status implements Line
|
||||
{
|
||||
final public function __construct(
|
||||
public string $tag,
|
||||
public StatusType $type,
|
||||
public ?Code $code,
|
||||
public string $message
|
||||
) {
|
||||
}
|
||||
}
|
||||
12
lib/Client/Protocol/Response/Line/Status/StatusType.php
Normal file
12
lib/Client/Protocol/Response/Line/Status/StatusType.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Line\Status;
|
||||
|
||||
enum StatusType: string
|
||||
{
|
||||
case OK = 'OK';
|
||||
case NO = 'NO';
|
||||
case BAD = 'BAD';
|
||||
case PREAUTH = 'PREAUTH';
|
||||
case BYE = 'BYE';
|
||||
}
|
||||
79
lib/Client/Protocol/Response/Parser/Lexer.php
Normal file
79
lib/Client/Protocol/Response/Parser/Lexer.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Parser;
|
||||
|
||||
use Doctrine\Common\Lexer\AbstractLexer;
|
||||
|
||||
/**
|
||||
* @extends AbstractLexer<TokenType, string>
|
||||
*/
|
||||
class Lexer extends AbstractLexer
|
||||
{
|
||||
protected function getCatchablePatterns(): array
|
||||
{
|
||||
return [
|
||||
'[a-zA-Z0-9\.\-]+',
|
||||
'\r\n',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getNonCatchablePatterns(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function getType(string &$value)
|
||||
{
|
||||
$normalizedValue = strtoupper($value);
|
||||
|
||||
return match($normalizedValue) {
|
||||
' ' => TokenType::SP,
|
||||
'.' => TokenType::DOT,
|
||||
'*' => TokenType::ASTERISK,
|
||||
'%' => TokenType::PERCENT_SIGN,
|
||||
'+' => TokenType::PLUS_SIGN,
|
||||
'=' => TokenType::EQUALS_SIGN,
|
||||
'"' => TokenType::DOUBLE_QUOTE,
|
||||
'[' => TokenType::OPEN_BRACKETS,
|
||||
']' => TokenType::CLOSE_BRACKETS,
|
||||
'{' => TokenType::OPEN_BRACES,
|
||||
'}' => TokenType::CLOSE_BRACES,
|
||||
'(' => TokenType::OPEN_PARENTHESIS,
|
||||
')' => TokenType::CLOSE_PARENTHESIS,
|
||||
'\\' => TokenType::BACKSLASH,
|
||||
"\r\n" => TokenType::CRLF,
|
||||
'NIL' => TokenType::NIL,
|
||||
'OK', 'NO', 'BAD', 'BYE', 'PREAUTH' => TokenType::STATUS,
|
||||
'APPENDUID' => TokenType::APPENDUID,
|
||||
'UNSEEN' => TokenType::UNSEEN,
|
||||
'UIDVALIDITY' => TokenType::UIDVALIDITY,
|
||||
'UIDNEXT' => TokenType::UIDNEXT,
|
||||
'PERMANENTFLAGS' => TokenType::PERMANENTFLAGS,
|
||||
'READ-WRITE' => TokenType::READ_WRITE,
|
||||
'READ-ONLY' => TokenType::READ_ONLY,
|
||||
'CAPABILITY' => TokenType::CAPABILITY,
|
||||
'LIST' => TokenType::LIST,
|
||||
'FLAGS' => TokenType::FLAGS,
|
||||
'RECENT' => TokenType::RECENT,
|
||||
'FETCH' => TokenType::FETCH,
|
||||
'INTERNALDATE' => TokenType::INTERNALDATE,
|
||||
'SEARCH' => TokenType::SEARCH,
|
||||
'EXISTS' => TokenType::EXISTS,
|
||||
'EXPUNGE' => TokenType::EXPUNGE,
|
||||
'BODY' => TokenType::BODY,
|
||||
'BODYSTRUCTURE' => TokenType::BODYSTRUCTURE,
|
||||
'ENVELOPE' => TokenType::ENVELOPE,
|
||||
'RFC822' => TokenType::RFC822,
|
||||
'RFC822.SIZE' => TokenType::RFC822_SIZE,
|
||||
'RFC822.TEXT' => TokenType::RFC822_TEXT,
|
||||
'RFC822.HEAD' => TokenType::RFC822_HEAD,
|
||||
'UID' => TokenType::UID,
|
||||
default => match (true) {
|
||||
is_numeric($value) => TokenType::NUMBER,
|
||||
ctype_alnum($value) => TokenType::ALPHANUMERIC,
|
||||
ctype_cntrl($value) => TokenType::CTL,
|
||||
default => TokenType::UNKNOWN,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
24
lib/Client/Protocol/Response/Parser/ParseError.php
Normal file
24
lib/Client/Protocol/Response/Parser/ParseError.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Parser;
|
||||
|
||||
final class ParseError extends \Exception
|
||||
{
|
||||
/**
|
||||
* @param TokenType[] $expected
|
||||
*/
|
||||
public static function unexpectedToken(?TokenType $given, array $expected, string $input): self
|
||||
{
|
||||
return new self(
|
||||
sprintf(
|
||||
"Expected token of type %s. Given %s.\n%s",
|
||||
implode(
|
||||
' or ',
|
||||
array_map(fn (TokenType $type) => $type->name, $expected)
|
||||
),
|
||||
$given?->name ?? 'null',
|
||||
$input
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
1017
lib/Client/Protocol/Response/Parser/Parser.php
Normal file
1017
lib/Client/Protocol/Response/Parser/Parser.php
Normal file
File diff suppressed because it is too large
Load Diff
56
lib/Client/Protocol/Response/Parser/TokenType.php
Normal file
56
lib/Client/Protocol/Response/Parser/TokenType.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response\Parser;
|
||||
|
||||
enum TokenType
|
||||
{
|
||||
case SP;
|
||||
case DOT;
|
||||
case ASTERISK;
|
||||
case PERCENT_SIGN;
|
||||
case PLUS_SIGN;
|
||||
case EQUALS_SIGN;
|
||||
case DOUBLE_QUOTE;
|
||||
case NUMBER;
|
||||
case ALPHANUMERIC;
|
||||
case NIL;
|
||||
case OPEN_BRACKETS;
|
||||
case CLOSE_BRACKETS;
|
||||
case OPEN_BRACES;
|
||||
case CLOSE_BRACES;
|
||||
case OPEN_PARENTHESIS;
|
||||
case CLOSE_PARENTHESIS;
|
||||
case BACKSLASH;
|
||||
case CRLF;
|
||||
case CTL;
|
||||
|
||||
case STATUS;
|
||||
|
||||
case APPENDUID;
|
||||
case UNSEEN;
|
||||
case UIDVALIDITY;
|
||||
case UIDNEXT;
|
||||
case PERMANENTFLAGS;
|
||||
case READ_WRITE;
|
||||
case READ_ONLY;
|
||||
|
||||
case CAPABILITY;
|
||||
case LIST;
|
||||
case FLAGS;
|
||||
case INTERNALDATE;
|
||||
case RECENT;
|
||||
case FETCH;
|
||||
case SEARCH;
|
||||
case EXISTS;
|
||||
case EXPUNGE;
|
||||
case BODY;
|
||||
case BODYSTRUCTURE;
|
||||
case ENVELOPE;
|
||||
case RFC822;
|
||||
case RFC822_SIZE;
|
||||
case RFC822_HEAD;
|
||||
case RFC822_TEXT;
|
||||
case UID;
|
||||
|
||||
case UNKNOWN;
|
||||
}
|
||||
37
lib/Client/Protocol/Response/Response.php
Normal file
37
lib/Client/Protocol/Response/Response.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response;
|
||||
|
||||
use Gricob\IMAP\Protocol\Response\Line\Line;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\Status;
|
||||
|
||||
final readonly class Response
|
||||
{
|
||||
/**
|
||||
* @param list<Line> $data
|
||||
*/
|
||||
public function __construct(
|
||||
public Status $status,
|
||||
public array $data,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T of Line
|
||||
* @param class-string<T> $type
|
||||
* @return T[]
|
||||
*/
|
||||
public function getData(string $type): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->data as $data) {
|
||||
if ($data instanceof $type) {
|
||||
$result[] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
50
lib/Client/Protocol/Response/ResponseBuilder.php
Normal file
50
lib/Client/Protocol/Response/ResponseBuilder.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol\Response;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Line;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\Status;
|
||||
|
||||
class ResponseBuilder
|
||||
{
|
||||
private ?Status $status = null;
|
||||
|
||||
/**
|
||||
* @var list<Line>
|
||||
*/
|
||||
private array $data = [];
|
||||
|
||||
public function __construct(private readonly string $statusTag)
|
||||
{
|
||||
}
|
||||
|
||||
public function addLine(Line $line): void
|
||||
{
|
||||
if ($line instanceof Status && $line->tag === $this->statusTag) {
|
||||
$this->status = $line;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data[] = $line;
|
||||
}
|
||||
|
||||
public function hasStatus(): bool
|
||||
{
|
||||
return $this->status !== null;
|
||||
}
|
||||
|
||||
public function build(): Response
|
||||
{
|
||||
if (null === $this->status) {
|
||||
throw new BadMethodCallException();
|
||||
}
|
||||
|
||||
return new Response(
|
||||
$this->status,
|
||||
$this->data,
|
||||
);
|
||||
}
|
||||
}
|
||||
88
lib/Client/Protocol/ResponseHandler.php
Normal file
88
lib/Client/Protocol/ResponseHandler.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol;
|
||||
|
||||
use Generator;
|
||||
use Gricob\IMAP\Protocol\Response\Line\CommandContinuation;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Line;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\Status;
|
||||
use Gricob\IMAP\Protocol\Response\Line\Status\StatusType;
|
||||
use Gricob\IMAP\Protocol\Response\Parser\Parser;
|
||||
use Gricob\IMAP\Protocol\Response\Response;
|
||||
use Gricob\IMAP\Protocol\Response\ResponseBuilder;
|
||||
use Gricob\IMAP\Transport\ResponseStream;
|
||||
use RuntimeException;
|
||||
|
||||
readonly class ResponseHandler
|
||||
{
|
||||
public function __construct(private Parser $parser)
|
||||
{
|
||||
}
|
||||
|
||||
public function handle(string $statusTag, ResponseStream $stream, ContinuationHandler $continuationHandler): Response
|
||||
{
|
||||
$responseBuilder = new ResponseBuilder($statusTag);
|
||||
|
||||
do {
|
||||
$raw = $stream->readLine();
|
||||
while (preg_match('/\{(?<bytes>\d+)}\r\n$/', $raw, $matches)) {
|
||||
$raw .= $stream->read((int) $matches['bytes']);
|
||||
$raw .= $stream->readLine();
|
||||
}
|
||||
$line = $this->parser->parse($raw);
|
||||
|
||||
if ($line instanceof CommandContinuation) {
|
||||
$continuationHandler->continue();
|
||||
continue;
|
||||
}
|
||||
|
||||
$responseBuilder->addLine($line);
|
||||
} while (!$responseBuilder->hasStatus());
|
||||
|
||||
return $responseBuilder->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Streams parsed response lines one at a time as a Generator, yielding each
|
||||
* untagged Line immediately as it arrives from the socket. The terminal
|
||||
* Status line is NOT yielded; instead it is set as the generator return
|
||||
* value so callers can retrieve it via $gen->getReturn() after exhaustion.
|
||||
*
|
||||
* @throws CommandFailed if the tagged status is NO or BAD
|
||||
*
|
||||
* @return Generator<int, Line, mixed, Status>
|
||||
*/
|
||||
public function stream(string $statusTag, ResponseStream $stream, ContinuationHandler $continuationHandler): Generator
|
||||
{
|
||||
$status = null;
|
||||
|
||||
do {
|
||||
$raw = $stream->readLine();
|
||||
while (preg_match('/\{(?<bytes>\d+)}\r\n$/', $raw, $matches)) {
|
||||
$raw .= $stream->read((int) $matches['bytes']);
|
||||
$raw .= $stream->readLine();
|
||||
}
|
||||
$line = $this->parser->parse($raw);
|
||||
|
||||
if ($line instanceof CommandContinuation) {
|
||||
$continuationHandler->continue();
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($line instanceof Status && $line->tag === $statusTag) {
|
||||
$status = $line;
|
||||
break;
|
||||
}
|
||||
|
||||
yield $line;
|
||||
} while (true);
|
||||
|
||||
if ($status->type !== StatusType::OK) {
|
||||
throw CommandFailed::withStatus($status);
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
36
lib/Client/Protocol/TagGenerator.php
Normal file
36
lib/Client/Protocol/TagGenerator.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol;
|
||||
|
||||
final class TagGenerator
|
||||
{
|
||||
private const MAX_NUMBER = 999;
|
||||
private const NUMBER_PART_LENGTH = 3;
|
||||
private const INITIAL_LETTER = 'A';
|
||||
private const INITIAL_NUMBER = 0;
|
||||
|
||||
private string $letter = self::INITIAL_LETTER;
|
||||
private int $number = self::INITIAL_NUMBER;
|
||||
|
||||
public function next(): string
|
||||
{
|
||||
$this->number += 1;
|
||||
|
||||
if ($this->number > self::MAX_NUMBER) {
|
||||
$this->letter++;
|
||||
$this->number = self::INITIAL_NUMBER;
|
||||
}
|
||||
|
||||
if (strlen($this->letter) > 1) {
|
||||
$this->letter = self::INITIAL_LETTER;
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'%s%s',
|
||||
$this->letter,
|
||||
str_pad((string) $this->number, self::NUMBER_PART_LENGTH, '0', STR_PAD_LEFT)
|
||||
);
|
||||
}
|
||||
}
|
||||
15
lib/Client/Protocol/UnexpectedContinuationHandler.php
Normal file
15
lib/Client/Protocol/UnexpectedContinuationHandler.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gricob\IMAP\Protocol;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class UnexpectedContinuationHandler implements ContinuationHandler
|
||||
{
|
||||
public function continue(): void
|
||||
{
|
||||
throw new RuntimeException('Unexpected continuation response');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user