feat: initial version

Signed-off-by: Sebastian Krupinski <root@LAPTOP-7DVOR6NC>
This commit is contained in:
Sebastian Krupinski
2026-02-20 16:41:19 -05:00
commit 7f562d6aba
139 changed files with 11256 additions and 0 deletions

View 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;
}
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Gricob\IMAP\Protocol\Command\Argument;
interface Argument
{
public function __toString(): string;
}

View 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');
}
}

View 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').'"';
}
}

View 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));
}
}

View 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);
}
}

View 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';
}
}

View 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();
}
}

View 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
{
}

View 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));
}
}

View 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.')';
}
}

View 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();
}
}

View 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);
}
}

View 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),
);
}
}

View File

@@ -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)
);
}
}

View 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
{
}

View 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)
);
}
}

View 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();
}
}

View 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)
);
}
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Gricob\IMAP\Protocol\Command;
interface Continuable
{
public function continue(): string;
}

View 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));
}
}

View 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');
}
}

View 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),
);
}
}

View 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)
);
}
}

View 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)
);
}
}

View 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,
);
}
}

View 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));
}
}

View 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');
}
}

View 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,
);
}
}