diff options
Diffstat (limited to 'plugins/managesieve/lib')
3 files changed, 334 insertions, 326 deletions
diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve.php b/plugins/managesieve/lib/Roundcube/rcube_sieve.php index 3bd2978da..4f66bf029 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve.php @@ -6,18 +6,18 @@ * Copyright (C) 2008-2011, The Roundcube Dev Team * Copyright (C) 2011, Kolab Systems AG * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ // Managesieve Protocol: RFC5804 @@ -84,7 +84,7 @@ class rcube_sieve return $this->_set_error(SIEVE_ERROR_LOGIN); } - $this->exts = $this->get_extensions(); + $this->exts = $this->get_extensions(); // disable features by config if (!empty($disabled)) { @@ -379,6 +379,6 @@ class rcube_sieve */ public function debug_handler(&$sieve, $message) { - rcube::write_log('sieve', preg_replace('/\r\n$/', '', $message)); + write_log('sieve', preg_replace('/\r\n$/', '', $message)); } } diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php index e4efef5b3..6c9f8048a 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php @@ -535,6 +535,7 @@ class rcube_sieve_engine $act_types = rcube_utils::get_input_value('_action_type', rcube_utils::INPUT_POST, true); $mailboxes = rcube_utils::get_input_value('_action_mailbox', rcube_utils::INPUT_POST, true); $act_targets = rcube_utils::get_input_value('_action_target', rcube_utils::INPUT_POST, true); + $domain_targets = rcube_utils::get_input_value('_action_target_domain', rcube_utils::INPUT_POST); $area_targets = rcube_utils::get_input_value('_action_target_area', rcube_utils::INPUT_POST, true); $reasons = rcube_utils::get_input_value('_action_reason', rcube_utils::INPUT_POST, true); $addresses = rcube_utils::get_input_value('_action_addresses', rcube_utils::INPUT_POST, true); @@ -622,7 +623,21 @@ class rcube_sieve_engine if (!count($target)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); } - else if ($type != 'regex' && $type != 'matches') { + else if (strpos($type, 'count-') === 0) { + foreach ($target as $arg) { + if (preg_match('/[^0-9]/', $arg)) { + $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars'); + } + } + } + else if (strpos($type, 'value-') === 0) { + // Some date/time formats do not support i;ascii-numeric comparator + if ($comparator == 'i;ascii-numeric' && in_array($datepart, array('date', 'time', 'iso8601', 'std11'))) { + $comparator = ''; + } + } + + if (!preg_match('/^(regex|matches|count-)/', $type) && count($target)) { foreach ($target as $arg) { if (!$this->validate_date_part($datepart, $arg)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('invaliddateformat'); @@ -668,7 +683,21 @@ class rcube_sieve_engine if (!count($target)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); } - else if ($type != 'regex' && $type != 'matches') { + else if (strpos($type, 'count-') === 0) { + foreach ($target as $arg) { + if (preg_match('/[^0-9]/', $arg)) { + $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars'); + } + } + } + else if (strpos($type, 'value-') === 0) { + // Some date/time formats do not support i;ascii-numeric comparator + if ($comparator == 'i;ascii-numeric' && in_array($datepart, array('date', 'time', 'iso8601', 'std11'))) { + $comparator = ''; + } + } + + if (count($target) && !preg_match('/^(regex|matches|count-)/', $type)) { foreach ($target as $arg) { if (!$this->validate_date_part($datepart, $arg)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('invaliddateformat'); @@ -699,7 +728,7 @@ class rcube_sieve_engine } else if (preg_match('/^(value|count)-/', $type)) { foreach ($target as $target_value) { - if (!preg_match('/[0-9]+/', $target_value)) { + if (preg_match('/[^0-9]/', $target_value)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars'); } } @@ -781,7 +810,7 @@ class rcube_sieve_engine } else if (preg_match('/^(value|count)-/', $type)) { foreach ($target as $target_value) { - if (!preg_match('/[0-9]+/', $target_value)) { + if (preg_match('/[^0-9]/', $target_value)) { $this->errors['tests'][$i]['target'] = $this->plugin->gettext('forbiddenchars'); } } @@ -794,9 +823,6 @@ class rcube_sieve_engine } if ($header != 'size' && $comparator) { - if (preg_match('/^(value|count)/', $this->form['tests'][$i]['type'])) - $comparator = 'i;ascii-numeric'; - $this->form['tests'][$i]['comparator'] = $comparator; } @@ -806,16 +832,15 @@ class rcube_sieve_engine $i = 0; // actions - foreach($act_types as $idx => $type) { - $type = $this->strip_value($type); - $target = $this->strip_value($act_targets[$idx]); + foreach ($act_types as $idx => $type) { + $type = $this->strip_value($type); switch ($type) { - case 'fileinto': case 'fileinto_copy': $mailbox = $this->strip_value($mailboxes[$idx], false, false); $this->form['actions'][$i]['target'] = $this->mod_mailbox($mailbox, 'in'); + if ($type == 'fileinto_copy') { $type = 'fileinto'; $this->form['actions'][$i]['copy'] = true; @@ -833,17 +858,31 @@ class rcube_sieve_engine case 'redirect': case 'redirect_copy': + $target = $this->strip_value($act_targets[$idx]); + $domain = $this->strip_value($domain_targets[$idx]); + + // force one of the configured domains + $domains = (array) $this->rc->config->get('managesieve_domains'); + if (!empty($domains) && !empty($target)) { + if (!$domain || !in_array($domain, $domains)) { + $domain = $domains[0]; + } + + $target .= '@' . $domain; + } + $this->form['actions'][$i]['target'] = $target; - if ($this->form['actions'][$i]['target'] == '') + if ($target == '') $this->errors['actions'][$i]['target'] = $this->plugin->gettext('cannotbeempty'); - else if (!rcube_utils::check_email($this->form['actions'][$i]['target'])) - $this->errors['actions'][$i]['target'] = $this->plugin->gettext('noemailwarning'); + else if (!rcube_utils::check_email($target)) + $this->errors['actions'][$i]['target'] = $this->plugin->gettext(!empty($domains) ? 'forbiddenchars' : 'noemailwarning'); if ($type == 'redirect_copy') { $type = 'redirect'; $this->form['actions'][$i]['copy'] = true; } + break; case 'addflag': @@ -864,6 +903,7 @@ class rcube_sieve_engine case 'vacation': $reason = $this->strip_value($reasons[$idx]); $interval_type = $interval_types[$idx] == 'seconds' ? 'seconds' : 'days'; + $this->form['actions'][$i]['reason'] = str_replace("\r\n", "\n", $reason); $this->form['actions'][$i]['subject'] = $subject[$idx]; $this->form['actions'][$i]['addresses'] = array_shift($addresses); @@ -871,7 +911,12 @@ class rcube_sieve_engine // @TODO: vacation :mime, :from, :handle foreach ((array)$this->form['actions'][$i]['addresses'] as $aidx => $address) { - if (!rcube_utils::check_email($address)) { + $this->form['actions'][$i]['addresses'][$aidx] = $address = trim($address); + + if (empty($address)) { + unset($this->form['actions'][$i]['addresses'][$aidx]); + } + else if (!rcube_utils::check_email($address)) { $this->errors['actions'][$i]['addresses'] = $this->plugin->gettext('noemailwarning'); break; } @@ -1354,22 +1399,6 @@ class rcube_sieve_engine $select_op->add(rcube::Q($this->plugin->gettext('valuenotequals')), 'value-ne'); } - // (current)date part select - if (in_array('date', $this->exts) || in_array('currentdate', $this->exts)) { - $date_parts = array('date', 'iso8601', 'std11', 'julian', 'time', - 'year', 'month', 'day', 'hour', 'minute', 'second', 'weekday', 'zone'); - $select_dp = new html_select(array('name' => "_rule_date_part[]", 'id' => 'rule_date_part'.$id, - 'style' => $rule['test'] == 'currentdate' || $rule['test'] == 'date' ? '' : 'display:none', - 'class' => 'datepart_selector', - )); - - foreach ($date_parts as $part) { - $select_dp->add(rcube::Q($this->plugin->gettext($part)), $part); - } - - $tout .= $select_dp->show($rule['test'] == 'currentdate' || $rule['test'] == 'date' ? $rule['part'] : ''); - } - // target(s) input if (in_array($rule['test'], array('header', 'address', 'envelope'))) { $test = ($rule['not'] ? 'not' : '').($rule['type'] ? $rule['type'] : 'is'); @@ -1396,6 +1425,22 @@ class rcube_sieve_engine $target = ''; } + // (current)date part select + if (in_array('date', $this->exts) || in_array('currentdate', $this->exts)) { + $date_parts = array('date', 'iso8601', 'std11', 'julian', 'time', + 'year', 'month', 'day', 'hour', 'minute', 'second', 'weekday', 'zone'); + $select_dp = new html_select(array('name' => "_rule_date_part[]", 'id' => 'rule_date_part'.$id, + 'style' => in_array($rule['test'], array('currentdate', 'date')) && !preg_match('/^(notcount|count)-/', $test) ? '' : 'display:none', + 'class' => 'datepart_selector', + )); + + foreach ($date_parts as $part) { + $select_dp->add(rcube::Q($this->plugin->gettext($part)), $part); + } + + $tout .= $select_dp->show($rule['test'] == 'currentdate' || $rule['test'] == 'date' ? $rule['part'] : ''); + } + $tout .= $select_op->show($test); $tout .= $this->list_input($id, 'rule_target', $target, $rule['test'] != 'size' && $rule['test'] != 'exists', @@ -1436,7 +1481,7 @@ class rcube_sieve_engine $select_type->add(rcube::Q($this->plugin->gettext('detail')), 'detail'); } - $need_mod = $rule['test'] != 'size' && $rule['test'] != 'body'; + $need_mod = !in_array($rule['test'], array('size', 'body', 'date', 'currentdate')); $mout = '<div id="rule_mod' .$id. '" class="adv"' . (!$need_mod ? ' style="display:none"' : '') . '>'; $mout .= ' <span class="label">' . rcube::Q($this->plugin->gettext('modifier')) . ' </span>'; $mout .= $select_mod->show($rule['test']); @@ -1578,11 +1623,34 @@ class rcube_sieve_engine // actions target inputs $out .= '<td class="rowtargets">'; - // shared targets - $out .= '<input type="text" name="_action_target['.$id.']" id="action_target' .$id. '" ' - .'value="' .($action['type']=='redirect' ? rcube::Q($action['target'], 'strict', false) : ''). '" size="35" ' - .'style="display:' .($action['type']=='redirect' ? 'inline' : 'none') .'" ' - . $this->error_class($id, 'action', 'target', 'action_target') .' />'; + + // force domain selection in redirect email input + $domains = (array) $this->rc->config->get('managesieve_domains'); + if (!empty($domains)) { + sort($domains); + + $domain_select = new html_select(array('name' => "_action_target_domain[$id]", 'id' => 'action_target_domain'.$id)); + $domain_select->add(array_combine($domains, $domains)); + + $parts = explode('@', $action['target']); + + if (!empty($parts)) { + $action['domain'] = array_pop($parts); + $action['target'] = implode('@', $parts); + } + } + + // redirect target + $out .= '<span id="redirect_target' . $id . '" style="white-space:nowrap;' + . ' display:' . ($action['type'] == 'redirect' ? 'inline' : 'none') . '">' + . '<input type="text" name="_action_target['.$id.']" id="action_target' .$id. '"' + . ' value="' .($action['type'] == 'redirect' ? rcube::Q($action['target'], 'strict', false) : '') . '"' + . (!empty($domains) ? ' size="20"' : ' size="35"') + . $this->error_class($id, 'action', 'target', 'action_target') .' />' + . (!empty($domains) ? ' @ ' . $domain_select->show($action['domain']) : '') + . '</span>'; + + // (e)reject target $out .= '<textarea name="_action_target_area['.$id.']" id="action_target_area' .$id. '" ' .'rows="3" cols="35" '. $this->error_class($id, 'action', 'targetarea', 'action_target_area') .'style="display:' .(in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none') .'">' @@ -2147,7 +2215,8 @@ class rcube_sieve_engine return; } - $headers = array(); + $headers = array(); + $exceptions = array('date', 'currentdate', 'size', 'body'); // find common headers used in script, will be added to the list // of available (predefined) headers (#1489271) @@ -2156,6 +2225,12 @@ class rcube_sieve_engine if ($test['test'] == 'header') { foreach ((array) $test['arg1'] as $header) { $lc_header = strtolower($header); + + // skip special names to not confuse UI + if (in_array($lc_header, $exceptions)) { + continue; + } + if (!isset($this->headers[$lc_header]) && !isset($headers[$lc_header])) { $headers[$lc_header] = $header; } diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php index 371b45d84..36eb1bcf8 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php @@ -6,18 +6,18 @@ * Copyright (C) 2008-2011, The Roundcube Dev Team * Copyright (C) 2011, Kolab Systems AG * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ class rcube_sieve_script @@ -27,26 +27,23 @@ class rcube_sieve_script private $vars = array(); // "global" variables private $prefix = ''; // script header (comments) private $supported = array( // Sieve extensions supported by class - 'body', // RFC5173 - 'copy', // RFC3894 - 'date', // RFC5260 - 'enotify', // RFC5435 + 'fileinto', // RFC5228 'envelope', // RFC5228 + 'reject', // RFC5429 'ereject', // RFC5429 - 'fileinto', // RFC5228 + 'copy', // RFC3894 + 'vacation', // RFC5230 + 'relational', // RFC3431 + 'regex', // draft-ietf-sieve-regex-01 'imapflags', // draft-melnikov-sieve-imapflags-06 'imap4flags', // RFC5232 'include', // draft-ietf-sieve-include-12 - 'index', // RFC5260 - 'notify', // draft-ietf-sieve-notify-00 - 'regex', // draft-ietf-sieve-regex-01 - 'reject', // RFC5429 - 'relational', // RFC3431 - 'subaddress', // RFC5233 - 'vacation', // RFC5230 - 'vacation-seconds', // RFC6131 'variables', // RFC5229 - // @TODO: spamtest+virustest, mailbox + 'body', // RFC5173 + 'subaddress', // RFC5233 + 'enotify', // RFC5435 + 'notify', // draft-ietf-sieve-notify-00 + // @TODO: spamtest+virustest, mailbox, date ); /** @@ -208,6 +205,7 @@ class rcube_sieve_script // rules foreach ($this->content as $rule) { + $extension = ''; $script = ''; $tests = array(); $i = 0; @@ -240,8 +238,24 @@ class rcube_sieve_script $tests[$i] .= ($test['not'] ? 'not ' : ''); $tests[$i] .= 'header'; - $this->add_index($test, $tests[$i], $exts); - $this->add_operator($test, $tests[$i], $exts); + if (!empty($test['type'])) { + // relational operator + comparator + if (preg_match('/^(value|count)-([gteqnl]{2})/', $test['type'], $m)) { + array_push($exts, 'relational'); + array_push($exts, 'comparator-i;ascii-numeric'); + + $tests[$i] .= ' :' . $m[1] . ' "' . $m[2] . '" :comparator "i;ascii-numeric"'; + } + else { + $this->add_comparator($test, $tests[$i], $exts); + + if ($test['type'] == 'regex') { + array_push($exts, 'regex'); + } + + $tests[$i] .= ' :' . $test['type']; + } + } $tests[$i] .= ' ' . self::escape_string($test['arg1']); $tests[$i] .= ' ' . self::escape_string($test['arg2']); @@ -256,19 +270,21 @@ class rcube_sieve_script $tests[$i] .= ($test['not'] ? 'not ' : ''); $tests[$i] .= $test['test']; - if ($test['test'] != 'envelope') { - $this->add_index($test, $tests[$i], $exts); - } - - // :all address-part is optional, skip it - if (!empty($test['part']) && $test['part'] != 'all') { + if (!empty($test['part'])) { $tests[$i] .= ' :' . $test['part']; if ($test['part'] == 'user' || $test['part'] == 'detail') { array_push($exts, 'subaddress'); } } - $this->add_operator($test, $tests[$i], $exts); + $this->add_comparator($test, $tests[$i], $exts); + + if (!empty($test['type'])) { + if ($test['type'] == 'regex') { + array_push($exts, 'regex'); + } + $tests[$i] .= ' :' . $test['type']; + } $tests[$i] .= ' ' . self::escape_string($test['arg1']); $tests[$i] .= ' ' . self::escape_string($test['arg2']); @@ -279,6 +295,8 @@ class rcube_sieve_script $tests[$i] .= ($test['not'] ? 'not ' : '') . 'body'; + $this->add_comparator($test, $tests[$i], $exts); + if (!empty($test['part'])) { $tests[$i] .= ' :' . $test['part']; @@ -287,35 +305,14 @@ class rcube_sieve_script } } - $this->add_operator($test, $tests[$i], $exts); - - $tests[$i] .= ' ' . self::escape_string($test['arg']); - break; - - case 'date': - case 'currentdate': - array_push($exts, 'date'); - - $tests[$i] .= ($test['not'] ? 'not ' : '') . $test['test']; - - $this->add_index($test, $tests[$i], $exts); - - if (!empty($test['originalzone']) && $test['test'] == 'date') { - $tests[$i] .= ' :originalzone'; - } - else if (!empty($test['zone'])) { - $tests[$i] .= ' :zone ' . self::escape_string($test['zone']); - } - - $this->add_operator($test, $tests[$i], $exts); - - if ($test['test'] == 'date') { - $tests[$i] .= ' ' . self::escape_string($test['header']); + if (!empty($test['type'])) { + if ($test['type'] == 'regex') { + array_push($exts, 'regex'); + } + $tests[$i] .= ' :' . $test['type']; } - $tests[$i] .= ' ' . self::escape_string($test['part']); $tests[$i] .= ' ' . self::escape_string($test['arg']); - break; } $i++; @@ -450,13 +447,8 @@ class rcube_sieve_script case 'vacation': array_push($exts, 'vacation'); $action_script .= 'vacation'; - if (isset($action['seconds'])) { - array_push($exts, 'vacation-seconds'); - $action_script .= " :seconds " . intval($action['seconds']); - } - else if (!empty($action['days'])) { - $action_script .= " :days " . intval($action['days']); - } + if (!empty($action['days'])) + $action_script .= " :days " . $action['days']; if (!empty($action['addresses'])) $action_script .= " :addresses " . self::escape_string($action['addresses']); if (!empty($action['subject'])) @@ -485,17 +477,8 @@ class rcube_sieve_script } // requires - if (!empty($exts)) { - $exts = array_unique($exts); - - if (in_array('vacation-seconds', $exts) && ($key = array_search('vacation', $exts)) !== false) { - unset($exts[$key]); - } - - sort($exts); // for convenience use always the same order - - $output = 'require ["' . implode('","', $exts) . "\"];\n" . $output; - } + if (!empty($exts)) + $output = 'require ["' . implode('","', array_unique($exts)) . "\"];\n" . $output; if (!empty($this->prefix)) { $output = $this->prefix . "\n\n" . $output; @@ -657,85 +640,86 @@ class rcube_sieve_script break; case 'size': - $test = array('test' => 'size', 'not' => $not); - - $test['arg'] = array_pop($tokens); - + $size = array('test' => 'size', 'not' => $not); for ($i=0, $len=count($tokens); $i<$len; $i++) { if (!is_array($tokens[$i]) && preg_match('/^:(under|over)$/i', $tokens[$i]) ) { - $test['type'] = strtolower(substr($tokens[$i], 1)); + $size['type'] = strtolower(substr($tokens[$i], 1)); + } + else { + $size['arg'] = $tokens[$i]; } } - $tests[] = $test; + $tests[] = $size; break; case 'header': - case 'address': - case 'envelope': - $test = array('test' => $token, 'not' => $not); - - $test['arg2'] = array_pop($tokens); - $test['arg1'] = array_pop($tokens); - - $test += $this->test_tokens($tokens); - - if ($token != 'header' && !empty($tokens)) { - for ($i=0, $len=count($tokens); $i<$len; $i++) { - if (!is_array($tokens[$i]) && preg_match('/^:(localpart|domain|all|user|detail)$/i', $tokens[$i])) { - $test['part'] = strtolower(substr($tokens[$i], 1)); - } + $header = array('test' => 'header', 'not' => $not, 'arg1' => '', 'arg2' => ''); + for ($i=0, $len=count($tokens); $i<$len; $i++) { + if (!is_array($tokens[$i]) && preg_match('/^:comparator$/i', $tokens[$i])) { + $header['comparator'] = $tokens[++$i]; + } + else if (!is_array($tokens[$i]) && preg_match('/^:(count|value)$/i', $tokens[$i])) { + $header['type'] = strtolower(substr($tokens[$i], 1)) . '-' . $tokens[++$i]; + } + else if (!is_array($tokens[$i]) && preg_match('/^:(is|contains|matches|regex)$/i', $tokens[$i])) { + $header['type'] = strtolower(substr($tokens[$i], 1)); + } + else { + $header['arg1'] = $header['arg2']; + $header['arg2'] = $tokens[$i]; } } - $tests[] = $test; + $tests[] = $header; break; - case 'body': - $test = array('test' => 'body', 'not' => $not); - - $test['arg'] = array_pop($tokens); - - $test += $this->test_tokens($tokens); - + case 'address': + case 'envelope': + $header = array('test' => $token, 'not' => $not, 'arg1' => '', 'arg2' => ''); for ($i=0, $len=count($tokens); $i<$len; $i++) { - if (!is_array($tokens[$i]) && preg_match('/^:(raw|content|text)$/i', $tokens[$i])) { - $test['part'] = strtolower(substr($tokens[$i], 1)); - - if ($test['part'] == 'content') { - $test['content'] = $tokens[++$i]; - } + if (!is_array($tokens[$i]) && preg_match('/^:comparator$/i', $tokens[$i])) { + $header['comparator'] = $tokens[++$i]; + } + else if (!is_array($tokens[$i]) && preg_match('/^:(is|contains|matches|regex)$/i', $tokens[$i])) { + $header['type'] = strtolower(substr($tokens[$i], 1)); + } + else if (!is_array($tokens[$i]) && preg_match('/^:(localpart|domain|all|user|detail)$/i', $tokens[$i])) { + $header['part'] = strtolower(substr($tokens[$i], 1)); + } + else { + $header['arg1'] = $header['arg2']; + $header['arg2'] = $tokens[$i]; } } - $tests[] = $test; + $tests[] = $header; break; - case 'date': - case 'currentdate': - $test = array('test' => $token, 'not' => $not); - - $test['arg'] = array_pop($tokens); - $test['part'] = array_pop($tokens); - - if ($token == 'date') { - $test['header'] = array_pop($tokens); - } - - $test += $this->test_tokens($tokens); - + case 'body': + $header = array('test' => 'body', 'not' => $not, 'arg' => ''); for ($i=0, $len=count($tokens); $i<$len; $i++) { - if (!is_array($tokens[$i]) && preg_match('/^:zone$/i', $tokens[$i])) { - $test['zone'] = $tokens[++$i]; + if (!is_array($tokens[$i]) && preg_match('/^:comparator$/i', $tokens[$i])) { + $header['comparator'] = $tokens[++$i]; + } + else if (!is_array($tokens[$i]) && preg_match('/^:(is|contains|matches|regex)$/i', $tokens[$i])) { + $header['type'] = strtolower(substr($tokens[$i], 1)); } - else if (!is_array($tokens[$i]) && preg_match('/^:originalzone$/i', $tokens[$i])) { - $test['originalzone'] = true; + else if (!is_array($tokens[$i]) && preg_match('/^:(raw|content|text)$/i', $tokens[$i])) { + $header['part'] = strtolower(substr($tokens[$i], 1)); + + if ($header['part'] == 'content') { + $header['content'] = $tokens[++$i]; + } + } + else { + $header['arg'] = $tokens[$i]; } } - $tests[] = $test; + $tests[] = $header; break; case 'exists': @@ -787,9 +771,15 @@ class rcube_sieve_script $result = null; while (strlen($content)) { - $tokens = self::tokenize($content, true); + $tokens = self::tokenize($content, true); $separator = array_pop($tokens); - $token = !empty($tokens) ? array_shift($tokens) : $separator; + + if (!empty($tokens)) { + $token = array_shift($tokens); + } + else { + $token = $separator; + } switch ($token) { case 'discard': @@ -800,78 +790,125 @@ class rcube_sieve_script case 'fileinto': case 'redirect': - $action = array('type' => $token, 'target' => array_pop($tokens)); - $args = array('copy'); - $action += $this->action_arguments($tokens, $args); + $copy = false; + $target = ''; - $result[] = $action; + for ($i=0, $len=count($tokens); $i<$len; $i++) { + if (strtolower($tokens[$i]) == ':copy') { + $copy = true; + } + else { + $target = $tokens[$i]; + } + } + + $result[] = array('type' => $token, 'copy' => $copy, + 'target' => $target); + break; + + case 'reject': + case 'ereject': + $result[] = array('type' => $token, 'target' => array_pop($tokens)); break; case 'vacation': - $action = array('type' => 'vacation', 'reason' => array_pop($tokens)); - $args = array('mime'); - $vargs = array('seconds', 'days', 'addresses', 'subject', 'handle', 'from'); - $action += $this->action_arguments($tokens, $args, $vargs); + $vacation = array('type' => 'vacation', 'reason' => array_pop($tokens)); - $result[] = $action; + for ($i=0, $len=count($tokens); $i<$len; $i++) { + $tok = strtolower($tokens[$i]); + if ($tok == ':days') { + $vacation['days'] = $tokens[++$i]; + } + else if ($tok == ':subject') { + $vacation['subject'] = $tokens[++$i]; + } + else if ($tok == ':addresses') { + $vacation['addresses'] = $tokens[++$i]; + } + else if ($tok == ':handle') { + $vacation['handle'] = $tokens[++$i]; + } + else if ($tok == ':from') { + $vacation['from'] = $tokens[++$i]; + } + else if ($tok == ':mime') { + $vacation['mime'] = true; + } + } + + $result[] = $vacation; break; - case 'reject': - case 'ereject': case 'setflag': case 'addflag': case 'removeflag': - $result[] = array('type' => $token, 'target' => array_pop($tokens)); + $result[] = array('type' => $token, + // Flags list: last token (skip optional variable) + 'target' => $tokens[count($tokens)-1] + ); break; case 'include': - $action = array('type' => 'include', 'target' => array_pop($tokens)); - $args = array('once', 'optional', 'global', 'personal'); - $action += $this->action_arguments($tokens, $args); + $include = array('type' => 'include', 'target' => array_pop($tokens)); - $result[] = $action; + // Parameters: :once, :optional, :global, :personal + for ($i=0, $len=count($tokens); $i<$len; $i++) { + $tok = strtolower($tokens[$i]); + if ($tok[0] == ':') { + $include[substr($tok, 1)] = true; + } + } + + $result[] = $include; break; case 'set': - $action = array('type' => 'set', 'value' => array_pop($tokens), 'name' => array_pop($tokens)); - $args = array('lower', 'upper', 'lowerfirst', 'upperfirst', 'quotewildcard', 'length'); - $action += $this->action_arguments($tokens, $args); + $set = array('type' => 'set', 'value' => array_pop($tokens), 'name' => array_pop($tokens)); + + // Parameters: :lower :upper :lowerfirst :upperfirst :quotewildcard :length + for ($i=0, $len=count($tokens); $i<$len; $i++) { + $tok = strtolower($tokens[$i]); + if ($tok[0] == ':') { + $set[substr($tok, 1)] = true; + } + } - $result[] = $action; + $result[] = $set; break; case 'require': // skip, will be build according to used commands - // $result[] = array('type' => 'require', 'target' => array_pop($tokens)); + // $result[] = array('type' => 'require', 'target' => $tokens); break; case 'notify': - $action = array('type' => 'notify'); - $priorities = array('high' => 1, 'normal' => 2, 'low' => 3); - $vargs = array('from', 'importance', 'options', 'message', 'method'); - $args = array_keys($priorities); - $action += $this->action_arguments($tokens, $args, $vargs); - - // Here we support only 00 version of notify draft, there - // were a couple regressions in 00 to 04 changelog, we use - // the version used by Cyrus - if (!isset($action['importance'])) { - foreach ($priorities as $key => $val) { - if (isset($action[$key])) { - $action['importance'] = $val; - unset($action[$key]); + $notify = array('type' => 'notify'); + $priorities = array(':high' => 1, ':normal' => 2, ':low' => 3); + + // Parameters: :from, :importance, :options, :message + // additional (optional) :method parameter for notify extension + for ($i=0, $len=count($tokens); $i<$len; $i++) { + $tok = strtolower($tokens[$i]); + if ($tok[0] == ':') { + // Here we support only 00 version of notify draft, there + // were a couple regressions in 00 to 04 changelog, we use + // the version used by Cyrus + if (isset($priorities[$tok])) { + $notify['importance'] = $priorities[$tok]; + } + else { + $notify[substr($tok, 1)] = $tokens[++$i]; } } + else { + // unnamed parameter is a :method in enotify extension + $notify['method'] = $tokens[$i]; + } } - // unnamed parameter is a :method in enotify extension - if (!isset($action['method'])) { - $action['method'] = array_pop($tokens); - } - - $method_components = parse_url($action['method']); + $method_components = parse_url($notify['method']); if ($method_components['scheme'] == 'mailto') { - $action['address'] = $method_components['path']; + $notify['address'] = $method_components['path']; $method_params = array(); if (array_key_exists('query', $method_components)) { parse_str($method_components['query'], $method_params); @@ -881,10 +918,10 @@ class rcube_sieve_script if (ini_get('magic_quotes_gpc') || ini_get('magic_quotes_sybase')) { array_map('stripslashes', $method_params); } - $action['body'] = (array_key_exists('body', $method_params)) ? $method_params['body'] : ''; + $notify['body'] = (array_key_exists('body', $method_params)) ? $method_params['body'] : ''; } - $result[] = $action; + $result[] = $notify; break; } @@ -897,7 +934,7 @@ class rcube_sieve_script } /** - * Add comparator to the test + * */ private function add_comparator($test, &$out, &$exts) { @@ -920,111 +957,6 @@ class rcube_sieve_script } /** - * Add index argument to the test - */ - private function add_index($test, &$out, &$exts) - { - if (!empty($test['index'])) { - array_push($exts, 'index'); - $out .= ' :index ' . intval($test['index']) . ($test['last'] ? ' :last' : ''); - } - } - - /** - * Add operators to the test - */ - private function add_operator($test, &$out, &$exts) - { - if (empty($test['type'])) { - return; - } - - // relational operator + comparator - if (preg_match('/^(value|count)-([gteqnl]{2})/', $test['type'], $m)) { - array_push($exts, 'relational'); - array_push($exts, 'comparator-i;ascii-numeric'); - - $out .= ' :' . $m[1] . ' "' . $m[2] . '" :comparator "i;ascii-numeric"'; - } - else { - $this->add_comparator($test, $out, $exts); - - if ($test['type'] == 'regex') { - array_push($exts, 'regex'); - } - - $out .= ' :' . $test['type']; - } - } - - /** - * Extract test tokens - */ - private function test_tokens(&$tokens) - { - $test = array(); - $result = array(); - - for ($i=0, $len=count($tokens); $i<$len; $i++) { - if (!is_array($tokens[$i]) && preg_match('/^:comparator$/i', $tokens[$i])) { - $test['comparator'] = $tokens[++$i]; - } - else if (!is_array($tokens[$i]) && preg_match('/^:(count|value)$/i', $tokens[$i])) { - $test['type'] = strtolower(substr($tokens[$i], 1)) . '-' . $tokens[++$i]; - } - else if (!is_array($tokens[$i]) && preg_match('/^:(is|contains|matches|regex)$/i', $tokens[$i])) { - $test['type'] = strtolower(substr($tokens[$i], 1)); - } - else if (!is_array($tokens[$i]) && preg_match('/^:index$/i', $tokens[$i])) { - $test['index'] = intval($tokens[++$i]); - if ($tokens[$i+1] && preg_match('/^:last$/i', $tokens[$i+1])) { - $test['last'] = true; - $i++; - } - } - else { - $result[] = $tokens[$i]; - } - } - - $tokens = $result; - - return $test; - } - - /** - * Extract action arguments - */ - private function action_arguments(&$tokens, $bool_args, $val_args = array()) - { - $action = array(); - $result = array(); - - for ($i=0, $len=count($tokens); $i<$len; $i++) { - $tok = $tokens[$i]; - if (!is_array($tok) && $tok[0] == ':') { - $tok = strtolower(substr($tok, 1)); - if (in_array($tok, $bool_args)) { - $action[$tok] = true; - } - else if (in_array($tok, $val_args)) { - $action[$tok] = $tokens[++$i]; - } - else { - $result[] = $tok; - } - } - else { - $result[] = $tok; - } - } - - $tokens = $result; - - return $action; - } - - /** * Escape special chars into quoted string value or multi-line string * or list of strings * @@ -1082,10 +1014,11 @@ class rcube_sieve_script * @param mixed $num Number of tokens to return, 0 for all * or True for all tokens until separator is found. * Separator will be returned as last token. + * @param int $in_list Enable to call recursively inside a list * * @return mixed Tokens array or string if $num=1 */ - static function tokenize(&$str, $num=0) + static function tokenize(&$str, $num=0, $in_list=false) { $result = array(); @@ -1120,7 +1053,7 @@ class rcube_sieve_script // Parenthesized list case '[': $str = substr($str, 1); - $result[] = self::tokenize($str, 0); + $result[] = self::tokenize($str, 0, true); break; case ']': $str = substr($str, 1); |