Skip to content

Commit 3a9385e

Browse files
committed
feat: enable account debugging for sieve
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 27bbb5b commit 3a9385e

File tree

3 files changed

+41
-48
lines changed

3 files changed

+41
-48
lines changed

lib/Controller/SieveController.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,36 +141,40 @@ public function updateAccount(int $id,
141141
$mailAccount->setSieveEnabled(false);
142142
$mailAccount->setSieveHost(null);
143143
$mailAccount->setSievePort(null);
144+
$mailAccount->setSieveSslMode(null);
144145
$mailAccount->setSieveUser(null);
145146
$mailAccount->setSievePassword(null);
146-
$mailAccount->setSieveSslMode(null);
147147

148148
$this->mailAccountMapper->save($mailAccount);
149149
return new JSONResponse(['sieveEnabled' => $mailAccount->isSieveEnabled()]);
150150
}
151151

152-
if (empty($sieveUser)) {
152+
if (empty($sieveUser) && empty($sievePassword)) {
153+
$useImapCredentials = true;
153154
$sieveUser = $mailAccount->getInboundUser();
154-
}
155-
156-
if (empty($sievePassword)) {
157-
$sievePassword = $mailAccount->getInboundPassword();
155+
/** @psalm-suppress PossiblyNullArgument */
156+
$sievePassword = $this->crypto->decrypt($mailAccount->getInboundPassword());
158157
} else {
159-
$sievePassword = $this->crypto->encrypt($sievePassword);
158+
$useImapCredentials = false;
160159
}
161160

162161
try {
163-
$this->sieveClientFactory->createClient($sieveHost, $sievePort, $sieveUser, $sievePassword, $sieveSslMode);
162+
$this->sieveClientFactory->createClient($sieveHost, $sievePort, $sieveUser, $sievePassword, $sieveSslMode, null);
164163
} catch (ManagesieveException $e) {
165164
throw new CouldNotConnectException($e, 'ManageSieve', $sieveHost, $sievePort);
166165
}
167166

168167
$mailAccount->setSieveEnabled(true);
169168
$mailAccount->setSieveHost($sieveHost);
170169
$mailAccount->setSievePort($sievePort);
171-
$mailAccount->setSieveUser($mailAccount->getInboundUser() === $sieveUser ? null : $sieveUser);
172-
$mailAccount->setSievePassword($mailAccount->getInboundPassword() === $sievePassword ? null : $sievePassword);
173170
$mailAccount->setSieveSslMode($sieveSslMode);
171+
if ($useImapCredentials) {
172+
$mailAccount->setSieveUser(null);
173+
$mailAccount->setSievePassword(null);
174+
} else {
175+
$mailAccount->setSieveUser($sieveUser);
176+
$mailAccount->setSievePassword($this->crypto->encrypt($sievePassword));
177+
}
174178

175179
$this->mailAccountMapper->save($mailAccount);
176180
return new JSONResponse(['sieveEnabled' => $mailAccount->isSieveEnabled()]);

lib/Sieve/SieveClientFactory.php

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,16 @@
1515
use OCP\Security\ICrypto;
1616

1717
class SieveClientFactory {
18-
/** @var ICrypto */
19-
private $crypto;
18+
private ICrypto $crypto;
19+
private IConfig $config;
20+
private array $cache = [];
2021

21-
/** @var IConfig */
22-
private $config;
23-
24-
private $cache = [];
25-
26-
/**
27-
* @param ICrypto $crypto
28-
* @param IConfig $config
29-
*/
3022
public function __construct(ICrypto $crypto, IConfig $config) {
3123
$this->crypto = $crypto;
3224
$this->config = $config;
3325
}
3426

3527
/**
36-
* @param Account $account
37-
* @return ManageSieve
3828
* @throws ManageSieve\Exception
3929
*/
4030
public function getClient(Account $account): ManageSieve {
@@ -48,28 +38,31 @@ public function getClient(Account $account): ManageSieve {
4838
$password = $account->getMailAccount()->getInboundPassword();
4939
}
5040

41+
if ($account->getDebug() || $this->config->getSystemValueBool('app.mail.debug')) {
42+
$logFile = $this->config->getSystemValue('datadirectory') . '/mail-' . $account->getUserId() . '-' . $account->getId() . '-sieve.log';
43+
} else {
44+
$logFile = null;
45+
}
46+
5147
$this->cache[$account->getId()] = $this->createClient(
5248
$account->getMailAccount()->getSieveHost(),
5349
$account->getMailAccount()->getSievePort(),
5450
$user,
55-
$password,
56-
$account->getMailAccount()->getSieveSslMode()
51+
$this->crypto->decrypt($password),
52+
$account->getMailAccount()->getSieveSslMode(),
53+
$logFile,
5754
);
5855
}
5956

6057
return $this->cache[$account->getId()];
6158
}
6259

6360
/**
64-
* @param string $host
65-
* @param int $port
66-
* @param string $user
67-
* @param string $password
68-
* @param string $sslMode
69-
* @return ManageSieve
61+
* @param string $sslMode possible values: '', 'none', 'ssl' or 'tls'
62+
* @param ?string $logFile absolute path for logFile or null to disable logging
7063
* @throws ManageSieve\Exception
7164
*/
72-
public function createClient(string $host, int $port, string $user, string $password, string $sslMode): ManageSieve {
65+
public function createClient(string $host, int $port, string $user, string $password, string $sslMode, ?string $logFile): ManageSieve {
7366
if (empty($sslMode)) {
7467
$sslMode = true;
7568
} elseif ($sslMode === 'none') {
@@ -80,9 +73,9 @@ public function createClient(string $host, int $port, string $user, string $pass
8073
'host' => $host,
8174
'port' => $port,
8275
'user' => $user,
83-
'password' => $this->crypto->decrypt($password),
76+
'password' => $password,
8477
'secure' => $sslMode,
85-
'timeout' => (int)$this->config->getSystemValue('app.mail.sieve.timeout', 5),
78+
'timeout' => $this->config->getSystemValueInt('app.mail.sieve.timeout', 5),
8679
'context' => [
8780
'ssl' => [
8881
'verify_peer' => $this->config->getSystemValueBool('app.mail.verify-tls-peer', true),
@@ -92,8 +85,8 @@ public function createClient(string $host, int $port, string $user, string $pass
9285
],
9386
];
9487

95-
if ($this->config->getSystemValue('debug', false)) {
96-
$params['logger'] = new SieveLogger($this->config->getSystemValue('datadirectory') . '/horde_sieve.log');
88+
if ($logFile !== null) {
89+
$params['logger'] = new SieveLogger($logFile);
9790
}
9891

9992
return new ManageSieve($params);

tests/Integration/Sieve/SieveClientFactoryTest.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,16 @@ protected function setUp(): void {
3535
$this->crypto = $this->createMock(ICrypto::class);
3636
$this->config = $this->createMock(IConfig::class);
3737

38-
$this->config->method('getSystemValue')
39-
->willReturnCallback(static function ($key, $default) {
40-
if ($key === 'app.mail.sieve.timeout') {
41-
return 5;
42-
}
43-
if ($key === 'debug') {
44-
return false;
45-
}
46-
return null;
47-
});
38+
$this->config->method('getSystemValueInt')
39+
->willReturnMap([
40+
['app.mail.sieve.timeout', 5, 5],
41+
]);
4842

4943
$this->config->method('getSystemValueBool')
50-
->with('app.mail.verify-tls-peer', true)
51-
->willReturn(false);
44+
->willReturnMap([
45+
['app.mail.verify-tls-peer', true, false],
46+
['app.mail.debug', false, false],
47+
]);
5248

5349
$this->factory = new SieveClientFactory($this->crypto, $this->config);
5450
}

0 commit comments

Comments
 (0)