summaryrefslogtreecommitdiff
path: root/lib/KolabAdmin/Sieve/Segment/Forward.php
blob: 7acf9937509481d10b83bd7acc4052d529e627d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
 * A sieve script that handles mail forwarding to a specific address.
 *
 * PHP version 5
 *
 * @category Kolab
 * @package  KolabAdmin
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
 * @link     http://www.kolab.org
 */

/**
 * A sieve script that handles mail forwarding to a specific address.
 *
 * 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@pardus.de>
 * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
 * @link     http://www.kolab.org
 */
class KolabAdmin_Sieve_Segment_Forward
extends KolabAdmin_Sieve_Segment
{
    /**
     * The segment type.
     *
     * @var string
     */
    protected $type = 'forward';

    /**
     * The forwarding address.
     *
     * @var string
     */
    private $_forward_address = '';

    /**
     * Should mails be kept on the server after forwarding?
     *
     * @var string
     */
    private $_keep_on_server = true;

    /**
     * Constructor.
     *
     * @param string $script The current script segment
     */
    public function __construct($script = '')
    {
        $this->template = 'if allof (%s' . "\r\n" .
            ') {' . "\r\n" .
            '%s%s' . "\r\n" .
            '}' . "\r\n";
        parent::__construct($script);
    }

    /**
     * Retrieve the forwarding address this script will deliver to.
     *
     * @return string The forwarding address.
     */
    public function getForwardAddress()
    {
        return $this->_forward_address;
    }

    /**
     * Set the forwarding address this script will deliver to.
     *
     * @param string $folder The forward address.
     *
     * @return NULL
     */
    public function setForwardAddress($address)
    {
        if (empty($address)) {
            throw new Exception('Please enter a valid e-mail address!');
        }
        $this->_forward_address = $address;
    }

    /**
     * Should the messages be kept on the server after forwarding?
     *
     * @return bool True if the messages should be kept.
     */
    public function getKeepOnServer()
    {
        return $this->_keep_on_server;
    }

    /**
     * Set if the messages should be kept on the server after forwarding.
     *
     * @param boolean $keep True if the messages should be kept.
     *
     * @return NULL
     */
    public function setKeepOnServer($keep)
    {
        $this->_keep_on_server = $keep;
    }

    public function getArguments()
    {
        $address = $this->getForwardAddress();
        if (!empty($address)) {
            $address = 'redirect "' . $address . '";';
        } else {
            $address = '';
        }
        return array(
            ($this->isActive()) ? 'true ## forward enabled' : 'false ## forward disabled',
            $address,
            ($this->getKeepOnServer()) ? ' keep;' : ''
        );
    }

    public function parseArguments($script)
    {
        $this->parseForwardAddress($script);
        $this->parseKeepOnServer($script);
    }

    public function parseForwardAddress($script)
    {
        if (preg_match("/redirect \"([^\"]*)\"/s", $script, $regs)) {
            $this->_forward_address = $regs[1];
        }
    }

    public function parseKeepOnServer($script)
    {
        if (preg_match('/redirect "[^\"]*"; keep;/s', $script, $regs)) {
            $this->_keep_on_server = true;
        } else {
            $this->_keep_on_server = false;
        }
    }
}