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,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user