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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Managesieve (Sieve Filters)
  4. *
  5. * Plugin that adds a possibility to manage Sieve filters in Thunderbird's style.
  6. * It's clickable interface which operates on text scripts and communicates
  7. * with server using managesieve protocol. Adds Filters tab in Settings.
  8. *
  9. * @version @package_version@
  10. * @author Aleksander Machniak <alec@alec.pl>
  11. *
  12. * Configuration (see config.inc.php.dist)
  13. *
  14. * Copyright (C) 2008-2013, The Roundcube Dev Team
  15. * Copyright (C) 2011-2013, Kolab Systems AG
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see http://www.gnu.org/licenses/.
  29. */
  30. class managesieve extends rcube_plugin
  31. {
  32. public $task = 'mail|settings';
  33. private $rc;
  34. private $engine;
  35. function init()
  36. {
  37. $this->rc = rcube::get_instance();
  38. // register actions
  39. $this->register_action('plugin.managesieve', array($this, 'managesieve_actions'));
  40. $this->register_action('plugin.managesieve-action', array($this, 'managesieve_actions'));
  41. $this->register_action('plugin.managesieve-vacation', array($this, 'managesieve_actions'));
  42. $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
  43. if ($this->rc->task == 'settings') {
  44. $this->add_hook('settings_actions', array($this, 'settings_actions'));
  45. $this->init_ui();
  46. }
  47. else if ($this->rc->task == 'mail') {
  48. // register message hook
  49. if ($this->rc->action == 'show') {
  50. $this->add_hook('message_headers_output', array($this, 'mail_headers'));
  51. }
  52. // inject Create Filter popup stuff
  53. if (empty($this->rc->action) || $this->rc->action == 'show'
  54. || strpos($this->rc->action, 'plugin.managesieve') === 0
  55. ) {
  56. $this->mail_task_handler();
  57. }
  58. }
  59. }
  60. /**
  61. * Initializes plugin's UI (localization, js script)
  62. */
  63. function init_ui()
  64. {
  65. if ($this->ui_initialized) {
  66. return;
  67. }
  68. // load localization
  69. $this->add_texts('localization/');
  70. $sieve_action = strpos($this->rc->action, 'plugin.managesieve') === 0;
  71. if ($this->rc->task == 'mail' || $sieve_action) {
  72. $this->include_script('managesieve.js');
  73. }
  74. // include styles
  75. $skin_path = $this->local_skin_path();
  76. if ($sieve_action || ($this->rc->task == 'settings' && empty($_REQUEST['_framed']))) {
  77. $this->include_stylesheet("$skin_path/managesieve.css");
  78. }
  79. else if ($this->rc->task == 'mail') {
  80. $this->include_stylesheet("$skin_path/managesieve_mail.css");
  81. }
  82. $this->ui_initialized = true;
  83. }
  84. /**
  85. * Adds Filters section in Settings
  86. */
  87. function settings_actions($args)
  88. {
  89. $this->load_config();
  90. $vacation_mode = (int) $this->rc->config->get('managesieve_vacation');
  91. // register Filters action
  92. if ($vacation_mode != 2) {
  93. $args['actions'][] = array(
  94. 'action' => 'plugin.managesieve',
  95. 'class' => 'filter',
  96. 'label' => 'filters',
  97. 'domain' => 'managesieve',
  98. 'title' => 'filterstitle',
  99. );
  100. }
  101. // register Vacation action
  102. if ($vacation_mode > 0) {
  103. $args['actions'][] = array(
  104. 'action' => 'plugin.managesieve-vacation',
  105. 'class' => 'vacation',
  106. 'label' => 'vacation',
  107. 'domain' => 'managesieve',
  108. 'title' => 'vacationtitle',
  109. );
  110. }
  111. return $args;
  112. }
  113. /**
  114. * Add UI elements to the 'mailbox view' and 'show message' UI.
  115. */
  116. function mail_task_handler()
  117. {
  118. // make sure we're not in ajax request
  119. if ($this->rc->output->type != 'html') {
  120. return;
  121. }
  122. // use jQuery for popup window
  123. $this->require_plugin('jqueryui');
  124. // include js script and localization
  125. $this->init_ui();
  126. // add 'Create filter' item to message menu
  127. $this->api->add_content(html::tag('li', null,
  128. $this->api->output->button(array(
  129. 'command' => 'managesieve-create',
  130. 'label' => 'managesieve.filtercreate',
  131. 'type' => 'link',
  132. 'classact' => 'icon filterlink active',
  133. 'class' => 'icon filterlink',
  134. 'innerclass' => 'icon filterlink',
  135. ))), 'messagemenu');
  136. // register some labels/messages
  137. $this->rc->output->add_label('managesieve.newfilter', 'managesieve.usedata',
  138. 'managesieve.nodata', 'managesieve.nextstep', 'save');
  139. $this->rc->session->remove('managesieve_current');
  140. }
  141. /**
  142. * Get message headers for popup window
  143. */
  144. function mail_headers($args)
  145. {
  146. // this hook can be executed many times
  147. if ($this->mail_headers_done) {
  148. return $args;
  149. }
  150. $this->mail_headers_done = true;
  151. $headers = $this->parse_headers($args['headers']);
  152. if ($this->rc->action == 'preview')
  153. $this->rc->output->command('parent.set_env', array('sieve_headers' => $headers));
  154. else
  155. $this->rc->output->set_env('sieve_headers', $headers);
  156. return $args;
  157. }
  158. /**
  159. * Plugin action handler
  160. */
  161. function managesieve_actions()
  162. {
  163. // handle fetching email headers for the new filter form
  164. if ($uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST)) {
  165. $uids = rcmail::get_uids();
  166. $mailbox = key($uids);
  167. $message = new rcube_message($uids[$mailbox][0], $mailbox);
  168. $headers = $this->parse_headers($message->headers);
  169. $this->rc->output->set_env('sieve_headers', $headers);
  170. $this->rc->output->command('managesieve_create', true);
  171. $this->rc->output->send();
  172. }
  173. // handle other actions
  174. $engine_type = $this->rc->action == 'plugin.managesieve-vacation' ? 'vacation' : '';
  175. $engine = $this->get_engine($engine_type);
  176. $this->init_ui();
  177. $engine->actions();
  178. }
  179. /**
  180. * Forms save action handler
  181. */
  182. function managesieve_save()
  183. {
  184. // load localization
  185. $this->add_texts('localization/', array('filters','managefilters'));
  186. // include main js script
  187. if ($this->api->output->type == 'html') {
  188. $this->include_script('managesieve.js');
  189. }
  190. $engine = $this->get_engine();
  191. $engine->save();
  192. }
  193. /**
  194. * Initializes engine object
  195. */
  196. public function get_engine($type = null)
  197. {
  198. if (!$this->engine) {
  199. $this->load_config();
  200. // Add include path for internal classes
  201. $include_path = $this->home . '/lib' . PATH_SEPARATOR;
  202. $include_path .= ini_get('include_path');
  203. set_include_path($include_path);
  204. $class_name = 'rcube_sieve_' . ($type ?: 'engine');
  205. $this->engine = new $class_name($this);
  206. }
  207. return $this->engine;
  208. }
  209. /**
  210. * Extract mail headers for new filter form
  211. */
  212. private function parse_headers($headers)
  213. {
  214. $result = array();
  215. if ($headers->subject)
  216. $result[] = array('Subject', rcube_mime::decode_header($headers->subject));
  217. // @TODO: List-Id, others?
  218. foreach (array('From', 'To') as $h) {
  219. $hl = strtolower($h);
  220. if ($headers->$hl) {
  221. $list = rcube_mime::decode_address_list($headers->$hl);
  222. foreach ($list as $item) {
  223. if ($item['mailto']) {
  224. $result[] = array($h, $item['mailto']);
  225. }
  226. }
  227. }
  228. }
  229. return $result;
  230. }
  231. }