You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

newmail_notifier.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * New Mail Notifier plugin
  4. *
  5. * Supports three methods of notification:
  6. * 1. Basic - focus browser window and change favicon
  7. * 2. Sound - play wav file
  8. * 3. Desktop - display desktop notification (using window.Notification API)
  9. *
  10. * @version @package_version@
  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. class newmail_notifier extends rcube_plugin
  29. {
  30. public $task = 'mail|settings';
  31. private $rc;
  32. private $notified;
  33. private $opt = array();
  34. private $exceptions = array();
  35. /**
  36. * Plugin initialization
  37. */
  38. function init()
  39. {
  40. $this->rc = rcmail::get_instance();
  41. // Preferences hooks
  42. if ($this->rc->task == 'settings') {
  43. $this->add_hook('preferences_list', array($this, 'prefs_list'));
  44. $this->add_hook('preferences_save', array($this, 'prefs_save'));
  45. }
  46. else { // if ($this->rc->task == 'mail') {
  47. // add script when not in ajax and not in frame and only in main window
  48. if ($this->rc->output->type == 'html' && empty($_REQUEST['_framed']) && $this->rc->action == '') {
  49. $this->add_texts('localization/');
  50. $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.body');
  51. $this->include_script('newmail_notifier.js');
  52. }
  53. if ($this->rc->action == 'refresh') {
  54. // Load configuration
  55. $this->load_config();
  56. $this->opt['basic'] = $this->rc->config->get('newmail_notifier_basic');
  57. $this->opt['sound'] = $this->rc->config->get('newmail_notifier_sound');
  58. $this->opt['desktop'] = $this->rc->config->get('newmail_notifier_desktop');
  59. if (!empty($this->opt)) {
  60. // Get folders to skip checking for
  61. $exceptions = array('drafts_mbox', 'sent_mbox', 'trash_mbox');
  62. foreach ($exceptions as $folder) {
  63. $folder = $this->rc->config->get($folder);
  64. if (strlen($folder) && $folder != 'INBOX') {
  65. $this->exceptions[] = $folder;
  66. }
  67. }
  68. $this->add_hook('new_messages', array($this, 'notify'));
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Handler for user preferences form (preferences_list hook)
  75. */
  76. function prefs_list($args)
  77. {
  78. if ($args['section'] != 'mailbox') {
  79. return $args;
  80. }
  81. // Load configuration
  82. $this->load_config();
  83. // Load localization and configuration
  84. $this->add_texts('localization/');
  85. if (!empty($_REQUEST['_framed'])) {
  86. $this->rc->output->add_label('newmail_notifier.title', 'newmail_notifier.testbody',
  87. 'newmail_notifier.desktopunsupported', 'newmail_notifier.desktopenabled', 'newmail_notifier.desktopdisabled');
  88. $this->include_script('newmail_notifier.js');
  89. }
  90. // Check that configuration is not disabled
  91. $dont_override = (array) $this->rc->config->get('dont_override', array());
  92. foreach (array('basic', 'desktop', 'sound') as $type) {
  93. $key = 'newmail_notifier_' . $type;
  94. if (!in_array($key, $dont_override)) {
  95. $field_id = '_' . $key;
  96. $input = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
  97. $content = $input->show($this->rc->config->get($key))
  98. . ' ' . html::a(array('href' => '#', 'onclick' => 'newmail_notifier_test_'.$type.'()'),
  99. $this->gettext('test'));
  100. $args['blocks']['new_message']['options'][$key] = array(
  101. 'title' => html::label($field_id, rcube::Q($this->gettext($type))),
  102. 'content' => $content
  103. );
  104. }
  105. }
  106. $type = 'desktop_timeout';
  107. $key = 'newmail_notifier_' . $type;
  108. if (!in_array($key, $dont_override)) {
  109. $field_id = '_' . $key;
  110. $select = new html_select(array('name' => $field_id, 'id' => $field_id));
  111. foreach (array(5, 10, 15, 30, 45, 60) as $sec) {
  112. $label = $this->rc->gettext(array('name' => 'afternseconds', 'vars' => array('n' => $sec)));
  113. $select->add($label, $sec);
  114. }
  115. $args['blocks']['new_message']['options'][$key] = array(
  116. 'title' => html::label($field_id, rcube::Q($this->gettext('desktoptimeout'))),
  117. 'content' => $select->show((int) $this->rc->config->get($key))
  118. );
  119. }
  120. return $args;
  121. }
  122. /**
  123. * Handler for user preferences save (preferences_save hook)
  124. */
  125. function prefs_save($args)
  126. {
  127. if ($args['section'] != 'mailbox') {
  128. return $args;
  129. }
  130. // Load configuration
  131. $this->load_config();
  132. // Check that configuration is not disabled
  133. $dont_override = (array) $this->rc->config->get('dont_override', array());
  134. foreach (array('basic', 'desktop', 'sound') as $type) {
  135. $key = 'newmail_notifier_' . $type;
  136. if (!in_array($key, $dont_override)) {
  137. $args['prefs'][$key] = rcube_utils::get_input_value('_' . $key, rcube_utils::INPUT_POST) ? true : false;
  138. }
  139. }
  140. $option = 'newmail_notifier_desktop_timeout';
  141. if (!in_array($option, $dont_override)) {
  142. if ($value = (int) rcube_utils::get_input_value('_' . $option, rcube_utils::INPUT_POST)) {
  143. $args['prefs'][$option] = $value;
  144. }
  145. }
  146. return $args;
  147. }
  148. /**
  149. * Handler for new message action (new_messages hook)
  150. */
  151. function notify($args)
  152. {
  153. // Already notified or unexpected input
  154. if ($this->notified || empty($args['diff']['new'])) {
  155. return $args;
  156. }
  157. $mbox = $args['mailbox'];
  158. $storage = $this->rc->get_storage();
  159. $delimiter = $storage->get_hierarchy_delimiter();
  160. // Skip exception (sent/drafts) folders (and their subfolders)
  161. foreach ($this->exceptions as $folder) {
  162. if (strpos($mbox.$delimiter, $folder.$delimiter) === 0) {
  163. return $args;
  164. }
  165. }
  166. // Check if any of new messages is UNSEEN
  167. $deleted = $this->rc->config->get('skip_deleted') ? 'UNDELETED ' : '';
  168. $search = $deleted . 'UNSEEN UID ' . $args['diff']['new'];
  169. $unseen = $storage->search_once($mbox, $search);
  170. if ($unseen->count()) {
  171. $this->notified = true;
  172. $this->rc->output->set_env('newmail_notifier_timeout', $this->rc->config->get('newmail_notifier_desktop_timeout'));
  173. $this->rc->output->command('plugin.newmail_notifier',
  174. array(
  175. 'basic' => $this->opt['basic'],
  176. 'sound' => $this->opt['sound'],
  177. 'desktop' => $this->opt['desktop'],
  178. ));
  179. }
  180. return $args;
  181. }
  182. }