diff options
author | Hugues Hiegel <root@paranoid> | 2015-04-21 12:49:44 +0200 |
---|---|---|
committer | Hugues Hiegel <root@paranoid> | 2015-04-21 12:49:44 +0200 |
commit | 733f8e8d0ce6217d906d06dc4fb08e36d48ed794 (patch) | |
tree | cff28366ff63ea6596f8026e1698090bd0b9405c /plugins/newmail_notifier/newmail_notifier.php | |
parent | ef2e7b3f9d264ec146d4dae257b1e295ab3b462a (diff) | |
parent | a4ba3df54834ee90fb2c9930669f1229dc80261a (diff) |
Conflicts:
composer.json-dist
config/defaults.inc.php
plugins
plugins/acl/acl.js
plugins/acl/acl.php
plugins/acl/skins/classic/templates/table.html
plugins/acl/skins/larry/templates/table.html
plugins/enigma/README
plugins/enigma/config.inc.php.dist
plugins/enigma/enigma.js
plugins/enigma/enigma.php
plugins/enigma/lib/enigma_driver.php
plugins/enigma/lib/enigma_driver_gnupg.php
plugins/enigma/lib/enigma_driver_phpssl.php
plugins/enigma/lib/enigma_engine.php
plugins/enigma/lib/enigma_error.php
plugins/enigma/lib/enigma_key.php
plugins/enigma/lib/enigma_signature.php
plugins/enigma/lib/enigma_subkey.php
plugins/enigma/lib/enigma_ui.php
plugins/enigma/lib/enigma_userid.php
plugins/enigma/localization/en_US.inc
plugins/enigma/localization/ja_JP.inc
plugins/enigma/localization/ru_RU.inc
plugins/enigma/skins/classic/enigma.css
plugins/enigma/skins/classic/templates/keys.html
plugins/help/config.inc.php.dist
plugins/help/help.php
plugins/help/localization/en_US.inc
plugins/jqueryui/jqueryui.php
plugins/managesieve/Changelog
plugins/managesieve/composer.json
plugins/managesieve/config.inc.php.dist
plugins/managesieve/lib/Roundcube/rcube_sieve.php
plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php
plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php
plugins/managesieve/localization/en_US.inc
plugins/managesieve/managesieve.js
plugins/managesieve/skins/classic/managesieve.css
plugins/managesieve/skins/larry/managesieve.css
plugins/password/README
plugins/password/config.inc.php.dist
plugins/password/drivers/ldap.php
plugins/password/drivers/poppassd.php
plugins/password/drivers/vpopmaild.php
plugins/vcard_attachments/vcardattach.js
plugins/zipdownload/zipdownload.php
Diffstat (limited to 'plugins/newmail_notifier/newmail_notifier.php')
-rw-r--r-- | plugins/newmail_notifier/newmail_notifier.php | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/plugins/newmail_notifier/newmail_notifier.php b/plugins/newmail_notifier/newmail_notifier.php new file mode 100644 index 000000000..efac69116 --- /dev/null +++ b/plugins/newmail_notifier/newmail_notifier.php @@ -0,0 +1,218 @@ +<?php + +/** + * New Mail Notifier plugin + * + * Supports three methods of notification: + * 1. Basic - focus browser window and change favicon + * 2. Sound - play wav file + * 3. Desktop - display desktop notification (using webkitNotifications feature, + * supported by Chrome and Firefox with 'HTML5 Notifications' plugin) + * + * @version @package_version@ + * @author Aleksander Machniak <alec@alec.pl> + * + * + * Copyright (C) 2011, 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 newmail_notifier extends rcube_plugin +{ + public $task = 'mail|settings'; + + private $rc; + private $notified; + private $opt = array(); + private $exceptions = array(); + + + /** + * Plugin initialization + */ + function init() + { + $this->rc = rcmail::get_instance(); + + // Preferences hooks + if ($this->rc->task == 'settings') { + $this->add_hook('preferences_list', array($this, 'prefs_list')); + $this->add_hook('preferences_save', array($this, 'prefs_save')); + } + else { // if ($this->rc->task == 'mail') { + // add script when not in ajax and not in frame and only in main window + if ($this->rc->output->type == 'html' && empty($_REQUEST['_framed']) && $this->rc->action == '') { + $this->add_texts('localization/'); + $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.body'); + $this->include_script('newmail_notifier.js'); + } + + if ($this->rc->action == 'refresh') { + // Load configuration + $this->load_config(); + + $this->opt['basic'] = $this->rc->config->get('newmail_notifier_basic'); + $this->opt['sound'] = $this->rc->config->get('newmail_notifier_sound'); + $this->opt['desktop'] = $this->rc->config->get('newmail_notifier_desktop'); + + if (!empty($this->opt)) { + // Get folders to skip checking for + $exceptions = array('drafts_mbox', 'sent_mbox', 'trash_mbox'); + foreach ($exceptions as $folder) { + $folder = $this->rc->config->get($folder); + if (strlen($folder) && $folder != 'INBOX') { + $this->exceptions[] = $folder; + } + } + + $this->add_hook('new_messages', array($this, 'notify')); + } + } + } + } + + /** + * Handler for user preferences form (preferences_list hook) + */ + function prefs_list($args) + { + if ($args['section'] != 'mailbox') { + return $args; + } + + // Load configuration + $this->load_config(); + + // Load localization and configuration + $this->add_texts('localization/'); + + if (!empty($_REQUEST['_framed'])) { + $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.testbody', + 'newmail_notifier.desktopunsupported', 'newmail_notifier.desktopenabled', 'newmail_notifier.desktopdisabled'); + $this->include_script('newmail_notifier.js'); + } + + // Check that configuration is not disabled + $dont_override = (array) $this->rc->config->get('dont_override', array()); + + foreach (array('basic', 'desktop', 'sound') as $type) { + $key = 'newmail_notifier_' . $type; + if (!in_array($key, $dont_override)) { + $field_id = '_' . $key; + $input = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1)); + $content = $input->show($this->rc->config->get($key)) + . ' ' . html::a(array('href' => '#', 'onclick' => 'newmail_notifier_test_'.$type.'()'), + $this->gettext('test')); + + $args['blocks']['new_message']['options'][$key] = array( + 'title' => html::label($field_id, rcube::Q($this->gettext($type))), + 'content' => $content + ); + } + } + + $type = 'desktop_timeout'; + $key = 'newmail_notifier_' . $type; + if (!in_array($key, $dont_override)) { + $field_id = '_' . $key; + $select = new html_select(array('name' => $field_id, 'id' => $field_id)); + + foreach (array(5, 10, 15, 30, 45, 60) as $sec) { + $label = $this->rc->gettext(array('name' => 'afternseconds', 'vars' => array('n' => $sec))); + $select->add($label, $sec); + } + + $args['blocks']['new_message']['options'][$key] = array( + 'title' => html::label($field_id, rcube::Q($this->gettext('desktoptimeout'))), + 'content' => $select->show((int) $this->rc->config->get($key)) + ); + } + + return $args; + } + + /** + * Handler for user preferences save (preferences_save hook) + */ + function prefs_save($args) + { + if ($args['section'] != 'mailbox') { + return $args; + } + + // Load configuration + $this->load_config(); + + // Check that configuration is not disabled + $dont_override = (array) $this->rc->config->get('dont_override', array()); + + foreach (array('basic', 'desktop', 'sound') as $type) { + $key = 'newmail_notifier_' . $type; + if (!in_array($key, $dont_override)) { + $args['prefs'][$key] = rcube_utils::get_input_value('_'.$key, rcube_utils::INPUT_POST) ? true : false; + } + } + + $option = 'newmail_notifier_desktop_timeout'; + if (!in_array($option, $dont_override)) { + if ($value = (int) rcube_utils::get_input_value('_' . $option, rcube_utils::INPUT_POST)) { + $args['prefs'][$option] = $value; + } + } + + return $args; + } + + /** + * Handler for new message action (new_messages hook) + */ + function notify($args) + { + // Already notified or unexpected input + if ($this->notified || empty($args['diff']['new'])) { + return $args; + } + + $mbox = $args['mailbox']; + $storage = $this->rc->get_storage(); + $delimiter = $storage->get_hierarchy_delimiter(); + + // Skip exception (sent/drafts) folders (and their subfolders) + foreach ($this->exceptions as $folder) { + if (strpos($mbox.$delimiter, $folder.$delimiter) === 0) { + return $args; + } + } + + // Check if any of new messages is UNSEEN + $deleted = $this->rc->config->get('skip_deleted') ? 'UNDELETED ' : ''; + $search = $deleted . 'UNSEEN UID ' . $args['diff']['new']; + $unseen = $storage->search_once($mbox, $search); + + if ($unseen->count()) { + $this->notified = true; + + $this->rc->output->set_env('newmail_notifier_timeout', $this->rc->config->get('newmail_notifier_desktop_timeout')); + $this->rc->output->command('plugin.newmail_notifier', + array( + 'basic' => $this->opt['basic'], + 'sound' => $this->opt['sound'], + 'desktop' => $this->opt['desktop'], + )); + } + + return $args; + } +} |