選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

managesieve.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. * @author Aleksander Machniak <alec@alec.pl>
  10. *
  11. * Configuration (see config.inc.php.dist)
  12. *
  13. * Copyright (C) 2008-2013, The Roundcube Dev Team
  14. * Copyright (C) 2011-2013, Kolab Systems AG
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see http://www.gnu.org/licenses/.
  28. */
  29. class managesieve extends rcube_plugin
  30. {
  31. public $task = 'mail|settings';
  32. private $rc;
  33. private $engine;
  34. function init()
  35. {
  36. $this->rc = rcube::get_instance();
  37. // register actions
  38. $this->register_action('plugin.managesieve', array($this, 'managesieve_actions'));
  39. $this->register_action('plugin.managesieve-action', array($this, 'managesieve_actions'));
  40. $this->register_action('plugin.managesieve-vacation', array($this, 'managesieve_actions'));
  41. $this->register_action('plugin.managesieve-save', array($this, 'managesieve_save'));
  42. $this->register_action('plugin.managesieve-saveraw', array($this, 'managesieve_saveraw'));
  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. $uids = rcmail::get_uids(null, null, $multifolder, rcube_utils::INPUT_POST);
  164. // handle fetching email headers for the new filter form
  165. if (!empty($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. * Raw form save action handler
  195. */
  196. function managesieve_saveraw()
  197. {
  198. $engine = $this->get_engine();
  199. if (!$this->rc->config->get('managesieve_raw_editor', true)) {
  200. return;
  201. }
  202. // load localization
  203. $this->add_texts('localization/', array('filters','managefilters'));
  204. $engine->saveraw();
  205. }
  206. /**
  207. * Initializes engine object
  208. */
  209. public function get_engine($type = null)
  210. {
  211. if (!$this->engine) {
  212. $this->load_config();
  213. // Add include path for internal classes
  214. $include_path = $this->home . '/lib' . PATH_SEPARATOR;
  215. $include_path .= ini_get('include_path');
  216. set_include_path($include_path);
  217. $class_name = 'rcube_sieve_' . ($type ?: 'engine');
  218. $this->engine = new $class_name($this);
  219. }
  220. return $this->engine;
  221. }
  222. /**
  223. * Extract mail headers for new filter form
  224. */
  225. private function parse_headers($headers)
  226. {
  227. $result = array();
  228. if ($headers->subject)
  229. $result[] = array('Subject', rcube_mime::decode_header($headers->subject));
  230. // @TODO: List-Id, others?
  231. foreach (array('From', 'To') as $h) {
  232. $hl = strtolower($h);
  233. if ($headers->$hl) {
  234. $list = rcube_mime::decode_address_list($headers->$hl);
  235. foreach ($list as $item) {
  236. if ($item['mailto']) {
  237. $result[] = array($h, $item['mailto']);
  238. }
  239. }
  240. }
  241. }
  242. return $result;
  243. }
  244. }