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

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