-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10135 from nextcloud/mail-filters
mail filters
- Loading branch information
Showing
62 changed files
with
2,676 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Controller; | ||
|
||
use OCA\Mail\AppInfo\Application; | ||
use OCA\Mail\Service\AccountService; | ||
use OCA\Mail\Service\FilterService; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\Route; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\AppFramework\OCSController; | ||
use OCP\IRequest; | ||
|
||
class FilterController extends OCSController { | ||
private string $currentUserId; | ||
|
||
public function __construct( | ||
IRequest $request, | ||
string $userId, | ||
private FilterService $mailFilterService, | ||
private AccountService $accountService, | ||
) { | ||
parent::__construct(Application::APP_ID, $request); | ||
$this->currentUserId = $userId; | ||
} | ||
|
||
#[Route(Route::TYPE_FRONTPAGE, verb: 'GET', url: '/api/filter/{accountId}', requirements: ['accountId' => '[\d]+'])] | ||
public function getFilters(int $accountId) { | ||
$account = $this->accountService->findById($accountId); | ||
|
||
if ($account->getUserId() !== $this->currentUserId) { | ||
return new JSONResponse([], Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$result = $this->mailFilterService->parse($account->getMailAccount()); | ||
|
||
return new JSONResponse($result->getFilters()); | ||
} | ||
|
||
#[Route(Route::TYPE_FRONTPAGE, verb: 'PUT', url: '/api/filter/{accountId}', requirements: ['accountId' => '[\d]+'])] | ||
public function updateFilters(int $accountId, array $filters) { | ||
$account = $this->accountService->findById($accountId); | ||
|
||
if ($account->getUserId() !== $this->currentUserId) { | ||
return new JSONResponse([], Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$this->mailFilterService->update($account->getMailAccount(), $filters); | ||
|
||
return new JSONResponse([]); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Exception; | ||
|
||
use Exception; | ||
|
||
class FilterParserException extends Exception { | ||
|
||
public static function invalidJson(\Throwable $exception): FilterParserException { | ||
return new self( | ||
'Failed to parse filter state json: ' . $exception->getMessage(), | ||
0, | ||
$exception, | ||
); | ||
} | ||
|
||
public static function invalidState(): FilterParserException { | ||
return new self( | ||
'Reached an invalid state', | ||
); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Exception; | ||
|
||
use Exception; | ||
|
||
class ImapFlagEncodingException extends Exception { | ||
public static function create($label): ImapFlagEncodingException { | ||
return new self( | ||
'Failed to convert the given label "' . $label . '" to UTF7-IMAP', | ||
0, | ||
); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\IMAP; | ||
|
||
use OCA\Mail\Exception\ImapFlagEncodingException; | ||
|
||
class ImapFlag { | ||
/** | ||
* @throws ImapFlagEncodingException | ||
*/ | ||
public function create(string $label): string { | ||
$flag = str_replace(' ', '_', $label); | ||
$flag = mb_convert_encoding($flag, 'UTF7-IMAP', 'UTF-8'); | ||
|
||
if ($flag === false) { | ||
throw ImapFlagEncodingException::create($label); | ||
} | ||
|
||
return '$' . strtolower(mb_strcut($flag, 0, 63)); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Service; | ||
|
||
use OCA\Mail\Db\MailAccount; | ||
|
||
class AllowedRecipientsService { | ||
|
||
public function __construct( | ||
private AliasesService $aliasesService, | ||
) { | ||
} | ||
|
||
/** | ||
* Return a list of allowed recipients for a given mail account | ||
* | ||
* @return string[] email addresses | ||
*/ | ||
public function get(MailAccount $mailAccount): array { | ||
$aliases = array_map( | ||
static fn ($alias) => $alias->getAlias(), | ||
$this->aliasesService->findAll($mailAccount->getId(), $mailAccount->getUserId()) | ||
); | ||
|
||
return array_merge([$mailAccount->getEmail()], $aliases); | ||
} | ||
} |
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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Service; | ||
|
||
use Horde\ManageSieve\Exception as ManageSieveException; | ||
use JsonException; | ||
use OCA\Mail\Db\MailAccount; | ||
use OCA\Mail\Exception\ClientException; | ||
use OCA\Mail\Exception\CouldNotConnectException; | ||
use OCA\Mail\Exception\FilterParserException; | ||
use OCA\Mail\Exception\OutOfOfficeParserException; | ||
use OCA\Mail\Service\MailFilter\FilterBuilder; | ||
use OCA\Mail\Service\MailFilter\FilterParser; | ||
use OCA\Mail\Service\MailFilter\FilterParserResult; | ||
use OCA\Mail\Service\OutOfOffice\OutOfOfficeParser; | ||
use OCA\Mail\Service\OutOfOffice\OutOfOfficeState; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class FilterService { | ||
|
||
public function __construct( | ||
private AllowedRecipientsService $allowedRecipientsService, | ||
private OutOfOfficeParser $outOfOfficeParser, | ||
private FilterParser $filterParser, | ||
private FilterBuilder $filterBuilder, | ||
private SieveService $sieveService, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws ClientException | ||
* @throws ManageSieveException | ||
* @throws CouldNotConnectException | ||
* @throws FilterParserException | ||
*/ | ||
public function parse(MailAccount $account): FilterParserResult { | ||
$script = $this->sieveService->getActiveScript($account->getUserId(), $account->getId()); | ||
return $this->filterParser->parseFilterState($script->getScript()); | ||
} | ||
|
||
/** | ||
* @throws CouldNotConnectException | ||
* @throws JsonException | ||
* @throws ClientException | ||
* @throws OutOfOfficeParserException | ||
* @throws ManageSieveException | ||
* @throws FilterParserException | ||
*/ | ||
public function update(MailAccount $account, array $filters): void { | ||
$script = $this->sieveService->getActiveScript($account->getUserId(), $account->getId()); | ||
|
||
$oooResult = $this->outOfOfficeParser->parseOutOfOfficeState($script->getScript()); | ||
$filterResult = $this->filterParser->parseFilterState($oooResult->getUntouchedSieveScript()); | ||
|
||
$newScript = $this->filterBuilder->buildSieveScript( | ||
$filters, | ||
$filterResult->getUntouchedSieveScript() | ||
); | ||
|
||
$oooState = $oooResult->getState(); | ||
|
||
if ($oooState instanceof OutOfOfficeState) { | ||
$newScript = $this->outOfOfficeParser->buildSieveScript( | ||
$oooState, | ||
$newScript, | ||
$this->allowedRecipientsService->get($account), | ||
); | ||
} | ||
|
||
try { | ||
$this->sieveService->updateActiveScript($account->getUserId(), $account->getId(), $newScript); | ||
} catch (ManageSieveException $e) { | ||
$this->logger->error('Failed to save sieve script: ' . $e->getMessage(), [ | ||
'exception' => $e, | ||
'script' => $newScript, | ||
]); | ||
throw $e; | ||
} | ||
} | ||
} |
Oops, something went wrong.