1 Commits

Author SHA1 Message Date
8093c031d9 chore(deps): update dependency phpunit/phpunit to v13
Some checks failed
renovate/artifacts Artifact file update failure
2026-05-19 03:03:48 +00:00
17 changed files with 255 additions and 486 deletions

View File

@@ -23,7 +23,7 @@
"doctrine/lexer": "^3.0" "doctrine/lexer": "^3.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^11.0" "phpunit/phpunit": "^13.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@@ -6,8 +6,8 @@ namespace KTXM\ProviderImap\Client;
use KTXM\ProviderImap\Client\Command\CapabilityCommand; use KTXM\ProviderImap\Client\Command\CapabilityCommand;
use KTXM\ProviderImap\Client\Command\CommandInterface; use KTXM\ProviderImap\Client\Command\CommandInterface;
use KTXM\ProviderImap\Client\FetchTarget;
use KTXM\ProviderImap\Client\Command\LoginCommand; use KTXM\ProviderImap\Client\Command\LoginCommand;
use KTXM\ProviderImap\Client\Command\StatusCommand;
use KTXM\ProviderImap\Client\Command\StartTlsCommand; use KTXM\ProviderImap\Client\Command\StartTlsCommand;
use KTXM\ProviderImap\Client\Protocol\CommandExecutor; use KTXM\ProviderImap\Client\Protocol\CommandExecutor;
use KTXM\ProviderImap\Client\Protocol\ProtocolReader; use KTXM\ProviderImap\Client\Protocol\ProtocolReader;
@@ -87,15 +87,6 @@ final class Client implements ClientInterface
return $this->executor->perform($command, $this->session); return $this->executor->perform($command, $this->session);
} }
public function download(FetchTarget $target, string $section, int $chunkSize = 8192): \Generator
{
if ($this->session === null || $this->executor === null) {
throw new ImapException('IMAP client is not connected.');
}
return $this->executor->download($target, $section, $chunkSize, $this->session);
}
public function session(): SessionContext public function session(): SessionContext
{ {
if ($this->session === null) { if ($this->session === null) {

View File

@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace KTXM\ProviderImap\Client; namespace KTXM\ProviderImap\Client;
use KTXM\ProviderImap\Client\Command\CommandInterface; use KTXM\ProviderImap\Client\Command\CommandInterface;
use KTXM\ProviderImap\Client\FetchTarget;
interface ClientInterface interface ClientInterface
{ {
@@ -22,11 +21,4 @@ interface ClientInterface
* @return TResult * @return TResult
*/ */
public function perform(CommandInterface $command): mixed; public function perform(CommandInterface $command): mixed;
/**
* Stream the raw bytes of a single IMAP BODY section without buffering.
*
* @return \Generator<string> raw (transfer-encoded) bytes from the socket
*/
public function download(FetchTarget $target, string $section, int $chunkSize = 8192): \Generator;
} }

View File

@@ -89,11 +89,6 @@ final class FetchOptions
return $this->with('BODY[TEXT]'); return $this->with('BODY[TEXT]');
} }
public function withBody(): self
{
return $this->with('BODY[]');
}
public function withBodySection(string $section): self public function withBodySection(string $section): self
{ {
$section = strtoupper(trim($section)); $section = strtoupper(trim($section));

View File

@@ -156,11 +156,6 @@ final class Message
return $this->bodySections; return $this->bodySections;
} }
public function bodyRaw(): ?string
{
return $this->bodySections[''] ?? null;
}
/** /**
* @param array<string, string> $bodySections * @param array<string, string> $bodySections
*/ */

View File

@@ -524,6 +524,9 @@ final class MessageParser
} }
$section = strtoupper(trim($matches[1])); $section = strtoupper(trim($matches[1]));
if ($section === '') {
continue;
}
if (preg_match('/^(\d+(?:\.\d+)*)\.TEXT$/', $section, $partMatches) === 1) { if (preg_match('/^(\d+(?:\.\d+)*)\.TEXT$/', $section, $partMatches) === 1) {
$section = $partMatches[1]; $section = $partMatches[1];

View File

@@ -167,9 +167,8 @@ final class MessagePart
{ {
$data = [ $data = [
'partId' => $this->partId, 'partId' => $this->partId,
'blobId' => $this->partId,
'cId' => $this->contentId,
'type' => $this->mimeType, 'type' => $this->mimeType,
'blobId' => $this->contentId,
'charset' => $this->parameters['charset'] ?? null, 'charset' => $this->parameters['charset'] ?? null,
'name' => $this->parameters['name'] ?? $this->dispositionParameters['filename'] ?? null, 'name' => $this->parameters['name'] ?? $this->dispositionParameters['filename'] ?? null,
'encoding' => $this->encoding, 'encoding' => $this->encoding,

View File

@@ -6,11 +6,9 @@ namespace KTXM\ProviderImap\Client\Protocol;
use Generator; use Generator;
use KTXM\ProviderImap\Client\Command\CommandInterface; use KTXM\ProviderImap\Client\Command\CommandInterface;
use KTXM\ProviderImap\Client\FetchTarget;
use KTXM\ProviderImap\Client\ImapException; use KTXM\ProviderImap\Client\ImapException;
use KTXM\ProviderImap\Client\Protocol\Response\TaggedResponse; use KTXM\ProviderImap\Client\Protocol\Response\TaggedResponse;
use KTXM\ProviderImap\Client\Protocol\Response\UntaggedResponse; use KTXM\ProviderImap\Client\Protocol\Response\UntaggedResponse;
use KTXM\ProviderImap\Client\Protocol\RequestFrame;
use KTXM\ProviderImap\Client\SessionContext; use KTXM\ProviderImap\Client\SessionContext;
use KTXM\ProviderImap\Client\SessionState; use KTXM\ProviderImap\Client\SessionState;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@@ -43,42 +41,10 @@ final class CommandExecutor
$this->writer->write($tag, $frame); $this->writer->write($tag, $frame);
return $command->handle(new ResponseStream(function () use ($tag, $context): Generator { return $command->handle(new ResponseStream(function () use ($tag, $context): Generator {
yield from $this->processPerform($tag, $context); yield from $this->responsesUntilCompletion($tag, $context);
}), $context); }), $context);
} }
/**
* Stream the raw bytes of a single IMAP BODY section without buffering.
*
* Sends a UID FETCH for the given section and yields the literal bytes
* directly from the socket in chunks, never assembling a full string.
* The caller MUST fully exhaust the returned Generator before issuing
* any further IMAP commands.
*
* @return \Generator<string> raw (transfer-encoded) bytes from the socket
*/
public function download(FetchTarget $target, string $section, int $chunkSize, SessionContext $context): \Generator
{
$this->assertState([SessionState::Selected], $context->state(), 'FETCH (download)');
$tag = $this->tags->next();
$this->writer->write($tag, new RequestFrame(sprintf(
'UID FETCH %s (UID BODY[%s])',
$target->sequenceSet()->toCommand(),
$section,
)));
$result = $this->reader->readUntilFetchLiteral($tag);
if ($result === null) {
return; // UID not found or empty FETCH result
}
yield from $this->reader->streamLiteral($result['literalLength'], $chunkSize);
$this->reader->readToEnd($tag);
}
/** /**
* @param list<SessionState> $allowedStates * @param list<SessionState> $allowedStates
*/ */
@@ -97,7 +63,7 @@ final class CommandExecutor
)); ));
} }
private function processPerform(string $tag, SessionContext $context): Generator private function responsesUntilCompletion(string $tag, SessionContext $context): Generator
{ {
while (true) { while (true) {
$response = $this->reader->readResponse(); $response = $this->reader->readResponse();

View File

@@ -42,14 +42,7 @@ final class ProtocolReader
public function readResponse(): ResponseInterface public function readResponse(): ResponseInterface
{ {
$raw = $this->connection->readLine(); $raw = $this->readRawResponse();
while (($literalLength = $this->trailingLiteralLength($raw)) !== null) {
$raw .= $this->connection->readBytes($literalLength);
$raw .= $this->connection->readLine();
}
$raw = $this->trimTrailingLineEnding($raw);
if ($raw === '') { if ($raw === '') {
throw new ImapException('Received empty IMAP response line.'); throw new ImapException('Received empty IMAP response line.');
@@ -92,72 +85,16 @@ final class ProtocolReader
return new TaggedResponse($parts[0], $status, $parts[2] ?? '', $raw); return new TaggedResponse($parts[0], $status, $parts[2] ?? '', $raw);
} }
/** private function readRawResponse(): string
* Read responses until an untagged FETCH response containing a literal marker is found,
* returning the literal byte count WITHOUT consuming the literal bytes from the socket.
* Returns null if the tagged OK/NO/BAD for $tag arrives before any literal is detected.
*
* ⚠️ After a non-null return the literal bytes MUST be consumed (via streamLiteral())
* before any further reads are made on this reader.
*
* @return array{literalLength: int, prefixLine: string}|null
*/
public function readUntilFetchLiteral(string $tag): ?array
{ {
while (true) { $raw = $this->connection->readLine();
$line = $this->connection->readLine();
$trimmed = $this->trimTrailingLineEnding($line);
// Literal marker at the end of the line — stop before consuming the bytes while (($literalLength = $this->trailingLiteralLength($raw)) !== null) {
$literalLength = $this->trailingLiteralLength($line); $raw .= $this->connection->readBytes($literalLength);
if ($literalLength !== null) { $raw .= $this->connection->readLine();
return ['literalLength' => $literalLength, 'prefixLine' => $trimmed];
}
// Tagged completion for our command
if (str_starts_with($trimmed, $tag . ' ')) {
$parts = preg_split('/\s+/', $trimmed, 3) ?: [];
$status = strtoupper($parts[1] ?? '');
if ($status === 'NO' || $status === 'BAD') {
throw new ImapException(sprintf('FETCH command failed: %s', $trimmed));
}
return null; // Tagged OK without ever finding a literal → UID not found
}
// Any other untagged response — discard and continue
} }
}
public function readToEnd(string $tag): TaggedResponse return $this->trimTrailingLineEnding($raw);
{
while (true) {
$response = $this->readResponse();
if ($response instanceof TaggedResponse && $response->tag() === $tag) {
if (!$response->isOk()) {
throw new ImapException(sprintf('FETCH failed: %s', $response->text()));
}
return $response;
}
}
}
/**
* Yield the literal bytes already waiting in the socket as chunks.
* After the generator is fully exhausted this method reads the one trailing
* line that closes the FETCH parenthesised response (e.g. ")\r\n").
*
* Contract: the caller MUST exhaust this generator before issuing any further
* reads on this reader.
*
* @return \Generator<string>
*/
public function streamLiteral(int $length, int $chunkSize = 8192): \Generator
{
yield from $this->connection->readBytesChunked($length, $chunkSize);
// Consume the closing portion of the FETCH parenthesised list (e.g. ")\r\n")
$this->connection->readLine();
} }
private function trailingLiteralLength(string $raw): ?int private function trailingLiteralLength(string $raw): ?int

View File

@@ -20,13 +20,5 @@ interface ConnectionInterface
public function readBytes(int $length): string; public function readBytes(int $length): string;
/**
* Yield the literal payload in chunks without buffering the full content.
* Reads exactly $length bytes from the socket, never crossing the literal boundary.
*
* @return \Generator<string>
*/
public function readBytesChunked(int $length, int $chunkSize = 8192): \Generator;
public function upgradeToTls(): void; public function upgradeToTls(): void;
} }

View File

@@ -135,26 +135,6 @@ final class SocketConnection implements ConnectionInterface
return $buffer; return $buffer;
} }
public function readBytesChunked(int $length, int $chunkSize = 8192): \Generator
{
if ($length < 0) {
throw new ImapException('IMAP socket cannot read a negative number of bytes.');
}
$remaining = $length;
while ($remaining > 0) {
$chunk = fread($this->stream(), min($chunkSize, $remaining));
if ($chunk === false || $chunk === '') {
throw new ImapException('Failed to read literal payload from IMAP socket.');
}
$remaining -= strlen($chunk);
yield $chunk;
}
}
public function upgradeToTls(): void public function upgradeToTls(): void
{ {
$stream = $this->stream(); $stream = $this->stream();

View File

@@ -9,14 +9,12 @@ declare(strict_types=1);
namespace KTXM\ProviderImap\Providers; namespace KTXM\ProviderImap\Providers;
use KTXF\Mail\Object\MessagePartInterface;
/** /**
* Mail Attachment Object * Mail Attachment Object
* *
* @since 1.0.0 * @since 1.0.0
*/ */
class MessageAttachment implements MessagePartInterface { class MessageAttachment implements \KTXF\Mail\Object\MessagePartInterface {
protected MessagePart $_meta; protected MessagePart $_meta;
protected ?string $_contents = null; protected ?string $_contents = null;

View File

@@ -9,8 +9,10 @@ declare(strict_types=1);
namespace KTXM\ProviderImap\Providers; namespace KTXM\ProviderImap\Providers;
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure\MultiPart;
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure\Part;
use Gricob\IMAP\Protocol\Response\Line\Data\Fetch\BodyStructure\SinglePart;
use KTXF\Mail\Object\MessagePartMutableAbstract; use KTXF\Mail\Object\MessagePartMutableAbstract;
use KTXM\ProviderImap\Client\MessagePart as ImapMessagePart;
/** /**
* Mail Message Part Implementation * Mail Message Part Implementation
@@ -23,7 +25,7 @@ class MessagePart extends MessagePartMutableAbstract {
* @param Part $part gricob BodyStructure Part (SinglePart or MultiPart) * @param Part $part gricob BodyStructure Part (SinglePart or MultiPart)
* @param string $partId numeric part identifier (e.g. "1", "1.1", "2") * @param string $partId numeric part identifier (e.g. "1", "1.1", "2")
*/ */
public function fromImap(ImapMessagePart $part, string $partId = '1'): static { public function fromImap(Part $part, string $partId = '1'): static {
$this->data['partId'] = $partId; $this->data['partId'] = $partId;
@@ -102,4 +104,82 @@ class MessagePart extends MessagePartMutableAbstract {
return $this; return $this;
} }
/**
* Convert message part to store array
*/
public function toStore(): array {
$data = $this->data;
if (count($this->parts) > 0) {
$data['subParts'] = [];
foreach ($this->parts as $subPart) {
if ($subPart instanceof MessagePart) {
$data['subParts'][] = $subPart->toStore();
}
}
} else {
$data['subParts'] = null;
}
return $data;
}
/**
* Hydrate message part from store array
*/
public function fromStore(array $data): static {
if (isset($data['subParts']) && is_array($data['subParts'])) {
foreach ($data['subParts'] as $subPart) {
$this->parts[] = (new MessagePart())->fromStore($subPart);
}
unset($data['subParts']);
}
$this->data = $data;
return $this;
}
/**
* Inject decoded body content from a map of IMAP section-ID → raw encoded text.
*
* Walks the MessagePart tree recursively. For each text/* leaf part whose
* partId is present in $sectionMap the raw text is decoded according to the
* part's Content-Transfer-Encoding and converted to UTF-8 before being
* stored in 'content'. Binary parts (images, PDFs, …) are skipped.
*
* @param array<string,string> $sectionMap Keys: IMAP section IDs (e.g. "1", "1.2");
* Values: raw (transfer-encoded) body text
*/
public function injectSections(array $sectionMap): void
{
// MultiPart: recurse into children
if (!empty($this->parts)) {
foreach ($this->parts as $childPart) {
if ($childPart instanceof MessagePart) {
$childPart->injectSections($sectionMap);
}
}
return;
}
// SinglePart: only inject decoded content for text/* MIME types
$type = strtolower($this->data['type'] ?? '');
if (!str_starts_with($type, 'text/')) {
return;
}
$partId = $this->data['partId'] ?? null;
if ($partId === null || !array_key_exists($partId, $sectionMap)) {
return;
}
$raw = $sectionMap[$partId];
$encoding = strtolower($this->data['encoding'] ?? '7bit');
$decoded = match ($encoding) {
'quoted-printable' => quoted_printable_decode($raw),
'base64' => base64_decode($raw, strict: false),
default => $raw, // 7bit, 8bit, binary
};
$charset = $this->data['charset'] ?? 'us-ascii';
$this->data['content'] = MessageProperties::toUtf8($decoded, $charset);
}
} }

View File

@@ -194,7 +194,7 @@ class Provider implements ProviderBaseInterface, ProviderServiceMutateInterface,
// Attempt to authenticate and list mailboxes as a connectivity check // Attempt to authenticate and list mailboxes as a connectivity check
$client = RemoteService::freshClient($service); $client = RemoteService::freshClient($service);
$service = RemoteService::mailService($service, $client); $service = RemoteService::mailService($service, $client);
$mailboxes = iterator_to_array($service->collectionList()); $mailboxes = $service->collectionList();
$latency = (int) round((microtime(true) - $startTime) * 1000); $latency = (int) round((microtime(true) - $startTime) * 1000);
@@ -205,9 +205,36 @@ class Provider implements ProviderBaseInterface, ProviderServiceMutateInterface,
. ' (Latency: ' . $latency . ' ms)', . ' (Latency: ' . $latency . ' ms)',
]; ];
} catch (\Throwable $e) { } catch (\Throwable $e) {
$latency = (int) round((microtime(true) - $startTime) * 1000);
$location = ($service instanceof Service) ? $service->getLocation() : null;
$target = $location
? $location->getEncryption() . '://' . $location->getHost() . ':' . $location->getPort()
: 'unknown host';
// stream_socket_client errors are suppressed with @ in gricob — recover them
$phpError = error_get_last();
$detail = $e->getMessage() !== '' ? $e->getMessage() : ($phpError['message'] ?? '');
if ($detail === '' && $location !== null) {
$host = $location->getHost();
if ($host !== '' && gethostbyname($host) === $host) {
$detail = "hostname '{$host}' could not be resolved";
} else {
$detail = 'connection refused or timed out — check port and encryption settings';
}
} elseif ($detail === '') {
$detail = 'no details — check host, port, and encryption settings';
}
return [ return [
'success' => false, 'success' => false,
'message' => 'Test failed: ' . $e->getMessage(), 'message' => sprintf(
'Connection to %s failed (%s): %s',
$target,
(new \ReflectionClass($e))->getShortName(),
$detail,
),
]; ];
} }
} }

