Skip to content

Commit

Permalink
fixup! feat: mail filters
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Sep 12, 2024
1 parent a1d5ba3 commit 196e61f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
35 changes: 13 additions & 22 deletions lib/Service/MailFilter/FilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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 ###';
Expand Down Expand Up @@ -38,21 +39,21 @@ public function buildSieveScript(array $filters, string $untouchedScript): strin
$tests[] = sprintf(
'header :%s "Subject" %s',
$test['operator'],
$this->stringList($test['values'])
SieveUtils::stringList($test['values']),
);
}
if ($test['field'] === 'to') {
$tests[] = sprintf(
'address :%s :all "To" %s',
$test['operator'],
$this->stringList($test['values'])
SieveUtils::stringList($test['values']),
);
}
if ($test['field'] === 'from') {
$tests[] = sprintf(
'address :%s :all "From" %s',
$test['operator'],
$this->stringList($test['values'])
SieveUtils::stringList($test['values']),
);
}
}
Expand All @@ -61,11 +62,17 @@ public function buildSieveScript(array $filters, string $untouchedScript): strin
foreach ($filter['actions'] as $action) {
if ($action['type'] === 'fileinto') {
$extensions[] = 'fileinto';
$actions[] = sprintf('fileinto "%s";', $action['mailbox']);
$actions[] = sprintf(
'fileinto "%s";',
SieveUtils::escapeString($action['mailbox'])
);
}
if ($action['type'] === 'addflag') {
$extensions[] = 'imap4flags';
$actions[] = sprintf('addflag %s;', $this->stringList($this->sanitizeFlag($action['flag'])));
$actions[] = sprintf(
'addflag "%s";',
SieveUtils::escapeString($this->sanitizeFlag($action['flag']))
);
}
if ($action['type'] === 'keep') {
$actions[] = 'keep;';
Expand Down Expand Up @@ -95,7 +102,7 @@ public function buildSieveScript(array $filters, string $untouchedScript): strin

if (count($extensions) > 0) {
$requireSection[] = self::SEPARATOR;
$requireSection[] = 'require ' . $this->stringList($extensions) . ';';
$requireSection[] = 'require ' . SieveUtils::stringList($extensions) . ';';
$requireSection[] = self::SEPARATOR;
$requireSection[] = '';
}
Expand All @@ -118,22 +125,6 @@ public function buildSieveScript(array $filters, string $untouchedScript): strin
));
}

private function stringList(string|array $value): string {
if (is_string($value)) {
$items = explode(',', $value);
} else {
$items = $value;
}

$items = array_map([$this, 'quoteString'], $items);

return '[' . implode(', ', $items) . ']';
}

private function quoteString(string $value): string {
return '"' . $value . '"';
}

private function sanitizeFlag(string $flag): string {
try {
return $this->imapFlag->create($flag);
Expand Down
10 changes: 3 additions & 7 deletions lib/Service/OutOfOffice/OutOfOfficeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DateTimeZone;
use JsonException;
use OCA\Mail\Exception\OutOfOfficeParserException;
use OCA\Mail\Sieve\SieveUtils;

/**
* Parses and builds out-of-office states from/to sieve scripts.
Expand Down Expand Up @@ -119,7 +120,7 @@ public function buildSieveScript(
$condition = "currentdate :value \"ge\" \"iso8601\" \"$formattedStart\"";
}

$escapedSubject = $this->escapeStringForSieve($state->getSubject());
$escapedSubject = SieveUtils::escapeString($state->getSubject());
$vacation = [
'vacation',
':days 4',
Expand All @@ -134,7 +135,7 @@ public function buildSieveScript(
$vacation[] = ":addresses [$joinedRecipients]";
}

$escapedMessage = $this->escapeStringForSieve($state->getMessage());
$escapedMessage = SieveUtils::escapeString($state->getMessage());
$vacation[] = "\"$escapedMessage\"";
$vacationCommand = implode(' ', $vacation);

Expand Down Expand Up @@ -183,9 +184,4 @@ public function buildSieveScript(
private function formatDateForSieve(DateTimeImmutable $date): string {
return $date->setTimezone($this->utc)->format('Y-m-d\TH:i:s\Z');
}

private function escapeStringForSieve(string $subject): string {
$subject = preg_replace('/\\\\/', '\\\\\\\\', $subject);
return preg_replace('/"/', '\\"', $subject);
}
}

0 comments on commit 196e61f

Please sign in to comment.