-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathFilterBuilder.php
More file actions
181 lines (155 loc) Β· 4.68 KB
/
FilterBuilder.php
File metadata and controls
181 lines (155 loc) Β· 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?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\MailFilter;
use OCA\Mail\Exception\ImapFlagEncodingException;
use OCA\Mail\IMAP\ImapFlag;
use OCA\Mail\Sieve\SieveUtils;
class FilterBuilder {
private const SEPARATOR = '### Nextcloud Mail: Filters ### DON\'T EDIT ###';
private const DATA_MARKER = '# FILTER: ';
private const SIEVE_NEWLINE = "\r\n";
public function __construct(
private ImapFlag $imapFlag,
) {
}
public function buildSieveScript(array $filters, string $untouchedScript): string {
$commands = [];
$extensions = [];
foreach ($filters as $filter) {
if ($filter['enable'] === false) {
continue;
}
$commands[] = '# ' . $filter['name'];
$tests = [];
foreach ($filter['tests'] as $test) {
if ($test['field'] === 'subject') {
$tests[] = sprintf(
'header :%s "Subject" %s',
$test['operator'],
SieveUtils::stringList($test['values']),
);
}
if ($test['field'] === 'to') {
$tests[] = sprintf(
'address :%s :all "To" %s',
$test['operator'],
SieveUtils::stringList($test['values']),
);
}
if ($test['field'] === 'from') {
$tests[] = sprintf(
'address :%s :all "From" %s',
$test['operator'],
SieveUtils::stringList($test['values']),
);
}
}
if ($tests === []) {
// skip filter without tests
$commands[] = '# No valid tests found';
continue;
}
$actions = [];
foreach ($filter['actions'] as $action) {
if ($action['type'] === 'fileinto') {
$extensions[] = 'fileinto';
$actions[] = sprintf(
'fileinto "%s";',
SieveUtils::escapeString($action['mailbox'])
);
}
if ($action['type'] === 'addflag' || $action['type'] === 'addsystemflag') {
$extensions[] = 'imap4flags';
if ($this->isSystemFlag($action['flag'])) {
$flag = SieveUtils::escapeString($action['flag']);
} else {
$flag = SieveUtils::escapeString($this->sanitizeFlag($action['flag']));
}
$actions[] = sprintf('addflag "%s";', $flag);
}
if ($action['type'] === 'keep') {
$actions[] = 'keep;';
}
if ($action['type'] === 'stop') {
$actions[] = 'stop;';
}
}
if (count($tests) > 1) {
$ifTest = sprintf('%s (%s)', $filter['operator'], implode(', ', $tests));
} else {
$ifTest = $tests[0];
}
$actions = array_map(
static fn ($action) => "\t" . $action,
$actions
);
$ifBlock = sprintf(
"if %s {\r\n%s\r\n}",
$ifTest,
implode(self::SIEVE_NEWLINE, $actions)
);
$commands[] = $ifBlock;
}
$lines = [];
$extensions = array_unique($extensions);
if ($extensions !== []) {
$lines[] = self::SEPARATOR;
$lines[] = 'require ' . SieveUtils::stringList($extensions) . ';';
$lines[] = self::SEPARATOR;
}
/*
* Using implode("\r\n", $lines) may introduce an extra newline if the original script already ends with one.
* There may be a cleaner solution, but I couldn't find one that works seamlessly with Filter and Autoresponder.
* Feel free to give it a try!
*/
if (str_ends_with($untouchedScript, self::SIEVE_NEWLINE . self::SIEVE_NEWLINE)) {
$untouchedScript = substr($untouchedScript, 0, -2);
}
$lines[] = $untouchedScript;
if ($filters !== []) {
$lines[] = self::SEPARATOR;
$lines[] = self::DATA_MARKER . json_encode($this->sanitizeDefinition($filters), JSON_THROW_ON_ERROR);
array_push($lines, ...$commands);
$lines[] = self::SEPARATOR;
}
return implode(self::SIEVE_NEWLINE, $lines);
}
private function sanitizeFlag(string $flag): string {
try {
return $this->imapFlag->create($flag);
} catch (ImapFlagEncodingException) {
return 'placeholder_for_invalid_label';
}
}
private function sanitizeDefinition(array $filters): array {
return array_map(static function ($filter) {
unset($filter['accountId'], $filter['id']);
$filter['tests'] = array_map(static function ($test) {
unset($test['id']);
return $test;
}, $filter['tests']);
$filter['actions'] = array_map(static function ($action) {
unset($action['id']);
return $action;
}, $filter['actions']);
$filter['priority'] = (int)$filter['priority'];
return $filter;
}, $filters);
}
private function isSystemFlag(string $flag): bool {
$flags = [
\Horde_Imap_Client::FLAG_ANSWERED,
\Horde_Imap_Client::FLAG_DELETED,
\Horde_Imap_Client::FLAG_DRAFT,
\Horde_Imap_Client::FLAG_FLAGGED,
\Horde_Imap_Client::FLAG_RECENT,
\Horde_Imap_Client::FLAG_SEEN,
];
// Check is done in lowercase to keep the notation from the RFC (e.g. \Seen)
return in_array(strtolower($flag), $flags, true);
}
}