View File

@@ -19,7 +19,6 @@ use KTXF\Mail\Service\ServiceCollectionMutableInterface;
use KTXF\Mail\Service\ServiceEntityMutableInterface; use KTXF\Mail\Service\ServiceEntityMutableInterface;
use KTXF\Mail\Service\ServiceConfigurableInterface; use KTXF\Mail\Service\ServiceConfigurableInterface;
use KTXF\Mail\Service\ServiceMutableInterface; use KTXF\Mail\Service\ServiceMutableInterface;
use KTXF\Resource\BinaryResource;
use KTXF\Resource\Provider\ResourceServiceIdentityInterface; use KTXF\Resource\Provider\ResourceServiceIdentityInterface;
use KTXF\Resource\Provider\ResourceServiceLocationInterface; use KTXF\Resource\Provider\ResourceServiceLocationInterface;
use KTXF\Resource\Delta\Delta; use KTXF\Resource\Delta\Delta;
@@ -42,7 +41,6 @@ use KTXF\Mail\Collection\CollectionRoles;
use KTXF\Mail\Object\MessagePropertiesMutableInterface; use KTXF\Mail\Object\MessagePropertiesMutableInterface;
use KTXF\Resource\Identifier\EntityIdentifierInterface; use KTXF\Resource\Identifier\EntityIdentifierInterface;
use KTXM\ProviderImap\Providers\EntityResource; use KTXM\ProviderImap\Providers\EntityResource;
use KTXM\ProviderImap\Client\Mailbox;
/** /**
* IMAP Mail Service * IMAP Mail Service
@@ -63,23 +61,20 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
private array $auxiliary = []; private array $auxiliary = [];
private array $serviceAbilities = [ private array $serviceAbilities = [
self::CAPABILITY_COLLECTION_LIST => true, self::CAPABILITY_COLLECTION_LIST => true,
self::CAPABILITY_COLLECTION_LIST_FILTER => [ self::CAPABILITY_COLLECTION_LIST_FILTER => [
self::CAPABILITY_COLLECTION_FILTER_LABEL => 's:128:256:256', self::CAPABILITY_COLLECTION_FILTER_LABEL => 's:128:256:256',
self::CAPABILITY_COLLECTION_FILTER_ROLE => 's:32:1:1', self::CAPABILITY_COLLECTION_FILTER_ROLE => 's:32:1:1',
self::CAPABILITY_COLLECTION_FILTER_SUBSCRIBED => 'b:0:1:1', self::CAPABILITY_COLLECTION_FILTER_SUBSCRIBED => 'b:0:1:1',
], ],
self::CAPABILITY_COLLECTION_LIST_SORT => [ self::CAPABILITY_COLLECTION_LIST_SORT => [],
self::CAPABILITY_COLLECTION_SORT_LABEL,
self::CAPABILITY_COLLECTION_SORT_RANK,
],
self::CAPABILITY_COLLECTION_FETCH => true,
self::CAPABILITY_COLLECTION_EXTANT => true, self::CAPABILITY_COLLECTION_EXTANT => true,
self::CAPABILITY_COLLECTION_FETCH => true,
self::CAPABILITY_COLLECTION_CREATE => true, self::CAPABILITY_COLLECTION_CREATE => true,
self::CAPABILITY_COLLECTION_UPDATE => true, self::CAPABILITY_COLLECTION_UPDATE => true,
self::CAPABILITY_COLLECTION_DELETE => true, self::CAPABILITY_COLLECTION_DELETE => true,
self::CAPABILITY_COLLECTION_MOVE => true, self::CAPABILITY_COLLECTION_MOVE => true,
self::CAPABILITY_ENTITY_LIST => true, self::CAPABILITY_ENTITY_LIST => true,
self::CAPABILITY_ENTITY_LIST_FILTER => [ self::CAPABILITY_ENTITY_LIST_FILTER => [
self::CAPABILITY_ENTITY_FILTER_FROM => 's:100:256:256', self::CAPABILITY_ENTITY_FILTER_FROM => 's:100:256:256',
self::CAPABILITY_ENTITY_FILTER_TO => 's:100:256:256', self::CAPABILITY_ENTITY_FILTER_TO => 's:100:256:256',
@@ -90,62 +85,56 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
self::CAPABILITY_ENTITY_FILTER_SIZE_MIN => 'i:0:16:16', self::CAPABILITY_ENTITY_FILTER_SIZE_MIN => 'i:0:16:16',
self::CAPABILITY_ENTITY_FILTER_SIZE_MAX => 'i:0:32:32', self::CAPABILITY_ENTITY_FILTER_SIZE_MAX => 'i:0:32:32',
], ],
self::CAPABILITY_ENTITY_LIST_SORT => [ self::CAPABILITY_ENTITY_LIST_SORT => [],
self::CAPABILITY_ENTITY_SORT_FROM, self::CAPABILITY_ENTITY_LIST_RANGE => ['tally' => ['absolute', 'relative']],
self::CAPABILITY_ENTITY_SORT_TO, self::CAPABILITY_ENTITY_EXTANT => true,
self::CAPABILITY_ENTITY_SORT_SUBJECT, self::CAPABILITY_ENTITY_FETCH => true,
self::CAPABILITY_ENTITY_SORT_DATE_RECEIVED, self::CAPABILITY_ENTITY_CREATE => false,
self::CAPABILITY_ENTITY_SORT_DATE_SENT, self::CAPABILITY_ENTITY_MODIFY => false,
self::CAPABILITY_ENTITY_SORT_SIZE, self::CAPABILITY_ENTITY_DELETE => true,
], self::CAPABILITY_ENTITY_MOVE => true,
self::CAPABILITY_ENTITY_LIST_RANGE => [ self::CAPABILITY_ENTITY_COPY => false,
'tally' => ['absolute', 'relative']
],
self::CAPABILITY_ENTITY_FETCH => true,
self::CAPABILITY_ENTITY_EXTANT => true,
self::CAPABILITY_ENTITY_CREATE => false,
self::CAPABILITY_ENTITY_MODIFY => false,
self::CAPABILITY_ENTITY_PATCH => true,
self::CAPABILITY_ENTITY_DELETE => true,
self::CAPABILITY_ENTITY_MOVE => true,
self::CAPABILITY_ENTITY_COPY => false,
]; ];
private RemoteMailService $remoteService; private RemoteMailService $mailService;
public function __construct() {} public function __construct() {}
// ── Lazy initialisation ───────────────────────────────────────────────────
private function initialize(): void private function initialize(): void
{ {
if (!isset($this->remoteService)) { if (!isset($this->mailService)) {
$wrapper = RemoteService::freshClient($this); $wrapper = RemoteService::freshClient($this);
$this->remoteService = RemoteService::mailService($this, $wrapper); $this->mailService = RemoteService::mailService($this, $wrapper);
} }
} }
// ── Store (MongoDB persistence) ───────────────────────────────────────────
public function toStore(): array public function toStore(): array
{ {
return array_filter([ return array_filter([
'tid' => $this->serviceTenantId, 'tid' => $this->serviceTenantId,
'uid' => $this->serviceUserId, 'uid' => $this->serviceUserId,
'sid' => $this->serviceIdentifier, 'sid' => $this->serviceIdentifier,
'enabled' => $this->serviceEnabled, 'label' => $this->serviceLabel,
'label' => $this->serviceLabel, 'enabled' => $this->serviceEnabled,
'primaryAddress' => $this->primaryAddress, 'primaryAddress' => $this->primaryAddress,
'secondaryAddresses'=> $this->secondaryAddresses, 'secondaryAddresses'=> $this->secondaryAddresses,
'location' => $this->location?->toStore(), 'location' => $this->location?->toStore(),
'identity' => $this->identity?->toStore(), 'identity' => $this->identity?->toStore(),
'auxiliary' => $this->auxiliary, 'auxiliary' => $this->auxiliary,
], fn($v) => $v !== null); ], fn($v) => $v !== null);
} }
public function fromStore(array $data): static public function fromStore(array $data): static
{ {
$this->serviceTenantId = $data['tid'] ?? null; $this->serviceTenantId = $data['tid'] ?? null;
$this->serviceUserId = $data['uid'] ?? null; $this->serviceUserId = $data['uid'] ?? null;
$this->serviceIdentifier = $data['sid'] ?? null; $this->serviceIdentifier = $data['sid'] ?? null;
$this->serviceLabel = $data['label'] ?? ''; $this->serviceLabel = $data['label'] ?? '';
$this->serviceEnabled = $data['enabled'] ?? false; $this->serviceEnabled = $data['enabled'] ?? false;
if (isset($data['primaryAddress'])) { if (isset($data['primaryAddress'])) {
$this->primaryAddress = $data['primaryAddress']; $this->primaryAddress = $data['primaryAddress'];
@@ -166,20 +155,22 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
return $this; return $this;
} }
// ── JSON ──────────────────────────────────────────────────────────────────
public function jsonSerialize(): array public function jsonSerialize(): array
{ {
return array_filter([ return array_filter([
self::JSON_PROPERTY_TYPE => self::JSON_TYPE, self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
self::JSON_PROPERTY_PROVIDER => self::PROVIDER_IDENTIFIER, self::JSON_PROPERTY_PROVIDER => self::PROVIDER_IDENTIFIER,
self::JSON_PROPERTY_IDENTIFIER => $this->serviceIdentifier, self::JSON_PROPERTY_IDENTIFIER => $this->serviceIdentifier,
self::JSON_PROPERTY_LABEL => $this->serviceLabel, self::JSON_PROPERTY_LABEL => $this->serviceLabel,
self::JSON_PROPERTY_ENABLED => $this->serviceEnabled, self::JSON_PROPERTY_ENABLED => $this->serviceEnabled,
self::JSON_PROPERTY_CAPABILITIES => $this->serviceAbilities, self::JSON_PROPERTY_CAPABILITIES => $this->serviceAbilities,
self::JSON_PROPERTY_PRIMARY_ADDRESS => $this->primaryAddress, self::JSON_PROPERTY_PRIMARY_ADDRESS => $this->primaryAddress,
self::JSON_PROPERTY_SECONDARY_ADDRESSES => $this->secondaryAddresses, self::JSON_PROPERTY_SECONDARY_ADDRESSES => $this->secondaryAddresses,
self::JSON_PROPERTY_LOCATION => $this->location?->jsonSerialize(), self::JSON_PROPERTY_LOCATION => $this->location?->jsonSerialize(),
self::JSON_PROPERTY_IDENTITY => $this->identity?->jsonSerialize(), self::JSON_PROPERTY_IDENTITY => $this->identity?->jsonSerialize(),
self::JSON_PROPERTY_AUXILIARY => $this->auxiliary, self::JSON_PROPERTY_AUXILIARY => $this->auxiliary,
], fn($v) => $v !== null); ], fn($v) => $v !== null);
} }
@@ -189,12 +180,12 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
$data = json_decode($data, true, 512, JSON_THROW_ON_ERROR); $data = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
} }
if (isset($data[self::JSON_PROPERTY_ENABLED])) {
$this->setEnabled($data[self::JSON_PROPERTY_ENABLED]);
}
if (isset($data[self::JSON_PROPERTY_LABEL])) { if (isset($data[self::JSON_PROPERTY_LABEL])) {
$this->setLabel($data[self::JSON_PROPERTY_LABEL]); $this->setLabel($data[self::JSON_PROPERTY_LABEL]);
} }
if (isset($data[self::JSON_PROPERTY_ENABLED])) {
$this->setEnabled($data[self::JSON_PROPERTY_ENABLED]);
}
if (isset($data[self::JSON_PROPERTY_LOCATION])) { if (isset($data[self::JSON_PROPERTY_LOCATION])) {
$this->setLocation($this->freshLocation(null, $data[self::JSON_PROPERTY_LOCATION])); $this->setLocation($this->freshLocation(null, $data[self::JSON_PROPERTY_LOCATION]));
} }
@@ -217,6 +208,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
return $this; return $this;
} }
// ── ServiceBaseInterface ──────────────────────────────────────────────────
public function capable(string $value): bool public function capable(string $value): bool
{ {
return isset($this->serviceAbilities[$value]); return isset($this->serviceAbilities[$value]);
@@ -237,6 +230,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
return $this->serviceIdentifier; return $this->serviceIdentifier;
} }
// ── ServiceMutableInterface ───────────────────────────────────────────────
public function getLabel(): ?string public function getLabel(): ?string
{ {
return $this->serviceLabel; return $this->serviceLabel;
@@ -299,6 +294,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
} }
// ── ServiceConfigurableInterface ──────────────────────────────────────────
public function getLocation(): ServiceLocation public function getLocation(): ServiceLocation
{ {
return $this->location; return $this->location;
@@ -357,13 +354,15 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
return $this; return $this;
} }
// ── Collection operations ─────────────────────────────────────────────────
public function collectionList(string|int|null $location, ?IFilter $filter = null, ?ISort $sort = null): array public function collectionList(string|int|null $location, ?IFilter $filter = null, ?ISort $sort = null): array
{ {
$this->initialize(); $this->initialize();
$list = []; $list = [];
foreach ($this->remoteService->collectionList($location, $filter, $sort) as $mailbox) { foreach ($this->mailService->collectionList($location, $filter, $sort) as $mailbox) {
$resource = $this->collectionFresh(); $resource = $this->collectionFresh();
$resource->fromImap($mailbox); $resource->fromImap($mailbox);
$list[$mailbox->name()] = $resource; $list[$mailbox->name()] = $resource;
@@ -390,7 +389,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
foreach ($identifiers as $identifier) { foreach ($identifiers as $identifier) {
$key = (string) $identifier; $key = (string) $identifier;
$result = $this->remoteService->collectionFetch($key); $result = $this->mailService->collectionFetch($key);
$list[$key] = $result !== false; $list[$key] = $result !== false;
} }
@@ -398,11 +397,11 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
return $list; return $list;
} }
public function collectionFetch(string|int $identifier): ?CollectionResource public function collectionFetch(string|int $identifier): ?CollectionBaseInterface
{ {
$this->initialize(); $this->initialize();
$mailbox = $this->remoteService->collectionFetch((string) $identifier); $mailbox = $this->mailService->collectionFetch((string) $identifier);
if ($mailbox === null) { if ($mailbox === null) {
return null; return null;
} }
@@ -431,13 +430,13 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
if ($target !== null) { if ($target !== null) {
$path = $target->collection(); $path = $target->collection();
// Determine the hierarchy delimiter from an existing mailbox, default to '/' // Determine the hierarchy delimiter from an existing mailbox, default to '/'
$mailboxes = iterator_to_array($this->remoteService->collectionList(null, null, null, '')); $mailboxes = iterator_to_array($this->mailService->collectionList(null, null, null, ''));
$rootMailbox = $mailboxes === [] ? null : reset($mailboxes); $rootMailbox = $mailboxes === [] ? null : reset($mailboxes);
$delimiter = $rootMailbox === false ? '/' : ($rootMailbox?->delimiter() ?? '/'); $delimiter = $rootMailbox === false ? '/' : ($rootMailbox?->delimiter() ?? '/');
$label = rtrim((string) $path, $delimiter) . $delimiter . ltrim($label, $delimiter); $label = rtrim((string) $path, $delimiter) . $delimiter . ltrim($label, $delimiter);
} }
$mailbox = $this->remoteService->collectionCreate($label); $mailbox = $this->mailService->collectionCreate($label);
$collection = $this->collectionFresh(); $collection = $this->collectionFresh();
$collection->fromImap($mailbox, ['delimiter' => $delimiter ?? null]); $collection->fromImap($mailbox, ['delimiter' => $delimiter ?? null]);
@@ -457,7 +456,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
// In IMAP, "update" = rename to the new label // In IMAP, "update" = rename to the new label
$oldPath = (string) $target->collection(); $oldPath = (string) $target->collection();
$newName = $properties->getLabel(); $newName = $properties->getLabel();
$mailbox = $this->remoteService->collectionRename($oldPath, $newName); $mailbox = $this->mailService->collectionRename($oldPath, $newName);
$collection = $this->collectionFresh(); $collection = $this->collectionFresh();
$collection->fromImap($mailbox); $collection->fromImap($mailbox);
@@ -477,14 +476,14 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
// Move to target collection (e.g. Trash) instead of deleting // Move to target collection (e.g. Trash) instead of deleting
if ($deleteMode === 'soft' && $deleteTarget !== null) { if ($deleteMode === 'soft' && $deleteTarget !== null) {
return $this->collectionMove(new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget), $target); return $this->collectionMove($target, new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget));
} }
if ($deleteMode === 'soft' && $deleteTarget === null) { if ($deleteMode === 'soft' && $deleteTarget === null) {
$filter = $this->collectionListFilter(); $filter = $this->collectionListFilter();
$filter->condition('role', CollectionRoles::Trash->value); $filter->condition('role', CollectionRoles::Trash->value);
$mailboxes = iterator_to_array($this->remoteService->collectionList(null, $filter, null)); $mailboxes = iterator_to_array($this->mailService->collectionList(null, $filter, null));
if (empty($mailboxes)) { if (empty($mailboxes)) {
throw new \RuntimeException('No Trash collection configured or found for deletion'); throw new \RuntimeException('No Trash collection configured or found for deletion');
} }
@@ -500,7 +499,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
$result = match ($deleteMode) { $result = match ($deleteMode) {
'soft' => $this->collectionMove(new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget), $target), 'soft' => $this->collectionMove(new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget), $target),
'hard' => $this->remoteService->collectionDestroy((string) $target->collection()), 'hard' => $this->mailService->collectionDestroy((string) $target->collection()),
}; };
return $result; return $result;
} }
@@ -509,8 +508,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
{ {
$this->initialize(); $this->initialize();
$sourceMailbox = $this->remoteService->collectionFetch((string) $source->collection()); $sourceMailbox = $this->mailService->collectionFetch((string) $source->collection());
$targetMailbox = $this->remoteService->collectionFetch((string) $target->collection()); $targetMailbox = $this->mailService->collectionFetch((string) $target->collection());
if ($sourceMailbox === null) { if ($sourceMailbox === null) {
throw new \RuntimeException('Source collection not found for move operation'); throw new \RuntimeException('Source collection not found for move operation');
} }
@@ -522,17 +521,16 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
$targetDelimiter = $targetMailbox->delimiter() ?? '/'; $targetDelimiter = $targetMailbox->delimiter() ?? '/';
$extantPath = $sourceMailbox->name(); $extantPath = $sourceMailbox->name();
$extantPathLeafs = explode($sourceDelimiter, rtrim($extantPath, $sourceDelimiter)); $freshPath = rtrim($targetMailbox->name(), $targetDelimiter) . $targetDelimiter . end(explode($sourceDelimiter, $extantPath));
$mutatedMailbox = $this->mailService->collectionRename($extantPath, $freshPath);
$freshPath = rtrim($targetMailbox->name(), $targetDelimiter) . $targetDelimiter . end($extantPathLeafs);
$mutatedMailbox = $this->remoteService->collectionRename($extantPath, $freshPath);
$collection = $this->collectionFresh(); $collection = $this->collectionFresh();
$collection->fromImap($mutatedMailbox, ['delimiter' => $targetDelimiter]); $collection->fromImap($mutatedMailbox, ['delimiter' => $targetDelimiter]);
return $collection; return $collection;
} }
// ── Entity operations ─────────────────────────────────────────────────────
public function entityListBulk(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): array public function entityListBulk(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): array
{ {
return iterator_to_array($this->entityListStream((string) $collection, $filter, $sort, $range), true); return iterator_to_array($this->entityListStream((string) $collection, $filter, $sort, $range), true);
@@ -542,7 +540,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
{ {
$this->initialize(); $this->initialize();
foreach ($this->remoteService->entityList((string) $collection, $filter, $sort, $range) as $identifier => $message) { foreach ($this->mailService->entityList((string) $collection, $filter, $sort, $range) as $identifier => $message) {
$resource = $this->entityFresh(); $resource = $this->entityFresh();
$resource->fromImap($message, $collection); $resource->fromImap($message, $collection);
yield $resource->urn() => $resource; yield $resource->urn() => $resource;
@@ -580,7 +578,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
foreach ($identifiers as $collection => $entities) { foreach ($identifiers as $collection => $entities) {
$uids = array_keys($entities); $uids = array_keys($entities);
foreach ($this->remoteService->entityFetch((string) $collection, ...$uids) as $uid => $message) { foreach ($this->mailService->entityFetch((string) $collection, ...$uids) as $uid => $message) {
$resource = $this->entityFresh(); $resource = $this->entityFresh();
$resource->fromImap($message, $collection); $resource->fromImap($message, $collection);
yield $resource->urn() => $resource; yield $resource->urn() => $resource;
@@ -588,16 +586,6 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
} }
} }
public function entityDownload(EntityIdentifierInterface $target, array|null $part): BinaryResource {
$this->initialize();
$collection = $target->collection();
$uid = (int) $target->entity();
$partId = isset($part['partId']) ? (string) $part['partId'] : null;
return $this->remoteService->entityDownload($collection, $uid, $partId);
}
public function entityDelta(string|int $collection, string $signature, string $detail = 'ids'): Delta public function entityDelta(string|int $collection, string $signature, string $detail = 'ids'): Delta
{ {
return new Delta(signature: $signature); return new Delta(signature: $signature);
@@ -607,7 +595,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
{ {
$this->initialize(); $this->initialize();
$allUids = $this->remoteService->entityList((string) $collection); $allUids = $this->mailService->entityList((string) $collection);
$uidSet = array_flip($allUids); // int[] → [uid => index] $uidSet = array_flip($allUids); // int[] → [uid => index]
$extant = []; $extant = [];
foreach ($identifiers as $id) { foreach ($identifiers as $id) {
@@ -643,18 +631,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
foreach ($targets as $targetCollection => $targetIdentifiers) { foreach ($targets as $targetCollection => $targetIdentifiers) {
$uids = array_keys($targetIdentifiers); $uids = array_keys($targetIdentifiers);
$flagsAdd = [];
$flagsRemove = [];
foreach ($properties->getFlags() as $flag => $value) { $mutations = $this->mailService->entityPatch($targetCollection, $properties, ...$uids);
if ($value === true) {
$flagsAdd[] = $flag;
} elseif ($value === false) {
$flagsRemove[] = $flag;
}
}
$mutations = $this->remoteService->entityPatch($targetCollection, $flagsAdd, $flagsRemove, ...$uids);
foreach ($uids as $uid) { foreach ($uids as $uid) {
$list[(string)$targetIdentifiers[$uid]] = ['disposition' => 'patched']; $list[(string)$targetIdentifiers[$uid]] = ['disposition' => 'patched'];
@@ -685,18 +663,17 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
$filter = $this->collectionListFilter(); $filter = $this->collectionListFilter();
$filter->condition('role', CollectionRoles::Trash->value); $filter->condition('role', CollectionRoles::Trash->value);
/** @var Mailbox[] $mailboxes */ $mailboxes = iterator_to_array($this->mailService->collectionList(null, $filter, null));
$mailboxes = iterator_to_array($this->remoteService->collectionList(null, $filter, null));
if (empty($mailboxes)) { if (empty($mailboxes)) {
throw new \RuntimeException('No Trash collection configured or found for deletion'); throw new \RuntimeException('No Trash collection configured or found for deletion');
} }
$targetMailbox = reset($mailboxes); $rootMailbox = reset($mailboxes);
if ($targetMailbox === false) { if ($rootMailbox === false) {
throw new \RuntimeException('No Trash collection configured or found for deletion'); throw new \RuntimeException('No Trash collection configured or found for deletion');
} }
$deleteTargetNative = $targetMailbox->name(); $deleteTargetNative = $rootMailbox->name();
$deleteTargetIdentifier = new CollectionIdentifier($this->provider(), (string) $this->identifier(), $deleteTargetNative); $deleteTargetIdentifier = new CollectionIdentifier($this->provider(), (string) $this->identifier(), $deleteTargetNative);
} else { } else {
$deleteTargetNative = $deleteTarget; $deleteTargetNative = $deleteTarget;
@@ -718,8 +695,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
$uids = array_keys($sourceEntities); $uids = array_keys($sourceEntities);
$mutations = match ($deleteMode) { $mutations = match ($deleteMode) {
'soft' => $this->remoteService->entityMove($deleteTargetNative, $sourceCollection, ...$uids), 'soft' => $this->mailService->entityMove($deleteTargetNative, $sourceCollection, ...$uids),
'hard' => $this->remoteService->entityDestroy($sourceCollection, ...$uids), 'hard' => $this->mailService->entityDestroy($sourceCollection, ...$uids),
}; };
foreach ($uids as $uid) { foreach ($uids as $uid) {
@@ -735,6 +712,39 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
return $list; return $list;
} }
public function entityCopy(CollectionIdentifier $target, EntityIdentifier ...$sources): array
{
// validate target belongs to this service
if ($target->provider() !== $this->provider() || $target->service() !== $this->identifier()) {
throw new \InvalidArgumentException('Target collection does not belong to this service: ' . $target);
}
// validate identifiers and group by collection
$sources = $this->groupEntitiesByCollection(...$sources);
// copy entities on remote store and construct result map
$this->initialize();
$list = [];
foreach ($sources as $sourceCollection => $sourceEntities) {
$uids = array_keys($sourceEntities);
$mutations = $this->mailService->entityCopy($target->collection(), $sourceCollection, ...$uids);
foreach ($uids as $uid) {
$mutatedUid = $mutations[$uid] ?? null;
$list[(string)$sourceEntities[$uid]] = [
'disposition' => $mutatedUid !== null ? 'copied' : 'error',
'destination' => $target,
'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $target->collection(), $mutatedUid) : null,
];
}
}
return $list;
}
public function entityMove(CollectionIdentifier $target, EntityIdentifier ...$sources): array public function entityMove(CollectionIdentifier $target, EntityIdentifier ...$sources): array
{ {
// validate target belongs to this service // validate target belongs to this service
@@ -753,7 +763,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
foreach ($sources as $sourceCollection => $sourceEntities) { foreach ($sources as $sourceCollection => $sourceEntities) {
$uids = array_keys($sourceEntities); $uids = array_keys($sourceEntities);
$mutations = $this->remoteService->entityMove($target->collection(), $sourceCollection, ...$uids); $mutations = $this->mailService->entityMove($target->collection(), $sourceCollection, ...$uids);
foreach ($uids as $uid) { foreach ($uids as $uid) {
$mutatedUid = $mutations[$uid] ?? null; $mutatedUid = $mutations[$uid] ?? null;
@@ -762,40 +772,6 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
'destination' => $target, 'destination' => $target,
'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $target->collection(), $mutatedUid) : null, 'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $target->collection(), $mutatedUid) : null,
]; ];
unset($sourceEntities[$uid]);
}
}
return $list;
}
public function entityCopy(CollectionIdentifier $target, EntityIdentifier ...$sources): array
{
// validate target belongs to this service
if ($target->provider() !== $this->provider() || $target->service() !== $this->identifier()) {
throw new \InvalidArgumentException('Target collection does not belong to this service: ' . $target);
}
// validate identifiers and group by collection
$sources = $this->groupEntitiesByCollection(...$sources);
// copy entities on remote store and construct result map
$this->initialize();
$list = [];
foreach ($sources as $sourceCollection => $sourceEntities) {
$uids = array_keys($sourceEntities);
$mutations = $this->remoteService->entityCopy($target->collection(), $sourceCollection, ...$uids);
foreach ($uids as $uid) {
$mutatedUid = $mutations[$uid] ?? null;
$list[(string)$sourceEntities[$uid]] = [
'disposition' => $mutatedUid !== null ? 'copied' : 'error',
'destination' => $target,
'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $target->collection(), $mutatedUid) : null,
];
} }
} }

