Skip to content

Commit 3682b5f

Browse files
committed
allow matching regex in custom mail filter
1 parent 9c6c181 commit 3682b5f

28 files changed

+67
-15
lines changed

interface/lib/plugins/mail_user_filter_plugin.inc.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,48 @@ private function mail_user_filter_get_rule($page_form) {
162162

163163
$content .= 'if header :regex ["'.strtolower($page_form->dataRecord["source"]).'"] ["';
164164

165-
$searchterm = preg_quote($page_form->dataRecord["searchterm"]);
166-
$searchterm = str_replace(
167-
array(
168-
'"',
169-
'\\[',
170-
'\\]'
171-
),
172-
array(
173-
'\\"',
174-
'\\\\[',
175-
'\\\\]'
176-
), $searchterm);
165+
# special chars in sieve regex must be escaped with double-backslash
166+
if($page_form->dataRecord["op"] == 'regex') {
167+
# if providing a regex, special chars must already be quoted as intended;
168+
# we will simply try to check for an obviously unquoted double-quote and handle that.
169+
$patterns = array( '/([^\\\\]{2})"/', '/([^\\\\])\\\\"/' );
170+
$replace = array( '${1}\\\\\\\\"', '${1}\\\\\\\\"' );
171+
$searchterm = preg_replace( $patterns, $replace, $page_form->dataRecord["searchterm"] );
172+
} else {
173+
$sieve_regex_escape = array(
174+
'\\' => '\\\\\\',
175+
'+' => '\\\\+',
176+
'*' => '\\\\*',
177+
'?' => '\\\\?',
178+
'[' => '\\\\[',
179+
'^' => '\\\\^',
180+
']' => '\\\\]',
181+
'$' => '\\\\$',
182+
'(' => '\\\\(',
183+
')' => '\\\\)',
184+
'{' => '\\\\{',
185+
'}' => '\\\\}',
186+
'|' => '\\\\|',
187+
'.' => '\\\\.',
188+
# these (from preg_quote) should not be needed
189+
#'=' => '\\\\=',
190+
#'!' => '\\\\!',
191+
#'<' => '\\\\<',
192+
#'>' => '\\\\>',
193+
#':' => '\\\\:',
194+
#'-' => '\\\\-',
195+
#'#' => '\\\\#',
196+
);
197+
$searchterm = strtr( $page_form->dataRecord["searchterm"], $sieve_regex_escape );
198+
199+
}
177200

178201
if($page_form->dataRecord["op"] == 'contains') {
179202
$content .= ".*".$searchterm;
180203
} elseif ($page_form->dataRecord["op"] == 'is') {
181204
$content .= "^".$searchterm."$";
205+
} elseif ($page_form->dataRecord["op"] == 'regex') {
206+
$content .= $searchterm;
182207
} elseif ($page_form->dataRecord["op"] == 'begins') {
183208
$content .= "^".$searchterm."";
184209
} elseif ($page_form->dataRecord["op"] == 'ends') {

interface/web/mail/form/mail_user_filter.tform.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@
9494
'datatype' => 'VARCHAR',
9595
'formtype' => 'SELECT',
9696
'default' => '',
97-
//'value' => array('contains'=>'contains_txt','is' => 'Is','begins'=>'Begins with','ends'=>'Ends with')
98-
'value' => array('contains'=>'contains_txt', 'is' => 'is_txt', 'begins'=>'begins_with_txt', 'ends'=>'ends_with_txt', 'localpart' => 'localpart_txt', 'domain' => 'domain_txt')
97+
'value' => array('contains'=>'contains_txt', 'is'=>'is_txt', 'begins'=>'begins_with_txt', 'ends'=>'ends_with_txt', 'regex'=>'regex_txt', 'localpart'=>'localpart_txt', 'domain'=>'domain_txt')
9998
),
10099
'searchterm' => array (
101100
'datatype' => 'VARCHAR',

interface/web/mail/lib/lang/ar_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Contains';
1616
$wb['is_txt'] = 'Is';
1717
$wb['begins_with_txt'] = 'Begins with';
1818
$wb['ends_with_txt'] = 'Ends with';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Delete';
2021
$wb['move_stop_txt'] = 'Move to';
2122
$wb['header_txt'] = 'Header';

interface/web/mail/lib/lang/bg_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Описание';
1616
$wb['is_txt'] = 'е';
1717
$wb['begins_with_txt'] = 'Начало с';
1818
$wb['ends_with_txt'] = 'Край с';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Изтрий';
2021
$wb['move_stop_txt'] = 'Move to';
2122
$wb['header_txt'] = 'Header';

interface/web/mail/lib/lang/br_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Contêm';
1616
$wb['is_txt'] = 'é';
1717
$wb['begins_with_txt'] = 'Iniciando com';
1818
$wb['ends_with_txt'] = 'Terminando com';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['move_stop_txt'] = 'Mover para';
2021
$wb['delete_txt'] = 'Remover';
2122
$wb['header_txt'] = 'Cabeçalho';

interface/web/mail/lib/lang/ca_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Contient';
1616
$wb['is_txt'] = 'Est';
1717
$wb['begins_with_txt'] = 'Commence par';
1818
$wb['ends_with_txt'] = 'Fini par';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Supprimer';
2021
$wb['move_stop_txt'] = 'Move to';
2122
$wb['header_txt'] = 'Header';

interface/web/mail/lib/lang/cz_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Obsahuje';
1616
$wb['is_txt'] = 'Je';
1717
$wb['begins_with_txt'] = 'Začíná na';
1818
$wb['ends_with_txt'] = 'Končí na';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Smazat';
2021
$wb['move_stop_txt'] = 'Přesunout';
2122
$wb['header_txt'] = 'Hlavička';

interface/web/mail/lib/lang/de_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Enthält';
1616
$wb['is_txt'] = 'Ist';
1717
$wb['begins_with_txt'] = 'Beginnt mit';
1818
$wb['ends_with_txt'] = 'Endet mit';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['move_stop_txt'] = 'Verschieben nach';
2021
$wb['delete_txt'] = 'Löschen';
2122
$wb['header_txt'] = 'Header';

interface/web/mail/lib/lang/dk_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Indeholder';
1616
$wb['is_txt'] = 'Er';
1717
$wb['begins_with_txt'] = 'Begynder med';
1818
$wb['ends_with_txt'] = 'Slutter med';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['move_stop_txt'] = 'Flyt til';
2021
$wb['delete_txt'] = 'Slet';
2122
$wb['header_txt'] = 'Hoved';

interface/web/mail/lib/lang/el_mail_user_filter.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $wb['contains_txt'] = 'Περιλαμβάνει';
1616
$wb['is_txt'] = 'είναι';
1717
$wb['begins_with_txt'] = 'Ξεκινά με';
1818
$wb['ends_with_txt'] = 'Τελειώνει σε';
19+
$wb['regex_txt'] = 'Matches Regex';
1920
$wb['delete_txt'] = 'Διαγραφή';
2021
$wb['move_stop_txt'] = 'Move to';
2122
$wb['header_txt'] = 'Header';

0 commit comments

Comments
 (0)