* SPDX-License-Identifier: AGPL-3.0-or-later */ namespace KTXM\ProviderImap\Providers; use Generator; use KTXF\Mail\Collection\CollectionBaseInterface; use KTXF\Mail\Collection\CollectionMutableInterface; use KTXF\Mail\Collection\CollectionPropertiesBaseInterface; use KTXF\Mail\Object\Address; use KTXF\Mail\Object\AddressInterface; use KTXF\Mail\Service\ServiceBaseInterface; use KTXF\Mail\Service\ServiceCollectionMutableInterface; use KTXF\Mail\Service\ServiceEntityMutableInterface; use KTXF\Mail\Service\ServiceConfigurableInterface; use KTXF\Mail\Service\ServiceMutableInterface; use KTXF\Resource\Provider\ResourceServiceIdentityInterface; use KTXF\Resource\Provider\ResourceServiceLocationInterface; use KTXF\Resource\Delta\Delta; use KTXF\Resource\Filter\Filter; use KTXF\Resource\Filter\IFilter; use KTXF\Resource\Identifier\CollectionIdentifier; use KTXF\Resource\Identifier\EntityIdentifier; use KTXF\Resource\Range\IRange; use KTXF\Resource\Range\Range; use KTXF\Resource\Range\RangeTally; use KTXF\Resource\Range\RangeType; use KTXF\Resource\Sort\ISort; use KTXF\Resource\Sort\Sort; use KTXM\ProviderImap\Providers\ServiceIdentityBasic; use KTXM\ProviderImap\Providers\ServiceLocation; use KTXM\ProviderImap\Service\Remote\RemoteMailService; use KTXM\ProviderImap\Service\Remote\RemoteService; use KTXM\ProviderImap\Providers\CollectionResource; use KTXF\Mail\Collection\CollectionRoles; use KTXF\Mail\Object\MessagePropertiesMutableInterface; use KTXM\ProviderImap\Providers\EntityResource; /** * IMAP Mail Service */ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceConfigurableInterface, ServiceCollectionMutableInterface, ServiceEntityMutableInterface { private const PROVIDER_IDENTIFIER = 'imap'; private ?string $serviceTenantId = null; private ?string $serviceUserId = null; private ?string $serviceIdentifier = null; private ?string $serviceLabel = null; private bool $serviceEnabled = false; private string $primaryAddress = ''; private array $secondaryAddresses = []; private ?ServiceLocation $location = null; private ?ServiceIdentityBasic $identity = null; private array $auxiliary = []; private array $serviceAbilities = [ self::CAPABILITY_COLLECTION_LIST => true, self::CAPABILITY_COLLECTION_LIST_FILTER => [ self::CAPABILITY_COLLECTION_FILTER_LABEL => 's:128:256:256', self::CAPABILITY_COLLECTION_FILTER_ROLE => 's:32:1:1', self::CAPABILITY_COLLECTION_FILTER_SUBSCRIBED => 'b:0:1:1', ], self::CAPABILITY_COLLECTION_LIST_SORT => [], self::CAPABILITY_COLLECTION_EXTANT => true, self::CAPABILITY_COLLECTION_FETCH => true, self::CAPABILITY_COLLECTION_CREATE => true, self::CAPABILITY_COLLECTION_UPDATE => true, self::CAPABILITY_COLLECTION_DELETE => true, self::CAPABILITY_COLLECTION_MOVE => true, self::CAPABILITY_ENTITY_LIST => true, self::CAPABILITY_ENTITY_LIST_FILTER => [ self::CAPABILITY_ENTITY_FILTER_FROM => 's:100:256:256', self::CAPABILITY_ENTITY_FILTER_TO => 's:100:256:256', self::CAPABILITY_ENTITY_FILTER_SUBJECT => 's:200:256:256', self::CAPABILITY_ENTITY_FILTER_BODY => 's:200:256:256', self::CAPABILITY_ENTITY_FILTER_DATE_BEFORE => 's:32:1:1', self::CAPABILITY_ENTITY_FILTER_DATE_AFTER => 's:32:1:1', self::CAPABILITY_ENTITY_FILTER_SIZE_MIN => 'i:0:16:16', self::CAPABILITY_ENTITY_FILTER_SIZE_MAX => 'i:0:32:32', ], self::CAPABILITY_ENTITY_LIST_SORT => [], self::CAPABILITY_ENTITY_LIST_RANGE => ['tally' => ['absolute', 'relative']], self::CAPABILITY_ENTITY_EXTANT => true, self::CAPABILITY_ENTITY_FETCH => true, self::CAPABILITY_ENTITY_CREATE => false, self::CAPABILITY_ENTITY_MODIFY => false, self::CAPABILITY_ENTITY_DELETE => true, self::CAPABILITY_ENTITY_MOVE => true, self::CAPABILITY_ENTITY_COPY => false, ]; private RemoteMailService $mailService; public function __construct() {} // ── Lazy initialisation ─────────────────────────────────────────────────── private function initialize(): void { if (!isset($this->mailService)) { $wrapper = RemoteService::freshClient($this); $this->mailService = RemoteService::mailService($this, $wrapper); } } // ── Store (MongoDB persistence) ─────────────────────────────────────────── public function toStore(): array { return array_filter([ 'tid' => $this->serviceTenantId, 'uid' => $this->serviceUserId, 'sid' => $this->serviceIdentifier, 'label' => $this->serviceLabel, 'enabled' => $this->serviceEnabled, 'primaryAddress' => $this->primaryAddress, 'secondaryAddresses'=> $this->secondaryAddresses, 'location' => $this->location?->toStore(), 'identity' => $this->identity?->toStore(), 'auxiliary' => $this->auxiliary, ], fn($v) => $v !== null); } public function fromStore(array $data): static { $this->serviceTenantId = $data['tid'] ?? null; $this->serviceUserId = $data['uid'] ?? null; $this->serviceIdentifier = $data['sid'] ?? null; $this->serviceLabel = $data['label'] ?? ''; $this->serviceEnabled = $data['enabled'] ?? false; if (isset($data['primaryAddress'])) { $this->primaryAddress = $data['primaryAddress']; } if (isset($data['secondaryAddresses']) && is_array($data['secondaryAddresses'])) { $this->secondaryAddresses = $data['secondaryAddresses']; } if (isset($data['location'])) { $this->location = (new ServiceLocation())->fromStore($data['location']); } if (isset($data['identity'])) { $this->identity = (new ServiceIdentityBasic())->fromStore($data['identity']); } if (isset($data['auxiliary']) && is_array($data['auxiliary'])) { $this->auxiliary = $data['auxiliary']; } return $this; } // ── JSON ────────────────────────────────────────────────────────────────── public function jsonSerialize(): array { return array_filter([ self::JSON_PROPERTY_TYPE => self::JSON_TYPE, self::JSON_PROPERTY_PROVIDER => self::PROVIDER_IDENTIFIER, self::JSON_PROPERTY_IDENTIFIER => $this->serviceIdentifier, self::JSON_PROPERTY_LABEL => $this->serviceLabel, self::JSON_PROPERTY_ENABLED => $this->serviceEnabled, self::JSON_PROPERTY_CAPABILITIES => $this->serviceAbilities, self::JSON_PROPERTY_PRIMARY_ADDRESS => $this->primaryAddress, self::JSON_PROPERTY_SECONDARY_ADDRESSES => $this->secondaryAddresses, self::JSON_PROPERTY_LOCATION => $this->location?->jsonSerialize(), self::JSON_PROPERTY_IDENTITY => $this->identity?->jsonSerialize(), self::JSON_PROPERTY_AUXILIARY => $this->auxiliary, ], fn($v) => $v !== null); } public function jsonDeserialize(array|string $data, bool $delta = false): static { if (is_string($data)) { $data = json_decode($data, true, 512, JSON_THROW_ON_ERROR); } if (isset($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])) { $this->setLocation($this->freshLocation(null, $data[self::JSON_PROPERTY_LOCATION])); } if (isset($data[self::JSON_PROPERTY_IDENTITY])) { $this->setIdentity($this->freshIdentity(null, $data[self::JSON_PROPERTY_IDENTITY])); } if (isset($data[self::JSON_PROPERTY_PRIMARY_ADDRESS]) && is_string($data[self::JSON_PROPERTY_PRIMARY_ADDRESS])) { $this->setPrimaryAddress(new Address($data[self::JSON_PROPERTY_PRIMARY_ADDRESS])); } if (isset($data[self::JSON_PROPERTY_SECONDARY_ADDRESSES]) && is_array($data[self::JSON_PROPERTY_SECONDARY_ADDRESSES])) { $this->setSecondaryAddresses(array_map( fn($addr) => new Address(is_array($addr) ? ($addr['address'] ?? $addr) : $addr), $data[self::JSON_PROPERTY_SECONDARY_ADDRESSES] )); } if (isset($data[self::JSON_PROPERTY_AUXILIARY]) && is_array($data[self::JSON_PROPERTY_AUXILIARY])) { $this->setAuxiliary($data[self::JSON_PROPERTY_AUXILIARY]); } return $this; } // ── ServiceBaseInterface ────────────────────────────────────────────────── public function capable(string $value): bool { return isset($this->serviceAbilities[$value]); } public function capabilities(): array { return $this->serviceAbilities; } public function provider(): string { return self::PROVIDER_IDENTIFIER; } public function identifier(): string|int { return $this->serviceIdentifier; } // ── ServiceMutableInterface ─────────────────────────────────────────────── public function getLabel(): ?string { return $this->serviceLabel; } public function setLabel(string $label): static { $this->serviceLabel = $label; return $this; } public function getEnabled(): bool { return $this->serviceEnabled; } public function setEnabled(bool $enabled): static { $this->serviceEnabled = $enabled; return $this; } public function getPrimaryAddress(): AddressInterface { return new Address($this->primaryAddress); } public function setPrimaryAddress(AddressInterface $value): static { $this->primaryAddress = $value->getAddress(); return $this; } public function getSecondaryAddresses(): array { return $this->secondaryAddresses; } public function setSecondaryAddresses(array $addresses): static { $this->secondaryAddresses = $addresses; return $this; } public function hasAddress(string $address): bool { $address = strtolower(trim($address)); if ($this->primaryAddress && strtolower($this->primaryAddress) === $address) { return true; } foreach ($this->secondaryAddresses as $secondary) { $secondaryAddr = $secondary instanceof AddressInterface ? $secondary->getAddress() : (string) $secondary; if (strtolower($secondaryAddr) === $address) { return true; } } return false; } // ── ServiceConfigurableInterface ────────────────────────────────────────── public function getLocation(): ServiceLocation { return $this->location; } public function setLocation(ResourceServiceLocationInterface $location): static { $this->location = $location; return $this; } public function freshLocation(?string $type = null, array $data = []): ServiceLocation { $loc = new ServiceLocation(); $loc->jsonDeserialize($data); return $loc; } public function getIdentity(): ServiceIdentityBasic { return $this->identity; } public function setIdentity(ResourceServiceIdentityInterface $identity): static { $this->identity = $identity; return $this; } public function freshIdentity(?string $type = null, array $data = []): ServiceIdentityBasic { $id = new ServiceIdentityBasic(); $id->jsonDeserialize($data); return $id; } public function getDebug(): bool { return ($this->auxiliary['debug'] ?? false) === true; } public function setDebug(bool $debug): static { $this->auxiliary['debug'] = $debug; return $this; } public function getAuxiliary(): array { return $this->auxiliary; } public function setAuxiliary(array $auxiliary): static { $this->auxiliary = $auxiliary; return $this; } // ── Collection operations ───────────────────────────────────────────────── public function collectionList(string|int|null $location, ?IFilter $filter = null, ?ISort $sort = null): array { $this->initialize(); $list = []; foreach ($this->mailService->collectionList($location, $filter, $sort) as $mailbox) { $resource = $this->collectionFresh(); $resource->fromImap($mailbox); $list[$mailbox->name()] = $resource; } return $list; } public function collectionListFilter(): Filter { return new Filter($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_FILTER] ?? []); } public function collectionListSort(): Sort { return new Sort($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_SORT] ?? []); } public function collectionExtant(string|int ...$identifiers): array { $this->initialize(); $list = []; foreach ($identifiers as $identifier) { $key = (string) $identifier; $result = $this->mailService->collectionFetch($key); $list[$key] = $result !== false; } return $list; } public function collectionFetch(string|int $identifier): ?CollectionBaseInterface { $this->initialize(); $mailbox = $this->mailService->collectionFetch((string) $identifier); if ($mailbox === null) { return null; } $collection = $this->collectionFresh(); $collection->fromImap($mailbox); return $collection; } public function collectionFresh(): CollectionResource { return new CollectionResource($this->provider(), $this->identifier()); } public function collectionCreate(CollectionIdentifier|null $target, CollectionPropertiesBaseInterface $properties, array $options = []): CollectionBaseInterface { $this->initialize(); if (!$properties->getLabel()) { throw new \InvalidArgumentException('Collection label is required property'); } $label = $properties->getLabel(); // Resolve the full name: if a parent location is given, prepend it if ($target !== null) { $path = $target->collection(); // Determine the hierarchy delimiter from an existing mailbox, default to '/' $mailboxes = iterator_to_array($this->mailService->collectionList(null, null, null, '')); $rootMailbox = $mailboxes === [] ? null : reset($mailboxes); $delimiter = $rootMailbox === false ? '/' : ($rootMailbox?->delimiter() ?? '/'); $label = rtrim((string) $path, $delimiter) . $delimiter . ltrim($label, $delimiter); } $mailbox = $this->mailService->collectionCreate($label); $collection = $this->collectionFresh(); $collection->fromImap($mailbox, ['delimiter' => $delimiter ?? null]); return $collection; } public function collectionUpdate(CollectionIdentifier $target, CollectionPropertiesBaseInterface $properties): CollectionBaseInterface { $this->initialize(); if (!$properties->getLabel()) { throw new \InvalidArgumentException('Collection label is a required property'); } $label = $properties->getLabel(); // In IMAP, "update" = rename to the new label $oldPath = (string) $target->collection(); $newName = $properties->getLabel(); $mailbox = $this->mailService->collectionRename($oldPath, $newName); $collection = $this->collectionFresh(); $collection->fromImap($mailbox); return $collection; } public function collectionDelete(CollectionIdentifier $target, bool $force = false): CollectionBaseInterface | true { $this->initialize(); $deleteMode = $this->auxiliary['deleteMode'] ?? 'soft'; $deleteTarget = $this->auxiliary['deleteTarget'] ?? null; if ($deleteMode !== 'soft' && $deleteMode !== 'hard') { throw new \InvalidArgumentException("Invalid delete mode: $deleteMode"); } // Move to target collection (e.g. Trash) instead of deleting if ($deleteMode === 'soft' && $deleteTarget !== null) { return $this->collectionMove($target, new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget)); } if ($deleteMode === 'soft' && $deleteTarget === null) { $filter = $this->collectionListFilter(); $filter->condition('role', CollectionRoles::Trash->value); $mailboxes = iterator_to_array($this->mailService->collectionList(null, $filter, null)); if (empty($mailboxes)) { throw new \RuntimeException('No Trash collection configured or found for deletion'); } $deleteTarget = key($mailboxes); } // we need to determine if the folder being deleted is already in the trash if (str_starts_with((string) $target->collection(), (string) $deleteTarget)) { // if so, we should hard delete instead of moving to avoid duplicates in the trash $deleteMode = 'hard'; } $result = match ($deleteMode) { 'soft' => $this->collectionMove($target, new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget)), 'hard' => $this->mailService->collectionDestroy((string) $target->collection()), }; return $result; } public function collectionMove(CollectionIdentifier $target, CollectionIdentifier $source): CollectionBaseInterface { $this->initialize(); $sourceMailbox = $this->mailService->collectionFetch((string) $source->collection()); $targetMailbox = $this->mailService->collectionFetch((string) $target->collection()); if ($sourceMailbox === null) { throw new \RuntimeException('Source collection not found for move operation'); } if ($targetMailbox === null) { throw new \RuntimeException('Target collection not found for move operation'); } $sourceDelimiter = $sourceMailbox->delimiter() ?? '/'; $targetDelimiter = $targetMailbox->delimiter() ?? '/'; $targetPath = rtrim($targetMailbox->name(), $targetDelimiter) . $targetDelimiter . end(explode($sourceDelimiter, $sourceMailbox->name())); $mutatedMailbox = $this->mailService->collectionRename($sourceMailbox->name(), $targetPath); $collection = $this->collectionFresh(); $collection->fromImap($mutatedMailbox, ['delimiter' => $targetDelimiter]); return $collection; } // ── Entity operations ───────────────────────────────────────────────────── public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): array { return iterator_to_array($this->entityList((string) $collection, $filter, $sort, $range), true); } public function entityListStream(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): Generator { $this->initialize(); foreach ($this->mailService->entityList((string) $collection, $filter, $sort, $range) as $identifier => $message) { $resource = $this->entityFresh(); $resource->fromImap($message, $collection); yield $identifier => $resource; } } public function entityListFilter(): Filter { return new Filter($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_FILTER] ?? []); } public function entityListSort(): Sort { return new Sort($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_SORT] ?? []); } public function entityListRange(RangeType $type): IRange { return match ($type) { RangeType::TALLY => new RangeTally(), default => new Range(), }; } public function entityFetch(string|int $collection, string|int ...$identifiers): array { $this->initialize(); $uids = array_map('intval', $identifiers); return $this->mailService->entityFetch((string) $collection, ...$uids); } public function entityDelta(string|int $collection, string $signature, string $detail = 'ids'): Delta { return new Delta(signature: $signature); } public function entityExtant(string|int $collection, string|int ...$identifiers): array { $this->initialize(); $allUids = $this->mailService->entityList((string) $collection); $uidSet = array_flip($allUids); // int[] → [uid => index] $extant = []; foreach ($identifiers as $id) { $extant[$id] = isset($uidSet[(int) $id]); } return $extant; } public function entityFresh(): EntityResource { return new EntityResource($this->provider(), $this->identifier()); } public function entityCreate(CollectionIdentifier $target, MessagePropertiesMutableInterface $properties, array $options = []): EntityResource { throw new \RuntimeException('Entity creation is not supported in this service'); } public function entityModify(EntityIdentifier $target, MessagePropertiesMutableInterface $properties): EntityResource { throw new \RuntimeException('Entity modification is not supported in this service'); } public function entityDelete(EntityIdentifier ...$targets): array { // validate identifiers and group by collection $targets = $this->groupEntitiesByCollection(...$targets); // determine delete mode and target collection (e.g. Trash) if applicable $deleteMode = $this->auxiliary['deleteMode'] ?? 'soft'; $deleteTarget = $this->auxiliary['deleteTarget'] ?? null; if ($deleteMode !== 'soft' && $deleteMode !== 'hard') { throw new \InvalidArgumentException("Invalid delete mode: $deleteMode"); } // connect to remote store $this->initialize(); // attempt to find a target collection for soft deletion if none was specified if ($deleteMode === 'soft' && $deleteTarget === null) { $filter = $this->collectionListFilter(); $filter->condition('role', CollectionRoles::Trash->value); $mailboxes = iterator_to_array($this->mailService->collectionList(null, $filter, null)); if (empty($mailboxes)) { throw new \RuntimeException('No Trash collection configured or found for deletion'); } $rootMailbox = reset($mailboxes); if ($rootMailbox === false) { throw new \RuntimeException('No Trash collection configured or found for deletion'); } $deleteTargetNative = $rootMailbox->name(); $deleteTargetIdentifier = new CollectionIdentifier($this->provider(), (string) $this->identifier(), $deleteTargetNative); } else { $deleteTargetNative = $deleteTarget; $deleteTargetIdentifier = new CollectionIdentifier($this->provider(), (string) $this->identifier(), $deleteTargetNative); } // entities need to be moved or deleted by collection $list = []; foreach ($targets as $sourceCollection => $sourceEntities) { if ($deleteMode === 'soft' && $sourceCollection === $deleteTargetNative) { continue; } $uids = array_keys($sourceEntities); $mutations = match ($deleteMode) { 'soft' => $this->mailService->entityMove($deleteTargetNative, $sourceCollection, ...$uids), 'hard' => $this->mailService->entityDestroy($sourceCollection, ...$uids), }; foreach ($uids as $uid) { $mutatedUid = $mutations[$uid] ?? null; $list[(string)$sourceEntities[$uid]] = [ 'disposition' => $deleteMode === 'soft' ? 'moved' : 'deleted', 'destination' => $deleteMode === 'soft' ? $deleteTargetIdentifier : null, 'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $deleteTargetIdentifier->collection(), $mutatedUid) : null, ]; } } return $list; } public function entityPatch(MessagePropertiesMutableInterface $properties, EntityIdentifier ...$targets): array { // validate identifiers and group by collection $targets = $this->groupEntitiesByCollection(...$targets); // move entities on remote store and construct result map $this->initialize(); $list = []; foreach ($targets as $targetCollection => $targetIdentifiers) { $uids = array_keys($targetIdentifiers); $mutations = $this->mailService->entityPatch($targetCollection, $properties, ...$uids); foreach ($uids as $uid) { $list[(string)$targetIdentifiers[$uid]] = ['disposition' => 'patched']; } } 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 { // 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); // move entities on remote store and construct result map $this->initialize(); $list = []; foreach ($sources as $sourceCollection => $sourceEntities) { $uids = array_keys($sourceEntities); $mutations = $this->mailService->entityMove($target->collection(), $sourceCollection, ...$uids); foreach ($uids as $uid) { $mutatedUid = $mutations[$uid] ?? null; $list[(string)$sourceEntities[$uid]] = [ 'disposition' => 'moved', 'destination' => $target, 'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $target->collection(), $mutatedUid) : null, ]; } } return $list; } private function groupEntitiesByCollection(EntityIdentifier ...$identifiers): array { $list = []; foreach ($identifiers as $identifier) { if ($identifier->provider() !== $this->provider() || $identifier->service() !== $this->identifier()) { throw new \InvalidArgumentException('Entity identifier does not belong to this service: ' . $identifier); } $list[$identifier->collection()][$identifier->entity()] = $identifier; } return $list; } }