Initial commit

This commit is contained in:
root
2026-01-04 22:05:37 -05:00
committed by Sebastian Krupinski
commit 4f979ced22
57 changed files with 11076 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers\Mail;
use KTXF\Mail\Collection\CollectionPropertiesMutableAbstract;
/**
* Mail Collection Properties Implementation
*/
class CollectionProperties extends CollectionPropertiesMutableAbstract {
/**
* Convert JMAP parameters array to mail collection properties object
*
* @param array $parameters JMAP parameters array
*/
public function fromJmap(array $parameters): static {
if (isset($parameters['totalEmails'])) {
$this->data['total'] = $parameters['totalEmails'];
}
if (isset($parameters['unreadEmails'])) {
$this->data['unread'] = $parameters['unreadEmails'];
}
if (isset($parameters['name'])) {
$this->data['label'] = $parameters['name'];
}
if (isset($parameters['role'])) {
$this->data['role'] = $parameters['role'];
}
if (isset($parameters['sortOrder'])) {
$this->data['rank'] = $parameters['sortOrder'];
}
if (isset($parameters['isSubscribed'])) {
$this->data['subscribed'] = $parameters['isSubscribed'];
}
return $this;
}
/**
* Convert mail collection properties object to JMAP parameters array
*/
public function toJmap(): array {
$parameters = [];
if (isset($this->data['label'])) {
$parameters['name'] = $this->data['label'];
}
if (isset($this->data['role'])) {
$parameters['role'] = $this->data['role'];
}
if (isset($this->data['rank'])) {
$parameters['sortOrder'] = $this->data['rank'];
}
if (isset($this->data['subscribed'])) {
$parameters['isSubscribed'] = $this->data['subscribed'];
}
return $parameters;
}
}

