comparison plugins/newmail_notifier/newmail_notifier.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /**
4 * New Mail Notifier plugin
5 *
6 * Supports three methods of notification:
7 * 1. Basic - focus browser window and change favicon
8 * 2. Sound - play wav file
9 * 3. Desktop - display desktop notification (using window.Notification API)
10 *
11 * @author Aleksander Machniak <alec@alec.pl>
12 *
13 * Copyright (C) 2011-2016, Kolab Systems AG
14 *
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see http://www.gnu.org/licenses/.
27 */
28
29 class newmail_notifier extends rcube_plugin
30 {
31 public $task = 'mail|settings';
32
33 private $rc;
34 private $notified;
35 private $opt = array();
36 private $exceptions = array();
37
38
39 /**
40 * Plugin initialization
41 */
42 function init()
43 {
44 $this->rc = rcmail::get_instance();
45
46 // Preferences hooks
47 if ($this->rc->task == 'settings') {
48 $this->add_hook('preferences_list', array($this, 'prefs_list'));
49 $this->add_hook('preferences_save', array($this, 'prefs_save'));
50 }
51 else { // if ($this->rc->task == 'mail') {
52 // add script when not in ajax and not in frame and only in main window
53 if ($this->rc->output->type == 'html' && empty($_REQUEST['_framed']) && $this->rc->action == '') {
54 $this->add_texts('localization/');
55 $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.body');
56 $this->include_script('newmail_notifier.js');
57 }
58
59 if ($this->rc->action == 'refresh') {
60 // Load configuration
61 $this->load_config();
62
63 $this->opt['basic'] = $this->rc->config->get('newmail_notifier_basic');
64 $this->opt['sound'] = $this->rc->config->get('newmail_notifier_sound');
65 $this->opt['desktop'] = $this->rc->config->get('newmail_notifier_desktop');
66
67 if (!empty($this->opt)) {
68 // Get folders to skip checking for
69 $exceptions = array('drafts_mbox', 'sent_mbox', 'trash_mbox');
70 foreach ($exceptions as $folder) {
71 $folder = $this->rc->config->get($folder);
72 if (strlen($folder) && $folder != 'INBOX') {
73 $this->exceptions[] = $folder;
74 }
75 }
76
77 $this->add_hook('new_messages', array($this, 'notify'));
78 }
79 }
80 }
81 }
82
83 /**
84 * Handler for user preferences form (preferences_list hook)
85 */
86 function prefs_list($args)
87 {
88 if ($args['section'] != 'mailbox') {
89 return $args;
90 }
91
92 // Load configuration
93 $this->load_config();
94
95 // Load localization and configuration
96 $this->add_texts('localization/');
97
98 if (!empty($_REQUEST['_framed'])) {
99 $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.testbody',
100 'newmail_notifier.desktopunsupported', 'newmail_notifier.desktopenabled', 'newmail_notifier.desktopdisabled');
101 $this->include_script('newmail_notifier.js');
102 }
103
104 // Check that configuration is not disabled
105 $dont_override = (array) $this->rc->config->get('dont_override', array());
106
107 foreach (array('basic', 'desktop', 'sound') as $type) {
108 $key = 'newmail_notifier_' . $type;
109 if (!in_array($key, $dont_override)) {
110 $field_id = '_' . $key;
111 $input = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
112 $content = $input->show($this->rc->config->get($key))
113 . ' ' . html::a(array('href' => '#', 'onclick' => 'newmail_notifier_test_'.$type.'()'),
114 $this->gettext('test'));
115
116 $args['blocks']['new_message']['options'][$key] = array(
117 'title' => html::label($field_id, rcube::Q($this->gettext($type))),
118 'content' => $content
119 );
120 }
121 }
122
123 $type = 'desktop_timeout';
124 $key = 'newmail_notifier_' . $type;
125 if (!in_array($key, $dont_override)) {
126 $field_id = '_' . $key;
127 $select = new html_select(array('name' => $field_id, 'id' => $field_id));
128
129 foreach (array(5, 10, 15, 30, 45, 60) as $sec) {
130 $label = $this->rc->gettext(array('name' => 'afternseconds', 'vars' => array('n' => $sec)));
131 $select->add($label, $sec);
132 }
133
134 $args['blocks']['new_message']['options'][$key] = array(
135 'title' => html::label($field_id, rcube::Q($this->gettext('desktoptimeout'))),
136 'content' => $select->show((int) $this->rc->config->get($key))
137 );
138 }
139
140 return $args;
141 }
142
143 /**
144 * Handler for user preferences save (preferences_save hook)
145 */
146 function prefs_save($args)
147 {
148 if ($args['section'] != 'mailbox') {
149 return $args;
150 }
151
152 // Load configuration
153 $this->load_config();
154
155 // Check that configuration is not disabled
156 $dont_override = (array) $this->rc->config->get('dont_override', array());
157
158 foreach (array('basic', 'desktop', 'sound') as $type) {
159 $key = 'newmail_notifier_' . $type;
160 if (!in_array($key, $dont_override)) {
161 $args['prefs'][$key] = rcube_utils::get_input_value('_' . $key, rcube_utils::INPUT_POST) ? true : false;
162 }
163 }
164
165 $option = 'newmail_notifier_desktop_timeout';
166 if (!in_array($option, $dont_override)) {
167 if ($value = (int) rcube_utils::get_input_value('_' . $option, rcube_utils::INPUT_POST)) {
168 $args['prefs'][$option] = $value;
169 }
170 }
171
172 return $args;
173 }
174
175 /**
176 * Handler for new message action (new_messages hook)
177 */
178 function notify($args)
179 {
180 // Already notified or unexpected input
181 if ($this->notified || empty($args['diff']['new'])) {
182 return $args;
183 }
184
185 $mbox = $args['mailbox'];
186 $storage = $this->rc->get_storage();
187 $delimiter = $storage->get_hierarchy_delimiter();
188
189 // Skip exception (sent/drafts) folders (and their subfolders)
190 foreach ($this->exceptions as $folder) {
191 if (strpos($mbox.$delimiter, $folder.$delimiter) === 0) {
192 return $args;
193 }
194 }
195
196 // Check if any of new messages is UNSEEN
197 $deleted = $this->rc->config->get('skip_deleted') ? 'UNDELETED ' : '';
198 $search = $deleted . 'UNSEEN UID ' . $args['diff']['new'];
199 $unseen = $storage->search_once($mbox, $search);
200
201 if ($unseen->count()) {
202 $this->notified = true;
203
204 $this->rc->output->set_env('newmail_notifier_timeout', $this->rc->config->get('newmail_notifier_desktop_timeout'));
205 $this->rc->output->command('plugin.newmail_notifier',
206 array(
207 'basic' => $this->opt['basic'],
208 'sound' => $this->opt['sound'],
209 'desktop' => $this->opt['desktop'],
210 ));
211 }
212
213 return $args;
214 }
215 }