diff options
author | marczi <marczi@dev-labs.com> | 2022-08-15 10:46:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-15 10:46:28 +0300 |
commit | 974e98baacff2fdddb4c4e7cf808dc9f7d35d16d (patch) | |
tree | e4c237a06ff5f251ba5448c141a7771779e4501b | |
parent | c2b6a9d278dd51ea499604d0c771b86b4550ee1b (diff) |
Managesieve plugin: sieverules compatibility (#8571)
Added compatibility with sieverules plugin: import disabled rules stored in sieverules format
3 files changed, 87 insertions, 0 deletions
diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php index 2d867b63e..3acd2e648 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php @@ -659,6 +659,10 @@ class rcube_sieve_script if (preg_match('/^# rule:\[(.*)\]/', $line, $matches)) { $rulename = $matches[1]; } + // "sieverules" compatibility: import disabled rule (sieverules stores as a serialized PHP object) + else if (preg_match('/^# disabledRule:\[(.*)\]/', $line, $matches)) { + $this->sieverules_import_disabled_rule($matches[1]); + } // KEP:14 variables else if (preg_match('/^# (EDITOR|EDITOR_VERSION) (.+)$/', $line, $matches)) { $this->set_var($matches[1], $matches[2]); @@ -1478,4 +1482,60 @@ class rcube_sieve_script return $position; } + + /** + * Converts disabled Sieverules serialized rules into Managesieve format + * + * 1. Test (condition) format: + * Sieverules Managesieve + * + * type -> test (like "header", "subject", etc...) + * operator -> type (operator like equal, contains, etc...) + * header -> arg1 (operand 1, optional) + * target -> arg2 (operand 2, optional) + * + * 2. Action format (different actions may have different parameters): + * + * - vacation action + * Sieverules Managesieve + * msg -> reason + */ + private function sieverules_import_disabled_rule($serialized_rule) + { + $serialized_rule = str_replace("[!r]", "\r", $serialized_rule); + $serialized_rule = str_replace("[!n]", "\n", $serialized_rule); + + $rule = unserialize($serialized_rule); + if (empty($rule)) { + return; + } + + if (!empty($rule["tests"])) { + foreach ($rule["tests"] as &$test) { + $test["test"] = $test["type"]; + + if (isset($test["operator"])) { + $test["type"] = $test["operator"]; + unset($test["operator"]); + } + + if (isset($test["header"])) { + $test["arg1"] = $test["header"]; + unset($test["header"]); + $test["arg2"] = $test["target"]; + unset($test["target"]); + } + } + } + + if (!empty($rule["actions"])) { + foreach ($rule["actions"] as &$action) { + if ($action["type"] == "vacation") { + $action["reason"] = $action["msg"]; + unset($action["msg"]); + } + } + } + $this->content[] = $rule; + } } diff --git a/plugins/managesieve/tests/src/parser_sieverules_import b/plugins/managesieve/tests/src/parser_sieverules_import new file mode 100644 index 000000000..b8fa99006 --- /dev/null +++ b/plugins/managesieve/tests/src/parser_sieverules_import @@ -0,0 +1,9 @@ +## Generated by Roundcube Webmail SieveRules Plugin ##
+require ["imap4flags","fileinto"];
+
+# rule:[Out of Office]
+# disabledRule:[a:5:{s:4:"join";b:0;s:4:"name";s:13:"Out of Office";s:8:"disabled";s:1:"1";s:5:"tests";a:1:{i:0;a:1:{s:4:"type";s:4:"true";}}s:7:"actions";a:1:{i:0;a:10:{s:4:"type";s:8:"vacation";s:4:"days";s:1:"3";s:7:"subject";s:13:"Out of office";s:11:"origsubject";s:1:"1";s:4:"from";s:0:"";s:9:"addresses";s:0:"";s:6:"handle";s:0:"";s:3:"msg";s:16:"Went for holiday";s:7:"htmlmsg";b:0;s:7:"charset";s:5:"UTF-8";}}}]
+# rule:[Spam]
+# disabledRule:[a:5:{s:4:"join";b:0;s:4:"name";s:4:"Spam";s:8:"disabled";s:1:"1";s:5:"tests";a:1:{i:0;a:5:{s:3:"not";s:0:"";s:4:"type";s:6:"header";s:8:"operator";s:8:"contains";s:6:"header";s:7:"Subject";s:6:"target";s:12:"****SPAM****";}}s:7:"actions";a:1:{i:0;a:3:{s:4:"type";s:8:"fileinto";s:6:"create";b:0;s:6:"target";s:4:"Junk";}}}]
+# rule:[Stop]
+# disabledRule:[a:5:{s:4:"join";b:0;s:4:"name";s:4:"Stop";s:8:"disabled";s:1:"1";s:5:"tests";a:1:{i:0;a:5:{s:3:"not";s:0:"";s:4:"type";s:6:"header";s:8:"operator";s:8:"contains";s:6:"header";s:7:"Subject";s:6:"target";s:18:"contains this text";}}s:7:"actions";a:1:{i:0;a:1:{s:4:"type";s:4:"stop";}}}]
diff --git a/plugins/managesieve/tests/src/parser_sieverules_import.out b/plugins/managesieve/tests/src/parser_sieverules_import.out new file mode 100644 index 000000000..caf29c89c --- /dev/null +++ b/plugins/managesieve/tests/src/parser_sieverules_import.out @@ -0,0 +1,18 @@ +## Generated by Roundcube Webmail SieveRules Plugin ##
+
+require ["fileinto","vacation"];
+# rule:[Out of Office]
+if false # true
+{
+ vacation :days 3 :subject "Out of office" "Went for holiday";
+}
+# rule:[Spam]
+if false # header :contains "Subject" "****SPAM****"
+{
+ fileinto "Junk";
+}
+# rule:[Stop]
+if false # header :contains "Subject" "contains this text"
+{
+ stop;
+}
|