2021-01-27 18:54:37 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
2024-06-21 16:58:11 +03:00
|
|
|
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2021-01-27 18:54:37 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Mail\Controller;
|
|
|
|
|
|
|
|
use Horde\ManageSieve\Exception as ManagesieveException;
|
|
|
|
use OCA\Mail\AppInfo\Application;
|
|
|
|
use OCA\Mail\Db\MailAccountMapper;
|
|
|
|
use OCA\Mail\Exception\ClientException;
|
|
|
|
use OCA\Mail\Exception\CouldNotConnectException;
|
2022-11-16 20:42:25 +03:00
|
|
|
use OCA\Mail\Http\JsonResponse as MailJsonResponse;
|
2023-01-25 17:40:41 +03:00
|
|
|
use OCA\Mail\Http\TrapError;
|
2023-11-21 00:50:16 +03:00
|
|
|
use OCA\Mail\Service\SieveService;
|
2021-01-27 18:54:37 +03:00
|
|
|
use OCA\Mail\Sieve\SieveClientFactory;
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2022-11-16 20:42:25 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2024-07-30 13:09:10 +03:00
|
|
|
use OCP\AppFramework\Http\Attribute\OpenAPI;
|
2021-01-27 18:54:37 +03:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\Security\ICrypto;
|
2023-04-19 15:59:20 +03:00
|
|
|
use OCP\Security\IRemoteHostValidator;
|
2023-05-09 18:15:26 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2021-01-27 18:54:37 +03:00
|
|
|
|
2024-07-30 13:09:10 +03:00
|
|
|
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
|
2021-01-27 18:54:37 +03:00
|
|
|
class SieveController extends Controller {
|
2022-10-01 23:01:07 +03:00
|
|
|
private MailAccountMapper $mailAccountMapper;
|
|
|
|
private SieveClientFactory $sieveClientFactory;
|
|
|
|
private string $currentUserId;
|
|
|
|
private ICrypto $crypto;
|
2023-03-29 02:58:34 +03:00
|
|
|
private IRemoteHostValidator $hostValidator;
|
2023-05-09 18:15:26 +03:00
|
|
|
private LoggerInterface $logger;
|
2021-01-27 18:54:37 +03:00
|
|
|
|
|
|
|
public function __construct(IRequest $request,
|
2023-06-28 15:44:08 +03:00
|
|
|
string $UserId,
|
|
|
|
MailAccountMapper $mailAccountMapper,
|
|
|
|
SieveClientFactory $sieveClientFactory,
|
|
|
|
ICrypto $crypto,
|
|
|
|
IRemoteHostValidator $hostValidator,
|
2023-11-21 00:50:16 +03:00
|
|
|
LoggerInterface $logger,
|
|
|
|
private SieveService $sieveService,
|
2021-01-27 18:54:37 +03:00
|
|
|
) {
|
|
|
|
parent::__construct(Application::APP_ID, $request);
|
|
|
|
$this->currentUserId = $UserId;
|
|
|
|
$this->mailAccountMapper = $mailAccountMapper;
|
|
|
|
$this->sieveClientFactory = $sieveClientFactory;
|
|
|
|
$this->crypto = $crypto;
|
2022-11-16 20:42:25 +03:00
|
|
|
$this->hostValidator = $hostValidator;
|
2023-05-09 18:15:26 +03:00
|
|
|
$this->logger = $logger;
|
2021-01-27 18:54:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param int $id account id
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*
|
|
|
|
* @throws CouldNotConnectException
|
|
|
|
* @throws ClientException
|
2023-11-21 00:50:16 +03:00
|
|
|
* @throws ManagesieveException
|
2021-01-27 18:54:37 +03:00
|
|
|
*/
|
2023-01-25 17:40:41 +03:00
|
|
|
#[TrapError]
|
2021-01-27 18:54:37 +03:00
|
|
|
public function getActiveScript(int $id): JSONResponse {
|
2023-11-21 00:50:16 +03:00
|
|
|
$activeScript = $this->sieveService->getActiveScript($this->currentUserId, $id);
|
2021-01-27 18:54:37 +03:00
|
|
|
return new JSONResponse([
|
2023-11-21 00:50:16 +03:00
|
|
|
'scriptName' => $activeScript->getName(),
|
|
|
|
'script' => $activeScript->getScript(),
|
2021-01-27 18:54:37 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param int $id account id
|
|
|
|
* @param string $script
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*
|
|
|
|
* @throws ClientException
|
|
|
|
* @throws CouldNotConnectException
|
|
|
|
*/
|
2023-01-25 17:40:41 +03:00
|
|
|
#[TrapError]
|
2021-01-27 18:54:37 +03:00
|
|
|
public function updateActiveScript(int $id, string $script): JSONResponse {
|
2023-05-09 18:15:26 +03:00
|
|
|
try {
|
2023-11-21 00:50:16 +03:00
|
|
|
$this->sieveService->updateActiveScript($this->currentUserId, $id, $script);
|
2023-05-09 18:15:26 +03:00
|
|
|
} catch (ManagesieveException $e) {
|
|
|
|
$this->logger->error('Installing sieve script failed: ' . $e->getMessage(), ['app' => 'mail', 'exception' => $e]);
|
|
|
|
return new JSONResponse(data: ['message' => $e->getMessage()], statusCode: Http::STATUS_UNPROCESSABLE_ENTITY);
|
|
|
|
}
|
2021-01-27 18:54:37 +03:00
|
|
|
|
|
|
|
return new JSONResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param int $id account id
|
|
|
|
* @param bool $sieveEnabled
|
|
|
|
* @param string $sieveHost
|
|
|
|
* @param int $sievePort
|
|
|
|
* @param string $sieveUser
|
|
|
|
* @param string $sievePassword
|
|
|
|
* @param string $sieveSslMode
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*
|
|
|
|
* @throws CouldNotConnectException
|
|
|
|
* @throws DoesNotExistException
|
|
|
|
*/
|
2023-01-25 17:40:41 +03:00
|
|
|
#[TrapError]
|
2021-01-27 18:54:37 +03:00
|
|
|
public function updateAccount(int $id,
|
2023-06-28 15:44:08 +03:00
|
|
|
bool $sieveEnabled,
|
|
|
|
string $sieveHost,
|
|
|
|
int $sievePort,
|
|
|
|
string $sieveUser,
|
|
|
|
string $sievePassword,
|
|
|
|
string $sieveSslMode
|
2021-01-27 18:54:37 +03:00
|
|
|
): JSONResponse {
|
2022-11-16 20:42:25 +03:00
|
|
|
if (!$this->hostValidator->isValid($sieveHost)) {
|
|
|
|
return MailJsonResponse::fail(
|
|
|
|
[
|
|
|
|
'error' => 'CONNECTION_ERROR',
|
|
|
|
'service' => 'ManageSieve',
|
|
|
|
'host' => $sieveHost,
|
|
|
|
'port' => $sievePort,
|
|
|
|
],
|
|
|
|
Http::STATUS_UNPROCESSABLE_ENTITY
|
|
|
|
);
|
|
|
|
}
|
2021-01-27 18:54:37 +03:00
|
|
|
$mailAccount = $this->mailAccountMapper->find($this->currentUserId, $id);
|
|
|
|
|
|
|
|
if ($sieveEnabled === false) {
|
|
|
|
$mailAccount->setSieveEnabled(false);
|
|
|
|
$mailAccount->setSieveHost(null);
|
|
|
|
$mailAccount->setSievePort(null);
|
|
|
|
$mailAccount->setSieveUser(null);
|
|
|
|
$mailAccount->setSievePassword(null);
|
|
|
|
$mailAccount->setSieveSslMode(null);
|
|
|
|
|
|
|
|
$this->mailAccountMapper->save($mailAccount);
|
|
|
|
return new JSONResponse(['sieveEnabled' => $mailAccount->isSieveEnabled()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($sieveUser)) {
|
|
|
|
$sieveUser = $mailAccount->getInboundUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($sievePassword)) {
|
|
|
|
$sievePassword = $mailAccount->getInboundPassword();
|
|
|
|
} else {
|
|
|
|
$sievePassword = $this->crypto->encrypt($sievePassword);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->sieveClientFactory->createClient($sieveHost, $sievePort, $sieveUser, $sievePassword, $sieveSslMode);
|
|
|
|
} catch (ManagesieveException $e) {
|
2022-02-28 20:35:09 +03:00
|
|
|
throw new CouldNotConnectException($e, 'ManageSieve', $sieveHost, $sievePort);
|
2021-01-27 18:54:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$mailAccount->setSieveEnabled(true);
|
|
|
|
$mailAccount->setSieveHost($sieveHost);
|
|
|
|
$mailAccount->setSievePort($sievePort);
|
|
|
|
$mailAccount->setSieveUser($mailAccount->getInboundUser() === $sieveUser ? null : $sieveUser);
|
|
|
|
$mailAccount->setSievePassword($mailAccount->getInboundPassword() === $sievePassword ? null : $sievePassword);
|
|
|
|
$mailAccount->setSieveSslMode($sieveSslMode);
|
|
|
|
|
|
|
|
$this->mailAccountMapper->save($mailAccount);
|
|
|
|
return new JSONResponse(['sieveEnabled' => $mailAccount->isSieveEnabled()]);
|
|
|
|
}
|
|
|
|
}
|