This repository has been archived by the owner on Nov 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
markasjunk2.php
353 lines (293 loc) · 13.4 KB
/
markasjunk2.php
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* MarkAsJunk2
*
* Sample plugin that adds a new button to the mailbox toolbar
* to mark the selected messages as Junk and move them to the Junk folder
* or to move messages in the Junk folder to the inbox - moving only the
* attachment if it is a Spamassassin spam report email
*
* @author Philip Weir
* Based on the Markasjunk plugin by Thomas Bruederli
*
* Copyright (C) 2009-2018 Philip Weir
*
* This program is a Roundcube (https://roundcube.net) plugin.
* For more information see README.md.
* For configuration see config.inc.php.dist.
*
* 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 Roundcube. If not, see https://www.gnu.org/licenses/.
*/
class markasjunk2 extends rcube_plugin
{
public $task = 'mail';
private $spam_mbox = null;
private $ham_mbox = null;
private $flags = array(
'JUNK' => 'Junk',
'NONJUNK' => 'NonJunk'
);
private $toolbar = true;
private $rcube;
public function init()
{
$this->register_action('plugin.markasjunk2.junk', array($this, 'mark_message'));
$this->register_action('plugin.markasjunk2.not_junk', array($this, 'mark_message'));
$this->rcube = rcube::get_instance();
$this->load_config();
$this->_load_host_config();
// Host exceptions
$hosts = $this->rcube->config->get('markasjunk2_allowed_hosts');
if (!empty($hosts) && !in_array($_SESSION['storage_host'], (array) $hosts)) {
return;
}
$this->ham_mbox = $this->rcube->config->get('markasjunk2_ham_mbox', 'INBOX');
$this->spam_mbox = $this->rcube->config->get('markasjunk2_spam_mbox', $this->rcube->config->get('junk_mbox', null));
$this->toolbar = $this->_set_toolbar_display($this->rcube->config->get('markasjunk2_toolbar', -1), $this->rcube->action);
$this->_init_flags();
// integration with Swipe plugin
$this->add_hook('swipe_actions_list', array($this, 'swipe_action'));
if ($this->rcube->action == '' || $this->rcube->action == 'show') {
$this->include_script('markasjunk2.js');
$this->add_texts('localization', true);
$this->include_stylesheet($this->local_skin_path() . '/markasjunk2.css');
if ($this->toolbar) {
// add the buttons to the main toolbar
$this->add_button(array('command' => 'plugin.markasjunk2.junk', 'type' => 'link', 'class' => 'button buttonPas markasjunk2 disabled', 'classact' => 'button markasjunk2', 'classsel' => 'button markasjunk2 pressed', 'title' => 'markasjunk2.buttonjunk', 'innerclass' => 'inner', 'label' => 'junk'), 'toolbar');
$this->add_button(array('command' => 'plugin.markasjunk2.not_junk', 'type' => 'link', 'class' => 'button buttonPas markasnotjunk2 disabled', 'classact' => 'button markasnotjunk2', 'classsel' => 'button markasnotjunk2 pressed', 'title' => 'markasjunk2.buttonnotjunk', 'innerclass' => 'inner', 'label' => 'markasjunk2.notjunk'), 'toolbar');
}
else {
// add the buttons to the mark message menu
$this->add_button(array('command' => 'plugin.markasjunk2.junk', 'type' => 'link-menuitem', 'label' => 'markasjunk2.asjunk', 'id' => 'markasjunk2', 'class' => 'icon markasjunk2', 'classact' => 'icon markasjunk2 active', 'innerclass' => 'icon markasjunk2'), 'markmenu');
$this->add_button(array('command' => 'plugin.markasjunk2.not_junk', 'type' => 'link-menuitem', 'label' => 'markasjunk2.asnotjunk', 'id' => 'markasnotjunk2', 'class' => 'icon markasnotjunk2', 'classact' => 'icon markasnotjunk2 active', 'innerclass' => 'icon markasnotjunk2'), 'markmenu');
}
// add markasjunk2 folder settings to the env for JS
$this->rcube->output->set_env('markasjunk2_ham_mailbox', $this->ham_mbox);
$this->rcube->output->set_env('markasjunk2_spam_mailbox', $this->spam_mbox);
$this->rcube->output->set_env('markasjunk2_move_spam', $this->rcube->config->get('markasjunk2_move_spam', false));
$this->rcube->output->set_env('markasjunk2_move_ham', $this->rcube->config->get('markasjunk2_move_ham', false));
$this->rcube->output->set_env('markasjunk2_permanently_remove', $this->rcube->config->get('markasjunk2_permanently_remove', false));
$this->rcube->output->set_env('markasjunk2_spam_only', $this->rcube->config->get('markasjunk2_spam_only', false));
// check for init method from driver
if ($this->rcube->config->get('markasjunk2_learning_driver', false)) {
$this->_call_driver('init');
}
}
}
public function mark_message()
{
$this->add_texts('localization');
$is_spam = $this->rcube->action == 'plugin.markasjunk2.junk' ? true : false;
$messageset = rcmail::get_uids(null, null, $multifolder, rcube_utils::INPUT_POST);
$mbox_name = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
$dest_mbox = $is_spam ? $this->spam_mbox : $this->ham_mbox;
$result = $is_spam ? $this->_spam($messageset, $dest_mbox) : $this->_ham($messageset, $dest_mbox);
if ($result) {
if ($dest_mbox && ($mbox_name !== $dest_mbox || $multifolder)) {
$this->rcube->output->command('rcmail_markasjunk2_move', $dest_mbox, $this->_messageset_to_uids($messageset, $multifolder));
}
else {
$this->rcube->output->command('command', 'list', $mbox_name);
}
$this->rcube->output->command('display_message', $is_spam ? $this->gettext('reportedasjunk') : $this->gettext('reportedasnotjunk'), 'confirmation');
}
$this->rcube->output->send();
}
public function set_flags($p)
{
$p['message_flags'] = array_merge((array) $p['message_flags'], $this->flags);
return $p;
}
public function swipe_action($p)
{
if ($this->spam_mbox && $p['source'] == 'messagelist' && $p['axis'] == 'horizontal') {
$p['actions']['markasjunk2'] = 'markasjunk2.markasjunk';
return $p;
}
}
private function _set_toolbar_display($display, $action)
{
$ret = true;
// backwards compatibility for old config options (removed in 1.10)
if ($display < 0) {
$mb = $this->rcube->config->get('markasjunk2_mb_toolbar', true);
$cp = $this->rcube->config->get('markasjunk2_cp_toolbar', true);
if ($mb && $cp) {
$display = 1;
}
elseif ($mb && !$cp) {
$display = 2;
}
elseif (!$mb && $cp) {
$display = 3;
}
else {
$display = 0;
}
}
switch ($display) {
case 0: // always show in mark message menu
$ret = false;
break;
case 1: // always show on toolbar
$ret = true;
break;
case 2: // show in toolbar on mailbox screen, show in mark message menu message on screen
$ret = ($action != 'show');
break;
case 3: // show in mark message menu on mailbox screen, show in toolbar message on screen
$ret = ($action == 'show');
break;
}
return $ret;
}
private function _spam(&$messageset, $dest_mbox = null)
{
$storage = $this->rcube->get_storage();
$result = true;
foreach ($messageset as $source_mbox => &$uids) {
$storage->set_folder($source_mbox);
if ($this->rcube->config->get('markasjunk2_learning_driver', false)) {
$result = $this->_call_driver('spam', $uids, $source_mbox, $dest_mbox);
// abort function of the driver says so
if (!$result) {
break;
}
}
if ($this->rcube->config->get('markasjunk2_read_spam', false)) {
$storage->set_flag($uids, 'SEEN', $source_mbox);
}
if (array_key_exists('JUNK', $this->flags)) {
$storage->set_flag($uids, 'JUNK', $source_mbox);
}
if (array_key_exists('NONJUNK', $this->flags)) {
$storage->unset_flag($uids, 'NONJUNK', $source_mbox);
}
}
return $result;
}
private function _ham(&$messageset, $dest_mbox = null)
{
$storage = $this->rcube->get_storage();
$result = true;
foreach ($messageset as $source_mbox => &$uids) {
$storage->set_folder($source_mbox);
if ($this->rcube->config->get('markasjunk2_learning_driver', false)) {
$result = $this->_call_driver('ham', $uids, $source_mbox, $dest_mbox);
// abort function of the driver says so
if (!$result) {
break;
}
}
if ($this->rcube->config->get('markasjunk2_unread_ham', false)) {
$storage->unset_flag($uids, 'SEEN', $source_mbox);
}
if (array_key_exists('JUNK', $this->flags)) {
$storage->unset_flag($uids, 'JUNK', $source_mbox);
}
if (array_key_exists('NONJUNK', $this->flags)) {
$storage->set_flag($uids, 'NONJUNK', $source_mbox);
}
}
return $result;
}
private function _call_driver($action, &$uids = null, $source_mbox = null, $dest_mbox = null)
{
$driver = $this->home . '/drivers/' . $this->rcube->config->get('markasjunk2_learning_driver', 'cmd_learn') . '.php';
$class = 'markasjunk2_' . $this->rcube->config->get('markasjunk2_learning_driver', 'cmd_learn');
if (!is_readable($driver)) {
rcube::raise_error(array(
'code' => 600,
'type' => 'php',
'file' => __FILE__,
'line' => __LINE__,
'message' => "MarkasJunk2 plugin: Unable to open driver file $driver"
), true, false);
}
include_once $driver;
if (!class_exists($class, false) || !method_exists($class, 'spam') || !method_exists($class, 'ham')) {
rcube::raise_error(array(
'code' => 600,
'type' => 'php',
'file' => __FILE__,
'line' => __LINE__,
'message' => "MarkasJunk2 plugin: Broken driver: $driver"
), true, false);
}
// call the relevant function from the driver
$object = new $class();
if ($action == 'spam') {
$object->spam($uids, $source_mbox, $dest_mbox);
}
elseif ($action == 'ham') {
$object->ham($uids, $source_mbox, $dest_mbox);
}
elseif ($action == 'init' && method_exists($object, 'init')) { // method_exists check here for backwards compatibility, init method added 20161127
$object->init();
}
return $object->is_error ? false : true;
}
private function _messageset_to_uids($messageset, $multifolder)
{
$a_uids = array();
foreach ($messageset as $mbox => $uids) {
foreach ($uids as $uid) {
$a_uids[] = $multifolder ? $uid . '-' . $mbox : $uid;
}
}
return $a_uids;
}
private function _load_host_config()
{
$configs = $this->rcube->config->get('markasjunk2_host_config');
if (empty($configs) || !array_key_exists($_SESSION['storage_host'], (array) $configs)) {
return;
}
$file = $configs[$_SESSION['storage_host']];
$this->load_config($file);
}
private function _init_flags()
{
$spam_flag = $this->rcube->config->get('markasjunk2_spam_flag');
$ham_flag = $this->rcube->config->get('markasjunk2_ham_flag');
// backwards compatibility
// defaults for markasjunk2_spam_flag and markasjunk2_ham_flag changed in 1.12
// from now on use false to disable rather than null
if ($spam_flag === null || $ham_flag === null) {
$all_configs = $this->rcube->config->all();
if ($spam_flag === null && array_key_exists('markasjunk2_spam_flag', $all_configs)) {
$spam_flag = false;
}
if ($ham_flag === null && array_key_exists('markasjunk2_ham_flag', $all_configs)) {
$ham_flag = false;
}
}
if ($spam_flag === false) {
unset($this->flags['JUNK']);
}
elseif (!empty($spam_flag)) {
$this->flags['JUNK'] = $spam_flag;
}
if ($ham_flag === false) {
unset($this->flags['NONJUNK']);
}
elseif (!empty($ham_flag)) {
$this->flags['NONJUNK'] = $ham_flag;
}
if (count($this->flags) > 0) {
// register the ham/spam flags with the core
$this->add_hook('storage_init', array($this, 'set_flags'));
}
}
}