gunnar: server/kolab-webadmin/kolab-webadmin/lib/KolabAdmin/Sieve/Segment Vacation.php, NONE, 1.1

cvs at kolab.org cvs at kolab.org
Tue Apr 6 10:55:31 CEST 2010


Author: gunnar

Update of /kolabrepository/server/kolab-webadmin/kolab-webadmin/lib/KolabAdmin/Sieve/Segment
In directory doto:/tmp/cvs-serv24304/kolab-webadmin/lib/KolabAdmin/Sieve/Segment

Added Files:
	Vacation.php 
Log Message:
Add the vacation segment.

--- NEW FILE: Vacation.php ---
<?php
/**
 * A sieve script that responds automatically during vacations.
 *
 * PHP version 5
 *
 * @category Kolab
 * @package  KolabAdmin
 * @author   Gunnar Wrobel <wrobel at pardus.de>
 * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
 * @link     http://www.kolab.org
 */

/**
 * A sieve script that responds automatically during vacations.
 *
 * Copyright 2010 Klarälvdalens Datakonsult AB
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 * @category Kolab
 * @package  KolabAdmin
 * @author   Gunnar Wrobel <wrobel at pardus.de>
 * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
 * @link     http://www.kolab.org
 */
class KolabAdmin_Sieve_Segment_Vacation
extends KolabAdmin_Sieve_Segment
{
    /**
     * The segment type.
     *
     * @var string
     */
    protected $type = 'vacation';

    /**
     * The text of the automatic response.
     *
     * @var string
     */
    private $_response;

    /**
     * Resend response after this amount of days elapsed.
     *
     * @var string
     */
    private $_resend_after = 7;

    /**
     * Recipient addresses for which the response should be sent.
     *
     * @var array
     */
    private $_recipient_addresses = array();

    /**
     * Should a response be sent in case the incoming message has been tagged as
     * spam?
     *
     * @var boolean
     */
    private $_react_to_spam = false;

    /**
     * The sender address must be part of this domain for the automatic
     * responses to be sent.
     *
     * @var string
     */
    private $_domain = '';

    /**
     * Constructor.
     *
     * @param string $script The current script segment
     */
    public function __construct($script = '')
    {
        $this->_response = '';

        $this->template = 'if allof (%s' . "\r\n" .
            '%s' . "\r\n" .
            '%s) {' . "\r\n" .
            'vacation :addresses [ %s ] :days %s text:' . "\r\n" .
            '%s' . "\r\n" .
            '.' . "\r\n" .
            ';' . "\r\n" .
            '}' . "\r\n";
        parent::__construct($script);
    }

    /**
     * Fetch the text of the automatic response.
     *
     * @return string The response.
     */
    public function getResponse()
    {
        return $this->undotstuff($this->_response);
    }

    /**
     * Set the text of the automatic response.
     *
     * @param string $response The response.
     *
     * @return NULL
     */
    public function setResponse($response)
    {
        $this->_response = $this->dotstuff($response);
    }

    /**
     * Resend the automatic response after how many days?
     *
     * @return int The number of days.
     */
    public function getResendAfter()
    {
        return $this->_resend_after;
    }

    /**
     * Set after how many days the automatic response should get sent again.
     *
     * @param int $days Resend after this many days.
     *
     * @return NULL
     */
    public function setResendAfter($days)
    {
        $this->_resend_after = $days;
    }

    /**
     * Send the responses to which recipient addresses?
     *
     * @return array The recipient addresses.
     */
    public function getRecipientAddresses()
    {
        return $this->_recipient_addresses;
    }

    /**
     * Set the recipient addresses for which the automatic reply will be sent.
     *
     * @param array $addresses The recipient addresses.
     *
     * @return NULL
     */
    public function setRecipientAddresses(array $addresses)
    {
        $this->_recipient_addresses = $addresses;
    }

    /**
     * Should the vacation notice also be sent in reply to messages
     * flagged as spam?
     *
     * @return boolean True in case the reply should also be sent to
     * potential spam messages.
     */
    public function getReactToSpam()
    {
        return $this->_react_to_spam;
    }

    /**
     * Set whether the replies during vacation should also get sent to
     * potential spam messages.
     *
     * @param boolean $react_to_spam Should the replies also be sent
     * for potential spam messages?
     *
     * @return NULL
     */
    public function setReactToSpam($react_to_spam)
    {
        $this->_react_to_spam = $react_to_spam;
    }

    /**
     * Should we only react to messages recieved from a specific domain?
     *
     * @return string The domain for which the vacation response will be sent.
     */
    public function getDomain()
    {
        return $this->_domain;
    }

    /**
     * Set the domain for which vacation replies will be sent.
     *
     * @param string $domain The domain for which the vacation response will be sent.
     *
     * @return NULL
     */
    public function setDomain($domain)
    {
        $this->_domain = $domain;
    }

    public function getArguments()
    {
        $domain = $this->getDomain();
        if (!empty($domain)) {
            $domain = 'address :domain :contains "From" "' . $domain . '"';
        } else {
            $domain = '';
        }
        if (!$this->getReactToSpam()) {
            $react_to_spam = 'not header :contains "X-Spam-Flag" "YES"';
        } else {
            $react_to_spam = '';
        }
        return array(
            ($this->isActive()) ? 'true, ## vacation enabled' : 'false, ## vacation disabled',
            $domain,
            $react_to_spam,
            '"' . join('", "', $this->getRecipientAddresses()) . '"',
            $this->getResendAfter(),
            $this->getResponse()
        );
    }

    public function parseArguments($script)
    {
        $this->parseResponse($script);
        $this->parseResendAfter($script);
        $this->parseDomain($script);
        $this->parseRecipientAddresses($script);
        $this->parseReactToSpam($script);
    }

    public function parseResponse($script)
    {
        if (preg_match("/text:(.*\r\n)\\.\r\n/s", $script, $regs)) {
            $this->_response = trim(str_replace( '\n', "\r\n", $regs[1]));
        }
    }

    public function parseResendAfter($script)
    {
        if (preg_match("/:days ([0-9]+)/s", $script, $regs)) {
            $this->_resend_after = $regs[1];
        }
    }

    public function parseDomain($script)
    {
        if (preg_match('/address :domain :contains "From" "(.*)"/i', $script, $regs)) {
            $this->_domain = $regs[1];
        }
    }

    public function parseRecipientAddresses($script)
    {
        if (preg_match("/:addresses \\[([^\\]]*)\\]/s", $script, $regs)) {
            $tmp = split(',', $regs[1]);
            $this->_recipient_addresses = array();
            foreach ($tmp as $a) {
                if (preg_match('/^ *"(.*)" *$/', $a, $regs)) {
                    $this->_recipient_addresses[] = $regs[1];
                } else {
                    $this->_recipient_addresses[] = $a;
                }
            }
        }
    }

    public function parseReactToSpam($script)
    {
        if (preg_match('/header :contains "X-Spam-Flag" "YES"/i', $script)) {
            $this->_react_to_spam = false;
        } else {
            $this->_react_to_spam = true;
        }
    }
}




More information about the commits mailing list