* @version 1.9 * @license GPL * @link https://sourceforge.net/projects/rcubevacation/ * @todo See README.TXT * * * * Based on ManageSieve plugin by Aleksander 'A.L.E.C' Machniak * */ class Sieve extends VacationDriver { private $sieve = null; // Net_Sieve object private $script; // rcube_sieve_script object private $server = "localhost"; private $port = 2000; private $tls = false; private $script_name = "vacation"; public function init() { // try to connect to managesieve server and to fetch the script $username = Q($this->user->data['username']); $userpass = $this->rcmail->decrypt($_SESSION['password']); // Standard plugin require_once 'plugins/managesieve/lib/Net/Sieve.php'; $this->sieve = new Net_Sieve(); if (PEAR::isError( $this->sieve->connect($this->cfg['server'] ? $this->cfg['server'] : $this->server, $this->cfg['port'] ? $this->cfg['port'] : $this->port, NULL, $this->cfg['tls'] ? $this->cfg['tls'] : $this->tls) )) { raise_error(array('code' => 601, 'type' => 'php', 'file' => __FILE__, 'message' => sprintf("Vacation plugin: Cannot connect to the Sieve server '%s'", $this->cfg['server'] ? $this->cfg['server'] : $this->server) ),true, true); } if (PEAR::isError($this->sieve->login($username, $userpass))) { raise_error(array('code' => 601, 'type' => 'php', 'file' => __FILE__, 'message' => sprintf("Vacation plugin: Cannot login to the Sieve server '%s'", $this->cfg['server'] ? $this->cfg['server'] : $this->server) ),true, true); } } // Download .forward and .vacation.message file public function _get() { $vacArr = array("subject"=>"","aliases"=>"", "body"=>"","forward"=>"","keepcopy"=>true,"enabled"=>false); if ($script = $this->sieve->getScript($this->script_name)) { /* if (PEAR::isError($script)) { raise_error(array('code' => 601, 'type' => 'php', 'file' => __FILE__, 'message' => sprintf("Vacation plugin: Cannot get sieve script from Sieve server '%s'", $this->cfg['server'] ? $this->cfg['server'] : $this->server) ),true, true); } */ $tokens = preg_split('/(# rule:\[.*\])\r?\n/', $script, -1, PREG_SPLIT_DELIM_CAPTURE); $vacation_rule = ""; $forward_rule = ""; while ($token = array_shift($tokens)) { if ($token == "# rule:[vacation]") $vacation_rule = array_shift($tokens); if ($token == "# rule:[forward]") $forward_rule = array_shift($tokens); } if ($vacation_rule) { if (preg_match('/vacation :subject "(.*?)" text:\s*\r?\n((?:.*\r?\n)*?.*?)\.\r?\n;/', $vacation_rule, $matches)) { $vacArr["subject"] = str_replace('\"', '"', $matches[1]); $vacArr["body"] = $matches[2]; } if (preg_match('/if false/', $vacation_rule)) { $vacArr["enabled"] = false; } else { $vacArr["enabled"] = true; } } if ($forward_rule) { if (preg_match('/discard\s*;/', $forward_rule)) { $vacArr["keepcopy"] = false; } else { $vacArr["keepcopy"] = true; } $addresses = array(); $forward_rule = str_replace("\n", " ", str_replace("\r", "", $forward_rule)); while (preg_match('/redirect .*?"(.*?)"\s*;\s*\r?\n?(.*)$/', $forward_rule, $matches)) { $addresses[] = $matches[1]; $forward_rule = $matches[2]; } $vacArr["forward"] = implode(", ", $addresses); } } return $vacArr; } protected function setVacation() { $script = ""; $script = sprintf("require [\"fileinto\", \"reject\", \"vacation\", \"copy\", \"regex\"]; # rule:[vacation] if %sallof (header :contains [ \"To\", \"Cc\" ] \"@\", not header :contains \"X-Spam-Flag\" \"YES\", not header :contains \"X-Quarantined\" \"YES\", not header :regex \"X-Spam-Status\" \"^Yes\", not header :contains \"X-SpamTest-Status\" \"SPAM\", not header :contains \"X-MimeOLE\" \"Produced By phpBB2\", not header :contains \"Precedence\" \"List\", not header :contains \"Precedence\" \"list\", not header :contains \"Precedence\" \"bulk\", not header :contains \"Precedence\" \"junk\", not exists \"List-Id\", not exists \"List-Post\", not exists \"List-Help\", not exists \"List-Archive\", not exists \"List-Subscribe\", not exists \"List-Unsubscribe\", not exists \"X-Mailman-Version\", not exists \"Mailing-List-Server\", not exists \"X-Mailing-List\", not exists \"X-List-Administrivia\", not exists \"X-list\", not exists \"X-Notifier\", not exists \"X-Broadcast-Id\") { vacation :subject \"%s\" text: %s . ; } ", ($this->enable ? "" : "false { # "), str_replace('"', '\"', $this->subject), $this->body); if ($this->forward) { $redirects = array(); foreach (preg_split('/\s*,\s*/', $this->forward) as $addr) $redirects[] = sprintf("\tredirect :copy \"%s\";", $addr); $script .= sprintf("# rule:[forward] if true { %s \t%s } ", implode("\n", $redirects), ($this->keepcopy ? "keep;" : "discard;")); } if (PEAR::isError($this->sieve->installScript($this->script_name, $script))) { raise_error(array('code' => 601, 'type' => 'php', 'file' => __FILE__, 'message' => sprintf("Vacation plugin: Cannot install sieve script on Sieve server '%s'", $this->cfg['server'] ? $this->cfg['server'] : $this->server) ),true, true); } else { if (PEAR::isError($this->sieve->setActive($this->script_name))) { raise_error(array('code' => 601, 'type' => 'php', 'file' => __FILE__, 'message' => sprintf("Vacation plugin: Cannot activate sieve script '%s' on Sieve server '%s'", $this->script_name, $this->cfg['server'] ? $this->cfg['server'] : $this->server) ),true, true); } else { return(true); } } return(false); } // Cleans up files private function disable() { } public function __destruct() { if ($this->sieve) $this->sieve->disconnect(); $this->sieve = null; } } ?>