alecpl
2011-02-16 207cc0b9b3cfdfb29e4f02e83014320fd12eeb68
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 @@
            $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 @@
     * @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 @@
                    $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 @@
                    }
                    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 @@
                        $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 @@
                        $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 @@
                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 @@
        $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 @@
     */
    private function _tokenize_rule($content)
    {
        $result = NULL;
        $cond = strtolower(self::tokenize($content, 1));
        if (preg_match('/^(if|elsif|else)\s+((true|false|not\s+true|allof|anyof|exists|header|not|size)(.*))\s+\{(.*)\}$/sm',
            trim($content), $matches)) {
        if ($cond != 'if' && $cond != 'elsif' && $cond != 'else') {
            return null;
        }
            $tests = trim($matches[2]);
        $disabled = false;
        $join     = false;
            // disabled rule (false + comment): if false #.....
            if ($matches[3] == 'false') {
                $tests = preg_replace('/^false\s+#\s+/', '', $tests);
                $disabled = true;
        // 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
                $disabled = false;
            else {
                $token = $separator;
            }
            list($tests, $join) = $this->_parse_tests($tests);
            $actions = $this->_parse_actions(trim($matches[5]));
            $token = strtolower($token);
            if ($tests && $actions)
                $result = array(
                    'type'     => $matches[1],
                    'tests'    => $tests,
                    'actions'  => $actions,
                    'join'     => $join,
                    'disabled' => $disabled,
            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];
                    }
                }
                $tests[] = $header;
                break;
            case 'exists':
                $tests[] = array('test' => 'exists', 'not'  => $not,
                    'arg'  => array_pop($tokens));
                break;
            case 'true':
                $tests[] = array('test' => 'true', 'not'  => $not);
                break;
            case 'false':
                $tests[] = array('test' => 'true', 'not'  => !$not);
                break;
            }
            // goto actions...
            if ($separator == '{') {
                break;
            }
        }
        // ...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 @@
     */
    private function _parse_actions($content)
    {
        $result = NULL;
        $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+(.*?[^\\\]);';
        while (strlen($content)) {
            $tokens = self::tokenize($content, true);
            $separator = array_pop($tokens);
        $pattern = '/(' . implode('\s*$)|(', $patterns) . '$\s*)/ms';
            if (!empty($tokens)) {
                $token = array_shift($tokens);
            }
            else {
                $token = $separator;
            }
        // parse actions body
        if (preg_match_all($pattern, $content, $mm, PREG_SET_ORDER)) {
            foreach ($mm as $m) {
                $content = trim($m[0]);
            switch ($token) {
            case 'discard':
            case 'keep':
            case 'stop':
                $result[] = array('type' => $token);
                break;
                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);
            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 {
                        $target = $tokens[$i];
                    }
                }
                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;
                    }
                    $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);
                    }
                    if (preg_match('/:subject\s+"(.*?[^\\\])"/', $content, $vm)) {
                        $vacation['subject'] = $vm[1];
                        $content = preg_replace('/:subject\s+"(.*?[^\\\])"/', '', $content);
                    }
                    if (preg_match('/:addresses\s+\[(.*?[^\\\])\]/', $content, $vm)) {
                        $vacation['addresses'] = $this->_parse_list($vm[1]);
                        $content = preg_replace('/:addresses\s+\[(.*?[^\\\])\]/', '', $content);
                    }
                    if (preg_match('/:handle\s+"(.*?[^\\\])"/', $content, $vm)) {
                        $vacation['handle'] = $vm[1];
                        $content = preg_replace('/:handle\s+"(.*?[^\\\])"/', '', $content);
                    }
                    if (preg_match('/:from\s+"(.*?[^\\\])"/', $content, $vm)) {
                        $vacation['from'] = $vm[1];
                        $content = preg_replace('/:from\s+"(.*?[^\\\])"/', '', $content);
                    }
                $result[] = array('type' => $token, 'copy' => $copy,
                    'target' => $target);
                break;
                    $content = preg_replace('/^vacation/', '', $content);
                    $content = preg_replace('/;$/', '', $content);
                    $content = trim($content);
            case 'reject':
            case 'ereject':
                $result[] = array('type' => $token, 'target' => array_pop($tokens));
                break;
                    if (preg_match('/^:mime/', $content, $vm)) {
            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];
                    }
                    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;
                        $content = preg_replace('/^:mime/', '', $content);
                    }
                    $vacation['reason'] = $this->_parse_string($content);
                    $result[] = $vacation;
                }
                $result[] = $vacation;
                break;
            }
        }
@@ -820,171 +884,196 @@
    }
    /**
     * 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;
        else if (is_array($str)) {
            $str = array_pop($str);
        }
        // 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+\[(.*?[^\\\]")\]';
        // 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 . '"';
        }
    }
        // join patterns...
        $pattern = '/(' . implode(')|(', $patterns) . ')/';
    /**
     * Escape special chars in multi-line string value
     *
     * @param string $str Text
     *
     * @return string Text
     */
    static function escape_multiline_string($str)
    {
        $str = preg_split('/(\r?\n)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
        // ...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,
                    );
                }
        foreach ($str as $idx => $line) {
            // dot-stuffing
            if (isset($line[0]) && $line[0] == '.') {
                $str[$idx] = '.' . $line;
            }
        }
        return array($result, $join);
        return implode($str);
    }
    /**
     * Parse string value
     * Splits script into string tokens
     *
     * @param string Text
     */
    private function _parse_string($content)
    {
        $text = '';
        $content = trim($content);
        if (preg_match('/^text:(.*)\.$/sm', $content, $matches))
            $text = trim($matches[1]);
        else if (preg_match('/^"(.*)"$/', $content, $matches))
            $text = str_replace('\"', '"', $matches[1]);
        return $text;
    }
    /**
     * Escape special chars in string value
     * @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)
    {
        $replace['/"/'] = '\\"';
        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]);
            return $content;
        }
        else
            return preg_replace(array_keys($replace), array_values($replace), $content);
    }
    /**
     * Parse string or list of strings to string or array of strings
     *
     * @param string Text
     */
    private function _parse_list($content)
    static function tokenize(&$str, $num=0, $in_list=false)
    {
        $result = array();
        for ($x=0, $len=strlen($content); $x<$len; $x++) {
            switch ($content[$x]) {
            case '\\':
                $str .= $content[++$x];
                break;
        // remove spaces from the beginning of the string
        while (($str = ltrim($str)) !== ''
            && (!$num || $num === true || count($result) < $num)
        ) {
            switch ($str[0]) {
            // Quoted string
            case '"':
                if (isset($str)) {
                    $result[] = $str;
                    unset($str);
                $len = strlen($str);
                for ($pos=1; $pos<$len; $pos++) {
                    if ($str[$pos] == '"') {
                        break;
                    }
                    if ($str[$pos] == "\\") {
                        if ($str[$pos + 1] == '"' || $str[$pos + 1] == "\\") {
                            $pos++;
                        }
                    }
                }
                else
                    $str = '';
                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;
            // Parenthesized list
            case '[':
                $str = substr($str, 1);
                $result[] = self::tokenize($str, 0, true);
                break;
            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 = '';
                    }
                }
                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) . '"]';
    }
}