View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers\Mail;
use KTXF\Mail\Collection\CollectionMutableAbstract;
/**
* Mail Collection Resource Implementation
*/
class CollectionResource extends CollectionMutableAbstract {
public function __construct(
string $provider = 'jmapc',
string|int|null $service = null,
) {
parent::__construct($provider, $service);
}
/**
* Converts JMAP parameters array to mail collection object
*
* @param array $parameters JMAP parameters array
*/
public function fromJmap(array $parameters): static {
if (isset($parameters['parentId'])) {
$this->data['collection'] = $parameters['parentId'];
}
if (isset($parameters['id'])) {
$this->data['identifier'] = $parameters['id'];
}
if (isset($parameters['signature'])) {
$this->data['signature'] = $parameters['signature'];
}
$this->getProperties()->fromJmap($parameters);
return $this;
}
/**
* Convert mail collection object to JMAP parameters array
*/
public function toJmap(): array {
$parameters = [];
if (isset($this->data['collection'])) {
$parameters['parentId'] = $this->data['collection'];
}
if (isset($this->data['identifier'])) {
$parameters['id'] = $this->data['identifier'];
}
$parameters = array_merge($parameters, $this->getProperties()->toJmap());
return $parameters;
}
/**
* @inheritDoc
*/
public function getProperties(): CollectionProperties {
if (!isset($this->properties)) {
$this->properties = new CollectionProperties([]);
}
return $this->properties;
}
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers\Mail;
use KTXF\Mail\Entity\EntityMutableAbstract;
/**
* Mail Entity Resource Implementation
*/
class EntityResource extends EntityMutableAbstract {
public function __construct(
string $provider = 'jmapc',
string|int|null $service = null,
) {
parent::__construct($provider, $service);
}
/**
* Convert JMAP parameters array to mail entity object
*
* @param array $parameters JMAP parameters array
*/
public function fromJmap(array $parameters): static {
if (isset($parameters['mailboxIds'])) {
$this->data['collection'] = array_keys($parameters['mailboxIds'])[0];
}
if (isset($parameters['id'])) {
$this->data['identifier'] = $parameters['id'];
}
if (isset($parameters['signature'])) {
$this->data['signature'] = $parameters['signature'];
}
if (isset($parameters['receivedAt']) || isset($parameters['sentAt'])) {
$this->data['created'] = $parameters['receivedAt'] ?? $parameters['sentAt'];
}
if (isset($parameters['updated'])) {
$this->data['modified'] = $parameters['updated'];
}
$this->getProperties()->fromJmap($parameters);
return $this;
}
/**
* Convert mail entity object to JMAP parameters array
*/
public function toJmap(): array {
$parameters = [];
if (isset($this->data['collection'])) {
$parameters['mailboxIds'] = [$this->data['collection']];
}
if (isset($this->data['identifier'])) {
$parameters['id'] = $this->data['identifier'];
}
$parameters = array_merge($parameters, $this->getProperties()->toJmap());
return $parameters;
}
/**
* @inheritDoc
*/
public function getProperties(): MessageProperties {
if (!isset($this->properties)) {
$this->properties = new MessageProperties([]);
}
return $this->properties;
}
}

View File

@@ -0,0 +1,186 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers\Mail;
/**
* Mail Attachment Object
*
* @since 30.0.0
*/
class MessageAttachment implements MessagePart {
protected MessagePart $_meta;
protected ?string $_contents = null;
public function __construct(?MessagePart $meta = null, ?string $contents = null) {
// determine if meta data exists
// if meta data is missing create new
if ($meta === null) {
$meta = new MessagePart();
$meta->setDisposition('attachment');
$meta->setType('application/octet-stream');
}
$this->setParameters($meta);
// determine if attachment contents exists
// if contents exists set the contents
if ($contents !== null) {
$this->setContents($contents);
}
}
/**
* sets the attachments parameters
*
* @since 1.0.0
*
* @param MessagePart|null $meta collection of all message parameters
*
* @return self return this object for command chaining
*/
public function setParameters(?MessagePart $meta): self {
// replace meta data store
$this->_meta = $meta;
// return this object for command chaining
return $this;
}
/**
* gets the attachments of this message
*
* @since 1.0.0
*
* @return array collection of all message parameters
*/
public function getParameters(): MessagePart {
// evaluate if data store field exists and return value(s) or null otherwise
return $this->_meta;
}
/**
* arbitrary unique text string identifying this message
*
* @since 1.0.0
*
* @return string id of this message
*/
public function id(): string {
// return id of message
return $this->_meta->getBlobId();
}
/**
* sets the attachment file name
*
* @since 30.0.0
*
* @param string $value file name (e.g example.txt)
*
* @return self return this object for command chaining
*/
public function setName(string $value): self {
$this->_meta->setName($value);
return $this;
}
/**
* gets the attachment file name
*
* @since 30.0.0
*
* @return string | null returns the attachment file name or null if not set
*/
public function getName(): ?string {
return $this->_meta->getName();
}
/**
* sets the attachment mime type
*
* @since 30.0.0
*
* @param string $value mime type (e.g. text/plain)
*
* @return self return this object for command chaining
*/
public function setType(string $value): self {
$this->_meta->setType($value);
return $this;
}
/**
* gets the attachment mime type
*
* @since 30.0.0
*
* @return string | null returns the attachment mime type or null if not set
*/
public function getType(): ?string {
return $this->_meta->getType();
}
/**
* sets the attachment contents (actual data)
*
* @since 30.0.0
*
* @param string $value binary contents of file
*
* @return self return this object for command chaining
*/
public function setContents(string $value): self {
$this->_contents = $value;
return $this;
}
/**
* gets the attachment contents (actual data)
*
* @since 30.0.0
*
* @return string | null returns the attachment contents or null if not set
*/
public function getContents(): ?string {
return $this->_contents;
}
/**
* sets the embedded status of the attachment
*
* @since 30.0.0
*
* @param bool $value true - embedded / false - not embedded
*
* @return self return this object for command chaining
*/
public function setEmbedded(bool $value): self {
if ($value) {
$this->_meta->setDisposition('inline');
} else {
$this->_meta->setDisposition('attachment');
}
return $this;
}
/**
* gets the embedded status of the attachment
*
* @since 30.0.0
*
* @return bool embedded status of the attachment
*/
public function getEmbedded(): bool {
if ($this->_meta->getDisposition() === 'inline') {
return true;
} else {
return false;
}
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers\Mail;
use KTXF\Mail\Object\MessagePartMutableAbstract;
class MessagePart extends MessagePartMutableAbstract {
/**
* convert jmap parameters collection to message object
*
* @since 1.0.0
*
* @param array $parameters jmap parameters collection
* @param bool $amend flag merged or replaced parameters
*/
public function fromJmap(array $parameters, bool $amend = false): self {
if ($amend) {
// merge parameters with existing ones
$this->data = array_merge($this->data, $parameters);
} else {
// replace parameters store
$this->data = $parameters;
}
// determine if parameters contains subparts
// if subParts exist convert them to a MessagePart object
// and remove subParts parameter
if (is_array($this->data['subParts'])) {
foreach ($this->data['subParts'] as $key => $entry) {
if (is_object($entry)) {
$entry = get_object_vars($entry);
}
$this->parts[$key] = (new MessagePart($parameters))->fromJmap($entry);
}
unset($this->data['subParts']);
}
return $this;
}
/**
* convert message object to jmap parameters array
*
* @since 1.0.0
*
* @return array collection of all message parameters
*/
public function toJmap(): array {
// copy parameter value
$parameters = $this->data;
// determine if this MessagePart has any sub MessageParts
// if sub MessageParts exist retrieve sub MessagePart parameters
// and add them to the subParts parameters, otherwise set the subParts parameter to nothing
if (count($this->parts) > 0) {
$parameters['subParts'] = [];
foreach ($this->parts as $entry) {
if ($entry instanceof MessagePart) {
$parameters['subParts'][] = $entry->toJmap();
}
}
} else {
$parameters['subParts'] = null;
}
return $parameters;
}
}

View File

@@ -0,0 +1,264 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers\Mail;
use KTXF\Mail\Object\MessagePropertiesMutableAbstract;
/**
* Mail Message Properties Implementation
*/
class MessageProperties extends MessagePropertiesMutableAbstract {
/**
* Convert JMAP parameters array to mail message properties object
*
* @param array $parameters JMAP parameters array
*/
public function fromJmap(array $parameters): static {
if (isset($parameters['messageId'])) {
$this->data['urid'] = $parameters['messageId'][0];
}
if (isset($parameters['size'])) {
$this->data['size'] = $parameters['size'];
}
if (isset($parameters['receivedAt'])) {
$this->data['receivedDate'] = $parameters['receivedAt'];
}
if (isset($parameters['sentAt'])) {
$this->data['date'] = $parameters['sentAt'];
}
if (isset($parameters['inReplyTo'])) {
$this->data['inReplyTo'] = $parameters['inReplyTo'];
}
if (isset($parameters['references'])) {
$this->data['references'] = is_array($parameters['references']) ? $parameters['references'] : [];
}
if (isset($parameters['subject'])) {
$this->data['subject'] = $parameters['subject'];
}
if (isset($parameters['preview'])) {
$this->data['snippet'] = $parameters['preview'];
}
if (isset($parameters['sender'])) {
$this->data['sender'] = $parameters['sender'];
}
if (isset($parameters['from']) && is_array($parameters['from']) && !empty($parameters['from'])) {
$this->data['from'] = [
'address' => $parameters['from'][0]['email'] ?? '',
'label' => $parameters['from'][0]['name'] ?? null
];
}
if (isset($parameters['to']) && is_array($parameters['to'])) {
$this->data['to'] = [];
foreach ($parameters['to'] as $addr) {
$this->data['to'][] = [
'address' => $addr['email'] ?? '',
'label' => $addr['name'] ?? null
];
}
}
if (isset($parameters['cc']) && is_array($parameters['cc'])) {
$this->data['cc'] = [];
foreach ($parameters['cc'] as $addr) {
$this->data['cc'][] = [
'address' => $addr['email'] ?? '',
'label' => $addr['name'] ?? null
];
}
}
if (isset($parameters['bcc']) && is_array($parameters['bcc'])) {
$this->data['bcc'] = [];
foreach ($parameters['bcc'] as $addr) {
$this->data['bcc'][] = [
'address' => $addr['email'] ?? '',
'label' => $addr['name'] ?? null
];
}
}
if (isset($parameters['replyTo']) && is_array($parameters['replyTo'])) {
$this->data['replyTo'] = [];
foreach ($parameters['replyTo'] as $addr) {
$this->data['replyTo'][] = [
'address' => $addr['email'] ?? '',
'label' => $addr['name'] ?? null
];
}
}
if (isset($parameters['keywords']) && is_array($parameters['keywords'])) {
$this->data['flags'] = [];
foreach ($parameters['keywords'] as $keyword => $value) {
$flag = match($keyword) {
'$seen' => 'read',
'$flagged' => 'flagged',
'$answered' => 'answered',
'$draft' => 'draft',
'$deleted' => 'deleted',
default => $keyword
};
$this->data['flags'][$flag] = $value;
}
}
if (isset($parameters['bodyStructure'])) {
$this->data['body'] = $parameters['bodyStructure'];
// Recursively add content from bodyValues to matching parts
if (isset($parameters['bodyValues']) && is_array($parameters['bodyValues'])) {
$addContentToParts = function(&$structure, $bodyValues) use (&$addContentToParts) {
// If this part has a partId and matching bodyValue, add content
if (isset($structure['partId']) && isset($bodyValues[$structure['partId']])) {
$structure['content'] = $bodyValues[$structure['partId']]['value'] ?? null;
}
// Recursively process subParts
if (isset($structure['subParts']) && is_array($structure['subParts'])) {
foreach ($structure['subParts'] as &$subPart) {
$addContentToParts($subPart, $bodyValues);
}
}
};
$addContentToParts($this->data['body'], $parameters['bodyValues']);
}
}
if (isset($parameters['headers']) && is_array($parameters['headers'])) {
$this->data['headers'] = $parameters['headers'];
}
if (isset($parameters['attachments'])) {
$this->data['attachments'] = $parameters['attachments'];
}
return $this;
}
/**
* Convert mail message properties object to JMAP parameters array
*/
public function toJmap(): array {
$parameters = [];
if (isset($this->data['urid'])) {
$parameters['messageId'] = [$this->data['urid']];
}
if (isset($this->data['size'])) {
$parameters['size'] = $this->data['size'];
}
if (isset($this->data['receivedDate'])) {
$parameters['receivedAt'] = $this->data['receivedDate'];
}
if (isset($this->data['date'])) {
$parameters['sentAt'] = $this->data['date'];
}
if (isset($this->data['inReplyTo'])) {
$parameters['inReplyTo'] = $this->data['inReplyTo'];
}
if (isset($this->data['references'])) {
$parameters['references'] = $this->data['references'];
}
if (isset($this->data['subject'])) {
$parameters['subject'] = $this->data['subject'];
}
if (isset($this->data['snippet'])) {
$parameters['preview'] = $this->data['snippet'];
}
if (isset($this->data['sender'])) {
$parameters['sender'] = $this->data['sender'];
}
if (isset($this->data['from'])) {
$parameters['from'] = [[
'email' => $this->data['from']['address'] ?? '',
'name' => $this->data['from']['label'] ?? null
]];
}
if (isset($this->data['to'])) {
$parameters['to'] = [];
foreach ($this->data['to'] as $addr) {
$parameters['to'][] = [
'email' => $addr['address'] ?? '',
'name' => $addr['label'] ?? null
];
}
}
if (isset($this->data['cc'])) {
$parameters['cc'] = [];
foreach ($this->data['cc'] as $addr) {
$parameters['cc'][] = [
'email' => $addr['address'] ?? '',
'name' => $addr['label'] ?? null
];
}
}
if (isset($this->data['bcc'])) {
$parameters['bcc'] = [];
foreach ($this->data['bcc'] as $addr) {
$parameters['bcc'][] = [
'email' => $addr['address'] ?? '',
'name' => $addr['label'] ?? null
];
}
}
if (isset($this->data['replyTo'])) {
$parameters['replyTo'] = [];
foreach ($this->data['replyTo'] as $addr) {
$parameters['replyTo'][] = [
'email' => $addr['address'] ?? '',
'name' => $addr['label'] ?? null
];
}
}
if (isset($this->data['flags'])) {
$parameters['keywords'] = [];
foreach ($this->data['flags'] as $flag => $value) {
$keyword = match($flag) {
'read' => '$seen',
'flagged' => '$flagged',
'answered' => '$answered',
'draft' => '$draft',
'deleted' => '$deleted',
default => $flag
};
$parameters['keywords'][$keyword] = $value;
}
}
if (isset($this->data['bodyStructure'])) {
$parameters['bodyStructure'] = $this->data['bodyStructure'];
}
if (isset($this->data['body'])) {
$parameters['bodyValues'] = [];
if (isset($this->data['body']['text']['content'])) {
$parameters['bodyValues']['0'] = [
'value' => $this->data['body']['text']['content'],
'isEncodingProblem' => false,
'isTruncated' => false
];
}
if (isset($this->data['body']['html']['content'])) {
$parameters['bodyValues']['1'] = [
'value' => $this->data['body']['html']['content'],
'isEncodingProblem' => false,
'isTruncated' => false
];
}
}
if (isset($this->data['headers'])) {
$parameters['headers'] = $this->data['headers'];
}
if (isset($this->data['attachments'])) {
$parameters['attachments'] = $this->data['attachments'];
$parameters['hasAttachment'] = !empty($this->data['attachments']);
} else {
$parameters['hasAttachment'] = false;
}
return $parameters;
}
}

View File

@@ -0,0 +1,212 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers\Mail;
use KTXF\Mail\Provider\ProviderBaseInterface;
use KTXF\Mail\Provider\ProviderServiceDiscoverInterface;
use KTXF\Mail\Provider\ProviderServiceMutateInterface;
use KTXF\Mail\Provider\ProviderServiceTestInterface;
use KTXF\Mail\Service\ServiceBaseInterface;
use KTXF\Resource\Provider\ResourceServiceLocationInterface;
use KTXF\Resource\Provider\ResourceServiceMutateInterface;
use KTXM\ProviderJmapc\Service\Discovery;
use KTXM\ProviderJmapc\Service\Remote\RemoteService;
use KTXM\ProviderJmapc\Stores\ServiceStore;
/**
* JMAP Mail Provider
*
* Provides Mail services via JMAP protocol.
* Filters services by urn:ietf:params:jmap:mail capability.
*/
class Provider implements ProviderServiceMutateInterface, ProviderServiceDiscoverInterface, ProviderServiceTestInterface
{
public const JSON_TYPE = ProviderBaseInterface::JSON_TYPE;
protected const PROVIDER_IDENTIFIER = 'jmap';
protected const PROVIDER_LABEL = 'JMAP Mail Provider';
protected const PROVIDER_DESCRIPTION = 'Provides mail services via JMAP protocol (RFC 8620)';
protected const PROVIDER_ICON = 'mdi-email-sync';
protected array $providerAbilities = [
self::CAPABILITY_SERVICE_LIST => true,
self::CAPABILITY_SERVICE_FETCH => true,
self::CAPABILITY_SERVICE_EXTANT => true,
self::CAPABILITY_SERVICE_CREATE => true,
self::CAPABILITY_SERVICE_MODIFY => true,
self::CAPABILITY_SERVICE_DESTROY => true,
self::CAPABILITY_SERVICE_TEST => true,
];
public function __construct(
private readonly ServiceStore $serviceStore,
) {}
public function jsonSerialize(): array
{
return [
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
self::JSON_PROPERTY_IDENTIFIER => self::PROVIDER_IDENTIFIER,
self::JSON_PROPERTY_LABEL => self::PROVIDER_LABEL,
self::JSON_PROPERTY_CAPABILITIES => $this->providerAbilities,
];
}
public function jsonDeserialize(array|string $data): static
{
return $this;
}
public function type(): string
{
return self::TYPE_MAIL;
}
public function identifier(): string
{
return self::PROVIDER_IDENTIFIER;
}
public function label(): string
{
return self::PROVIDER_LABEL;
}
public function description(): string
{
return self::PROVIDER_DESCRIPTION;
}
public function icon(): string
{
return self::PROVIDER_ICON;
}
public function capable(string $value): bool
{
return !empty($this->providerAbilities[$value]);
}
public function capabilities(): array
{
return $this->providerAbilities;
}
public function serviceList(string $tenantId, string $userId, array $filter = []): array
{
$list = $this->serviceStore->list($tenantId, $userId, $filter);
foreach ($list as $entry) {
$service = new Service();
$service->fromStore($entry);
$list[$service->identifier()] = $service;
}
return $list;
}
public function serviceExtant(string $tenantId, string $userId, string|int ...$identifiers): array
{
return $this->serviceStore->extant($tenantId, $userId, $identifiers);
}
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?Service
{
return $this->serviceStore->fetch($tenantId, $userId, $identifier);
}
public function serviceFindByAddress(string $tenantId, string $userId, string $address): ?Service
{
/** @var Service[] $services */
$services = $this->serviceList($tenantId, $userId);
foreach ($services as $service) {
if ($service->hasAddress($address)) {
return $service;
}
}
return null;
}
public function serviceFresh(): ResourceServiceMutateInterface
{
return new Service();
}
public function serviceCreate(string $tenantId, string $userId, ResourceServiceMutateInterface $service): string
{
if (!($service instanceof Service)) {
throw new \InvalidArgumentException('Service must be instance of JMAP Service');
}
$created = $this->serviceStore->create($tenantId, $userId, $service);
return (string) $created->identifier();
}
public function serviceModify(string $tenantId, string $userId, ResourceServiceMutateInterface $service): string
{
if (!($service instanceof Service)) {
throw new \InvalidArgumentException('Service must be instance of JMAP Service');
}
$updated = $this->serviceStore->modify($tenantId, $userId, $service);
return (string) $updated->identifier();
}
public function serviceDestroy(string $tenantId, string $userId, ResourceServiceMutateInterface $service): bool
{
if (!($service instanceof Service)) {
return false;
}
return $this->serviceStore->delete($tenantId, $userId, $service->identifier());
}
public function serviceDiscover(
string $tenantId,
string $userId,
string $identity,
?string $location = null,
?string $secret = null
): ResourceServiceLocationInterface|null {
$discovery = new Discovery();
// TODO: Make SSL verification configurable based on tenant/user settings
$verifySSL = true;
return $discovery->discover($identity, $location, $secret, $verifySSL);
}
public function serviceTest(ServiceBaseInterface $service, array $options = []): array {
$startTime = microtime(true);
try {
if (!($service instanceof Service)) {
throw new \InvalidArgumentException('Service must be instance of JMAP Service');
}
$client = RemoteService::freshClient($service);
$session = $client->connect();
$latency = round((microtime(true) - $startTime) * 1000); // ms4
return [
'success' => true,
'message' => 'JMAP connection successful'
. ' (Account ID: ' . ($session->username() ?? 'N/A') . ')'
. ' (Latency: ' . $latency . ' ms)',
];
} catch (\Exception $e) {
$latency = round((microtime(true) - $startTime) * 1000);
return [
'success' => false,
'message' => 'Test failed: ' . $e->getMessage(),
];
}
}
}

View File

@@ -0,0 +1,535 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers\Mail;
use KTXF\Mail\Collection\CollectionBaseInterface;
use KTXF\Mail\Collection\CollectionMutableInterface;
use KTXF\Mail\Object\Address;
use KTXF\Mail\Object\AddressInterface;
use KTXF\Mail\Service\ServiceBaseInterface;
use KTXF\Mail\Service\ServiceCollectionMutableInterface;
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\Range\IRange;
use KTXF\Resource\Range\Range;
use KTXF\Resource\Range\RangeType;
use KTXF\Resource\Sort\ISort;
use KTXF\Resource\Sort\Sort;
use KTXM\ProviderJmapc\Providers\ServiceIdentityBasic;
use KTXM\ProviderJmapc\Providers\ServiceLocation;
use KTXM\ProviderJmapc\Service\Remote\RemoteMailService;
use KTXM\ProviderJmapc\Service\Remote\RemoteService;
/**
* JMAP Service
*
* Represents a configured JMAP account
*/
class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceConfigurableInterface, ServiceCollectionMutableInterface
{
public const JSON_TYPE = ServiceBaseInterface::JSON_TYPE;
private const PROVIDER_IDENTIFIER = 'jmap';
private ?string $serviceTenantId = null;
private ?string $serviceUserId = null;
private ?string $serviceIdentifier = null;
private ?string $serviceLabel = null;
private bool $serviceEnabled = false;
private bool $serviceDebug = 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:100:256:256',
self::CAPABILITY_COLLECTION_FILTER_ROLE => 's:100:256:256',
],
self::CAPABILITY_COLLECTION_LIST_SORT => [
self::CAPABILITY_COLLECTION_SORT_LABEL,
self::CAPABILITY_COLLECTION_SORT_RANK,
],
self::CAPABILITY_COLLECTION_EXTANT => true,
self::CAPABILITY_COLLECTION_FETCH => true,
self::CAPABILITY_COLLECTION_CREATE => true,
self::CAPABILITY_COLLECTION_MODIFY => true,
self::CAPABILITY_COLLECTION_DESTROY => true,
self::CAPABILITY_ENTITY_LIST => true,
self::CAPABILITY_ENTITY_LIST_FILTER => [
self::CAPABILITY_ENTITY_FILTER_ALL => 's:200:256:256',
self::CAPABILITY_ENTITY_FILTER_FROM => 's:100:256:256',
self::CAPABILITY_ENTITY_FILTER_TO => 's:100:256:256',
self::CAPABILITY_ENTITY_FILTER_CC => 's:100:256:256',
self::CAPABILITY_ENTITY_FILTER_BCC => '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 => 'd:0:32:32',
self::CAPABILITY_ENTITY_FILTER_DATE_AFTER => 'd: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_LIST_SORT => [
self::CAPABILITY_ENTITY_SORT_FROM,
self::CAPABILITY_ENTITY_SORT_TO,
self::CAPABILITY_ENTITY_SORT_SUBJECT,
self::CAPABILITY_ENTITY_SORT_DATE_RECEIVED,
self::CAPABILITY_ENTITY_SORT_DATE_SENT,
self::CAPABILITY_ENTITY_SORT_SIZE,
],
self::CAPABILITY_ENTITY_LIST_RANGE => [
'tally' => ['absolute', 'relative']
],
self::CAPABILITY_ENTITY_DELTA => true,
self::CAPABILITY_ENTITY_EXTANT => true,
self::CAPABILITY_ENTITY_FETCH => true,
];
private readonly RemoteMailService $mailService;
public function __construct(
) {}
private function initialize(): void
{
if (!isset($this->mailService)) {
$client = RemoteService::freshClient($this);
$this->mailService = RemoteService::mailService($client);
}
}
public function toStore(): array
{
return array_filter([
'tid' => $this->serviceTenantId,
'uid' => $this->serviceUserId,
'sid' => $this->serviceIdentifier,
'label' => $this->serviceLabel,
'enabled' => $this->serviceEnabled,
'debug' => $this->serviceDebug,
'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'];
$this->serviceLabel = $data['label'] ?? '';
$this->serviceEnabled = $data['enabled'] ?? false;
$this->serviceDebug = $data['debug'] ?? 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;
}
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): 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])) {
if (is_array($data[self::JSON_PROPERTY_PRIMARY_ADDRESS]) && isset($data[self::JSON_PROPERTY_PRIMARY_ADDRESS]['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($addr['address']),
$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;
}
public function capable(string $value): bool
{
return isset($this->serviceAbilities[$value]);
}
public function capabilities(): array
{
$caps = [];
foreach (array_keys($this->serviceAbilities) as $cap) {
$caps[$cap] = true;
}
return $caps;
}
public function provider(): string
{
return self::PROVIDER_IDENTIFIER;
}
public function identifier(): string|int
{
return $this->serviceIdentifier;
}
public function getLabel(): string|null
{
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 $secondaryAddress) {
if (strtolower($secondaryAddress->getAddress()) === $address) {
return true;
}
}
return false;
}
public function getLocation(): ServiceLocation
{
return $this->location;
}
public function setLocation(ResourceServiceLocationInterface $location): static
{
$this->location = $location;
return $this;
}
public function freshLocation(string|null $type = null, array $data = []): ServiceLocation
{
$location = new ServiceLocation();
$location->jsonDeserialize($data);
return $location;
}
public function getIdentity(): ServiceIdentityBasic
{
return $this->identity;
}
public function setIdentity(ResourceServiceIdentityInterface $identity): static
{
$this->identity = $identity;
return $this;
}
public function freshIdentity(string|null $type, array $data = []): ServiceIdentityBasic
{
$identity = new ServiceIdentityBasic();
$identity->jsonDeserialize($data);
return $identity;
}
public function getDebug(): bool
{
return $this->serviceDebug;
}
public function setDebug(bool $debug): static
{
$this->serviceDebug = $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();
$collections = $this->mailService->collectionList($location, $filter, $sort);
foreach ($collections as &$collection) {
if (is_array($collection) && isset($collection['id'])) {
$object = new CollectionResource(provider: $this->provider(), service: $this->identifier());
$object->fromJmap($collection);
$collection = $object;
}
}
return $collections;
}
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();
return $this->mailService->collectionExtant(...$identifiers);
}
public function collectionFetch(string|int $identifier): ?CollectionBaseInterface
{
$this->initialize();
$collection = $this->mailService->collectionFetch($identifier);
if (is_array($collection) && isset($collection['id'])) {
$object = new CollectionResource(provider: $this->provider(), service: $this->identifier());
$object->fromJmap($collection);
$collection = $object;
}
return $collection;
}
public function collectionFresh(): CollectionMutableInterface
{
return new CollectionResource(provider: $this->provider(), service: $this->identifier());
}
public function collectionCreate(string|int|null $location, CollectionMutableInterface $collection, array $options = []): CollectionBaseInterface
{
$this->initialize();
if ($collection instanceof CollectionResource === false) {
$object = new CollectionResource(provider: $this->provider(), service: $this->identifier());
$object->jsonDeserialize($collection->jsonSerialize());
$collection = $object;
}
$collection = $collection->toJmap();
$collection = $this->mailService->collectionCreate($location, $collection, $options);
$object = new CollectionResource(provider: $this->provider(), service: $this->identifier());
$object->fromJmap($collection);
return $object;
}
public function collectionModify(string|int $identifier, CollectionMutableInterface $collection): CollectionBaseInterface
{
$this->initialize();
if ($collection instanceof CollectionResource === false) {
$object = new CollectionResource(provider: $this->provider(), service: $this->identifier());
$object->jsonDeserialize($collection->jsonSerialize());
$collection = $object;
}
$collection = $collection->toJmap();
$collection = $this->mailService->collectionModify($identifier, $collection);
$object = new CollectionResource(provider: $this->provider(), service: $this->identifier());
$object->fromJmap($collection);
return $object;
}
public function collectionDestroy(string|int $identifier, bool $force = false, bool $recursive = false): bool
{
$this->initialize();
return $this->mailService->collectionDestroy($identifier, $force, $recursive) !== null;
}
public function collectionMove(string|int $identifier, string|int|null $targetLocation): CollectionBaseInterface
{
// TODO: Implement collection move
$this->initialize();
$collection = new CollectionResource(provider: $this->provider(), service: $this->identifier());
return $collection;
}
// Entity operations
public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): array
{
$this->initialize();
$result = $this->mailService->entityList($collection, $filter, $sort, $range, $properties);
$list = [];
foreach ($result['list'] as $index => $entry) {
if (is_array($entry) && isset($entry['id'])) {
$object = new EntityResource(provider: $this->provider(), service: $this->identifier());
$object->fromJmap($entry);
$list[$object->identifier()] = $object;
}
unset($result['list'][$index]);
}
return $list;
}
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 new Range();
}
public function entityDelta(string|int $collection, string $signature, string $detail = 'ids'): Delta
{
$this->initialize();
return $this->mailService->entityDelta($collection, $signature, $detail);
}
public function entityExtant(string|int $collection, string|int ...$identifiers): array
{
$this->initialize();
return $this->mailService->entityExtant(...$identifiers);
}
public function entityFetch(string|int $collection, string|int ...$identifiers): array
{
$this->initialize();
$entities = $this->mailService->entityFetch(...$identifiers);
foreach ($entities as &$entity) {
if (is_array($entity) && isset($entity['id'])) {
$object = new EntityResource(provider: $this->provider(), service: $this->identifier());
$object->fromJmap($entity);
$entity = $object;
}
}
return $entities;
}
}