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,13 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Exception;
class JmapUnknownMethod extends \Exception {
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Request\Contacts;
use JmapClient\Requests\RequestParameters;
class ContactEmailParameters extends RequestParameters {
public function __construct(&$parameters = null) {
parent::__construct($parameters);
}
public function type(string $value): self {
$this->parameter('type', $value);
return $this;
}
public function value(string $value): self {
$this->parameter('value', $value);
return $this;
}
public function label(string $value): self {
$this->parameter('label', $value);
return $this;
}
public function default(bool $value): self {
$this->parameter('isDefault', $value);
return $this;
}
}

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Request\Contacts;
use JmapClient\Requests\RequestParameters;
class ContactLocationParameters extends RequestParameters {
public function __construct(&$parameters = null) {
parent::__construct($parameters);
}
public function type(string $value): self {
$this->parameter('type', $value);
return $this;
}
public function label(string $value): self {
$this->parameter('label', $value);
return $this;
}
public function street(string $value): self {
$this->parameter('street', $value);
return $this;
}
public function locality(string $value): self {
$this->parameter('locality', $value);
return $this;
}
public function region(string $value): self {
$this->parameter('region', $value);
return $this;
}
public function code(string $value): self {
$this->parameter('postcode', $value);
return $this;
}
public function country(string $value): self {
$this->parameter('country', $value);
return $this;
}
public function default(bool $value): self {
$this->parameter('isDefault', $value);
return $this;
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Request\Contacts;
use JmapClient\Requests\RequestParameters;
class ContactOnlineParameters extends RequestParameters {
public function __construct(&$parameters = null) {
parent::__construct($parameters);
}
public function type(string $value): self {
$this->parameter('type', $value);
return $this;
}
public function value(string $value): self {
$this->parameter('value', $value);
return $this;
}
public function label(string $value): self {
$this->parameter('label', $value);
return $this;
}
}

View File

@@ -0,0 +1,171 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Request\Contacts;
use JmapClient\Requests\RequestParameters;
use function PHPUnit\Framework\isEmpty;
class ContactParameters extends RequestParameters {
public const DATE_FORMAT_ANNIVERSARY = 'YYYY-MM-DD';
public function __construct(&$parameters = null) {
parent::__construct($parameters);
}
public function in(string $value): self {
if (isEmpty($value)) {
$this->parameter('addressbookId', 'Default');
} else {
$this->parameter('addressbookId', $value);
}
return $this;
}
public function id(string $value): self {
$this->parameter('id', $value);
return $this;
}
public function uid(string $value): self {
$this->parameter('uid', $value);
return $this;
}
public function type(string $value): self {
$this->parameter('kind', $value);
return $this;
}
public function nameLast(string $value): self {
$this->parameter('lastName', $value);
return $this;
}
public function nameFirst(string $value): self {
$this->parameter('firstName', $value);
return $this;
}
public function namePrefix(string $value): self {
$this->parameter('prefix', $value);
return $this;
}
public function nameSuffix(string $value): self {
$this->parameter('suffix', $value);
return $this;
}
public function organizationName(string $value): self {
$this->parameter('company', $value);
return $this;
}
public function organizationUnit(string $value): self {
$this->parameter('department', $value);
return $this;
}
public function title(string $value): self {
$this->parameter('jobTitle', $value);
return $this;
}
public function notes(string $value): self {
$this->parameter('notes', $value);
return $this;
}
public function priority(int $value): self {
$this->parameter('importance', $value);
return $this;
}
public function birthDay(string $value): self {
$this->parameter('birthday', $value);
return $this;
}
public function nuptialDay(string $value): self {
$this->parameter('anniversary', $value);
return $this;
}
public function email(?int $id = null): ContactEmailParameters {
// Ensure the parameter exists
if (!isset($this->_parameters->emails)) {
$this->_parameters->emails = [];
}
// If an ID is provided, ensure the specific email entry exists
if ($id !== null) {
if (!isset($this->_parameters->emails[$id])) {
$this->_parameters->emails[$id] = new \stdClass();
}
return new ContactEmailParameters($this->_parameters->emails[$id]);
}
// If no ID is provided, create a new email entry
$this->_parameters->emails[] = new \stdClass();
return new ContactEmailParameters(end($this->_parameters->emails));
}
public function phone(?int $id = null): ContactPhoneParameters {
// Ensure the parameter exists
if (!isset($this->_parameters->phones)) {
$this->_parameters->phones = [];
}
// If an ID is provided, ensure the specific phone entry exists
if ($id !== null) {
if (!isset($this->_parameters->phones[$id])) {
$this->_parameters->phones[$id] = new \stdClass();
}
return new ContactPhoneParameters($this->_parameters->phones[$id]);
}
// If no ID is provided, create a new phone entry
$this->_parameters->phones[] = new \stdClass();
return new ContactPhoneParameters(end($this->_parameters->phones));
}
public function location(?int $id = null): ContactLocationParameters {
// Ensure the parameter exists
if (!isset($this->_parameters->addresses)) {
$this->_parameters->addresses = [];
}
// If an ID is provided, ensure the specific address entry exists
if ($id !== null) {
if (!isset($this->_parameters->addresses[$id])) {
$this->_parameters->addresses[$id] = new \stdClass();
}
return new ContactLocationParameters($this->_parameters->addresses[$id]);
}
// If no ID is provided, create a new address entry
$this->_parameters->addresses[] = new \stdClass();
return new ContactLocationParameters(end($this->_parameters->addresses));
}
public function online(?int $id = null): ContactOnlineParameters {
// Ensure the parameter exists
if (!isset($this->_parameters->addresses)) {
$this->_parameters->addresses = [];
}
// If an ID is provided, ensure the specific address entry exists
if ($id !== null) {
if (!isset($this->_parameters->addresses[$id])) {
$this->_parameters->addresses[$id] = new \stdClass();
}
return new ContactOnlineParameters($this->_parameters->addresses[$id]);
}
// If no ID is provided, create a new address entry
$this->_parameters->addresses[] = new \stdClass();
return new ContactOnlineParameters(end($this->_parameters->addresses));
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Request\Contacts;
use JmapClient\Requests\RequestParameters;
class ContactPhoneParameters extends RequestParameters {
public function __construct(&$parameters = null) {
parent::__construct($parameters);
}
public function type(string $value): self {
$this->parameter('type', $value);
return $this;
}
public function value(string $value): self {
$this->parameter('value', $value);
return $this;
}
public function label(string $value): self {
$this->parameter('label', $value);
return $this;
}
public function default(bool $value): self {
$this->parameter('isDefault', $value);
return $this;
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Request\Events;
use JmapClient\Requests\Calendar\EventFilter as EventFilterJmap;
class EventFilter extends EventFilterJmap {
public function in(string $value): self {
$this->condition('inCalendars', [$value]);
return $this;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Response\Contacts;
use JmapClient\Responses\ResponseParameters;
class ContactEmailParameters extends ResponseParameters {
public function type(): ?string {
return $this->parameter('type') ?? 'personal';
}
public function value(): ?string {
return $this->parameter('value');
}
public function label(): ?string {
return $this->parameter('label');
}
public function default(): bool {
return $this->parameter('isDefault');
}
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Response\Contacts;
use JmapClient\Responses\ResponseParameters;
class ContactLocationParameters extends ResponseParameters {
public function type(): ?string {
return $this->parameter('type') ?? 'home';
}
public function label(): ?string {
return $this->parameter('label');
}
public function street(): ?string {
return $this->parameter('street');
}
public function locality(): ?string {
return $this->parameter('locality');
}
public function region(): ?string {
return $this->parameter('region');
}
public function code(): ?string {
return $this->parameter('postcode');
}
public function country(): ?string {
return $this->parameter('country');
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Response\Contacts;
use JmapClient\Responses\ResponseParameters;
class ContactOnlineParameters extends ResponseParameters {
public function type(): ?string {
return $this->parameter('type') ?? 'other';
}
public function value(): ?string {
return $this->parameter('value');
}
public function label(): ?string {
return $this->parameter('label');
}
}

View File

@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Response\Contacts;
use JmapClient\Responses\ResponseParameters;
class ContactParameters extends ResponseParameters {
/* Metadata Properties */
public function in(): ?array {
// return value of parameter
$value = $this->parameter('addressbookId');
if ($value !== null) {
return [$value];
}
return null;
}
public function id(): ?string {
return $this->parameter('id');
}
public function uid(): ?string {
return $this->parameter('uid');
}
public function type(): ?string {
return $this->parameter('kind') ?? 'individual';
}
public function nameLast(): ?string {
return $this->parameter('lastName');
}
public function nameFirst(): ?string {
return $this->parameter('firstName');
}
public function namePrefix(): ?string {
return $this->parameter('prefix');
}
public function nameSuffix(): ?string {
return $this->parameter('suffix');
}
public function organizationName(): ?string {
return $this->parameter('company');
}
public function organizationUnit(): ?string {
return $this->parameter('department');
}
public function title(): ?string {
return $this->parameter('jobTitle');
}
public function notes(): ?string {
return $this->parameter('notes');
}
public function priority(): ?int {
return (int)$this->parameter('importance');
}
public function birthDay(): ?string {
return $this->parameter('birthday');
}
public function nuptialDay(): ?string {
return $this->parameter('anniversary');
}
public function email(): array {
$collection = $this->parameter('emails') ?? [];
foreach ($collection as $key => $data) {
$collection[$key] = new ContactEmailParameters($data);
}
return $collection;
}
public function phone(): array {
$collection = $this->parameter('phones') ?? [];
foreach ($collection as $key => $data) {
$collection[$key] = new ContactPhoneParameters($data);
}
return $collection;
}
public function location(): array {
$collection = $this->parameter('addresses') ?? [];
foreach ($collection as $key => $data) {
$collection[$key] = new ContactLocationParameters($data);
}
return $collection;
}
public function online(): array {
$collection = $this->parameter('online') ?? [];
foreach ($collection as $key => $data) {
$collection[$key] = new ContactOnlineParameters($data);
}
return $collection;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Jmap\FM\Response\Contacts;
use JmapClient\Responses\ResponseParameters;
class ContactPhoneParameters extends ResponseParameters {
public function type(): ?string {
return $this->parameter('type') ?? 'home';
}
public function value(): ?string {
return $this->parameter('value');
}
public function label(): ?string {
return $this->parameter('label');
}
public function default(): bool {
return (bool)$this->parameter('isDefault');
}
}

86
lib/Module.php Normal file
View File

@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc;
use KTXC\Resource\ProviderManager;
use KTXF\Module\ModuleBrowserInterface;
use KTXF\Module\ModuleInstanceAbstract;
use KTXF\Resource\Provider\ProviderInterface;
use KTXM\ProviderJmapc\Providers\Mail\Provider as MailProvider;
use KTXM\ProviderJmapc\Providers\Chrono\Provider as ChronoProvider;
use KTXM\ProviderJmapc\Providers\People\Provider as PeopleProvider;
/**
* JMAP Client Provider Module
*
* Provides mail, calendar, and contacts services via JMAP protocol.
*/
class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
{
public function __construct(
private readonly ProviderManager $providerManager,
) {}
public function handle(): string
{
return 'provider_jmapc';
}
public function label(): string
{
return 'JMAP Provider';
}
public function author(): string
{
return 'Ktrix';
}
public function description(): string
{
return 'JMAP provider module for Ktrix - provides mail, calendar, and contacts via JMAP protocol';
}
public function version(): string
{
return '0.0.1';
}
public function permissions(): array
{
return [
'provider_jmapc' => [
'label' => 'Access JMAP Provider',
'description' => 'View and access the JMAP provider module',
'group' => 'Providers'
],
];
}
public function boot(): void
{
// Register JMAP providers - all three share the same service store
$this->providerManager->register(ProviderInterface::TYPE_MAIL, 'jmap', MailProvider::class);
$this->providerManager->register(ProviderInterface::TYPE_CHRONO, 'jmap', ChronoProvider::class);
$this->providerManager->register(ProviderInterface::TYPE_PEOPLE, 'jmap', PeopleProvider::class);
}
public function registerBI(): array {
return [
'handle' => $this->handle(),
'namespace' => 'ProviderJmapc',
'version' => $this->version(),
'label' => $this->label(),
'author' => $this->author(),
'description' => $this->description(),
'boot' => 'static/module.mjs',
];
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Objects\Mail;
use KTXF\Resource\Filter\Filter;
class RemoteCollectionFilter extends Filter {
protected array $attributes = [
'in' => true,
'name' => true,
'role' => true,
'hasRoles' => true,
'subscribed' => true,
];
}

View File

@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace KTXM\ProviderJmapc\Providers\Chrono;
use KTXF\Chrono\Provider\IProviderBase;
use KTXF\Chrono\Provider\IProviderServiceMutate;
use KTXF\Chrono\Service\IServiceBase;
use KTXF\Chrono\Service\ServiceScope;
use KTXM\ProviderJmapc\Stores\ServiceStore;
/**
* JMAP Chrono Provider
*
* Provides Calendar services via JMAP protocol.
* Filters services by urn:ietf:params:jmap:calendars capability.
*/
class Provider implements IProviderBase, IProviderServiceMutate
{
protected const CALENDAR_CAPABILITY = 'urn:ietf:params:jmap:calendars';
public function __construct(
protected readonly ServiceStore $serviceStore,
) {}
public function capable(string $value): bool
{
$capabilities = [
self::CAPABILITY_SERVICE_LIST,
self::CAPABILITY_SERVICE_FETCH,
self::CAPABILITY_SERVICE_EXTANT,
self::CAPABILITY_SERVICE_FRESH,
self::CAPABILITY_SERVICE_CREATE,
self::CAPABILITY_SERVICE_MODIFY,
self::CAPABILITY_SERVICE_DESTROY,
];
return in_array($value, $capabilities, true);
}
public function capabilities(): array
{
return [
self::CAPABILITY_SERVICE_LIST => true,
self::CAPABILITY_SERVICE_FETCH => true,
self::CAPABILITY_SERVICE_EXTANT => true,
self::CAPABILITY_SERVICE_FRESH => true,
self::CAPABILITY_SERVICE_CREATE => true,
self::CAPABILITY_SERVICE_MODIFY => true,
self::CAPABILITY_SERVICE_DESTROY => true,
];
}
public function id(): string
{
return 'jmap';
}
public function label(): string
{
return 'JMAP Calendar Provider';
}
public function serviceList(string $tenantId, string $userId, array $filter): array
{
// Filter by Calendar capability
return $this->serviceStore->listServices($tenantId, $userId, [self::CALENDAR_CAPABILITY]);
}
public function serviceExtant(string $tenantId, string $userId, array $identifiers): array
{
$result = [];
foreach ($identifiers as $id) {
$service = $this->serviceStore->getService($tenantId, $userId, $id);
$result[$id] = $service !== null;
}
return $result;
}
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?IServiceBase
{
return $this->serviceStore->getService($tenantId, $userId, $identifier);
}
public function serviceFresh(string $userId = ''): IServiceBase
{
return new Service(
scope: ServiceScope::User,
enabled: true,
);
}
public function serviceCreate(string $userId, IServiceBase $service): string
{
if (!($service instanceof Service)) {
throw new \InvalidArgumentException('Service must be instance of JMAP Service');
}
throw new \RuntimeException('Use Mail Provider interface for service creation');
}
public function serviceModify(string $userId, IServiceBase $service): string
{
if (!($service instanceof Service)) {
throw new \InvalidArgumentException('Service must be instance of JMAP Service');
}
throw new \RuntimeException('Use Mail Provider interface for service modification');
}
public function serviceDestroy(string $userId, IServiceBase $service): bool
{
if (!($service instanceof Service)) {
return false;
}
throw new \RuntimeException('Use Mail Provider interface for service destruction');
}
public function jsonSerialize(): array
{
return [
'@type' => 'chrono.provider',
'id' => $this->id(),
'label' => $this->label(),
'capabilities' => $this->capabilities(),
];
}
public function jsonDeserialize(array|string $data): static
{
return $this;
}
}

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

View File

@@ -0,0 +1,136 @@
<?php
declare(strict_types=1);
namespace KTXM\ProviderJmapc\Providers\People;
use KTXF\People\Provider\IProviderBase;
use KTXF\People\Provider\IProviderServiceMutate;
use KTXF\People\Service\IServiceBase;
use KTXF\People\Service\ServiceScope;
use KTXM\ProviderJmapc\Stores\ServiceStore;
/**
* JMAP Contacts Provider
*
* Provides Contacts services via JMAP protocol.
* Filters services by urn:ietf:params:jmap:contacts capability.
*/
class Provider implements IProviderBase, IProviderServiceMutate
{
protected const CONTACTS_CAPABILITY = 'urn:ietf:params:jmap:contacts';
public function __construct(
protected readonly ServiceStore $serviceStore,
) {}
public function capable(string $value): bool
{
$capabilities = [
self::CAPABILITY_SERVICE_LIST,
self::CAPABILITY_SERVICE_FETCH,
self::CAPABILITY_SERVICE_EXTANT,
self::CAPABILITY_SERVICE_FRESH,
self::CAPABILITY_SERVICE_CREATE,
self::CAPABILITY_SERVICE_MODIFY,
self::CAPABILITY_SERVICE_DESTROY,
];
return in_array($value, $capabilities, true);
}
public function capabilities(): array
{
return [
self::CAPABILITY_SERVICE_LIST => true,
self::CAPABILITY_SERVICE_FETCH => true,
self::CAPABILITY_SERVICE_EXTANT => true,
self::CAPABILITY_SERVICE_FRESH => true,
self::CAPABILITY_SERVICE_CREATE => true,
self::CAPABILITY_SERVICE_MODIFY => true,
self::CAPABILITY_SERVICE_DESTROY => true,
];
}
public function id(): string
{
return 'jmap';
}
public function label(): string
{
return 'JMAP Contacts Provider';
}
public function serviceList(string $tenantId, string $userId, array $filter): array
{
// Filter by Contacts capability
return $this->serviceStore->listServices($tenantId, $userId, [self::CONTACTS_CAPABILITY]);
}
public function serviceExtant(string $tenantId, string $userId, array $identifiers): array
{
$result = [];
foreach ($identifiers as $id) {
$service = $this->serviceStore->getService($tenantId, $userId, $id);
$result[$id] = $service !== null;
}
return $result;
}
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?IServiceBase
{
return $this->serviceStore->getService($tenantId, $userId, $identifier);
}
public function serviceFresh(string $userId = ''): IServiceBase
{
return new Service(
scope: ServiceScope::User,
enabled: true,
);
}
public function serviceCreate(string $userId, IServiceBase $service): string
{
if (!($service instanceof Service)) {
throw new \InvalidArgumentException('Service must be instance of JMAP Service');
}
// Note: This simplified interface doesn't pass tenantId
// Will need to get it from SessionTenant in actual implementation
throw new \RuntimeException('Use Mail Provider interface for service creation');
}
public function serviceModify(string $userId, IServiceBase $service): string
{
if (!($service instanceof Service)) {
throw new \InvalidArgumentException('Service must be instance of JMAP Service');
}
throw new \RuntimeException('Use Mail Provider interface for service modification');
}
public function serviceDestroy(string $userId, IServiceBase $service): bool
{
if (!($service instanceof Service)) {
return false;
}
throw new \RuntimeException('Use Mail Provider interface for service destruction');
}
public function jsonSerialize(): array
{
return [
'@type' => 'people.provider',
'id' => $this->id(),
'label' => $this->label(),
'capabilities' => $this->capabilities(),
];
}
public function jsonDeserialize(array|string $data): static
{
return $this;
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers;
use KTXF\Resource\Provider\ResourceServiceIdentityBasic;
/**
* JMAP Service Basic Identity
*
* Username/password authentication for JMAP.
*/
class ServiceIdentityBasic implements ResourceServiceIdentityBasic
{
public function __construct(
private string $identity = '',
private string $secret = '',
) {}
public function toStore(): array
{
return [
'type' => self::TYPE_BASIC,
'identity' => $this->identity,
'secret' => $this->secret,
];
}
public function fromStore(array $data): self
{
return new self(
identity: $data['identity'] ?? '',
secret: $data['secret'] ?? '',
);
}
public function jsonSerialize(): array
{
return [
'type' => self::TYPE_BASIC,
'identity' => $this->identity,
// Password intentionally omitted from serialization for security
];
}
public function jsonDeserialize(array|string $data): static
{
if (is_string($data)) {
$data = json_decode($data, true);
}
$this->identity = $data['identity'] ?? '';
$this->secret = $data['secret'] ?? '';
return $this;
}
public function type(): string
{
return self::TYPE_BASIC;
}
public function getIdentity(): string
{
return $this->identity;
}
public function setIdentity(string $value): void
{
$this->identity = $value;
}
public function getSecret(): string
{
return $this->secret;
}
public function setSecret(string $value): void
{
$this->secret = $value;
}
}

View File

@@ -0,0 +1,153 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Providers;
use KTXF\Resource\Provider\ResourceServiceLocationUri;
/**
* JMAP Service Location
*
* Connection details for JMAP server.
*/
class ServiceLocation implements ResourceServiceLocationUri
{
public function __construct(
private string $host = '',
private int $port = 443,
private string $scheme = 'https',
private string $path = '/.well-known/jmap',
private bool $verifyPeer = true,
private bool $verifyHost = true,
) {
$testing = 'test';
}
public function toStore(): array
{
return $this->jsonSerialize();
}
public function fromStore(array $data): static
{
return $this->jsonDeserialize($data);
}
public function jsonSerialize(): array
{
return array_filter([
'type' => self::TYPE_URI,
'host' => $this->host ?? '',
'port' => $this->port ?? 443,
'scheme' => $this->scheme,
'path' => $this->path,
'verifyPeer' => $this->verifyPeer,
'verifyHost' => $this->verifyHost,
], fn($v) => $v !== null && $v !== '');
}
public function jsonDeserialize(array|string $data): static
{
if (is_string($data)) {
$data = json_decode($data, true);
}
$this->host = $data['host'] ?? '';
$this->port = (int)($data['port'] ?? 443);
$this->scheme = $data['scheme'] ?? 'https';
$this->path = $data['path'] ?? '';
$this->verifyPeer = $data['verifyPeer'] ?? true;
$this->verifyHost = $data['verifyHost'] ?? true;
return $this;
}
public function type(): string
{
return self::TYPE_URI;
}
public function location(): string
{
$uri = $this->scheme . '://' . $this->host;
// Add port if not default for scheme
if (($this->scheme === 'https' && $this->port !== 443) ||
($this->scheme === 'http' && $this->port !== 80)) {
$uri .= ':' . $this->port;
}
// Add path if present
if ($this->path !== '') {
$uri .= '/' . ltrim($this->path, '/');
}
return $uri;
}
public function getScheme(): string
{
return $this->scheme;
}
public function setScheme(string $value): void
{
$this->scheme = $value;
}
public function getHost(): string
{
return $this->host;
}
public function setHost(string $value): void
{
$this->host = $value;
}
public function getPort(): int
{
return $this->port;
}
public function setPort(int $value): void
{
$this->port = $value;
}
public function getPath(): string
{
return $this->path;
}
public function setPath(string $value): void
{
$this->path = $value;
}
public function getVerifyPeer(): bool
{
return $this->verifyPeer;
}
public function setVerifyPeer(bool $value): void
{
$this->verifyPeer = $value;
}
public function getVerifyHost(): bool
{
return $this->verifyHost;
}
public function setVerifyHost(bool $value): void
{
$this->verifyHost = $value;
}
}

289
lib/Service/Discovery.php Normal file
View File

@@ -0,0 +1,289 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Service;
use KTXM\ProviderJmapc\Providers\ServiceLocation;
/**
* JMAP Service Discovery
*
* Implements RFC 8620 service discovery via:
* 1. DNS SRV records (_jmap._tcp.<domain>)
* 2. Well-known URI (https://<host>/.well-known/jmap)
*/
class Discovery
{
private const WELL_KNOWN_PATH = '/.well-known/jmap';
private const DEFAULT_PORT_HTTPS = 443;
private const DEFAULT_PORT_HTTP = 80;
private const CONNECTION_TIMEOUT = 10;
private const MAX_REDIRECTS = 3;
/**
* Discover JMAP service location from email address or domain
*
* @param string $identity Email address or domain
* @param string|null $location Optional hostname to test directly (bypasses DNS SRV)
* @param string|null $secret Optional password/token to validate the service
* @param bool $verifySSL Whether to verify SSL certificates
* @return ServiceLocation|null Discovered service location or null if not found
*/
public function discover(
string $identity,
?string $location = null,
?string $secret = null,
bool $verifySSL = true
): ?ServiceLocation {
// If location is provided, test it directly
if ($location !== null && $location !== '') {
$host = $this->extractDomain($location);
if ($host !== null) {
$result = $this->testWellKnownUri($host, self::DEFAULT_PORT_HTTPS, $verifySSL, 'https', $identity, $secret);
if ($result !== null) {
return $result;
}
// Try HTTP if HTTPS failed
$result = $this->testWellKnownUri($host, self::DEFAULT_PORT_HTTP, $verifySSL, 'http', $identity, $secret);
if ($result !== null) {
return $result;
}
}
return null;
}
// Extract domain from email address if needed
$domain = $this->extractDomain($identity);
if ($domain === null) {
return null;
}
// Try DNS SRV lookup first (RFC 8620 recommended method)
$srvResult = $this->discoverViaSRV($domain);
if ($srvResult !== null) {
$result = $this->testWellKnownUri(
$srvResult['host'],
$srvResult['port'],
$verifySSL,
'https',
$identity,
$secret
);
if ($result !== null) {
return $result;
}
}
// Fallback: Try well-known URI directly on domain with HTTPS
$result = $this->testWellKnownUri($domain, self::DEFAULT_PORT_HTTPS, $verifySSL, 'https', $identity, $secret);
if ($result !== null) {
return $result;
}
// Last resort: Try HTTP (not recommended, but some servers may use it)
$result = $this->testWellKnownUri($domain, self::DEFAULT_PORT_HTTP, $verifySSL, 'http', $identity, $secret);
if ($result !== null) {
return $result;
}
return null;
}
/**
* Extract domain from email address or return as-is if already a domain
*/
private function extractDomain(string $identity): ?string
{
$identity = trim($identity);
// If it contains @, extract domain part
if (str_contains($identity, '@')) {
$parts = explode('@', $identity);
return strtolower(trim($parts[1] ?? ''));
}
// Otherwise treat as domain
$domain = strtolower($identity);
// Remove protocol if present
$domain = preg_replace('#^https?://#i', '', $domain);
// Remove path if present
$domain = explode('/', $domain)[0];
return $domain !== '' ? $domain : null;
}
/**
* Discover JMAP service via DNS SRV record
*
* Queries for _jmap._tcp.<domain> SRV record
*
* @return array{host: string, port: int}|null
*/
private function discoverViaSRV(string $domain): ?array
{
$srvRecord = "_jmap._tcp.{$domain}";
try {
$records = @dns_get_record($srvRecord, DNS_SRV);
if ($records === false || empty($records)) {
return null;
}
// Use first record (they can be prioritized, but we'll keep it simple)
$record = $records[0];
if (isset($record['target']) && isset($record['port'])) {
return [
'host' => rtrim($record['target'], '.'),
'port' => (int)$record['port'],
];
}
} catch (\Exception $e) {
// DNS lookup failed, silently continue to fallback methods
}
return null;
}
/**
* Test well-known JMAP URI and validate response
*
* Optionally validates with credentials if secret is provided
*
* @return ServiceLocation|null
*/
private function testWellKnownUri(
string $host,
int $port,
bool $verifySSL,
string $scheme = 'https',
?string $identity = null,
?string $secret = null
): ?ServiceLocation {
$url = $this->buildWellKnownUrl($host, $port, $scheme);
try {
$ch = curl_init($url);
if ($ch === false) {
return null;
}
$curlOptions = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => self::MAX_REDIRECTS,
CURLOPT_TIMEOUT => self::CONNECTION_TIMEOUT,
CURLOPT_SSL_VERIFYPEER => $verifySSL,
CURLOPT_SSL_VERIFYHOST => $verifySSL ? 2 : 0,
CURLOPT_HTTPHEADER => [
'Accept: application/json',
],
];
// Add basic auth if credentials provided
if ($identity !== null && $secret !== null) {
$curlOptions[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
$curlOptions[CURLOPT_USERPWD] = "{$identity}:{$secret}";
}
curl_setopt_array($ch, $curlOptions);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Must be 200 OK (or 401 if we didn't provide auth - still proves service exists)
if ($httpCode === 401 && ($identity === null || $secret === null)) {
// Service exists but requires auth - that's fine for discovery
return new ServiceLocation(
host: $host,
port: $port,
scheme: $scheme,
path: self::WELL_KNOWN_PATH,
verifyPeer: $verifySSL,
verifyHost: $verifySSL,
);
}
if ($httpCode !== 200 || $response === false) {
return null;
}
// Parse and validate JMAP session response
$data = json_decode($response, true);
if (!$this->isValidJmapSession($data)) {
return null;
}
// Create ServiceLocation with discovered settings
return new ServiceLocation(
host: $host,
port: $port,
scheme: $scheme,
path: self::WELL_KNOWN_PATH,
verifyPeer: $verifySSL,
verifyHost: $verifySSL,
);
} catch (\Exception $e) {
return null;
}
}
/**
* Build well-known JMAP URL
*/
private function buildWellKnownUrl(string $host, int $port, string $scheme): string
{
$url = "{$scheme}://{$host}";
// Add port if non-standard
if (($scheme === 'https' && $port !== self::DEFAULT_PORT_HTTPS) ||
($scheme === 'http' && $port !== self::DEFAULT_PORT_HTTP)) {
$url .= ":{$port}";
}
$url .= self::WELL_KNOWN_PATH;
return $url;
}
/**
* Validate that response is a proper JMAP session object
*
* According to RFC 8620, session must contain at minimum:
* - apiUrl: The URL to use for JMAP API requests
* - capabilities: Object describing server capabilities
*/
private function isValidJmapSession(mixed $data): bool
{
if (!is_array($data)) {
return false;
}
// Must have apiUrl
if (!isset($data['apiUrl']) || !is_string($data['apiUrl'])) {
return false;
}
// Must have capabilities object
if (!isset($data['capabilities']) || !is_array($data['capabilities'])) {
return false;
}
// Should have mail capability for our use case
// But we'll be lenient and just check the basics above
return true;
}
}

187
lib/Service/MailService.php Normal file
View File

@@ -0,0 +1,187 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Service;
use JmapClient\Client;
use KTXF\Resource\Range\IRange;
use KTXM\ProviderJmapc\Service\Remote\RemoteMailService;
use KTXM\ProviderJmapc\Service\Remote\RemoteService;
class MailService {
protected Client $dataStore;
protected RemoteMailService $remoteMailService;
protected $localMetaStore;
protected $localBlobStore;
protected string $servicePrimaryAccount = '';
protected string $serviceSelectedAccount = '';
protected array $serviceAvailableAccounts = [];
protected string $servicePrimaryIdentity = '';
protected string $serviceSelectedIdentity = '';
protected array $serviceAvailableIdentities = [];
protected array $serviceCollectionRoles = [];
public function __construct(
) { }
public function initialize(Client $dataStore): void {
$this->dataStore = $dataStore;
// evaluate if client is connected
if (!$this->dataStore->sessionStatus()) {
$this->dataStore->connect();
}
// initialize remote service
$this->remoteMailService = RemoteService::mailService($dataStore);
// initialize internal settings
$this->initializeSession();
$this->initializeCollectionRoles();
}
protected function initializeSession() {
// retrieve default account
$this->servicePrimaryAccount = $this->dataStore->sessionAccountDefault('mail');
$this->serviceSelectedAccount = $this->servicePrimaryAccount;
// retrieve accounts
$this->serviceAvailableAccounts = $this->dataStore->sessionAccounts();
// retrieve identities
$collection = $this->remoteMailService->identityFetch($this->servicePrimaryAccount);
foreach ($collection as $entry) {
$this->serviceAvailableIdentities[$entry->address()] = $entry;
}
}
protected function initializeCollectionRoles() {
// retrieve collections
$collectionList = $this->collectionList('', '');
// find collection with roles
foreach ($collectionList as $entry) {
$this->serviceCollectionRoles[$entry->getRole()] = $entry->id();
}
}
public function collectionList(string $location, string $scope, array $options = []): array {
return $this->remoteMailService->collectionList($this->serviceSelectedAccount, $location, $scope);
}
public function collectionFetch(string $location, string $id, array $options = []): object {
return $this->remoteMailService->collectionFetch($this->serviceSelectedAccount, $location, $id);
}
public function collectionCreate(string $location, string $label, array $options = []): string {
return $this->remoteMailService->collectionCreate($this->serviceSelectedAccount, $location, $label);
}
public function collectionUpdate(string $location, string $id, string $label, array $options = []): string {
return $this->remoteMailService->collectionUpdate($this->serviceSelectedAccount, $location, $id, $label);
}
public function collectionDelete(string $location, string $id, array $options = []): string {
return $this->remoteMailService->collectionDelete($this->serviceSelectedAccount, $location, $id);
}
public function collectionMove(string $sourceLocation, string $id, string $destinationLocation, array $options = []): string {
return $this->remoteMailService->collectionMove($this->serviceSelectedAccount, $sourceLocation, $id, $destinationLocation);
}
public function entityList(string $location, ?IRange $range = null, ?string $sort = null, string $particulars = 'D', array $options = []): array {
return $this->remoteMailService->entityList($this->serviceSelectedAccount, $location, $range, $sort, $particulars);
}
public function entityFetch(string $location, string $id, string $particulars = 'D', array $options = []): object {
return $this->remoteMailService->entityFetch($this->serviceSelectedAccount, $location, $id, $particulars);
}
public function entityCreate(string $location, IMessage $message, array $options = []): string {
return $this->remoteMailService->entityCreate($this->serviceSelectedAccount, $location, $message);
}
public function entityUpdate(string $location, string $id, IMessage $message, array $options = []): string {
return $this->remoteMailService->entityUpdate($this->serviceSelectedAccount, $location, $id, $message);
}
public function entityDelete(string $location, string $id, array $options = []): string {
return $this->remoteMailService->entityDelete($this->serviceSelectedAccount, $location, $id);
}
public function entityCopy(string $sourceLocation, string $id, string $destinationLocation, array $options = []): string {
// perform action
return $this->remoteMailService->entityCopy($this->serviceSelectedAccount, $sourceLocation, $id, $destinationLocation);
}
public function entityMove(string $sourceLocation, string $id, string $destinationLocation, array $options = []): string {
// perform action
return $this->remoteMailService->entityMove($this->serviceSelectedAccount, $sourceLocation, $id, $destinationLocation);
}
public function entityForward(string $location, string $id, IMessage $message, array $options = []): string {
// perform action
return $this->remoteMailService->entityForward($this->serviceSelectedAccount, $location, $id, $message);
}
public function entityReply(string $location, string $id, IMessage $message, array $options = []): string {
// perform action
return $this->remoteMailService->entityReply($this->serviceSelectedAccount, $location, $id, $message);
}
public function entitySend(IMessage $message, array $options = []): string {
// extract from address
$from = $message->getFrom();
// determine if identity exists for this from address
if (isset($this->serviceAvailableIdentities[$from->getAddress()])) {
$selectedIdentity = $this->serviceAvailableIdentities[$from->getAddress()]->id();
}
// perform action
return $this->remoteMailService->entitySend($selectedIdentity, $message, $this->serviceCollectionRoles['drafts'], $this->serviceCollectionRoles['sent']);
}
public function blobFetch(string $id): object {
return $this->remoteMailService->blobFetch($this->serviceSelectedAccount, $id);
}
}

View File

@@ -0,0 +1,343 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Sebastian Krupinski <krupinski01@gmail.com>
*
* @author Sebastian Krupinski <krupinski01@gmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace KTXM\ProviderJmapc\Service\Remote\FM;
use DateTimeImmutable;
use JmapClient\Client;
use OCA\JMAPC\Jmap\FM\Request\Contacts\ContactParameters as ContactParametersRequest;
use OCA\JMAPC\Objects\Contact\ContactAnniversaryObject;
use OCA\JMAPC\Objects\Contact\ContactAnniversaryTypes;
use OCA\JMAPC\Objects\Contact\ContactEmailObject;
use OCA\JMAPC\Objects\Contact\ContactNoteObject;
use OCA\JMAPC\Objects\Contact\ContactObject as ContactObject;
use OCA\JMAPC\Objects\Contact\ContactOrganizationObject;
use OCA\JMAPC\Objects\Contact\ContactPhoneObject;
use OCA\JMAPC\Objects\Contact\ContactPhysicalLocationObject;
use OCA\JMAPC\Objects\Contact\ContactTitleObject;
use OCA\JMAPC\Objects\Contact\ContactTitleTypes;
use OCA\JMAPC\Objects\Contact\ContactVirtualLocationObject;
use OCA\JMAPC\Objects\OriginTypes;
use OCA\JMAPC\Service\Remote\RemoteContactsService;
class RemoteContactsServiceFM extends RemoteContactsService {
private const DATE_ANNIVERSARY = 'Y-m-d';
public function __construct() {
}
public function initialize(Client $dataStore, ?string $dataAccount = null) {
parent::initialize($dataStore, $dataAccount);
$this->resourceNamespace = 'https://www.fastmail.com/dev/contacts';
$this->resourceCollectionLabel = null;
$this->resourceEntityLabel = 'Contact';
$dataStore->configureRequestTypes('parameters', 'Contact.object', 'OCA\JMAPC\Jmap\FM\Request\Contacts\ContactParameters');
$dataStore->configureResponseTypes('command', 'Contact/get', 'JmapClient\Responses\Contacts\ContactGet');
$dataStore->configureResponseTypes('command', 'Contact/set', 'JmapClient\Responses\Contacts\ContactSet');
$dataStore->configureResponseTypes('command', 'Contact/changes', 'JmapClient\Responses\Contacts\ContactChanges');
$dataStore->configureResponseTypes('command', 'Contact/query', 'JmapClient\Responses\Contacts\ContactQuery');
$dataStore->configureResponseTypes('command', 'Contact/queryChanges', 'JmapClient\Responses\Contacts\ContactQueryChanges');
$dataStore->configureResponseTypes('parameters', 'Contact', 'OCA\JMAPC\Jmap\FM\Response\ContactParameters');
$dataStore->configureResponseTypes('parameters', 'Contact', 'OCA\JMAPC\Jmap\FM\Response\Contacts\ContactParameters');
}
/**
* convert jmap object to contact object
*
* @since Release 1.0.0
*
*/
public function toContactObject($so): ContactObject {
// create object
$do = new ContactObject();
// source origin
$do->Origin = OriginTypes::External;
// id
if ($so->id()) {
$do->ID = $so->id();
}
// universal id
if ($so->uid()) {
$do->UUID = $so->uid();
}
// name - last
if ($so->nameLast()) {
$do->Name->Last = $so->nameLast();
}
// name - first
if ($so->nameFirst()) {
$do->Name->First = $so->nameFirst();
}
// name - prefix
if ($so->namePrefix()) {
$do->Name->Prefix = $so->namePrefix();
}
// name - suffix
if ($so->nameSuffix()) {
$do->Name->Suffix = $so->nameSuffix();
}
// anniversary - birth day
if ($so->birthday() && $so->birthday() !== '0000-00-00') {
$when = new DateTimeImmutable($so->birthday());
if ($when) {
$anniversary = new ContactAnniversaryObject();
$anniversary->Type = ContactAnniversaryTypes::Birth;
$anniversary->When = $when;
$do->Anniversaries[] = $anniversary;
}
}
// anniversary - nuptial day
if ($so->nuptialDay() && $so->nuptialDay() !== '0000-00-00') {
$when = new DateTimeImmutable($so->nuptialDay());
if ($when) {
$anniversary = new ContactAnniversaryObject();
$anniversary->Type = ContactAnniversaryTypes::Nuptial;
$anniversary->When = $when;
$do->Anniversaries[] = $anniversary;
}
}
// physical location(s)
foreach ($so->location() as $id => $entry) {
$location = new ContactPhysicalLocationObject();
$location->Context = $entry->type();
$location->Label = $entry->label();
$location->Street = $entry->street();
$location->Locality = $entry->locality();
$location->Region = $entry->region();
$location->Code = $entry->code();
$location->Country = $entry->country();
$location->Id = (string)$id;
$location->Index = $id;
$do->PhysicalLocations[$id] = $location;
}
// phone(s)
foreach ($so->phone() as $id => $entry) {
$phone = new ContactPhoneObject();
$phone->Context = $entry->type();
$phone->Number = $entry->value();
$phone->Label = $entry->label();
$phone->Id = (string)$id;
$phone->Index = $id;
if ($entry->default()) {
$phone->Priority = 1;
}
$do->Phone[$id] = $phone;
}
// email(s)
foreach ($so->email() as $id => $entry) {
$email = new ContactEmailObject();
$email->Context = $entry->type();
$email->Address = $entry->value();
$email->Id = (string)$id;
$email->Index = $id;
$do->Email[$id] = $email;
}
// organization - name
if ($so->organizationName()) {
$organization = new ContactOrganizationObject();
$organization->Label = $so->organizationName();
$organization->Id = '0';
$organization->Index = 0;
$organization->Priority = 1;
$do->Organizations[] = $organization;
}
// title
if ($so->title()) {
$title = new ContactTitleObject();
$title->Kind = ContactTitleTypes::Title;
$title->Label = $so->title();
$title->Id = '0';
$title->Index = 0;
$title->Priority = 1;
$do->Titles[] = $title;
}
// notes
if ($so->notes()) {
$note = new ContactNoteObject();
$note->Content = $so->notes();
$note->Id = '0';
$note->Index = 0;
$note->Priority = 1;
$do->Notes[] = $note;
}
// virtual locations
if ($so->online()) {
foreach ($so->online() as $id => $entry) {
$entity = new ContactVirtualLocationObject();
$entity->Location = $entry->value();
$entity->Context = $entry->type();
$entity->Label = $entry->label();
$email->Id = (string)$id;
$email->Index = $id;
$do->VirtualLocations[$id] = $entity;
}
}
return $do;
}
/**
* convert contact object to jmap object
*
* @since Release 1.0.0
*
*/
public function fromContactObject(ContactObject $so): mixed {
// create object
$do = new ContactParametersRequest();
// universal id
if ($so->UUID) {
$do->uid($so->UUID);
}
// name - last
if ($so->Name->Last) {
$do->nameLast($so->Name->Last);
}
// name - first
if ($so->Name->First) {
$do->nameFirst($so->Name->First);
}
// name - prefix
if ($so->Name->Prefix) {
$do->namePrefix($so->Name->Prefix);
}
// name - suffix
if ($so->Name->Suffix) {
$do->nameSuffix($so->Name->Suffix);
}
// aliases
// only one aliases is supported
if ($so->Name->Aliases->count() > 0) {
$priority = $so->Name->Aliases->highestPriority();
$do->organizationName($so->Name->Aliases[$priority]->Label);
}
// anniversaries
$delta = [ContactAnniversaryTypes::Birth->name => true, ContactAnniversaryTypes::Nuptial->name => true];
foreach ($so->Anniversaries as $id => $entry) {
if ($entry->When === null) {
continue;
}
if ($entry->Type === ContactAnniversaryTypes::Birth) {
$do->birthday($entry->When->format(self::DATE_ANNIVERSARY));
unset($delta[ContactAnniversaryTypes::Birth->name]);
}
if ($entry->Type === ContactAnniversaryTypes::Nuptial) {
$do->nuptialDay($entry->When->format(self::DATE_ANNIVERSARY));
unset($delta[ContactAnniversaryTypes::Nuptial->name]);
}
}
foreach ($delta as $key => $value) {
if ($key === ContactAnniversaryTypes::Birth->name) {
$do->birthday('0000-00-00');
}
if ($key === ContactAnniversaryTypes::Nuptial->name) {
$do->nuptialDay('0000-00-00');
}
}
// phone(s)
foreach ($so->Phone as $id => $entry) {
$entity = $do->phone($id);
$entity->value((string)$entry->Number);
$context = strtolower($entry->Context);
if (in_array($context, ['home', 'work', 'mobile', 'fax', 'page', 'other'], true)) {
$entity->type($entry->Context);
} else {
$entity->type('other');
$entity->label($entry->Context);
}
if ($entry->Priority === 1) {
$entity->default(true);
}
}
// email(s)
foreach ($so->Email as $id => $entry) {
$entity = $do->email($id);
$entity->value((string)$entry->Address);
$context = strtolower($entry->Context);
if (in_array($context, ['personal', 'work', 'other'], true)) {
$entity->type($entry->Context);
} else {
$entity->type('other');
$entity->label($entry->Context);
}
if ($entry->Priority === 1) {
$entity->default(true);
}
}
// physical location(s)
foreach ($so->PhysicalLocations as $id => $entry) {
$entity = $do->location($id);
$entity->type((string)$entry->Context);
$entity->label((string)$entry->Label);
$entity->street((string)$entry->Street);
$entity->locality((string)$entry->Locality);
$entity->region((string)$entry->Region);
$entity->code((string)$entry->Code);
$entity->country((string)$entry->Country);
if ($entry->Priority === 1) {
$entity->default(true);
}
}
// organization - name
// only one organization is supported
if ($so->Organizations->count() > 0) {
$priority = $so->Organizations->highestPriority();
$do->organizationName($so->Organizations[$priority]->Label);
}
// titles
// only one title is supported
if ($so->Titles->count() > 0) {
$priority = $so->Titles->highestPriority(ContactTitleTypes::Title);
if ($priority !== null) {
$do->title($so->Titles[$priority]->Label);
}
}
// notes
// only one note is supported
if ($so->Notes->count() > 0) {
$do->notes($so->Notes[0]->Content);
}
// virtual locations
foreach ($so->VirtualLocations as $id => $entry) {
$entity = $do->online($id);
$entity->type((string)$entry->Context);
$entity->value((string)$entry->Location);
$entity->label((string)$entry->Label);
}
return $do;
}
}

View File

@@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Sebastian Krupinski <krupinski01@gmail.com>
*
* @author Sebastian Krupinski <krupinski01@gmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace KTXM\ProviderJmapc\Service\Remote\FM;
use Exception;
use JmapClient\Client;
use JmapClient\Requests\Calendar\EventChanges;
use JmapClient\Requests\Calendar\EventGet;
use JmapClient\Responses\ResponseException;
use OCA\JMAPC\Exceptions\JmapUnknownMethod;
use OCA\JMAPC\Objects\BaseStringCollection;
use OCA\JMAPC\Objects\DeltaObject;
use OCA\JMAPC\Service\Remote\RemoteEventsService;
class RemoteEventsServiceFM extends RemoteEventsService {
public function initialize(Client $dataStore, ?string $dataAccount = null) {
parent::initialize($dataStore, $dataAccount);
$dataStore->configureRequestTypes('parameters', 'CalendarEvent.filter', 'OCA\JMAPC\Jmap\FM\Request\Events\EventFilter');
}
/**
* delta of changes for specific collection in remote storage
*
* @since Release 1.0.0
*
*/
public function entityDeltaSpecific(?string $location, string $state, string $granularity = 'D'): DeltaObject {
// construct set request
$r0 = new EventChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// set state constraint
if (!empty($state)) {
$r0->state($state);
} else {
$r0->state('0');
}
// construct get for created
$r1 = new EventGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r1->targetFromRequest($r0, '/created');
$r1->property('calendarIds', 'id', 'created', 'updated');
// construct get for updated
$r2 = new EventGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r2->targetFromRequest($r0, '/updated');
$r2->property('calendarIds', 'id', 'created', 'updated');
// transceive
$bundle = $this->dataStore->perform([$r0, $r1, $r2]);
// extract response
$response0 = $bundle->response(0);
$response1 = $bundle->response(1);
$response2 = $bundle->response(2);
// determine if command errored
if ($response0 instanceof ResponseException) {
if ($response0->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response0->description(), 1);
} else {
throw new Exception($response0->type() . ': ' . $response0->description(), 1);
}
}
// convert jmap object to delta object
$delta = new DeltaObject();
$delta->signature = $response0->stateNew();
$delta->additions = new BaseStringCollection();
foreach ($response1->objects() as $entry) {
if (in_array($location, $entry->in())) {
$delta->additions[] = $entry->id();
}
}
$delta->modifications = new BaseStringCollection();
foreach ($response2->objects() as $entry) {
if (in_array($location, $entry->in())) {
$delta->modifications[] = $entry->id();
}
}
$delta->deletions = new BaseStringCollection();
foreach ($response0->deleted() as $entry) {
$delta->deletions[] = $entry;
}
return $delta;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,219 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Sebastian Krupinski <krupinski01@gmail.com>
*
* @author Sebastian Krupinski <krupinski01@gmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace KTXM\ProviderJmapc\Service\Remote;
use Exception;
use JmapClient\Client;
use JmapClient\Requests\Blob\BlobGet;
use JmapClient\Requests\Core\SubscriptionGet;
use JmapClient\Requests\Core\SubscriptionParameters as SubscriptionParametersRequest;
use JmapClient\Requests\Core\SubscriptionSet;
use JmapClient\Responses\Core\SubscriptionParameters as SubscriptionParametersResponse;
use JmapClient\Responses\ResponseException;
use OCA\JMAPC\Exceptions\JmapUnknownMethod;
class RemoteCoreService {
protected Client $dataStore;
protected string $dataAccount;
protected ?string $resourceNamespace = null;
protected ?string $resourceCollectionLabel = null;
protected ?string $resourceEntityLabel = null;
public function __construct() {
}
public function initialize(Client $dataStore, ?string $dataAccount = null) {
$this->dataStore = $dataStore;
// evaluate if client is connected
if (!$this->dataStore->sessionStatus()) {
$this->dataStore->connect();
}
// determine account
if ($dataAccount === null) {
if ($this->resourceNamespace !== null) {
$account = $dataStore->sessionAccountDefault($this->resourceNamespace, false);
} else {
$account = $dataStore->sessionAccountDefault('contacts');
}
$this->dataAccount = $account !== null ? $account->id() : '';
} else {
$this->dataAccount = $dataAccount;
}
}
/**
* list of subscriptions in remote storage
*
* @since Release 1.0.0
*
* @return array<string,SubscriptionParametersResponse>
*/
public function subscriptionList(): array {
// construct request
$r0 = new SubscriptionGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// determine if command errored
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap objects to collection objects
$list = [];
foreach ($response->objects() as $so) {
if (!$so instanceof SubscriptionParametersResponse) {
continue;
}
$list[] = $so;
}
// return collection of collections
return $list;
}
/**
* retrieve subscription for specific collection
*
* @since Release 1.0.0
*/
public function subscriptionFetch(string $id): ?SubscriptionParametersResponse {
// construct request
$r0 = new SubscriptionGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
if (!empty($id)) {
$r0->target($id);
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert jmap object to collection object
$so = $response->object(0);
$to = null;
if ($so instanceof SubscriptionParametersResponse) {
$to = $so;
}
return $to;
}
/**
* create subscription in remote storage
*
* @since Release 1.0.0
*/
public function subscriptionCreate(SubscriptionParametersRequest $so): string {
// construct request
$r0 = new SubscriptionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
$r0->create('1', $so);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// return collection id
return (string)$response->created()['1']['id'];
}
/**
* modify collection in remote storage
*
* @since Release 1.0.0
*/
public function subscriptionModify(string $id, SubscriptionParametersRequest $so): string {
// construct request
$r0 = new SubscriptionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
$r0->update($id, $so);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// return collection id
return array_key_exists($id, $response->updated()) ? (string)$id : '';
}
/**
* retrieve blob from remote storage
*
* @since Release 1.0.0
*
*/
public function blobFetch(string $account, string $id): Object {
// TODO: testing remove later
//$data = '';
//$this->dataStore->download($account, $id, $data);
//return null;
// construct get request
$r0 = new BlobGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// construct object
$r0->target($id);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert json object to message object and return
return $response->object(0);
}
/**
* deposit bolb to remote storage
*
* @since Release 1.0.0
*
*/
public function blobDeposit(string $account, string $type, &$data): array {
// TODO: testing remove later
$response = $this->dataStore->upload($account, $type, $data);
// convert response to object
$response = json_decode($response, true);
return $response;
/*
// construct set request
$r0 = new BlobSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel)
// construct object
$r0->target($id);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert json object to message object and return
return $response->object(0);
*/
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,897 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Service\Remote;
use Exception;
use JmapClient\Client;
use JmapClient\Requests\Blob\BlobGet;
use JmapClient\Requests\Blob\BlobSet;
use JmapClient\Requests\Mail\MailboxGet;
use JmapClient\Requests\Mail\MailboxParameters as MailboxParametersRequest;
use JmapClient\Requests\Mail\MailboxQuery;
use JmapClient\Requests\Mail\MailboxSet;
use JmapClient\Requests\Mail\MailChanges;
use JmapClient\Requests\Mail\MailGet;
use JmapClient\Requests\Mail\MailIdentityGet;
use JmapClient\Requests\Mail\MailParameters as MailParametersRequest;
use JmapClient\Requests\Mail\MailQuery;
use JmapClient\Requests\Mail\MailQueryChanges;
use JmapClient\Requests\Mail\MailSet;
use JmapClient\Requests\Mail\MailSubmissionSet;
use JmapClient\Responses\Mail\MailboxParameters as MailboxParametersResponse;
use JmapClient\Responses\Mail\MailParameters as MailParametersResponse;
use JmapClient\Responses\ResponseException;
use KTXF\Resource\Delta\Delta;
use KTXF\Resource\Delta\DeltaCollection;
use KTXF\Resource\Filter\Filter;
use KTXF\Resource\Filter\IFilter;
use KTXF\Resource\Range\IRange;
use KTXF\Resource\Range\IRangeTally;
use KTXF\Resource\Range\Range;
use KTXF\Resource\Range\RangeAnchorType;
use KTXF\Resource\Range\RangeTally;
use KTXF\Resource\Sort\ISort;
use KTXF\Resource\Sort\Sort;
use KTXM\ProviderJmapc\Exception\JmapUnknownMethod;
use KTXM\ProviderJmapc\Objects\Mail\Collection as MailCollectionObject;
class RemoteMailService {
protected Client $dataStore;
protected string $dataAccount;
protected ?string $resourceNamespace = null;
protected ?string $resourceCollectionLabel = null;
protected ?string $resourceEntityLabel = null;
protected array $defaultMailProperties = [
'id', 'blobId', 'threadId', 'mailboxIds', 'keywords', 'size',
'receivedAt', 'messageId', 'inReplyTo', 'references', 'sender', 'from',
'to', 'cc', 'bcc', 'replyTo', 'subject', 'sentAt', 'hasAttachment',
'attachments', 'preview', 'bodyStructure', 'bodyValues'
];
public function __construct() {
}
public function initialize(Client $dataStore, ?string $dataAccount = null) {
$this->dataStore = $dataStore;
// evaluate if client is connected
if (!$this->dataStore->sessionStatus()) {
$this->dataStore->connect();
}
// determine account
if ($dataAccount === null) {
if ($this->resourceNamespace !== null) {
$account = $dataStore->sessionAccountDefault($this->resourceNamespace, false);
} else {
$account = $dataStore->sessionAccountDefault('mail');
}
$this->dataAccount = $account !== null ? $account->id() : '';
} else {
$this->dataAccount = $dataAccount;
}
}
/**
* list of collections in remote storage
*
* @since Release 1.0.0
*/
public function collectionList(?string $location = null, IFilter|null $filter = null, ISort|null $sort = null, IRange|null $range = null): array {
// construct request
$r0 = new MailboxQuery($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// define location
if (!empty($location)) {
$r0->filter()->in($location);
}
// define filter
if ($filter !== null) {
foreach ($filter->conditions() as $condition) {
$value = $condition['value'];
match($condition['attribute']) {
'in' => $r0->filter()->in($value),
'name' => $r0->filter()->name($value),
'role' => $r0->filter()->role($value),
'hasRoles' => $r0->filter()->hasRoles($value),
'subscribed' => $r0->filter()->isSubscribed($value),
default => null
};
}
}
// define order
if ($sort !== null) {
foreach ($sort->conditions() as $condition) {
$direction = $condition['direction'];
match($condition['attribute']) {
'name' => $r0->sort()->name($direction),
'order' => $r0->sort()->order($direction),
default => null
};
}
}
// construct request
$r1 = new MailboxGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// define target
$r1->targetFromRequest($r0, '/ids');
// transceive
$bundle = $this->dataStore->perform([$r0, $r1]);
// extract response
$response = $bundle->response(1);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap objects to collection objects
$list = [];
foreach ($response->objects() as $so) {
if (!$so instanceof MailboxParametersResponse) {
continue;
}
$id = $so->id();
$list[$id] = $so->parametersRaw();
$list[$id]['signature'] = $response->state();
}
// return collection of collections
return $list;
}
/**
* fresh instance of collection filter object
*
* @since Release 1.0.0
*/
public function collectionListFilter(): Filter {
return new Filter(['in', 'name', 'role', 'hasRoles', 'subscribed']);
}
/**
* fresh instance of collection sort object
*
* @since Release 1.0.0
*/
public function collectionListSort(): Sort {
return new Sort(['name', 'order']);
}
/**
* check existence of collections in remote storage
*
* @since Release 1.0.0
*/
public function collectionExtant(string ...$identifiers): array {
$extant = [];
// construct request
$r0 = new MailboxGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->target(...$identifiers);
$r0->property('id');
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap objects to collection objects
foreach ($response->objects() as $so) {
if (!$so instanceof MailboxParametersResponse) {
continue;
}
$extant[$so->id()] = true;
}
return $extant;
}
/**
* retrieve properties for specific collection
*
* @since Release 1.0.0
*/
public function collectionFetch(string $identifier): ?array {
// construct request
$r0 = new MailboxGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->target($identifier);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap object to collection object
$so = $response->object(0);
$to = null;
if ($so instanceof MailboxParametersResponse) {
$to = $so->parametersRaw();
$to['signature'] = $response->state();
}
return $to;
}
/**
* create collection in remote storage
*
* @since Release 1.0.0
*/
public function collectionCreate(string|null $location, array $so): ?array {
// convert entity
$to = new MailboxParametersRequest();
$to->parametersRaw($so);
// define location
if (!empty($location)) {
$to->in($location);
}
$id = uniqid();
// construct request
$r0 = new MailboxSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->create($id, $to);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// check for success
$result = $response->createSuccess($id);
if ($result !== null) {
return array_merge($so, $result);
}
// check for failure
$result = $response->createFailure($id);
if ($result !== null) {
$type = $result['type'] ?? 'unknownError';
$description = $result['description'] ?? 'An unknown error occurred during collection creation.';
throw new Exception("$type: $description", 1);
}
// return null if creation failed without failure reason
return null;
}
/**
* modify collection in remote storage
*
* @since Release 1.0.0
*
*/
public function collectionModify(string $identifier, array $so): ?array {
// convert entity
$to = new MailboxParametersRequest();
$to->parametersRaw($so);
// construct request
$r0 = new MailboxSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->update($identifier, $to);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// check for success
$result = $response->updateSuccess($identifier);
if ($result !== null) {
return array_merge($so, $result);
}
// check for failure
$result = $response->updateFailure($identifier);
if ($result !== null) {
$type = $result['type'] ?? 'unknownError';
$description = $result['description'] ?? 'An unknown error occurred during collection modification.';
throw new Exception("$type: $description", 1);
}
// return null if modification failed without failure reason
return null;
}
/**
* delete collection in remote storage
*
* @since Release 1.0.0
*
*/
public function collectionDestroy(string $identifier, bool $force = false, bool $recursive = false): ?string {
// construct request
$r0 = new MailboxSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->delete($identifier);
if ($force) {
$r0->destroyContents(true);
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// check for success
$result = $response->deleteSuccess($identifier);
if ($result !== null) {
return (string)$result['id'];
}
// check for failure
$result = $response->deleteFailure($identifier);
if ($result !== null) {
$type = $result['type'] ?? 'unknownError';
$description = $result['description'] ?? 'An unknown error occurred during collection deletion.';
throw new Exception("$type: $description", 1);
}
// return null if deletion failed without failure reason
return null;
}
/**
* retrieve entities from remote storage
*
* @since Release 1.0.0
*/
public function entityList(?string $location = null, IFilter|null $filter = null, ISort|null $sort = null, IRange|null $range = null, string|null $granularity = null): array {
// construct request
$r0 = new MailQuery($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// define location
if (!empty($location)) {
$r0->filter()->in($location);
}
// define filter
if ($filter !== null) {
foreach ($filter->conditions() as $condition) {
$value = $condition['value'];
match($condition['attribute']) {
'*' => $r0->filter()->text($value),
'in' => $r0->filter()->in($value),
'inOmit' => $r0->filter()->inOmit($value),
'from' => $r0->filter()->from($value),
'to' => $r0->filter()->to($value),
'cc' => $r0->filter()->cc($value),
'bcc' => $r0->filter()->bcc($value),
'subject' => $r0->filter()->subject($value),
'body' => $r0->filter()->body($value),
'attachmentPresent' => $r0->filter()->hasAttachment($value),
'tagPresent' => $r0->filter()->keywordPresent($value),
'tagAbsent' => $r0->filter()->keywordAbsent($value),
'before' => $r0->filter()->receivedBefore($value),
'after' => $r0->filter()->receivedAfter($value),
'min' => $r0->filter()->sizeMin((int)$value),
'max' => $r0->filter()->sizeMax((int)$value),
default => null
};
}
}
// define order
if ($sort !== null) {
foreach ($sort->conditions() as $condition) {
$direction = $condition['direction'];
match($condition['attribute']) {
'from' => $r0->sort()->from($direction),
'to' => $r0->sort()->to($direction),
'subject' => $r0->sort()->subject($direction),
'received' => $r0->sort()->received($direction),
'sent' => $r0->sort()->sent($direction),
'size' => $r0->sort()->size($direction),
'tag' => $r0->sort()->keyword($direction),
default => null
};
}
}
// define range
if ($range !== null) {
if ($range instanceof RangeTally && $range->getAnchor() === RangeAnchorType::ABSOLUTE) {
$r0->limitAbsolute($range->getPosition(), $range->getTally());
}
if ($range instanceof RangeTally && $range->getAnchor() === RangeAnchorType::RELATIVE) {
$r0->limitRelative($range->getPosition(), $range->getTally());
}
}
// construct get request
$r1 = new MailGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// set target to query request
$r1->targetFromRequest($r0, '/ids');
// select properties to return
$r1->property(...$this->defaultMailProperties);
$r1->bodyAll(true);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0, $r1]);
// extract response
$response = $bundle->response(1);
// convert json objects to message objects
$state = $response->state();
$list = $response->objects();
foreach ($list as $id => $entry) {
if (!$entry instanceof MailParametersResponse) {
continue;
}
$list[$id] = $entry->parametersRaw();
}
// return message collection
return ['list' => $list, 'state' => $state];
}
/**
* fresh instance of object filter
*
* @since Release 1.0.0
*/
public function entityListFilter(): Filter {
return new Filter([
'in',
'inOmit',
'text',
'from',
'to',
'cc',
'bcc',
'subject',
'body',
'attachmentPresent',
'tagPresent',
'tagAbsent',
'receivedBefore',
'receivedAfter',
'sizeMin',
'sizeMax'
]);
}
/**
* fresh instance of object sort
*
* @since Release 1.0.0
*/
public function entityListSort(): Sort {
return new Sort([
'received',
'sent',
'from',
'to',
'subject',
'size',
'tag'
]);
}
/**
* fresh instance of object range
*
* @since Release 1.0.0
*/
public function entityListRange(): RangeTally {
return new RangeTally();
}
/**
* check existence of entities in remote storage
*
* @since Release 1.0.0
*/
public function entityExtant(string ...$identifiers): array {
$extant = [];
// construct request
$r0 = new MailGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->target(...$identifiers);
$r0->property('id');
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert json objects to message objects
foreach ($response->objects() as $so) {
if (!$so instanceof MailParametersResponse) {
continue;
}
$extant[$so->id()] = true;
}
return $extant;
}
/**
* delta for entities in remote storage
*
* @since Release 1.0.0
*
* @return Delta
*/
public function entityDelta(?string $location, string $state, string $granularity = 'D'): Delta {
if (empty($state)) {
$results = $this->entityList($location, null, null, null, 'B');
$delta = new Delta();
$delta->signature = $results['state'];
foreach ($results['list'] as $entry) {
$delta->additions[] = $entry['id'];
}
return $delta;
}
if (empty($location)) {
return $this->entityDeltaDefault($state, $granularity);
} else {
return $this->entityDeltaSpecific($location, $state, $granularity);
}
}
/**
* delta of changes for specific collection in remote storage
*
* @since Release 1.0.0
*
*/
public function entityDeltaSpecific(?string $location, string $state, string $granularity = 'D'): Delta {
// construct set request
$r0 = new MailQueryChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// set location constraint
if (!empty($location)) {
$r0->filter()->in($location);
}
// set state constraint
if (!empty($state)) {
$r0->state($state);
} else {
$r0->state('0');
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap object to delta object
$delta = new Delta();
$delta->signature = $response->stateNew();
$delta->additions = new DeltaCollection(array_column($response->added(), 'id'));
$delta->modifications = new DeltaCollection([]);
$delta->deletions = new DeltaCollection(array_column($response->removed(), 'id'));
return $delta;
}
/**
* delta of changes in remote storage
*
* @since Release 1.0.0
*
*/
public function entityDeltaDefault(string $state, string $granularity = 'D'): Delta {
// construct set request
$r0 = new MailChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// set state constraint
if (!empty($state)) {
$r0->state($state);
} else {
$r0->state('');
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap object to delta object
$delta = new Delta();
$delta->signature = $response->stateNew();
$delta->additions = new DeltaCollection(array_column($response->added(), 'id'));
$delta->modifications = new DeltaCollection([]);
$delta->deletions = new DeltaCollection(array_column($response->removed(), 'id'));
return $delta;
}
/**
* retrieve entity from remote storage
*
* @since Release 1.0.0
*/
public function entityFetch(string ...$identifiers): ?array {
// construct request
$r0 = new MailGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->target(...$identifiers);
// select properties to return
$r0->property(...$this->defaultMailProperties);
$r0->bodyAll(true);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert json objects to message objects
$list = [];
foreach ($response->objects() as $so) {
if (!$so instanceof MailParametersResponse) {
continue;
}
$id = $so->id();
$list[$id] = $so->parametersRaw();
$list[$id]['signature'] = $response->state();
}
// return message collection
return $list;
}
/**
* create entity in remote storage
*
* @since Release 1.0.0
*/
public function entityCreate(string $location, array $so): ?array {
// convert entity
$to = new MailParametersRequest();
$to->parametersRaw($so);
$to->in($location);
$id = uniqid();
// construct request
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->create($id, $to);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// check for command error
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// check for success
$result = $response->createSuccess($id);
if ($result !== null) {
return array_merge($so, $result);
}
// check for failure
$result = $response->createFailure($id);
if ($result !== null) {
$type = $result['type'] ?? 'unknownError';
$description = $result['description'] ?? 'An unknown error occurred during collection creation.';
throw new Exception("$type: $description", 1);
}
// return null if creation failed without failure reason
return null;
}
/**
* update entity in remote storage
*
* @since Release 1.0.0
*/
public function entityModify(array $so): ?array {
// extract entity id
$id = $so['id'];
// convert entity
$to = new MailParametersRequest();
$to->parametersRaw($so);
// construct request
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->update($id, $to);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// determine if command succeeded
if (array_key_exists($id, $response->updated())) {
// update entity
$ro = $response->updated()[$id];
$so = array_merge($so, $ro);
return $so;
}
return null;
}
/**
* delete entity from remote storage
*
* @since Release 1.0.0
*/
public function entityDelete(string $id): ?string {
// construct set request
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// construct object
$r0->delete($id);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// determine if command succeeded
if (array_search($id, $response->deleted()) !== false) {
return $response->stateNew();
}
return null;
}
/**
* copy entity in remote storage
*
* @since Release 1.0.0
*
*/
public function entityCopy(string $location, MailMessageObject $so): ?MailMessageObject {
return null;
}
/**
* move entity in remote storage
*
* @since Release 1.0.0
*
*/
public function entityMove(string $location, array $so): ?array {
// extract entity id
$id = $so['id'];
// construct request
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->update($id)->in($location);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// determine if command succeeded
if (array_key_exists($id, $response->updated())) {
$so = array_merge($so, ['mailboxIds' => [$location => true]]);
return $so;
}
return null;
}
/**
* send entity
*
* @since Release 1.0.0
*
*/
public function entitySend(string $identity, MailMessageObject $message, ?string $presendLocation = null, ?string $postsendLocation = null): string {
// determine if pre-send location is present
if ($presendLocation === null || empty($presendLocation)) {
throw new Exception('Pre-Send Location is missing', 1);
}
// determine if post-send location is present
if ($postsendLocation === null || empty($postsendLocation)) {
throw new Exception('Post-Send Location is missing', 1);
}
// determine if we have the basic required data and fail otherwise
if (empty($message->getFrom())) {
throw new Exception('Missing Requirements: Message MUST have a From address', 1);
}
if (empty($message->getTo())) {
throw new Exception('Missing Requirements: Message MUST have a To address(es)', 1);
}
// determine if message has attachments
if (count($message->getAttachments()) > 0) {
// process attachments first
$message = $this->depositAttachmentsFromMessage($message);
}
// convert from address object to string
$from = $message->getFrom()->getAddress();
// convert to, cc and bcc address object arrays to single strings array
$to = array_map(
function ($entry) { return $entry->getAddress(); },
array_merge($message->getTo(), $message->getCc(), $message->getBcc())
);
unset($cc, $bcc);
// construct set request
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->create('1', $message)->in($presendLocation);
// construct set request
$r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// construct envelope
$e1 = $r1->create('2');
$e1->identity($identity);
$e1->message('#1');
$e1->from($from);
$e1->to($to);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0, $r1]);
// extract response
$response = $bundle->response(1);
// return collection information
return (string)$response->created()['2']['id'];
}
/**
* retrieve collection entity attachment from remote storage
*
* @since Release 1.0.0
*
*/
public function depositAttachmentsFromMessage(MailMessageObject $message): MailMessageObject {
$parameters = $message->toJmap();
$attachments = $message->getAttachments();
$matches = [];
$this->findAttachmentParts($parameters['bodyStructure'], $matches);
foreach ($attachments as $attachment) {
$part = $attachment->toJmap();
if (isset($matches[$part->getId()])) {
// deposit attachment in data store
$response = $this->blobDeposit($account, $part->getType(), $attachment->getContents());
// transfer blobId and size to mail part
$matches[$part->getId()]->blobId = $response['blobId'];
$matches[$part->getId()]->size = $response['size'];
unset($matches[$part->getId()]->partId);
}
}
return (new MailMessageObject())->fromJmap($parameters);
}
protected function findAttachmentParts(object &$part, array &$matches) {
if ($part->disposition === 'attachment' || $part->disposition === 'inline') {
$matches[$part->partId] = $part;
}
foreach ($part->subParts as $entry) {
$this->findAttachmentParts($entry, $matches);
}
}
/**
* retrieve identity from remote storage
*
* @since Release 1.0.0
*
*/
public function identityFetch(?string $account = null): array {
if ($account === null) {
$account = $this->dataAccount;
}
// construct set request
$r0 = new MailIdentityGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// transmit request and receive response
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert json object to message object and return
return $response->objects();
}
}

View File

@@ -0,0 +1,213 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Service\Remote;
use JmapClient\Authentication\Basic;
use JmapClient\Authentication\Bearer;
use JmapClient\Authentication\JsonBasic;
use JmapClient\Authentication\JsonBasicCookie;
use JmapClient\Client as JmapClient;
use KTXF\Resource\Provider\ResourceServiceBaseInterface;
use KTXF\Resource\Provider\ResourceServiceIdentityBasic;
use KTXF\Resource\Provider\ResourceServiceIdentityBearer;
use KTXF\Resource\Provider\ResourceServiceIdentityOAuth;
use KTXF\Resource\Provider\ResourceServiceLocationUri;
use KTXM\ProviderJmapc\Providers\Mail\Service;
use KTXM\ProviderJmapc\Service\Remote\FM\RemoteContactsServiceFM;
use KTXM\ProviderJmapc\Service\Remote\FM\RemoteCoreServiceFM;
use KTXM\ProviderJmapc\Service\Remote\FM\RemoteEventsServiceFM;
class RemoteService {
static string $clientTransportAgent = 'KtrixJMAP/1.0 (1.0; x64)';
//public static string $clientTransportAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0';
/**
* Initialize remote data store client
*
* @since Release 1.0.0
*/
public static function freshClient(Service $service): JmapClient {
// defaults
$client = new JmapClient();
$client->setTransportAgent(self::$clientTransportAgent);
$location = $service->getLocation();
$identity = $service->getIdentity();
// location
if ($location instanceof ResourceServiceLocationUri === false) {
throw new \InvalidArgumentException('Service location is not a valid URI');
}
$client->configureTransportMode($location->getScheme());
$client->setHost($location->getHost() . ':' . $location->getPort());
if (!empty($location->getPath())) {
$client->setDiscoveryPath($location->getPath());
}
$client->configureTransportVerification((bool)$location->getVerifyPeer());
// authentication
if (($identity instanceof ResourceServiceIdentityBasic) === false) {
throw new \InvalidArgumentException('Service identity is not a valid Basic or Bearer authentication');
}
if ($identity instanceof ResourceServiceIdentityBasic) {
$client->setAuthentication(new Basic(
$identity->getIdentity(),
$identity->getSecret()
));
}
// debugging
if ($service->getDebug()) {
$client->configureTransportLogState(true);
$client->configureTransportLogLocation(
sys_get_temp_dir() . '/' . $location->getHost() . '-' . $identity->getIdentity() . '.log'
);
}
// return
return $client;
}
/**
* Destroys remote data store client (Jmap Client)
*
* @since Release 1.0.0
*/
public static function destroyClient(JmapClient $Client): void {
// destroy remote data store client
$Client = null;
}
/**
* Appropriate Mail Service for Connection
*
* @since Release 1.0.0
*/
public static function coreService(JmapClient $Client, ?string $dataAccount = null): RemoteCoreService {
// determine if client is connected
if (!$Client->sessionStatus()) {
$Client->connect();
}
// construct service based on capabilities
if ($Client->sessionCapable('https://www.fastmail.com/dev/user', false)) {
$service = new RemoteCoreServiceFM();
} else {
$service = new RemoteCoreService();
}
$service->initialize($Client, $dataAccount);
return $service;
}
/**
* Appropriate Mail Service for Connection
*
* @since Release 1.0.0
*/
public static function mailService(JmapClient $Client, ?string $dataAccount = null): RemoteMailService {
// determine if client is connected
if (!$Client->sessionStatus()) {
$Client->connect();
}
$service = new RemoteMailService();
$service->initialize($Client, $dataAccount);
return $service;
}
/**
* Appropriate Contacts Service for Connection
*
* @since Release 1.0.0
*/
public static function contactsService(JmapClient $Client, ?string $dataAccount = null): RemoteContactsService {
// determine if client is connected
if (!$Client->sessionStatus()) {
$Client->connect();
}
// construct service based on capabilities
if ($Client->sessionCapable('https://www.fastmail.com/dev/contacts', false)) {
$service = new RemoteContactsServiceFM();
} else {
$service = new RemoteContactsService();
}
$service->initialize($Client, $dataAccount);
return $service;
}
/**
* Appropriate Events Service for Connection
*
* @since Release 1.0.0
*/
public static function eventsService(JmapClient $Client, ?string $dataAccount = null): RemoteEventsService {
// determine if client is connected
if (!$Client->sessionStatus()) {
$Client->connect();
}
// construct service based on capabilities
if ($Client->sessionCapable('https://www.fastmail.com/dev/calendars', false)) {
$service = new RemoteEventsServiceFM();
} else {
$service = new RemoteEventsService();
}
$service->initialize($Client, $dataAccount);
return $service;
}
/**
* Appropriate Tasks Service for Connection
*
* @since Release 1.0.0
*/
public static function tasksService(JmapClient $Client, ?string $dataAccount = null): RemoteTasksService {
// determine if client is connected
if (!$Client->sessionStatus()) {
$Client->connect();
}
$service = new RemoteTasksService();
$service->initialize($Client, $dataAccount);
return $service;
}
public static function cookieStoreRetrieve(mixed $id): ?array {
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . (string)$id . '.jmapc';
if (!file_exists($file)) {
return null;
}
$data = file_get_contents($file);
$crypto = \OC::$server->get(\OCP\Security\ICrypto::class);
$data = $crypto->decrypt($data);
if (!empty($data)) {
return json_decode($data, true);
}
return null;
}
public static function cookieStoreDeposit(mixed $id, array $value): void {
if (empty($value)) {
return;
}
$crypto = \OC::$server->get(\OCP\Security\ICrypto::class);
$data = $crypto->encrypt(json_encode($value));
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . (string)$id . '.jmapc';
file_put_contents($file, $data);
}
}

View File

@@ -0,0 +1,652 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Sebastian Krupinski <krupinski01@gmail.com>
*
* @author Sebastian Krupinski <krupinski01@gmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace KTXM\ProviderJmapc\Service\Remote;
use DateTimeImmutable;
use DateTimeZone;
use Exception;
use JmapClient\Client;
use JmapClient\Requests\Tasks\TaskChanges;
use JmapClient\Requests\Tasks\TaskGet;
use JmapClient\Requests\Tasks\TaskListGet;
use JmapClient\Requests\Tasks\TaskListSet;
use JmapClient\Requests\Tasks\TaskParameters as TaskParametersRequest;
use JmapClient\Requests\Tasks\TaskQuery;
use JmapClient\Requests\Tasks\TaskQueryChanges;
use JmapClient\Requests\Tasks\TaskSet;
use JmapClient\Responses\ResponseException;
use JmapClient\Responses\Tasks\TaskListParameters as TaskListParametersResponse;
use JmapClient\Responses\Tasks\TaskParameters as TaskParametersResponse;
use OCA\JMAPC\Exceptions\JmapUnknownMethod;
use OCA\JMAPC\Objects\BaseStringCollection;
use OCA\JMAPC\Objects\DeltaObject;
use OCA\JMAPC\Objects\OriginTypes;
use OCA\JMAPC\Objects\Task\TaskCollectionObject;
use OCA\JMAPC\Objects\Task\TaskObject;
use OCA\JMAPC\Store\Common\Filters\IFilter;
use OCA\JMAPC\Store\Common\Range\IRangeTally;
use OCA\JMAPC\Store\Common\Sort\ISort;
class RemoteTasksService {
public ?DateTimeZone $SystemTimeZone = null;
public ?DateTimeZone $UserTimeZone = null;
protected Client $dataStore;
protected string $dataAccount;
protected ?string $resourceNamespace = null;
protected ?string $resourceCollectionLabel = null;
protected ?string $resourceEntityLabel = null;
protected array $collectionPropertiesDefault = [];
protected array $collectionPropertiesBasic = [];
protected array $entityPropertiesDefault = [];
protected array $entityPropertiesBasic = [
'id', 'calendarIds', 'uid', 'created', 'updated'
];
public function __construct() {
}
public function initialize(Client $dataStore, ?string $dataAccount = null) {
$this->dataStore = $dataStore;
// evaluate if client is connected
if (!$this->dataStore->sessionStatus()) {
$this->dataStore->connect();
}
// determine account
if ($dataAccount === null) {
if ($this->resourceNamespace !== null) {
$account = $dataStore->sessionAccountDefault($this->resourceNamespace, false);
} else {
$account = $dataStore->sessionAccountDefault('contacts');
}
$this->dataAccount = $account !== null ? $account->id() : '';
} else {
$this->dataAccount = $dataAccount;
}
}
/**
* retrieve properties for specific collection
*
* @since Release 1.0.0
*
*/
public function collectionFetch(string $id): ?TaskCollectionObject {
// construct request
$r0 = new TaskListGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
if (!empty($id)) {
$r0->target($id);
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert jmap object to collection object
if ($response->object(0) instanceof TaskListParametersResponse) {
$co = $response->object(0);
$collection = new TaskCollectionObject();
$collection->Id = $co->id();
$collection->Label = $co->label();
$collection->Description = $co->description();
$collection->Priority = $co->priority();
$collection->Visibility = $co->visible();
$collection->Color = $co->color();
return $collection;
} else {
return null;
}
}
/**
* create collection in remote storage
*
* @since Release 1.0.0
*
*/
public function collectionCreate(TaskCollectionObject $collection): string {
// construct request
$r0 = new TaskListSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
$m0 = $r0->create('1');
if ($collection->Label) {
$m0->label($collection->Label);
}
if ($collection->Description) {
$m0->description($collection->Description);
}
if ($collection->Priority) {
$m0->priority($collection->Priority);
}
if ($collection->Visibility) {
$m0->visible($collection->Visibility);
}
if ($collection->Color) {
$m0->color($collection->Color);
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// return collection id
return (string)$response->created()['1']['id'];
}
/**
* update collection in remote storage
*
* @since Release 1.0.0
*
*/
public function collectionUpdate(string $id, TaskCollectionObject $collection): string {
// construct request
$r0 = new TaskListSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
$m0 = $r0->update($id);
$m0->label($collection->Label);
$m0->description($collection->Description);
$m0->priority($collection->Priority);
$m0->visible($collection->Visibility);
$m0->color($collection->Color);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// return collection id
return array_key_exists($id, $response->updated()) ? (string)$id : '';
}
/**
* delete collection in remote storage
*
* @since Release 1.0.0
*
*/
public function collectionDelete(string $id): string {
// construct request
$r0 = new TaskListSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
$r0->delete($id);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// return collection id
return (string)$response->deleted()[0];
}
/**
* list of collections in remote storage
*
* @since Release 1.0.0
*
* @param string|null $location Id of parent collection
* @param string|null $granularity Amount of detail to return
* @param int|null $depth Depth of sub collections to return
*
* @return array<string,TaskCollectionObject>
*/
public function collectionList(?string $location = null, ?string $granularity = null, ?int $depth = null): array {
// construct request
$r0 = new TaskListGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
// set target to query request
if ($location !== null) {
$r0->target($location);
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// determine if command errored
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap objects to collection objects
$list = [];
foreach ($response->objects() as $co) {
$collection = new TaskCollectionObject();
$collection->Id = $co->id();
$collection->Label = $co->label();
$collection->Description = $co->description();
$collection->Priority = $co->priority();
$collection->Visibility = $co->visible();
$collection->Color = $co->color();
$list[] = $collection;
}
// return collection of collections
return $list;
}
/**
* retrieve entity from remote storage
*
* @since Release 1.0.0
*
*/
public function entityFetch(string $location, string $id, string $granularity = 'D'): ?TaskObject {
// construct request
$r0 = new TaskGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->target($id);
// select properties to return
if ($granularity === 'B') {
$r0->property(...$this->entityPropertiesBasic);
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert jmap object to Task object
$eo = $this->toTaskObject($response->object(0));
$eo->Signature = $this->generateSignature($eo);
return $eo;
}
/**
* create entity in remote storage
*
* @since Release 1.0.0
*
*/
public function entityCreate(string $location, TaskObject $so): ?TaskObject {
// convert entity
$entity = $this->fromTaskObject($so);
// construct set request
$r0 = new TaskSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->create('1', $entity)->in($location);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// return entity
if (isset($response->created()['1']['id'])) {
$ro = clone $so;
$ro->Origin = OriginTypes::External;
$ro->ID = $response->created()['1']['id'];
$ro->CreatedOn = isset($response->created()['1']['updated']) ? new DateTimeImmutable($response->created()['1']['updated']) : null;
$ro->ModifiedOn = $ro->CreatedOn;
$ro->Signature = $this->generateSignature($ro);
return $ro;
} else {
return null;
}
}
/**
* update entity in remote storage
*
* @since Release 1.0.0
*
*/
public function entityModify(string $location, string $id, TaskObject $so): ?TaskObject {
// convert entity
$entity = $this->fromTaskObject($so);
// construct set request
$r0 = new TaskSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->update($id, $entity)->in($location);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// convert jmap object to Task object
if (array_key_exists($id, $response->updated())) {
$ro = clone $so;
$ro->Origin = OriginTypes::External;
$ro->ID = $id;
$ro->ModifiedOn = isset($response->updated()[$id]['updated']) ? new DateTimeImmutable($response->updated()[$id]['updated']) : null;
$ro->Signature = $this->generateSignature($ro);
} else {
$ro = null;
}
// return entity information
return $ro;
}
/**
* delete entity from remote storage
*
* @since Release 1.0.0
*
*/
public function entityDelete(string $location, string $id): string {
// construct set request
$r0 = new TaskSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// construct object
$r0->delete($id);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// return collection information
return (string)$response->deleted()[0];
}
/**
* copy entity in remote storage
*
* @since Release 1.0.0
*
*/
public function entityCopy(string $sourceLocation, string $id, string $destinationLocation): string {
return '';
}
/**
* move entity in remote storage
*
* @since Release 1.0.0
*
*/
public function entityMove(string $sourceLocation, string $id, string $destinationLocation): string {
// construct set request
$r0 = new TaskSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// construct object
$m0 = $r0->update($id);
$m0->in($destinationLocation);
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// return collection information
return array_key_exists($id, $response->updated()) ? (string)$id : '';
}
/**
* retrieve entities from remote storage
*
* @since Release 1.0.0
*
* @param string|null $location Id of parent collection
* @param string|null $granularity Amount of detail to return
* @param IRange|null $range Range of collections to return
* @param IFilter|null $filter Properties to filter by
* @param ISort|null $sort Properties to sort by
*/
public function entityList(?string $location = null, ?string $granularity = null, ?IRangeTally $range = null, ?IFilter $filter = null, ?ISort $sort = null, ?int $depth = null): array {
// construct request
$r0 = new TaskQuery($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// define location
if (!empty($location)) {
$r0->filter()->in($location);
}
// define filter
if ($filter !== null) {
foreach ($filter->conditions() as $condition) {
[$operator, $property, $value] = $condition;
match($property) {
'before' => $r0->filter()->before($value),
'after' => $r0->filter()->after($value),
'uid' => $r0->filter()->uid($value),
default => null
};
}
}
// define sort
if ($sort !== null) {
foreach ($sort->conditions() as $condition) {
[$property, $direction] = $condition;
match($property) {
'created' => $r0->sort()->created($direction),
'modified' => $r0->sort()->updated($direction),
'start' => $r0->sort()->start($direction),
'uid' => $r0->sort()->uid($direction),
default => null
};
}
}
// define order
if ($sort !== null) {
foreach ($sort->conditions() as $condition) {
match($condition['attribute']) {
'created' => $r0->sort()->created($condition['direction']),
'modified' => $r0->sort()->updated($condition['direction']),
'start' => $r0->sort()->start($condition['direction']),
'uid' => $r0->sort()->uid($condition['direction']),
'recurrence' => $r0->sort()->recurrence($condition['direction']),
default => null
};
}
}
// construct request
$r1 = new TaskGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// set target to query request
$r1->targetFromRequest($r0, '/ids');
// select properties to return
if ($granularity === 'B') {
$r1->property(...$this->entityPropertiesBasic);
}
// transceive
$bundle = $this->dataStore->perform([$r0, $r1]);
// extract response
$response = $bundle->response(1);
// convert json objects to message objects
$state = $response->state();
$list = $response->objects();
foreach ($list as $id => $entry) {
$list[$id] = $this->toTaskObject($entry);
}
// return message collection
return ['list' => $list, 'state' => $state];
}
/**
* delta for entities in remote storage
*
* @since Release 1.0.0
*
* @return DeltaObject
*/
public function entityDelta(?string $location, string $state, string $granularity = 'D'): DeltaObject {
if (empty($state)) {
$results = $this->entityList($location, 'B');
$delta = new DeltaObject();
$delta->signature = $results['state'];
foreach ($results['list'] as $entry) {
$delta->additions[] = $entry->ID;
}
return $delta;
}
if (empty($location)) {
return $this->entityDeltaDefault($state, $granularity);
} else {
return $this->entityDeltaSpecific($location, $state, $granularity);
}
}
/**
* delta of changes for specific collection in remote storage
*
* @since Release 1.0.0
*
*/
public function entityDeltaSpecific(?string $location, string $state, string $granularity = 'D'): DeltaObject {
// construct set request
$r0 = new TaskQueryChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// set location constraint
if (!empty($location)) {
$r0->filter()->in($location);
}
// set state constraint
if (!empty($state)) {
$r0->state($state);
} else {
$r0->state('0');
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// determine if command errored
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap object to delta object
$delta = new DeltaObject();
$delta->signature = $response->stateNew();
$delta->additions = new BaseStringCollection($response->created());
$delta->modifications = new BaseStringCollection($response->updated());
$delta->deletions = new BaseStringCollection($response->deleted());
return $delta;
}
/**
* delta of changes in remote storage
*
* @since Release 1.0.0
*
*/
public function entityDeltaDefault(string $state, string $granularity = 'D'): DeltaObject {
// construct set request
$r0 = new TaskChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
// set state constraint
if (!empty($state)) {
$r0->state($state);
} else {
$r0->state('');
}
// transceive
$bundle = $this->dataStore->perform([$r0]);
// extract response
$response = $bundle->response(0);
// determine if command errored
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} else {
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
}
// convert jmap object to delta object
$delta = new DeltaObject();
$delta->signature = $response->stateNew();
$delta->additions = new BaseStringCollection($response->created());
$delta->modifications = new BaseStringCollection($response->updated());
$delta->deletions = new BaseStringCollection($response->deleted());
return $delta;
}
/**
* convert jmap object to Task object
*
* @since Release 1.0.0
*
*/
public function toTaskObject(TaskParametersResponse $so): TaskObject {
// create object
$eo = new TaskObject();
// source origin
$eo->Origin = OriginTypes::External;
// id
if ($so->id()) {
$eo->ID = $so->id();
}
if ($so->in()) {
$eo->CID = $so->in()[0];
}
// universal id
if ($so->uid()) {
$eo->UUID = $so->uid();
}
// creation date time
if ($so->created()) {
$eo->CreatedOn = $so->created();
}
// modification date time
if ($so->updated()) {
$eo->ModifiedOn = $so->updated();
}
return $eo;
}
/**
* convert Task object to jmap object
*
* @since Release 1.0.0
*
*/
public function fromTaskObject(TaskObject $eo): TaskParametersRequest {
// create object
$to = new TaskParametersRequest();
// universal id
if ($eo->UUID) {
$to->uid($eo->UUID);
}
// creation date time
if ($eo->CreatedOn) {
$to->created($eo->CreatedOn);
}
// modification date time
if ($eo->ModifiedOn) {
$to->updated($eo->ModifiedOn);
}
return $to;
}
public function generateSignature(TaskObject $eo): string {
// clone self
$o = clone $eo;
// remove non needed values
unset(
$o->Origin,
$o->ID,
$o->CID,
$o->Signature,
$o->CCID,
$o->CEID,
$o->CESN,
$o->UUID,
$o->CreatedOn,
$o->ModifiedOn
);
// generate signature
return md5(json_encode($o, JSON_PARTIAL_OUTPUT_ON_ERROR));
}
}

179
lib/Stores/ServiceStore.php Normal file
View File

@@ -0,0 +1,179 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\ProviderJmapc\Stores;
use KTXC\Db\DataStore;
use KTXF\Security\Crypto;
use KTXF\Utile\UUID;
use KTXM\ProviderJmapc\Providers\Mail\Service;
/**
* JMAP Service Store
*
* Shared by Mail, Calendar, and Contacts providers.
*/
class ServiceStore
{
protected const COLLECTION_NAME = 'provider_jmapc_services';
public function __construct(
protected readonly DataStore $dataStore,
protected readonly Crypto $crypto,
) {}
/**
* List services for a tenant and user, optionally filtered by service IDs
*/
public function list(string $tenantId, string $userId, ?array $filter = null): array
{
$filterCondition = [
'tid' => $tenantId,
'uid' => $userId,
];
if ($filter !== null && !empty($filter)) {
$filterCondition['sid'] = ['$in' => $filter];
}
$cursor = $this->dataStore->selectCollection(self::COLLECTION_NAME)->find($filterCondition);
$list = [];
foreach ($cursor as $entry) {
if (isset($entry['identity']['secret'])) {
$entry['identity']['secret'] = $this->crypto->decrypt($entry['identity']['secret']);
}
$list[$entry['sid']] = $entry;
}
return $list;
}
/**
* Check existence of services by IDs for a tenant and user
*/
public function extant(string $tenantId, string $userId, array $identifiers): array
{
if (empty($identifiers)) {
return [];
}
$cursor = $this->dataStore->selectCollection(self::COLLECTION_NAME)->find(
[
'tid' => $tenantId,
'uid' => $userId,
'sid' => ['$in' => array_map('strval', $identifiers)]
],
['projection' => ['sid' => 1]]
);
$existingIds = [];
foreach ($cursor as $document) {
$existingIds[] = $document['sid'];
}
// Build result map: all identifiers default to false, existing ones set to true
$result = [];
foreach ($identifiers as $id) {
$result[(string) $id] = in_array((string) $id, $existingIds, true);
}
return $result;
}
/**
* Retrieve a single service by ID
*/
public function fetch(string $tenantId, string $userId, string|int $serviceId): ?Service
{
$document = $this->dataStore->selectCollection(self::COLLECTION_NAME)->findOne([
'tid' => $tenantId,
'uid' => $userId,
'sid' => (string)$serviceId,
]);
if (!$document) {
return null;
}
if (isset($document['identity']['secret'])) {
$document['identity']['secret'] = $this->crypto->decrypt($document['identity']['secret']);
}
return (new Service())->fromStore($document);
}
/**
* Create a new service
*/
public function create(string $tenantId, string $userId, Service $service): Service
{
$document = $service->toStore();
// prepare document for insertion
$document['tid'] = $tenantId;
$document['uid'] = $userId;
$document['sid'] = UUID::v4();
$document['createdOn'] = new \MongoDB\BSON\UTCDateTime();
$document['modifiedOn'] = new \MongoDB\BSON\UTCDateTime();
if (isset($document['identity']['secret'])) {
$document['identity']['secret'] = $this->crypto->encrypt($document['identity']['secret']);
}
$result = $this->dataStore->selectCollection(self::COLLECTION_NAME)->insertOne($document);
return (new Service())->fromStore($document);
}
/**
* Modify an existing service
*/
public function modify(string $tenantId, string $userId, Service $service): Service
{
$serviceId = $service->id();
if (empty($serviceId)) {
throw new \InvalidArgumentException('Service ID is required for update');
}
// prepare document for modification
$document = $service->toStore();
$document['modifiedOn'] = new \MongoDB\BSON\UTCDateTime();
if (isset($document['identity']['secret'])) {
$document['identity']['secret'] = $this->crypto->encrypt($document['identity']['secret']);
}
unset($document['sid'], $document['tid'], $document['uid'], $document['createdOn']);
$this->dataStore->selectCollection(self::COLLECTION_NAME)->updateOne(
[
'tid' => $tenantId,
'uid' => $userId,
'sid' => (string)$serviceId,
],
['$set' => $document]
);
return (new Service())->fromStore($document);
}
/**
* Delete a service
*/
public function delete(string $tenantId, string $userId, string|int $serviceId): bool
{
$result = $this->dataStore->selectCollection(self::COLLECTION_NAME)->deleteOne([
'tid' => $tenantId,
'uid' => $userId,
'sid' => (string)$serviceId,
]);
return $result->getDeletedCount() > 0;
}
}