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
|
<?php
/**
* Managesieve (Sieve Filters)
*
* Plugin that adds a possibility to manage Sieve filters in Thunderbird's style.
* It's clickable interface which operates on text scripts and communicates
* with server using managesieve protocol. Adds Filters tab in Settings.
*
* @version @package_version@
* @author Aleksander Machniak <alec@alec.pl>
*
* Configuration (see config.inc.php.dist)
*
* Copyright (C) 2008-2013, The Roundcube Dev Team
* Copyright (C) 2011-2013, Kolab Systems AG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
class managesieve extends rcube_plugin
{
public $task = 'mail|settings';
private $rc;
private $engine;
function init()
{
$this->rc = rcmail::get_instance();
// register actions
$this->register_action('plugin.managesieve', array($this, 'managesieve_actions'));
$this->register_action('plugin.managesieve-action', array($this, 'managesieve_actions'));
$this->register_action('plugin.managesieve-vacation', array($this, 'managesieve_actions'));
$this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
if ($this->rc->task == 'settings') {
$this->add_hook('settings_actions', array($this, 'settings_actions'));
$this->init_ui();
}
else if ($this->rc->task == 'mail') {
// register message hook
$this->add_hook('message_headers_output', array($this, 'mail_headers'));
// inject Create Filter popup stuff
if (empty($this->rc->action) || $this->rc->action == 'show') {
$this->mail_task_handler();
}
}
}
/**
* Initializes plugin's UI (localization, js script)
*/
function init_ui()
{
if ($this->ui_initialized) {
return;
}
// load localization
$this->add_texts('localization/');
if (strpos($this->rc->action, 'plugin.managesieve') === 0) {
$this->include_script('managesieve.js');
}
// include styles
$skin_path = $this->local_skin_path();
if ($this->rc->task == 'settings') {
if (is_file($this->home . "/$skin_path/managesieve.css")) {
$this->include_stylesheet("$skin_path/managesieve.css");
}
}
else {
if (is_file($this->home . "/$skin_path/managesieve_mail.css")) {
$this->include_stylesheet("$skin_path/managesieve_mail.css");
}
}
$this->ui_initialized = true;
}
/**
* Adds Filters section in Settings
*/
function settings_actions($args)
{
$this->load_config();
$vacation_mode = (int) $this->rc->config->get('managesieve_vacation');
// register Filters action
if ($vacation_mode != 2) {
$args['actions'][] = array(
'action' => 'plugin.managesieve',
'class' => 'filter',
'label' => 'filters',
'domain' => 'managesieve',
);
}
// register Vacation action
if ($vacation_mode > 0) {
$args['actions'][] = array(
'action' => 'plugin.managesieve-vacation',
'class' => 'vacation',
'label' => 'vacation',
'domain' => 'managesieve',
);
}
return $args;
}
/**
* Add UI elements to the 'mailbox view' and 'show message' UI.
*/
function mail_task_handler()
{
// make sure we're not in ajax request
if ($this->rc->output->type != 'html') {
return;
}
// use jQuery for popup window
$this->require_plugin('jqueryui');
// include js script and localization
$this->init_ui();
// add 'Create filter' item to message menu
$this->api->add_content(html::tag('li', null,
$this->api->output->button(array(
'command' => 'managesieve-create',
'label' => 'managesieve.filtercreate',
'type' => 'link',
'classact' => 'icon filterlink active',
'class' => 'icon filterlink',
'innerclass' => 'icon filterlink',
))), 'messagemenu');
// register some labels/messages
$this->rc->output->add_label('managesieve.newfilter', 'managesieve.usedata',
'managesieve.nodata', 'managesieve.nextstep', 'save');
$this->rc->session->remove('managesieve_current');
}
/**
* Get message headers for popup window
*/
function mail_headers($args)
{
// this hook can be executed many times
if ($this->mail_headers_done) {
return $args;
}
$this->mail_headers_done = true;
$headers = $this->parse_headers($args['headers']);
if ($this->rc->action == 'preview')
$this->rc->output->command('parent.set_env', array('sieve_headers' => $headers));
else
$this->rc->output->set_env('sieve_headers', $headers);
return $args;
}
/**
* Plugin action handler
*/
function managesieve_actions()
{
// handle fetching email headers for the new filter form
if ($uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_GPC)) {
$mailbox = $this->rc->get_storage()->get_folder();
$message = new rcube_message($uid, $mailbox);
$headers = $this->parse_headers($message->headers);
$this->rc->output->set_env('sieve_headers', $headers);
$this->rc->output->command('managesieve_create', true);
$this->rc->output->send();
}
// handle other actions
$engine_type = $this->rc->action == 'plugin.managesieve-vacation' ? 'vacation' : '';
$engine = $this->get_engine($engine_type);
$this->init_ui();
$engine->actions();
}
/**
* Forms save action handler
*/
function managesieve_save()
{
// load localization
$this->add_texts('localization/', array('filters','managefilters'));
// include main js script
if ($this->api->output->type == 'html') {
$this->include_script('managesieve.js');
}
$engine = $this->get_engine();
$engine->save();
}
/**
* Initializes engine object
*/
private function get_engine($type = null)
{
if (!$this->engine) {
$this->load_config();
// Add include path for internal classes
$include_path = $this->home . '/lib' . PATH_SEPARATOR;
$include_path .= ini_get('include_path');
set_include_path($include_path);
$class_name = 'rcube_sieve_' . ($type ? $type : 'engine');
$this->engine = new $class_name($this);
}
return $this->engine;
}
/**
* Extract mail headers for new filter form
*/
private function parse_headers($headers)
{
$result = array();
if ($headers->subject)
$result[] = array('Subject', rcube_mime::decode_header($headers->subject));
// @TODO: List-Id, others?
foreach (array('From', 'To') as $h) {
$hl = strtolower($h);
if ($headers->$hl) {
$list = rcube_mime::decode_address_list($headers->$hl);
foreach ($list as $item) {
if ($item['mailto']) {
$result[] = array($h, $item['mailto']);
}
}
}
}
return $result;
}
}
|