-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
- Loading branch information
1 parent
662c2a5
commit 20dc43a
Showing
10 changed files
with
1,049 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Mail\Provider\Command; | ||
|
||
use OCA\Mail\Db\LocalMessage; | ||
use OCA\Mail\Service\AccountService; | ||
use OCA\Mail\Service\Attachment\AttachmentService; | ||
use OCA\Mail\Service\OutboxService; | ||
use OCA\Mail\Service\SmimeService; | ||
use OCP\IConfig; | ||
use OCP\Mail\Provider\IMessage; | ||
|
||
class MessageSend { | ||
|
||
public function __construct( | ||
protected IConfig $config, | ||
protected AccountService $accountService, | ||
protected OutboxService $outboxService, | ||
protected AttachmentService $attachmentService, | ||
protected SmimeService $smimeService | ||
) { | ||
} | ||
|
||
public function perform(string $userId, string $serviceId, IMessage $message, array $option = []): void { | ||
Check failure on line 30 in lib/Provider/Command/MessageSend.php
|
||
// find user mail account details | ||
$account = $this->accountService->find($userId, (int) $serviceId); | ||
// convert mail provider message to local message | ||
$lm = new LocalMessage(); | ||
$lm->setType($lm::TYPE_OUTGOING); | ||
$lm->setAccountId($account->getId()); | ||
$lm->setSubject($message->getSubject()); | ||
$lm->setBody($message->getBody()); | ||
$lm->setHtml(true); | ||
$lm->setSendAt(time()); | ||
|
||
// convert all mail provider attachments to local attachments | ||
$attachments = []; | ||
if (count($message->getAttachments()) > 0) { | ||
// iterate attachments and save them | ||
foreach ($message->getAttachments() as $entry) { | ||
$attachments[] = $this->attachmentService->addFileFromString( | ||
$userId, | ||
$entry->getName(), | ||
$entry->getType(), | ||
$entry->getContents() | ||
)->jsonSerialize(); | ||
} | ||
} | ||
// convert recipiant addresses | ||
$to = $this->convertAddressArray($message->getTo()); | ||
$cc = $this->convertAddressArray($message->getCc()); | ||
$bcc = $this->convertAddressArray($message->getBcc()); | ||
// save message for sending | ||
$lm = $this->outboxService->saveMessage( | ||
$account, | ||
$lm, | ||
$to, | ||
$cc, | ||
$bcc, | ||
$attachments | ||
); | ||
|
||
// evaluate if job scheduler is NOT cron, send message right away otherwise let cron job handle it | ||
if ($this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax') !== 'cron') { | ||
$lm = $this->outboxService->sendMessage($lm, $account); | ||
} | ||
|
||
} | ||
|
||
protected function convertAddressArray(array|null $in) { | ||
// construct place holder | ||
$out = []; | ||
// convert format | ||
foreach ($in as $entry) { | ||
$out[] = (!empty($entry->getLabel())) ? ['email' => $entry->getAddress(), 'label' => $entry->getLabel()] : ['email' => $entry->getAddress()]; | ||
} | ||
// return converted addressess | ||
return $out; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Mail\Provider; | ||
|
||
use OCA\Mail\Account; | ||
use OCA\Mail\Service\AccountService; | ||
|
||
use OCP\Mail\Provider\Address as MailAddress; | ||
use OCP\Mail\Provider\IProvider; | ||
use OCP\Mail\Provider\IService; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
class MailProvider implements IProvider { | ||
Check failure on line 20 in lib/Provider/MailProvider.php
|
||
|
||
private ?array $ServiceCollection = []; | ||
|
||
public function __construct( | ||
protected ContainerInterface $container, | ||
protected AccountService $AccountService | ||
) { | ||
} | ||
|
||
/** | ||
* arbitrary unique text string identifying this provider | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @return string id of this provider (e.g. UUID or 'IMAP/SMTP' or anything else) | ||
*/ | ||
public function id(): string { | ||
|
||
return 'mail-application'; | ||
|
||
} | ||
|
||
/** | ||
* localized human frendly name of this provider | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @return string label/name of this provider (e.g. Plain Old IMAP/SMTP) | ||
*/ | ||
public function label(): string { | ||
|
||
return 'Mail Application'; | ||
|
||
} | ||
|
||
/** | ||
* determain if any services are configured for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $userId user id | ||
* | ||
* @return bool true if any services are configure for the user | ||
*/ | ||
public function hasServices(string $userId): bool { | ||
|
||
return (count($this->listServices($userId)) > 0); | ||
|
||
} | ||
|
||
/** | ||
* retrieve collection of services for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $userId user id | ||
* | ||
* @return array<string,IService> collection of service id and object ['1' => IServiceObject] | ||
*/ | ||
public function listServices(string $userId): array { | ||
|
||
try { | ||
// retrieve service(s) details from data store | ||
$accounts = $this->AccountService->findByUserId($userId); | ||
} catch (\Throwable $th) { | ||
return []; | ||
} | ||
// construct temporary collection | ||
$services = []; | ||
// add services to collection | ||
foreach ($accounts as $entry) { | ||
// extract values | ||
$serviceId = (string) $entry->getId(); | ||
$label = $entry->getName(); | ||
$address = new MailAddress($entry->getEmail(), $entry->getName()); | ||
$identity = new MailServiceIdentity(); | ||
$location = new MailServiceLocation(); | ||
// add service to collection | ||
$services[$serviceId] = new MailService($this->container, $userId, $serviceId, $label, $address, $identity, $location); | ||
} | ||
// return list of services for user | ||
return $services; | ||
|
||
} | ||
|
||
/** | ||
* retrieve a service with a specific id | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $userId user id | ||
* @param string $serviceId service id | ||
* | ||
* @return IService|null returns service object or null if none found | ||
*/ | ||
public function findServiceById(string $userId, string $serviceId): IService | null { | ||
|
||
// evaluate if id is a number | ||
if (is_numeric($serviceId)) { | ||
try { | ||
// retrieve service details from data store | ||
$account = $this->AccountService->find($userId, (int) $serviceId); | ||
} catch(\Throwable $th) { | ||
return null; | ||
} | ||
} | ||
// evaliate if service details where found | ||
if ($account instanceof Account) { | ||
// extract values | ||
$serviceId = (string) $account->getId(); | ||
$label = $account->getName(); | ||
$address = new MailAddress($account->getEmail(), $account->getName()); | ||
$identity = new MailServiceIdentity(); | ||
$location = new MailServiceLocation(); | ||
// return mail service object | ||
return (new MailService($this->container, $userId, $serviceId, $label, $address, $identity, $location)); | ||
} | ||
|
||
return null; | ||
|
||
} | ||
|
||
/** | ||
* retrieve a service for a specific mail address | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $userId user id | ||
* @param string $address mail address (e.g. test@example.com) | ||
* | ||
* @return IService returns service object or null if none found | ||
*/ | ||
public function findServiceByAddress(string $userId, string $address): IService | null { | ||
|
||
try { | ||
// retrieve service details from data store | ||
$accounts = $this->AccountService->findByUserIdAndAddress($userId, $address); | ||
} catch(\Throwable $th) { | ||
return null; | ||
} | ||
// evaliate if service details where found | ||
if (is_array($accounts) && count($accounts) > 0 && $accounts[0] instanceof Account) { | ||
// extract values | ||
$serviceId = (string) $accounts[0]->getId(); | ||
$label = $accounts[0]->getName(); | ||
$address = new MailAddress($accounts[0]->getEmail(), $accounts[0]->getName()); | ||
$identity = new MailServiceIdentity(); | ||
$location = new MailServiceLocation(); | ||
// return mail service object | ||
return (new MailService($this->container, $userId, $serviceId, $label, $address, $identity, $location)); | ||
} | ||
|
||
return null; | ||
|
||
} | ||
|
||
/** | ||
* construct a new empty service object | ||
* | ||
* @since 30.0.0 | ||
* | ||
* @return IService blank service object | ||
*/ | ||
public function initiateService(): IService { | ||
|
||
return (new MailService($this->container)); | ||
|
||
} | ||
|
||
/** | ||
* create a service configuration for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $userId user id | ||
* @param IService $service service object | ||
* | ||
* @return string id of created service | ||
*/ | ||
public function createService(string $userId, IService $service): string { | ||
|
||
return ''; | ||
|
||
} | ||
|
||
/** | ||
* modify a service configuration for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $userId user id | ||
* @param IService $service service object | ||
* | ||
* @return string id of modifided service | ||
*/ | ||
public function modifyService(string $userId, IService $service): string { | ||
|
||
return ''; | ||
|
||
} | ||
|
||
/** | ||
* delete a service configuration for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $userId user id | ||
* @param IService $service service object | ||
* | ||
* @return bool status of delete action | ||
*/ | ||
public function deleteService(string $userId, IService $service): bool { | ||
|
||
return false; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.