summaryrefslogtreecommitdiff
path: root/plugins/managesieve
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/managesieve')
-rw-r--r--plugins/managesieve/Changelog9
-rw-r--r--plugins/managesieve/lib/Net/Sieve.php55
-rw-r--r--plugins/managesieve/lib/rcube_sieve.php649
-rw-r--r--plugins/managesieve/localization/es_ES.inc2
-rw-r--r--plugins/managesieve/localization/fr_FR.inc2
-rw-r--r--plugins/managesieve/localization/ru_RU.inc12
-rw-r--r--plugins/managesieve/managesieve.js2
-rw-r--r--plugins/managesieve/managesieve.php4
-rw-r--r--plugins/managesieve/tests/Makefile7
-rw-r--r--plugins/managesieve/tests/parser.phpt103
-rw-r--r--plugins/managesieve/tests/tokenize.phpt66
11 files changed, 609 insertions, 302 deletions
diff --git a/plugins/managesieve/Changelog b/plugins/managesieve/Changelog
index 61d198ce6..ef7573959 100644
--- a/plugins/managesieve/Changelog
+++ b/plugins/managesieve/Changelog
@@ -1,6 +1,15 @@
+- Fix escaping of backslash character in quoted strings (#1487780)
+- Fix STARTTLS for timsieved < 2.3.10
+
+* version 3.0 [2011-02-01]
+-----------------------------------------------------------
- Added support for SASL proxy authentication (#1486691)
- Fixed parsing of scripts with \r\n line separator
- Apply forgotten changes for form errors handling
+- Fix multi-line strings parsing (#1487685)
+- Added tests for script parser
+- Rewritten script parser
+- Fix double request when clicking on Filters tab using Firefox
* version 2.10 [2010-10-10]
-----------------------------------------------------------
diff --git a/plugins/managesieve/lib/Net/Sieve.php b/plugins/managesieve/lib/Net/Sieve.php
index ecc907194..d4cc3eeda 100644
--- a/plugins/managesieve/lib/Net/Sieve.php
+++ b/plugins/managesieve/lib/Net/Sieve.php
@@ -763,7 +763,7 @@ class Net_Sieve
return $res;
}
- return preg_replace('/{[0-9]+}\r\n/', '', $res);
+ return preg_replace('/^{[0-9]+}\r\n/', '', $res);
}
/**
@@ -981,6 +981,28 @@ class Net_Sieve
}
/**
+ * Receives x bytes from the server.
+ *
+ * @param int $length Number of bytes to read
+ *
+ * @return string The server response.
+ */
+ function _recvBytes($length)
+ {
+ $response = '';
+ $response_length = 0;
+
+ while ($response_length < $length) {
+ $response .= $this->_sock->read($length - $response_length);
+ $response_length = $this->_getLineLength($response);
+ }
+
+ $this->_debug("S: " . rtrim($response));
+
+ return $response;
+ }
+
+ /**
* Send a command and retrieves a response from the server.
*
* @param string $cmd The command to send.
@@ -1013,11 +1035,11 @@ class Net_Sieve
if ('NO' == substr($uc_line, 0, 2)) {
// Check for string literal error message.
- if (preg_match('/^no {([0-9]+)\+?}/i', $line, $matches)) {
- $line .= str_replace(
- "\r\n", ' ', $this->_sock->read($matches[1] + 2)
- );
- $this->_debug("S: $line");
+ if (preg_match('/{([0-9]+)}$/i', $line, $matches)) {
+ $line = substr($line, 0, -(strlen($matches[1])+2))
+ . str_replace(
+ "\r\n", ' ', $this->_recvBytes($matches[1] + 2)
+ );
}
return PEAR::raiseError(trim($response . substr($line, 2)), 3);
}
@@ -1052,16 +1074,9 @@ class Net_Sieve
return PEAR::raiseError(trim($response . $line), 6);
}
- if (preg_match('/^{([0-9]+)\+?}/i', $line, $matches)) {
- // Matches String Responses.
- $str_size = $matches[1] + 2;
- $line = '';
- $line_length = 0;
- while ($line_length < $str_size) {
- $line .= $this->_sock->read($str_size - $line_length);
- $line_length = $this->_getLineLength($line);
- }
- $this->_debug("S: $line");
+ if (preg_match('/^{([0-9]+)}/i', $line, $matches)) {
+ // Matches literal string responses.
+ $line = $this->_recvBytes($matches[1] + 2);
if (!$auth) {
// Receive the pending OK only if we aren't
@@ -1146,7 +1161,13 @@ class Net_Sieve
// The server should be sending a CAPABILITY response after
// negotiating TLS. Read it, and ignore if it doesn't.
- $this->_doCmd();
+ // Doesn't work with older timsieved versions
+ $regexp = '/^CYRUS TIMSIEVED V([0-9.]+)/';
+ if (!preg_match($regexp, $this->_capability['implementation'], $matches)
+ || version_compare($matches[1], '2.3.10', '>=')
+ ) {
+ $this->_doCmd();
+ }
// RFC says we need to query the server capabilities again now that we
// are under encryption.
diff --git a/plugins/managesieve/lib/rcube_sieve.php b/plugins/managesieve/lib/rcube_sieve.php
index ba43f1093..dff7d7b91 100644
--- a/plugins/managesieve/lib/rcube_sieve.php
+++ b/plugins/managesieve/lib/rcube_sieve.php
@@ -1,6 +1,6 @@
<?php
-/*
+/**
Classes for managesieve operations (using PEAR::Net_Sieve)
Author: Aleksander Machniak <alec@alec.pl>
@@ -57,7 +57,7 @@ class rcube_sieve
$this->sieve->setDebug(true, array($this, 'debug_handler'));
}
- if (PEAR::isError($this->sieve->connect($host, $port, NULL, $usetls))) {
+ if (PEAR::isError($this->sieve->connect($host, $port, null, $usetls))) {
return $this->_set_error(SIEVE_ERROR_CONNECTION);
}
@@ -414,12 +414,17 @@ class rcube_sieve_script
* @param string Script's text content
* @param array Disabled extensions
*/
- public function __construct($script, $disabled=NULL)
+ public function __construct($script, $disabled=null)
{
- if (!empty($disabled))
- foreach ($disabled as $ext)
- if (($idx = array_search($ext, $this->supported)) !== false)
+ if (!empty($disabled)) {
+ // we're working on lower-cased names
+ $disabled = array_map('strtolower', (array) $disabled);
+ foreach ($disabled as $ext) {
+ if (($idx = array_search($ext, $this->supported)) !== false) {
unset($this->supported[$idx]);
+ }
+ }
+ }
$this->content = $this->_parse_text($script);
}
@@ -513,14 +518,11 @@ class rcube_sieve_script
$tests[$i] .= 'size :' . ($test['type']=='under' ? 'under ' : 'over ') . $test['arg'];
break;
case 'true':
- $tests[$i] .= ($test['not'] ? 'not true' : 'true');
+ $tests[$i] .= ($test['not'] ? 'false' : 'true');
break;
case 'exists':
$tests[$i] .= ($test['not'] ? 'not ' : '');
- if (is_array($test['arg']))
- $tests[$i] .= 'exists ["' . implode('", "', $this->_escape_string($test['arg'])) . '"]';
- else
- $tests[$i] .= 'exists "' . $this->_escape_string($test['arg']) . '"';
+ $tests[$i] .= 'exists ' . self::escape_string($test['arg']);
break;
case 'header':
$tests[$i] .= ($test['not'] ? 'not ' : '');
@@ -533,33 +535,34 @@ class rcube_sieve_script
}
else
$tests[$i] .= 'header :' . $test['type'];
-
- if (is_array($test['arg1']))
- $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['arg1'])) . '"]';
- else
- $tests[$i] .= ' "' . $this->_escape_string($test['arg1']) . '"';
-
- if (is_array($test['arg2']))
- $tests[$i] .= ' ["' . implode('", "', $this->_escape_string($test['arg2'])) . '"]';
- else
- $tests[$i] .= ' "' . $this->_escape_string($test['arg2']) . '"';
+ $tests[$i] .= ' ' . self::escape_string($test['arg1']);
+ $tests[$i] .= ' ' . self::escape_string($test['arg2']);
break;
}
$i++;
}
-// $script .= ($idx>0 ? 'els' : '').($rule['join'] ? 'if allof (' : 'if anyof (');
// disabled rule: if false #....
- $script .= 'if' . ($rule['disabled'] ? ' false #' : '');
- $script .= $rule['join'] ? ' allof (' : ' anyof (';
- if (sizeof($tests) > 1)
- $script .= implode(", ", $tests);
- else if (sizeof($tests))
- $script .= $tests[0];
- else
- $script .= 'true';
- $script .= ")\n{\n";
+ $script .= 'if ' . ($rule['disabled'] ? 'false # ' : '');
+
+ if (empty($tests)) {
+ $tests_str = 'true';
+ }
+ else if (count($tests) > 1) {
+ $tests_str = implode(', ', $tests);
+ }
+ else {
+ $tests_str = $tests[0];
+ }
+
+ if ($rule['join'] || count($tests) > 1) {
+ $script .= sprintf('%s (%s)', $rule['join'] ? 'allof' : 'anyof', $tests_str);
+ }
+ else {
+ $script .= $tests_str;
+ }
+ $script .= "\n{\n";
// action(s)
foreach ($rule['actions'] as $action) {
@@ -571,7 +574,7 @@ class rcube_sieve_script
$script .= ':copy ';
array_push($exts, 'copy');
}
- $script .= "\"" . $this->_escape_string($action['target']) . "\";\n";
+ $script .= self::escape_string($action['target']) . ";\n";
break;
case 'redirect':
$script .= "\tredirect ";
@@ -579,15 +582,13 @@ class rcube_sieve_script
$script .= ':copy ';
array_push($exts, 'copy');
}
- $script .= "\"" . $this->_escape_string($action['target']) . "\";\n";
+ $script .= self::escape_string($action['target']) . ";\n";
break;
case 'reject':
case 'ereject':
array_push($exts, $action['type']);
- if (strpos($action['target'], "\n")!==false)
- $script .= "\t".$action['type']." text:\n" . $action['target'] . "\n.\n;\n";
- else
- $script .= "\t".$action['type']." \"" . $this->_escape_string($action['target']) . "\";\n";
+ $script .= "\t".$action['type']." "
+ . self::escape_string($action['target']) . ";\n";
break;
case 'keep':
case 'discard':
@@ -597,22 +598,19 @@ class rcube_sieve_script
case 'vacation':
array_push($exts, 'vacation');
$script .= "\tvacation";
- if ($action['days'])
+ if (!empty($action['days']))
$script .= " :days " . $action['days'];
- if ($action['addresses'])
- $script .= " :addresses " . $this->_print_list($action['addresses']);
- if ($action['subject'])
- $script .= " :subject \"" . $this->_escape_string($action['subject']) . "\"";
- if ($action['handle'])
- $script .= " :handle \"" . $this->_escape_string($action['handle']) . "\"";
- if ($action['from'])
- $script .= " :from \"" . $this->_escape_string($action['from']) . "\"";
- if ($action['mime'])
+ if (!empty($action['addresses']))
+ $script .= " :addresses " . self::escape_string($action['addresses']);
+ if (!empty($action['subject']))
+ $script .= " :subject " . self::escape_string($action['subject']);
+ if (!empty($action['handle']))
+ $script .= " :handle " . self::escape_string($action['handle']);
+ if (!empty($action['from']))
+ $script .= " :from " . self::escape_string($action['from']);
+ if (!empty($action['mime']))
$script .= " :mime";
- if (strpos($action['reason'], "\n")!==false)
- $script .= " text:\n" . $action['reason'] . "\n.\n;\n";
- else
- $script .= " \"" . $this->_escape_string($action['reason']) . "\";\n";
+ $script .= " " . self::escape_string($action['reason']) . ";\n";
break;
}
}
@@ -656,9 +654,6 @@ class rcube_sieve_script
$i = 0;
$content = array();
- // remove C comments
- $script = preg_replace('|/\*.*?\*/|sm', '', $script);
-
// tokenize rules
if ($tokens = preg_split('/(# rule:\[.*\])\r?\n/', $script, -1, PREG_SPLIT_DELIM_CAPTURE)) {
foreach($tokens as $token) {
@@ -686,31 +681,118 @@ class rcube_sieve_script
*/
private function _tokenize_rule($content)
{
- $result = NULL;
+ $cond = strtolower(self::tokenize($content, 1));
+
+ if ($cond != 'if' && $cond != 'elsif' && $cond != 'else') {
+ return null;
+ }
+
+ $disabled = false;
+ $join = false;
+
+ // disabled rule (false + comment): if false # .....
+ if (preg_match('/^\s*false\s+#/i', $content)) {
+ $content = preg_replace('/^\s*false\s+#\s*/i', '', $content);
+ $disabled = true;
+ }
+
+ while (strlen($content)) {
+ $tokens = self::tokenize($content, true);
+ $separator = array_pop($tokens);
+
+ if (!empty($tokens)) {
+ $token = array_shift($tokens);
+ }
+ else {
+ $token = $separator;
+ }
+
+ $token = strtolower($token);
+
+ if ($token == 'not') {
+ $not = true;
+ $token = strtolower(array_shift($tokens));
+ }
+ else {
+ $not = false;
+ }
+
+ switch ($token) {
+ case 'allof':
+ $join = true;
+ break;
+ case 'anyof':
+ break;
+
+ case 'size':
+ $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])
+ ) {
+ $size['type'] = strtolower(substr($tokens[$i], 1));
+ }
+ else {
+ $size['arg'] = $tokens[$i];
+ }
+ }
+
+ $tests[] = $size;
+ break;
+
+ case 'header':
+ $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])) {
+ $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)$/i', $tokens[$i])) {
+ $header['type'] = strtolower(substr($tokens[$i], 1));
+ }
+ else {
+ $header['arg1'] = $header['arg2'];
+ $header['arg2'] = $tokens[$i];
+ }
+ }
- if (preg_match('/^(if|elsif|else)\s+((true|false|not\s+true|allof|anyof|exists|header|not|size)(.*))\s+\{(.*)\}$/sm',
- trim($content), $matches)) {
+ $tests[] = $header;
+ break;
- $tests = trim($matches[2]);
+ case 'exists':
+ $tests[] = array('test' => 'exists', 'not' => $not,
+ 'arg' => array_pop($tokens));
+ break;
+
+ case 'true':
+ $tests[] = array('test' => 'true', 'not' => $not);
+ break;
- // disabled rule (false + comment): if false #.....
- if ($matches[3] == 'false') {
- $tests = preg_replace('/^false\s+#\s+/', '', $tests);
- $disabled = true;
+ case 'false':
+ $tests[] = array('test' => 'true', 'not' => !$not);
+ break;
+ }
+
+ // goto actions...
+ if ($separator == '{') {
+ break;
}
- else
- $disabled = false;
-
- list($tests, $join) = $this->_parse_tests($tests);
- $actions = $this->_parse_actions(trim($matches[5]));
-
- if ($tests && $actions)
- $result = array(
- 'type' => $matches[1],
- 'tests' => $tests,
- 'actions' => $actions,
- 'join' => $join,
- 'disabled' => $disabled,
+ }
+
+ // ...and actions block
+ if ($tests) {
+ $actions = $this->_parse_actions($content);
+ }
+
+ if ($tests && $actions) {
+ $result = array(
+ 'type' => $cond,
+ 'tests' => $tests,
+ 'actions' => $actions,
+ 'join' => $join,
+ 'disabled' => $disabled,
);
}
@@ -725,94 +807,76 @@ class rcube_sieve_script
*/
private function _parse_actions($content)
{
- $result = NULL;
-
- // supported actions
- $patterns[] = '^\s*discard;';
- $patterns[] = '^\s*keep;';
- $patterns[] = '^\s*stop;';
- $patterns[] = '^\s*redirect\s+(.*?[^\\\]);';
- if (in_array('fileinto', $this->supported))
- $patterns[] = '^\s*fileinto\s+(.*?[^\\\]);';
- if (in_array('reject', $this->supported)) {
- $patterns[] = '^\s*reject\s+text:(.*)\n\.\n;';
- $patterns[] = '^\s*reject\s+(.*?[^\\\]);';
- $patterns[] = '^\s*ereject\s+text:(.*)\n\.\n;';
- $patterns[] = '^\s*ereject\s+(.*?[^\\\]);';
- }
- if (in_array('vacation', $this->supported))
- $patterns[] = '^\s*vacation\s+(.*?[^\\\]);';
+ $result = null;
- $pattern = '/(' . implode('\s*$)|(', $patterns) . '$\s*)/ms';
+ while (strlen($content)) {
+ $tokens = self::tokenize($content, true);
+ $separator = array_pop($tokens);
- // parse actions body
- if (preg_match_all($pattern, $content, $mm, PREG_SET_ORDER)) {
- foreach ($mm as $m) {
- $content = trim($m[0]);
+ if (!empty($tokens)) {
+ $token = array_shift($tokens);
+ }
+ else {
+ $token = $separator;
+ }
- if(preg_match('/^(discard|keep|stop)/', $content, $matches)) {
- $result[] = array('type' => $matches[1]);
- }
- else if(preg_match('/^fileinto/', $content)) {
- $target = $m[sizeof($m)-1];
- $copy = false;
- if (preg_match('/^:copy\s+/', $target)) {
- $target = preg_replace('/^:copy\s+/', '', $target);
+ switch ($token) {
+ case 'discard':
+ case 'keep':
+ case 'stop':
+ $result[] = array('type' => $token);
+ break;
+
+ case 'fileinto':
+ case 'redirect':
+ $copy = false;
+ $target = '';
+
+ for ($i=0, $len=count($tokens); $i<$len; $i++) {
+ if (strtolower($tokens[$i]) == ':copy') {
$copy = true;
}
- $result[] = array('type' => 'fileinto', 'copy' => $copy,
- 'target' => $this->_parse_string($target));
- }
- else if(preg_match('/^redirect/', $content)) {
- $target = $m[sizeof($m)-1];
- $copy = false;
- if (preg_match('/^:copy\s+/', $target)) {
- $target = preg_replace('/^:copy\s+/', '', $target);
- $copy = true;
+ else {
+ $target = $tokens[$i];
}
- $result[] = array('type' => 'redirect', 'copy' => $copy,
- 'target' => $this->_parse_string($target));
- }
- else if(preg_match('/^(reject|ereject)\s+(.*);$/sm', $content, $matches)) {
- $result[] = array('type' => $matches[1], 'target' => $this->_parse_string($matches[2]));
}
- else if(preg_match('/^vacation\s+(.*);$/sm', $content, $matches)) {
- $vacation = array('type' => 'vacation');
- if (preg_match('/:days\s+([0-9]+)/', $content, $vm)) {
- $vacation['days'] = $vm[1];
- $content = preg_replace('/:days\s+([0-9]+)/', '', $content);
+ $result[] = array('type' => $token, 'copy' => $copy,
+ 'target' => $target);
+ break;
+
+ case 'reject':
+ case 'ereject':
+ $result[] = array('type' => $token, 'target' => array_pop($tokens));
+ break;
+
+ case 'vacation':
+ $vacation = array('type' => 'vacation', 'reason' => array_pop($tokens));
+
+ for ($i=0, $len=count($tokens); $i<$len; $i++) {
+ $tok = strtolower($tokens[$i]);
+ if ($tok == ':days') {
+ $vacation['days'] = $tokens[++$i];
}
- if (preg_match('/:subject\s+"(.*?[^\\\])"/', $content, $vm)) {
- $vacation['subject'] = $vm[1];
- $content = preg_replace('/:subject\s+"(.*?[^\\\])"/', '', $content);
+ else if ($tok == ':subject') {
+ $vacation['subject'] = $tokens[++$i];
}
- if (preg_match('/:addresses\s+\[(.*?[^\\\])\]/', $content, $vm)) {
- $vacation['addresses'] = $this->_parse_list($vm[1]);
- $content = preg_replace('/:addresses\s+\[(.*?[^\\\])\]/', '', $content);
+ else if ($tok == ':addresses') {
+ $vacation['addresses'] = $tokens[++$i];
}
- if (preg_match('/:handle\s+"(.*?[^\\\])"/', $content, $vm)) {
- $vacation['handle'] = $vm[1];
- $content = preg_replace('/:handle\s+"(.*?[^\\\])"/', '', $content);
+ else if ($tok == ':handle') {
+ $vacation['handle'] = $tokens[++$i];
}
- if (preg_match('/:from\s+"(.*?[^\\\])"/', $content, $vm)) {
- $vacation['from'] = $vm[1];
- $content = preg_replace('/:from\s+"(.*?[^\\\])"/', '', $content);
+ else if ($tok == ':from') {
+ $vacation['from'] = $tokens[++$i];
}
-
- $content = preg_replace('/^vacation/', '', $content);
- $content = preg_replace('/;$/', '', $content);
- $content = trim($content);
-
- if (preg_match('/^:mime/', $content, $vm)) {
+ else if ($tok == ':mime') {
$vacation['mime'] = true;
- $content = preg_replace('/^:mime/', '', $content);
}
-
- $vacation['reason'] = $this->_parse_string($content);
-
- $result[] = $vacation;
}
+
+ $result[] = $vacation;
+ break;
}
}
@@ -820,171 +884,196 @@ class rcube_sieve_script
}
/**
- * Parse test/conditions section
+ * Escape special chars into quoted string value or multi-line string
+ * or list of strings
*
- * @param string Text
+ * @param string $str Text or array (list) of strings
+ *
+ * @return string Result text
*/
- private function _parse_tests($content)
+ static function escape_string($str)
{
- $result = NULL;
+ if (is_array($str) && count($str) > 1) {
+ foreach($str as $idx => $val)
+ $str[$idx] = self::escape_string($val);
- // lists
- if (preg_match('/^(allof|anyof)\s+\((.*)\)$/sm', $content, $matches)) {
- $content = $matches[2];
- $join = $matches[1]=='allof' ? true : false;
+ return '[' . implode(',', $str) . ']';
}
- else
- $join = false;
-
- // supported tests regular expressions
- // TODO: comparators, envelope
- $patterns[] = '(not\s+)?(exists)\s+\[(.*?[^\\\])\]';
- $patterns[] = '(not\s+)?(exists)\s+(".*?[^\\\]")';
- $patterns[] = '(not\s+)?(true)';
- $patterns[] = '(not\s+)?(size)\s+:(under|over)\s+([0-9]+[KGM]{0,1})';
- $patterns[] = '(not\s+)?(header)\s+:(contains|is|matches)((\s+))\[(.*?[^\\\]")\]\s+\[(.*?[^\\\]")\]';
- $patterns[] = '(not\s+)?(header)\s+:(contains|is|matches)((\s+))(".*?[^\\\]")\s+(".*?[^\\\]")';
- $patterns[] = '(not\s+)?(header)\s+:(contains|is|matches)((\s+))\[(.*?[^\\\]")\]\s+(".*?[^\\\]")';
- $patterns[] = '(not\s+)?(header)\s+:(contains|is|matches)((\s+))(".*?[^\\\]")\s+\[(.*?[^\\\]")\]';
- $patterns[] = '(not\s+)?(header)\s+:(count\s+"[gtleqn]{2}"|value\s+"[gtleqn]{2}")(\s+:comparator\s+"(.*?[^\\\])")?\s+\[(.*?[^\\\]")\]\s+\[(.*?[^\\\]")\]';
- $patterns[] = '(not\s+)?(header)\s+:(count\s+"[gtleqn]{2}"|value\s+"[gtleqn]{2}")(\s+:comparator\s+"(.*?[^\\\])")?\s+(".*?[^\\\]")\s+(".*?[^\\\]")';
- $patterns[] = '(not\s+)?(header)\s+:(count\s+"[gtleqn]{2}"|value\s+"[gtleqn]{2}")(\s+:comparator\s+"(.*?[^\\\])")?\s+\[(.*?[^\\\]")\]\s+(".*?[^\\\]")';
- $patterns[] = '(not\s+)?(header)\s+:(count\s+"[gtleqn]{2}"|value\s+"[gtleqn]{2}")(\s+:comparator\s+"(.*?[^\\\])")?\s+(".*?[^\\\]")\s+\[(.*?[^\\\]")\]';
-
- // join patterns...
- $pattern = '/(' . implode(')|(', $patterns) . ')/';
-
- // ...and parse tests list
- if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) {
- foreach ($matches as $match) {
- $size = sizeof($match);
-
- if (preg_match('/^(not\s+)?size/', $match[0])) {
- $result[] = array(
- 'test' => 'size',
- 'not' => $match[$size-4] ? true : false,
- 'type' => $match[$size-2], // under/over
- 'arg' => $match[$size-1], // value
- );
- }
- else if (preg_match('/^(not\s+)?header/', $match[0])) {
- $type = $match[$size-5];
- if (preg_match('/^(count|value)\s+"([gtleqn]{2})"/', $type, $m))
- $type = $m[1] . '-' . $m[2];
-
- $result[] = array(
- 'test' => 'header',
- 'type' => $type, // is/contains/matches
- 'not' => $match[$size-7] ? true : false,
- 'arg1' => $this->_parse_list($match[$size-2]), // header(s)
- 'arg2' => $this->_parse_list($match[$size-1]), // string(s)
- );
- }
- else if (preg_match('/^(not\s+)?exists/', $match[0])) {
- $result[] = array(
- 'test' => 'exists',
- 'not' => $match[$size-3] ? true : false,
- 'arg' => $this->_parse_list($match[$size-1]), // header(s)
- );
- }
- else if (preg_match('/^(not\s+)?true/', $match[0])) {
- $result[] = array(
- 'test' => 'true',
- 'not' => $match[$size-2] ? true : false,
- );
- }
- }
+ else if (is_array($str)) {
+ $str = array_pop($str);
}
- return array($result, $join);
+ // multi-line string
+ if (preg_match('/[\r\n\0]/', $str) || strlen($str) > 1024) {
+ return sprintf("text:\n%s\n.\n", self::escape_multiline_string($str));
+ }
+ // quoted-string
+ else {
+ $replace = array('\\' => '\\\\', '"' => '\\"');
+ $str = str_replace(array_keys($replace), array_values($replace), $str);
+ return '"' . $str . '"';
+ }
}
/**
- * Parse string value
+ * Escape special chars in multi-line string value
+ *
+ * @param string $str Text
*
- * @param string Text
+ * @return string Text
*/
- private function _parse_string($content)
+ static function escape_multiline_string($str)
{
- $text = '';
- $content = trim($content);
+ $str = preg_split('/(\r?\n)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
- if (preg_match('/^text:(.*)\.$/sm', $content, $matches))
- $text = trim($matches[1]);
- else if (preg_match('/^"(.*)"$/', $content, $matches))
- $text = str_replace('\"', '"', $matches[1]);
+ foreach ($str as $idx => $line) {
+ // dot-stuffing
+ if (isset($line[0]) && $line[0] == '.') {
+ $str[$idx] = '.' . $line;
+ }
+ }
- return $text;
+ return implode($str);
}
/**
- * Escape special chars in string value
+ * Splits script into string tokens
+ *
+ * @param string &$str The 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 called recursively inside a list
*
- * @param string Text
+ * @return mixed Tokens array or string if $num=1
*/
- private function _escape_string($content)
+ static function tokenize(&$str, $num=0, $in_list=false)
{
- $replace['/"/'] = '\\"';
+ $result = array();
- if (is_array($content)) {
- for ($x=0, $y=sizeof($content); $x<$y; $x++)
- $content[$x] = preg_replace(array_keys($replace),
- array_values($replace), $content[$x]);
+ // remove spaces from the beginning of the string
+ while (($str = ltrim($str)) !== ''
+ && (!$num || $num === true || count($result) < $num)
+ ) {
+ switch ($str[0]) {
- return $content;
- }
- else
- return preg_replace(array_keys($replace), array_values($replace), $content);
- }
+ // Quoted string
+ case '"':
+ $len = strlen($str);
- /**
- * Parse string or list of strings to string or array of strings
- *
- * @param string Text
- */
- private function _parse_list($content)
- {
- $result = array();
+ for ($pos=1; $pos<$len; $pos++) {
+ if ($str[$pos] == '"') {
+ break;
+ }
+ if ($str[$pos] == "\\") {
+ if ($str[$pos + 1] == '"' || $str[$pos + 1] == "\\") {
+ $pos++;
+ }
+ }
+ }
+ if ($str[$pos] != '"') {
+ // error
+ }
+ // we need to strip slashes for a quoted string
+ $result[] = stripslashes(substr($str, 1, $pos - 1));
+ $str = substr($str, $pos + 1);
+ break;
- for ($x=0, $len=strlen($content); $x<$len; $x++) {
- switch ($content[$x]) {
- case '\\':
- $str .= $content[++$x];
+ // Parenthesized list
+ case '[':
+ $str = substr($str, 1);
+ $result[] = self::tokenize($str, 0, true);
break;
- case '"':
- if (isset($str)) {
- $result[] = $str;
- unset($str);
+ case ']':
+ $str = substr($str, 1);
+ return $result;
+ break;
+
+ // list/test separator
+ case ',':
+ // command separator
+ case ';':
+ // block/tests-list
+ case '(':
+ case ')':
+ case '{':
+ case '}':
+ $sep = $str[0];
+ $str = substr($str, 1);
+ if ($num === true) {
+ $result[] = $sep;
+ break 2;
+ }
+ break;
+
+ // bracket-comment
+ case '/':
+ if ($str[1] == '*') {
+ if ($end_pos = strpos($str, '*/')) {
+ $str = substr($str, $end_pos + 2);
+ }
+ else {
+ // error
+ $str = '';
+ }
}
- else
- $str = '';
break;
+
+ // hash-comment
+ case '#':
+ if ($lf_pos = strpos($str, "\n")) {
+ $str = substr($str, $lf_pos);
+ break;
+ }
+ else {
+ $str = '';
+ }
+
+ // String atom
default:
- if(isset($str))
- $str .= $content[$x];
- break;
+ // empty or one character
+ if ($str === '') {
+ break 2;
+ }
+ if (strlen($str) < 2) {
+ $result[] = $str;
+ $str = '';
+ break;
+ }
+
+ // tag/identifier/number
+ if (preg_match('/^([a-z0-9:_]+)/i', $str, $m)) {
+ $str = substr($str, strlen($m[1]));
+
+ if ($m[1] != 'text:') {
+ $result[] = $m[1];
+ }
+ // multiline string
+ else {
+ // possible hash-comment after "text:"
+ if (preg_match('/^( |\t)*(#[^\n]+)?\n/', $str, $m)) {
+ $str = substr($str, strlen($m[0]));
+ }
+ // get text until alone dot in a line
+ if (preg_match('/^(.*)\r?\n\.\r?\n/sU', $str, $m)) {
+ $text = $m[1];
+ // remove dot-stuffing
+ $text = str_replace("\n..", "\n.", $text);
+ $str = substr($str, strlen($m[0]));
+ }
+ else {
+ $text = '';
+ }
+
+ $result[] = $text;
+ }
+ }
+
+ break;
}
}
- if (sizeof($result)>1)
- return $result;
- else if (sizeof($result) == 1)
- return $result[0];
- else
- return NULL;
+ return $num === 1 ? (isset($result[0]) ? $result[0] : null) : $result;
}
- /**
- * Convert array of elements to list of strings
- *
- * @param string Text
- */
- private function _print_list($list)
- {
- $list = (array) $list;
- foreach($list as $idx => $val)
- $list[$idx] = $this->_escape_string($val);
-
- return '["' . implode('","', $list) . '"]';
- }
}
diff --git a/plugins/managesieve/localization/es_ES.inc b/plugins/managesieve/localization/es_ES.inc
index 6130d7018..1dad18d99 100644
--- a/plugins/managesieve/localization/es_ES.inc
+++ b/plugins/managesieve/localization/es_ES.inc
@@ -9,7 +9,7 @@ $labels['filteradd'] = 'Agregar filtro';
$labels['filterdel'] = 'Eliminar filtro';
$labels['moveup'] = 'Mover arriba';
$labels['movedown'] = 'Mover abajo';
-$labels['filterallof'] = 'coinidir con todas las reglas siguientes';
+$labels['filterallof'] = 'coincidir con todas las reglas siguientes';
$labels['filteranyof'] = 'coincidir con alguna de las reglas siguientes';
$labels['filterany'] = 'todos los mensajes';
$labels['filtercontains'] = 'contiene';
diff --git a/plugins/managesieve/localization/fr_FR.inc b/plugins/managesieve/localization/fr_FR.inc
index 0b494f041..49bd3c676 100644
--- a/plugins/managesieve/localization/fr_FR.inc
+++ b/plugins/managesieve/localization/fr_FR.inc
@@ -27,6 +27,8 @@ $labels['messagereply'] = 'Répondre avec le message';
$labels['messagedelete'] = 'Supprimer le message';
$labels['messagediscard'] = 'Rejeter avec le message';
$labels['messagecopyto'] = 'Copier le message vers';
+$labels['messagesendcopy'] = 'Envoyer une copie du message à';
+$labels['messagecopyto'] = 'Copier le message vers';
$labels['messagesendcopy'] = 'Envoyer une copie du message à';
$labels['messagesrules'] = 'Pour les mails entrants:';
$labels['messagesactions'] = '...exécuter les actions suivantes:';
diff --git a/plugins/managesieve/localization/ru_RU.inc b/plugins/managesieve/localization/ru_RU.inc
index c7c8bb074..c9cd33608 100644
--- a/plugins/managesieve/localization/ru_RU.inc
+++ b/plugins/managesieve/localization/ru_RU.inc
@@ -76,6 +76,18 @@ $labels['countislessthanequal'] = 'количество меньше или ра
$labels['countequals'] = 'количество равно';
$labels['countnotequals'] = 'количество не равно';
$labels['valueisgreaterthan'] = 'значение больше, чем';
+$labels['countisgreaterthan'] = 'кПлОчествП бПльше, чеЌ';
+$labels['countisgreaterthanequal'] = 'кПлОчествП бПльше ОлО равМП';
+$labels['countislessthan'] = 'кПлОчествП ЌеМьше, чеЌ';
+$labels['countislessthanequal'] = 'кПлОчествП ЌеМьше ОлО равМП';
+$labels['countequals'] = 'кПлОчествП равМП';
+$labels['countnotequals'] = 'кПлОчествП Ме равМП';
+$labels['valueisgreaterthan'] = 'зМачеМОе бПльше, чеЌ';
+$labels['valueisgreaterthanequal'] = 'зМачеМОе бПльше ОлО равМП';
+$labels['valueislessthan'] = 'зМачеМОе ЌеМьше, чеЌ';
+$labels['valueislessthanequal'] = 'зМачеМОе ЌеМьше ОлО равМП';
+$labels['valueequals'] = 'зМачеМОе равМП';
+$labels['valuenotequals'] = 'зМачеМОе Ме равМП';
$labels['valueisgreaterthanequal'] = 'значение больше или равно';
$labels['valueislessthan'] = 'значение меньше, чем';
$labels['valueislessthanequal'] = 'значение меньше или равно';
diff --git a/plugins/managesieve/managesieve.js b/plugins/managesieve/managesieve.js
index 6b96561b4..04977eb1a 100644
--- a/plugins/managesieve/managesieve.js
+++ b/plugins/managesieve/managesieve.js
@@ -7,12 +7,10 @@ if (window.rcmail) {
var button = $('<a>').attr('href', rcmail.env.comm_path+'&_action=plugin.managesieve')
.attr('title', rcmail.gettext('managesieve.managefilters'))
.html(rcmail.gettext('managesieve.filters'))
- .bind('click', function(e){ return rcmail.command('plugin.managesieve', this) })
.appendTo(tab);
// add button and register commands
rcmail.add_element(tab, 'tabs');
- rcmail.register_command('plugin.managesieve', function() { rcmail.goto_url('plugin.managesieve') }, true);
rcmail.register_command('plugin.managesieve-save', function() { rcmail.managesieve_save() }, true);
rcmail.register_command('plugin.managesieve-add', function() { rcmail.managesieve_add() }, true);
rcmail.register_command('plugin.managesieve-del', function() { rcmail.managesieve_del() }, true);
diff --git a/plugins/managesieve/managesieve.php b/plugins/managesieve/managesieve.php
index 5de839da6..96e6ebcce 100644
--- a/plugins/managesieve/managesieve.php
+++ b/plugins/managesieve/managesieve.php
@@ -7,7 +7,7 @@
* It's clickable interface which operates on text scripts and communicates
* with server using managesieve protocol. Adds Filters tab in Settings.
*
- * @version 2.10
+ * @version 3.0
* @author Aleksander 'A.L.E.C' Machniak <alec@alec.pl>
*
* Configuration (see config.inc.php.dist)
@@ -66,7 +66,7 @@ class managesieve extends rcube_plugin
$host = rcube_parse_host($this->rc->config->get('managesieve_host', 'localhost'));
$port = $this->rc->config->get('managesieve_port', 2000);
- $host = idn_to_ascii($host);
+ $host = rcube_idn_to_ascii($host);
// try to connect to managesieve server and to fetch the script
$this->sieve = new rcube_sieve($_SESSION['username'],
diff --git a/plugins/managesieve/tests/Makefile b/plugins/managesieve/tests/Makefile
new file mode 100644
index 000000000..072be2f2c
--- /dev/null
+++ b/plugins/managesieve/tests/Makefile
@@ -0,0 +1,7 @@
+
+clean:
+ rm -f *.log *.php *.diff *.exp *.out
+
+
+test:
+ pear run-tests *.phpt
diff --git a/plugins/managesieve/tests/parser.phpt b/plugins/managesieve/tests/parser.phpt
new file mode 100644
index 000000000..a3b820d45
--- /dev/null
+++ b/plugins/managesieve/tests/parser.phpt
@@ -0,0 +1,103 @@
+--TEST--
+Main test of script parser
+--SKIPIF--
+--FILE--
+<?php
+include('../lib/rcube_sieve.php');
+
+$txt = '
+require ["fileinto","vacation","reject","relational","comparator-i;ascii-numeric"];
+# rule:[spam]
+if anyof (header :contains "X-DSPAM-Result" "Spam")
+{
+ fileinto "Spam";
+ stop;
+}
+# rule:[test1]
+if anyof (header :contains ["From","To"] "test@domain.tld")
+{
+ discard;
+ stop;
+}
+# rule:[test2]
+if anyof (not header :contains ["Subject"] "[test]", header :contains "Subject" "[test2]")
+{
+ fileinto "test";
+ stop;
+}
+# rule:[test-vacation]
+if anyof (header :contains "Subject" "vacation")
+{
+ vacation :days 1 text:
+# test
+test test /* test */
+test
+.
+;
+ stop;
+}
+# rule:[comments]
+if anyof (true) /* comment
+ * "comment" #comment */ {
+ /* comment */ stop;
+# comment
+}
+# rule:[reject]
+if size :over 5000K {
+ reject "Message over 5MB size limit. Please contact me before sending this.";
+}
+# rule:[redirect]
+if header :value "ge" :comparator "i;ascii-numeric"
+ ["X-Spam-score"] ["14"] {redirect "test@test.tld";}
+';
+
+$s = new rcube_sieve_script($txt);
+echo $s->as_text();
+
+?>
+--EXPECT--
+require ["fileinto","vacation","reject","relational","comparator-i;ascii-numeric"];
+# rule:[spam]
+if header :contains "X-DSPAM-Result" "Spam"
+{
+ fileinto "Spam";
+ stop;
+}
+# rule:[test1]
+if header :contains ["From","To"] "test@domain.tld"
+{
+ discard;
+ stop;
+}
+# rule:[test2]
+if anyof (not header :contains "Subject" "[test]", header :contains "Subject" "[test2]")
+{
+ fileinto "test";
+ stop;
+}
+# rule:[test-vacation]
+if header :contains "Subject" "vacation"
+{
+ vacation :days 1 text:
+# test
+test test /* test */
+test
+.
+;
+ stop;
+}
+# rule:[comments]
+if true
+{
+ stop;
+}
+# rule:[reject]
+if size :over 5000K
+{
+ reject "Message over 5MB size limit. Please contact me before sending this.";
+}
+# rule:[redirect]
+if header :value "ge" :comparator "i;ascii-numeric" "X-Spam-score" "14"
+{
+ redirect "test@test.tld";
+}
diff --git a/plugins/managesieve/tests/tokenize.phpt b/plugins/managesieve/tests/tokenize.phpt
new file mode 100644
index 000000000..d1f68acea
--- /dev/null
+++ b/plugins/managesieve/tests/tokenize.phpt
@@ -0,0 +1,66 @@
+--TEST--
+Script parsing: tokenizer
+--SKIPIF--
+--FILE--
+<?php
+include('../lib/rcube_sieve.php');
+
+$txt[1] = array(1, 'text: #test
+This is test ; message;
+Multi line
+.
+;
+');
+$txt[2] = array(0, '["test1","test2"]');
+$txt[3] = array(1, '["test"]');
+$txt[4] = array(1, '"te\\"st"');
+$txt[5] = array(0, 'test #comment');
+$txt[6] = array(0, 'text:
+test
+.
+text:
+test
+.
+');
+$txt[7] = array(1, '"\\a\\\\\\"a"');
+
+foreach ($txt as $idx => $t) {
+ echo "[$idx]---------------\n";
+ var_dump(rcube_sieve_script::tokenize($t[1], $t[0]));
+}
+?>
+--EXPECT--
+[1]---------------
+string(34) "This is test ; message;
+Multi line"
+[2]---------------
+array(1) {
+ [0]=>
+ array(2) {
+ [0]=>
+ string(5) "test1"
+ [1]=>
+ string(5) "test2"
+ }
+}
+[3]---------------
+array(1) {
+ [0]=>
+ string(4) "test"
+}
+[4]---------------
+string(5) "te"st"
+[5]---------------
+array(1) {
+ [0]=>
+ string(4) "test"
+}
+[6]---------------
+array(2) {
+ [0]=>
+ string(4) "test"
+ [1]=>
+ string(4) "test"
+}
+[7]---------------
+string(4) "a\"a"