View File

@@ -13,7 +13,6 @@ use DateTimeImmutable;
use Generator; use Generator;
use KTXM\ProviderImap\Client\Client; use KTXM\ProviderImap\Client\Client;
use KTXM\ProviderImap\Client\Command\FetchManyCommand; use KTXM\ProviderImap\Client\Command\FetchManyCommand;
use KTXM\ProviderImap\Client\Command\FetchOneCommand;
use KTXM\ProviderImap\Client\Command\ExpungeCommand; use KTXM\ProviderImap\Client\Command\ExpungeCommand;
use KTXM\ProviderImap\Client\Command\ListCommand; use KTXM\ProviderImap\Client\Command\ListCommand;
use KTXM\ProviderImap\Client\Command\SearchCommand; use KTXM\ProviderImap\Client\Command\SearchCommand;
@@ -46,7 +45,8 @@ use KTXF\Resource\Range\IRangeTally;
use KTXF\Resource\Range\RangeAnchorType; use KTXF\Resource\Range\RangeAnchorType;
use KTXF\Resource\Range\RangeTally; use KTXF\Resource\Range\RangeTally;
use KTXF\Resource\Sort\ISort; use KTXF\Resource\Sort\ISort;
use KTXF\Resource\BinaryResource; use KTXM\ProviderImap\Providers\CollectionResource;
use KTXM\ProviderImap\Providers\EntityResource;
/** /**
* IMAP Remote Mail Service * IMAP Remote Mail Service
@@ -295,116 +295,6 @@ class RemoteMailService
} }
} }
/**
* Stream the raw bytes of a message or a specific MIME part without buffering.
*
* When $partId is given, first fetches BODYSTRUCTURE to determine the
* correct filename and MIME type, then starts the streaming body fetch.
*
* @param string $collection Mailbox name
* @param int $uid Message UID
* @param string|null $partId MIME section (e.g. '1', '1.2'); null = full RFC 822
*/
public function entityDownload(string $collection, int $uid, ?string $partId = null): BinaryResource
{
$this->client->perform(new SelectCommand($collection, true));
$encoding = null;
if ($partId === null) {
$filename = 'message.eml';
$mimeType = 'message/rfc822';
} else {
// Fetch BODYSTRUCTURE first to determine metadata (no body bytes transferred)
$message = $this->client->perform(new FetchOneCommand(
FetchTarget::uid(SequenceSet::items($uid)),
FetchOptions::of('BODYSTRUCTURE'),
));
$bodyStructure = $message->bodyStructure();
$part = $bodyStructure !== null ? $this->findBodyPart($bodyStructure, $partId) : null;
$mimeType = $part?->mimeType() ?? 'application/octet-stream';
$partData = $part?->toArray() ?? [];
$filename = isset($partData['name']) && $partData['name'] !== ''
? $partData['name']
: "attachment-{$partId}";
$encoding = $part?->encoding();
}
// Start download stream
$stream = $this->decodeStream(
$this->client->download(FetchTarget::uid(SequenceSet::items($uid)), $partId ?? ''),
$encoding
);
return new BinaryResource($filename, $mimeType, $stream);
}
private function findBodyPart(MessagePart $root, string $partId): ?MessagePart
{
if ($root->partId() === $partId) {
return $root;
}
foreach ($root->parts() as $child) {
$found = $this->findBodyPart($child, $partId);
if ($found !== null) {
return $found;
}
}
return null;
}
/**
* Wraps a raw IMAP body stream with a transfer-encoding decoder.
*
* IMAP BODY[n] literals are delivered in the transfer encoding declared
* by BODYSTRUCTURE (typically base64 or quoted-printable for attachments).
* 7bit / 8bit / binary sections pass through unchanged.
*/
private function decodeStream(\Generator $stream, ?string $encoding): \Generator
{
return match (strtolower($encoding ?? '')) {
'base64' => $this->decodeBase64Stream($stream),
'quoted-printable' => $this->decodeQpStream($stream),
default => $stream,
};
}
private function decodeBase64Stream(\Generator $source): \Generator
{
$buffer = '';
foreach ($source as $chunk) {
// IMAP folds base64 at 76 chars with CRLF — strip all whitespace
$buffer .= preg_replace('/\s+/', '', $chunk);
// Decode complete 4-character groups; keep any partial tail
$remainder = strlen($buffer) % 4;
$complete = strlen($buffer) - $remainder;
if ($complete > 0) {
yield base64_decode(substr($buffer, 0, $complete), true);
$buffer = substr($buffer, $complete);
}
}
// Flush remainder (handles padded or stripped trailing '=')
if ($buffer !== '') {
yield base64_decode($buffer, true);
}
}
private function decodeQpStream(\Generator $source): \Generator
{
// Buffer until we have complete lines so soft-line-breaks are intact
$buffer = '';
foreach ($source as $chunk) {
$buffer .= $chunk;
while (($pos = strpos($buffer, "\n")) !== false) {
yield quoted_printable_decode(substr($buffer, 0, $pos + 1));
$buffer = substr($buffer, $pos + 1);
}
}
if ($buffer !== '') {
yield quoted_printable_decode($buffer);
}
}
/** /**
* Append a raw RFC 822 message to a mailbox and return the assigned UID. * Append a raw RFC 822 message to a mailbox and return the assigned UID.
* *
@@ -463,9 +353,6 @@ class RemoteMailService
$this->client->perform(new SelectCommand($collection, false)); $this->client->perform(new SelectCommand($collection, false));
$flagsToAdd = $this->normalizeFlags($flagsToAdd);
$flagsToRemove = $this->normalizeFlags($flagsToRemove);
if (!empty($flagsToAdd)) { if (!empty($flagsToAdd)) {
$this->client->perform(new StoreCommand( $this->client->perform(new StoreCommand(
FetchTarget::uid(SequenceSet::items(...array_values($uids))), FetchTarget::uid(SequenceSet::items(...array_values($uids))),
@@ -535,36 +422,6 @@ class RemoteMailService
} }
public function entityCopy(string $targetCollection, string $sourceCollection, int ...$uids): array
{
if (empty($uids)) {
return [];
}
$this->client->perform(new SelectCommand($sourceCollection, false));
$response = $this->client->perform(new CopyCommand(
FetchTarget::uid(SequenceSet::items(...array_values($uids))),
$targetCollection,
));
if (!$response->isOk()) {
throw new ImapException('Failed to copy messages: ' . implode(', ', $response->responseCodes()));
}
// construct operation result as a map of source UID to boolean or destination UID, depending on server support
$map = $response->copyUidMap();
if ($map === []) {
$result = array_fill_keys(array_map('strval', $uids), true);
} else {
$result = array_fill_keys(array_map('strval', $uids), false);
foreach ($uids as $uid) {
$result[$uid] = $map[$uid] ?? false;
}
}
return $result;
}
private function buildEntitySearchCriteria(?IFilter $filter): SearchCriteriaBuilder private function buildEntitySearchCriteria(?IFilter $filter): SearchCriteriaBuilder
{ {
if ($filter === null || $filter->conditions() === []) { if ($filter === null || $filter->conditions() === []) {
@@ -1012,23 +869,4 @@ class RemoteMailService
return CollectionRoles::None->value; return CollectionRoles::None->value;
} }
private function normalizeFlags(array $flags): array
{
$map = [
'read' => '\\Seen',
'answered' => '\\Answered',
'flagged' => '\\Flagged',
'deleted' => '\\Deleted',
'draft' => '\\Draft',
];
$normalized = [];
foreach ($flags as $flag) {
$flag = strtolower(trim($flag));
if (isset($map[$flag])) {
$normalized[] = $map[$flag];
}
}
return $normalized;
}
} }

20
package-lock.json generated
View File

@@ -701,9 +701,9 @@
} }
}, },
"node_modules/@vue/language-core": { "node_modules/@vue/language-core": {
"version": "3.3.0", "version": "3.2.9",
"resolved": "https://registry.npmjs.org/@vue/language-core/-/language-core-3.3.0.tgz", "resolved": "https://registry.npmjs.org/@vue/language-core/-/language-core-3.2.9.tgz",
"integrity": "sha512-EyUxq1b8Yoxk6hQ6X33BIRnfFLb9Rbm9w/8G8y6uMxlQu7CW7yy9JS/z54xSpIvBvVWX6Lt5v1aBGwmrqD4aJw==", "integrity": "sha512-ie0ojt/0fU/GfIogh+zgHbaYRPlt9S+cLOxcWwF7nTSFh897BVgnFKL2byT4kpp1mlqYWZ2psGwSniyE2xsxYw==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
@@ -1793,14 +1793,14 @@
} }
}, },
"node_modules/vue-tsc": { "node_modules/vue-tsc": {
"version": "3.3.0", "version": "3.2.9",
"resolved": "https://registry.npmjs.org/vue-tsc/-/vue-tsc-3.3.0.tgz", "resolved": "https://registry.npmjs.org/vue-tsc/-/vue-tsc-3.2.9.tgz",
"integrity": "sha512-kY8RcoTOENASi0P1GLPvJgA2+hoGF+t8We1UGgmnAb1r/GjTUMSE3zz+WGfjPORZNnBHdAt67sVPhBLXWunkeg==", "integrity": "sha512-qm8/nbo+9eZc1SCndm9wT+gq23pM+wRIdHY0wjm83B3lIginHTwcdrLUyTrKjDWXbMVNjKegNrnymhpdqnCL3A==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@volar/typescript": "2.4.28", "@volar/typescript": "2.4.28",
"@vue/language-core": "3.3.0" "@vue/language-core": "3.2.9"
}, },
"bin": { "bin": {
"vue-tsc": "bin/vue-tsc.js" "vue-tsc": "bin/vue-tsc.js"
@@ -1810,9 +1810,9 @@
} }
}, },
"node_modules/vuetify": { "node_modules/vuetify": {
"version": "4.0.8", "version": "4.0.7",
"resolved": "https://registry.npmjs.org/vuetify/-/vuetify-4.0.8.tgz", "resolved": "https://registry.npmjs.org/vuetify/-/vuetify-4.0.7.tgz",
"integrity": "sha512-tde1nicQ0fUxZ/drEW7oILArkCYhDqJVTVOaaLQdAzuDNX1FwqYfAd5eeJ06lYDSLGlf8anR3iFlmZaYs629hQ==", "integrity": "sha512-SV+YJkBmudY3s9qfZO2ZGUsrD0TDQU8pMBH1ERga9AEyjGvioZuXXh93V9wHxXJphvqRC2NW10Nt2lW9IZQcPw==",
"license": "MIT", "license": "MIT",
"funding": { "funding": {
"type": "github", "type": "github",