|
0
|
1 <?php
|
|
|
2
|
|
|
3 /**
|
|
|
4 * Managesieve (Sieve Filters)
|
|
|
5 *
|
|
|
6 * Plugin that adds a possibility to manage Sieve filters in Thunderbird's style.
|
|
|
7 * It's clickable interface which operates on text scripts and communicates
|
|
|
8 * with server using managesieve protocol. Adds Filters tab in Settings.
|
|
|
9 *
|
|
|
10 * @author Aleksander Machniak <alec@alec.pl>
|
|
|
11 *
|
|
|
12 * Configuration (see config.inc.php.dist)
|
|
|
13 *
|
|
|
14 * Copyright (C) 2008-2013, The Roundcube Dev Team
|
|
|
15 * Copyright (C) 2011-2013, Kolab Systems AG
|
|
|
16 *
|
|
|
17 * This program is free software: you can redistribute it and/or modify
|
|
|
18 * it under the terms of the GNU General Public License as published by
|
|
|
19 * the Free Software Foundation, either version 3 of the License, or
|
|
|
20 * (at your option) any later version.
|
|
|
21 *
|
|
|
22 * This program is distributed in the hope that it will be useful,
|
|
|
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
25 * GNU General Public License for more details.
|
|
|
26 *
|
|
|
27 * You should have received a copy of the GNU General Public License
|
|
|
28 * along with this program. If not, see http://www.gnu.org/licenses/.
|
|
|
29 */
|
|
|
30
|
|
|
31 class managesieve extends rcube_plugin
|
|
|
32 {
|
|
|
33 public $task = 'mail|settings';
|
|
|
34 private $rc;
|
|
|
35 private $engine;
|
|
58
|
36 private $ui_initialized;
|
|
0
|
37
|
|
|
38 function init()
|
|
|
39 {
|
|
|
40 $this->rc = rcube::get_instance();
|
|
|
41
|
|
|
42 // register actions
|
|
|
43 $this->register_action('plugin.managesieve', array($this, 'managesieve_actions'));
|
|
|
44 $this->register_action('plugin.managesieve-action', array($this, 'managesieve_actions'));
|
|
|
45 $this->register_action('plugin.managesieve-vacation', array($this, 'managesieve_actions'));
|
|
|
46 $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
|
|
|
47 $this->register_action('plugin.managesieve-saveraw', array($this, 'managesieve_saveraw'));
|
|
|
48
|
|
|
49 if ($this->rc->task == 'settings') {
|
|
|
50 $this->add_hook('settings_actions', array($this, 'settings_actions'));
|
|
|
51 $this->init_ui();
|
|
|
52 }
|
|
|
53 else if ($this->rc->task == 'mail') {
|
|
|
54 // register message hook
|
|
|
55 if ($this->rc->action == 'show') {
|
|
|
56 $this->add_hook('message_headers_output', array($this, 'mail_headers'));
|
|
|
57 }
|
|
|
58
|
|
|
59 // inject Create Filter popup stuff
|
|
|
60 if (empty($this->rc->action) || $this->rc->action == 'show'
|
|
|
61 || strpos($this->rc->action, 'plugin.managesieve') === 0
|
|
|
62 ) {
|
|
|
63 $this->mail_task_handler();
|
|
|
64 }
|
|
|
65 }
|
|
|
66 }
|
|
|
67
|
|
|
68 /**
|
|
|
69 * Initializes plugin's UI (localization, js script)
|
|
|
70 */
|
|
|
71 function init_ui()
|
|
|
72 {
|
|
|
73 if ($this->ui_initialized) {
|
|
|
74 return;
|
|
|
75 }
|
|
|
76
|
|
|
77 // load localization
|
|
|
78 $this->add_texts('localization/');
|
|
|
79
|
|
|
80 $sieve_action = strpos($this->rc->action, 'plugin.managesieve') === 0;
|
|
|
81
|
|
|
82 if ($this->rc->task == 'mail' || $sieve_action) {
|
|
|
83 $this->include_script('managesieve.js');
|
|
|
84 }
|
|
|
85
|
|
|
86 // include styles
|
|
|
87 $skin_path = $this->local_skin_path();
|
|
|
88 if ($sieve_action || ($this->rc->task == 'settings' && empty($_REQUEST['_framed']))) {
|
|
|
89 $this->include_stylesheet("$skin_path/managesieve.css");
|
|
|
90 }
|
|
|
91 else if ($this->rc->task == 'mail') {
|
|
|
92 $this->include_stylesheet("$skin_path/managesieve_mail.css");
|
|
|
93 }
|
|
|
94
|
|
|
95 $this->ui_initialized = true;
|
|
|
96 }
|
|
|
97
|
|
|
98 /**
|
|
|
99 * Adds Filters section in Settings
|
|
|
100 */
|
|
|
101 function settings_actions($args)
|
|
|
102 {
|
|
|
103 $this->load_config();
|
|
|
104
|
|
|
105 $vacation_mode = (int) $this->rc->config->get('managesieve_vacation');
|
|
|
106
|
|
|
107 // register Filters action
|
|
|
108 if ($vacation_mode != 2) {
|
|
|
109 $args['actions'][] = array(
|
|
|
110 'action' => 'plugin.managesieve',
|
|
|
111 'class' => 'filter',
|
|
|
112 'label' => 'filters',
|
|
|
113 'domain' => 'managesieve',
|
|
|
114 'title' => 'filterstitle',
|
|
|
115 );
|
|
|
116 }
|
|
|
117
|
|
|
118 // register Vacation action
|
|
|
119 if ($vacation_mode > 0) {
|
|
|
120 $args['actions'][] = array(
|
|
|
121 'action' => 'plugin.managesieve-vacation',
|
|
|
122 'class' => 'vacation',
|
|
|
123 'label' => 'vacation',
|
|
|
124 'domain' => 'managesieve',
|
|
|
125 'title' => 'vacationtitle',
|
|
|
126 );
|
|
|
127 }
|
|
|
128
|
|
|
129 return $args;
|
|
|
130 }
|
|
|
131
|
|
|
132 /**
|
|
|
133 * Add UI elements to the 'mailbox view' and 'show message' UI.
|
|
|
134 */
|
|
|
135 function mail_task_handler()
|
|
|
136 {
|
|
|
137 // make sure we're not in ajax request
|
|
|
138 if ($this->rc->output->type != 'html') {
|
|
|
139 return;
|
|
|
140 }
|
|
|
141
|
|
|
142 // use jQuery for popup window
|
|
|
143 $this->require_plugin('jqueryui');
|
|
|
144
|
|
|
145 // include js script and localization
|
|
|
146 $this->init_ui();
|
|
|
147
|
|
|
148 // add 'Create filter' item to message menu
|
|
|
149 $this->api->add_content(html::tag('li', null,
|
|
|
150 $this->api->output->button(array(
|
|
|
151 'command' => 'managesieve-create',
|
|
|
152 'label' => 'managesieve.filtercreate',
|
|
|
153 'type' => 'link',
|
|
|
154 'classact' => 'icon filterlink active',
|
|
|
155 'class' => 'icon filterlink',
|
|
|
156 'innerclass' => 'icon filterlink',
|
|
|
157 ))), 'messagemenu');
|
|
|
158
|
|
|
159 // register some labels/messages
|
|
|
160 $this->rc->output->add_label('managesieve.newfilter', 'managesieve.usedata',
|
|
|
161 'managesieve.nodata', 'managesieve.nextstep', 'save');
|
|
|
162
|
|
|
163 $this->rc->session->remove('managesieve_current');
|
|
|
164 }
|
|
|
165
|
|
|
166 /**
|
|
|
167 * Get message headers for popup window
|
|
|
168 */
|
|
|
169 function mail_headers($args)
|
|
|
170 {
|
|
|
171 // this hook can be executed many times
|
|
|
172 if ($this->mail_headers_done) {
|
|
|
173 return $args;
|
|
|
174 }
|
|
|
175
|
|
|
176 $this->mail_headers_done = true;
|
|
|
177
|
|
|
178 $headers = $this->parse_headers($args['headers']);
|
|
|
179
|
|
|
180 if ($this->rc->action == 'preview')
|
|
|
181 $this->rc->output->command('parent.set_env', array('sieve_headers' => $headers));
|
|
|
182 else
|
|
|
183 $this->rc->output->set_env('sieve_headers', $headers);
|
|
|
184
|
|
|
185 return $args;
|
|
|
186 }
|
|
|
187
|
|
|
188 /**
|
|
|
189 * Plugin action handler
|
|
|
190 */
|
|
|
191 function managesieve_actions()
|
|
|
192 {
|
|
|
193 // handle fetching email headers for the new filter form
|
|
|
194 if ($uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST)) {
|
|
|
195 $uids = rcmail::get_uids();
|
|
|
196 $mailbox = key($uids);
|
|
|
197 $message = new rcube_message($uids[$mailbox][0], $mailbox);
|
|
|
198 $headers = $this->parse_headers($message->headers);
|
|
|
199
|
|
|
200 $this->rc->output->set_env('sieve_headers', $headers);
|
|
|
201 $this->rc->output->command('managesieve_create', true);
|
|
|
202 $this->rc->output->send();
|
|
|
203 }
|
|
|
204
|
|
|
205 // handle other actions
|
|
|
206 $engine_type = $this->rc->action == 'plugin.managesieve-vacation' ? 'vacation' : '';
|
|
|
207 $engine = $this->get_engine($engine_type);
|
|
|
208
|
|
|
209 $this->init_ui();
|
|
|
210 $engine->actions();
|
|
|
211 }
|
|
|
212
|
|
|
213 /**
|
|
|
214 * Forms save action handler
|
|
|
215 */
|
|
|
216 function managesieve_save()
|
|
|
217 {
|
|
|
218 // load localization
|
|
|
219 $this->add_texts('localization/', array('filters','managefilters'));
|
|
|
220
|
|
|
221 // include main js script
|
|
|
222 if ($this->api->output->type == 'html') {
|
|
|
223 $this->include_script('managesieve.js');
|
|
|
224 }
|
|
|
225
|
|
|
226 $engine = $this->get_engine();
|
|
|
227 $engine->save();
|
|
|
228 }
|
|
|
229
|
|
|
230 /**
|
|
|
231 * Raw form save action handler
|
|
|
232 */
|
|
|
233 function managesieve_saveraw()
|
|
|
234 {
|
|
|
235 $engine = $this->get_engine();
|
|
|
236
|
|
|
237 if (!$this->rc->config->get('managesieve_raw_editor', true)) {
|
|
|
238 return;
|
|
|
239 }
|
|
|
240
|
|
|
241 // load localization
|
|
|
242 $this->add_texts('localization/', array('filters','managefilters'));
|
|
|
243
|
|
|
244 $engine->saveraw();
|
|
|
245 }
|
|
|
246
|
|
|
247 /**
|
|
|
248 * Initializes engine object
|
|
|
249 */
|
|
|
250 public function get_engine($type = null)
|
|
|
251 {
|
|
|
252 if (!$this->engine) {
|
|
|
253 $this->load_config();
|
|
|
254
|
|
|
255 // Add include path for internal classes
|
|
|
256 $include_path = $this->home . '/lib' . PATH_SEPARATOR;
|
|
|
257 $include_path .= ini_get('include_path');
|
|
|
258 set_include_path($include_path);
|
|
|
259
|
|
|
260 $class_name = 'rcube_sieve_' . ($type ?: 'engine');
|
|
|
261 $this->engine = new $class_name($this);
|
|
|
262 }
|
|
|
263
|
|
|
264 return $this->engine;
|
|
|
265 }
|
|
|
266
|
|
|
267 /**
|
|
|
268 * Extract mail headers for new filter form
|
|
|
269 */
|
|
|
270 private function parse_headers($headers)
|
|
|
271 {
|
|
|
272 $result = array();
|
|
|
273
|
|
|
274 if ($headers->subject)
|
|
|
275 $result[] = array('Subject', rcube_mime::decode_header($headers->subject));
|
|
|
276
|
|
|
277 // @TODO: List-Id, others?
|
|
|
278 foreach (array('From', 'To') as $h) {
|
|
|
279 $hl = strtolower($h);
|
|
|
280 if ($headers->$hl) {
|
|
|
281 $list = rcube_mime::decode_address_list($headers->$hl);
|
|
|
282 foreach ($list as $item) {
|
|
|
283 if ($item['mailto']) {
|
|
|
284 $result[] = array($h, $item['mailto']);
|
|
|
285 }
|
|
|
286 }
|
|
|
287 }
|
|
|
288 }
|
|
|
289
|
|
|
290 return $result;
|
|
|
291 }
|
|
|
292 }
|