summaryrefslogtreecommitdiff
path: root/lib/KolabAdmin/Sieve/Segment/Vacation.php
blob: a025bc50ebae62d539ac5c9bc547e290928394bb (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
 * A sieve script that responds automatically during vacations.
 *
 * 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 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@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 %s :days %s text:' . "\r\n" .
            '%s' . "\r\n" .
            '.' . "\r\n" .
            ';' . "\r\n" .
            '}' . "\r\n";

        $this->_response = sprintf(
            _("I am out of office until %s.\r\n").
            _("In urgent cases, please contact Mrs. <vacation replacement>\r\n\r\n").
            _("email: <email address of vacation replacement>\r\n").
            _("phone: +49 711 1111 11\r\n").
            _("fax.:  +49 711 1111 12\r\n\r\n").
            _("Yours sincerely,\r\n").
            _("-- \r\n").
            _("<enter your name and email address here>"),
            strftime(_('%x'))
        );

        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()
    {
        //@todo: Should be placed into a validate method.
        if ($this->_resend_after < 1) {
            $this->_resend_after = 7;
        }
        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)
    {
        //@todo: Should be placed into a validate method.
        if ($this->_resend_after < 1) {
            throw new Exception(_('Days must be at least one'));
        }
        $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 = 'true';
        }
        if (!$this->getReactToSpam()) {
            $react_to_spam = 'not header :contains "X-Spam-Flag" "YES"';
        } else {
            $react_to_spam = 'true';
        }
        $addresses = $this->getRecipientAddresses();
        if (!empty($addresses)) {
            $addresses = ':addresses [ "' . join('", "', $this->getRecipientAddresses()) . '" ]';
        } else {
            $addresses = '';
        }
        return array(
            ($this->isActive()) ? 'true, ## vacation enabled' : 'false, ## vacation disabled',
            $domain,
            $react_to_spam,
            $addresses,
            $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;
        }
    }
}