gnus-sieve.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ;;; gnus-sieve.el --- Utilities to manage sieve scripts for Gnus
  2. ;; Copyright (C) 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: NAGY Andras <nagya@inf.elte.hu>,
  4. ;; Simon Josefsson <simon@josefsson.org>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Gnus glue to generate complete Sieve scripts from Gnus Group
  18. ;; Parameters with "if" test predicates.
  19. ;;; Code:
  20. (require 'gnus)
  21. (require 'gnus-sum)
  22. (require 'format-spec)
  23. (autoload 'sieve-mode "sieve-mode")
  24. (eval-when-compile
  25. (require 'sieve))
  26. ;; Variables
  27. (defgroup gnus-sieve nil
  28. "Manage sieve scripts in Gnus."
  29. :group 'gnus)
  30. (defcustom gnus-sieve-file "~/.sieve"
  31. "Path to your Sieve script."
  32. :type 'file
  33. :group 'gnus-sieve)
  34. (defcustom gnus-sieve-region-start "\n## Begin Gnus Sieve Script\n"
  35. "Line indicating the start of the autogenerated region in
  36. your Sieve script."
  37. :type 'string
  38. :group 'gnus-sieve)
  39. (defcustom gnus-sieve-region-end "\n## End Gnus Sieve Script\n"
  40. "Line indicating the end of the autogenerated region in
  41. your Sieve script."
  42. :type 'string
  43. :group 'gnus-sieve)
  44. (defcustom gnus-sieve-select-method nil
  45. "Which select method we generate the Sieve script for.
  46. For example: \"nnimap:mailbox\""
  47. :group 'gnus-sieve)
  48. (defcustom gnus-sieve-crosspost t
  49. "Whether the generated Sieve script should do crossposting."
  50. :type 'boolean
  51. :group 'gnus-sieve)
  52. (defcustom gnus-sieve-update-shell-command "echo put %f | sieveshell %s"
  53. "Shell command to execute after updating your Sieve script. The following
  54. formatting characters are recognized:
  55. %f Script's file name (gnus-sieve-file)
  56. %s Server name (from gnus-sieve-select-method)"
  57. :type 'string
  58. :group 'gnus-sieve)
  59. ;;;###autoload
  60. (defun gnus-sieve-update ()
  61. "Update the Sieve script in gnus-sieve-file, by replacing the region
  62. between gnus-sieve-region-start and gnus-sieve-region-end with
  63. \(gnus-sieve-script gnus-sieve-select-method gnus-sieve-crosspost\), then
  64. execute gnus-sieve-update-shell-command.
  65. See the documentation for these variables and functions for details."
  66. (interactive)
  67. (gnus-sieve-generate)
  68. (save-buffer)
  69. (shell-command
  70. (format-spec gnus-sieve-update-shell-command
  71. (format-spec-make ?f gnus-sieve-file
  72. ?s (or (cadr (gnus-server-get-method
  73. nil gnus-sieve-select-method))
  74. "")))))
  75. ;;;###autoload
  76. (defun gnus-sieve-generate ()
  77. "Generate the Sieve script in gnus-sieve-file, by replacing the region
  78. between gnus-sieve-region-start and gnus-sieve-region-end with
  79. \(gnus-sieve-script gnus-sieve-select-method gnus-sieve-crosspost\).
  80. See the documentation for these variables and functions for details."
  81. (interactive)
  82. (require 'sieve)
  83. (find-file gnus-sieve-file)
  84. (goto-char (point-min))
  85. (if (re-search-forward (regexp-quote gnus-sieve-region-start) nil t)
  86. (delete-region (match-beginning 0)
  87. (or (re-search-forward (regexp-quote
  88. gnus-sieve-region-end) nil t)
  89. (point)))
  90. (insert sieve-template))
  91. (insert gnus-sieve-region-start
  92. (gnus-sieve-script gnus-sieve-select-method gnus-sieve-crosspost)
  93. gnus-sieve-region-end))
  94. (defun gnus-sieve-guess-rule-for-article ()
  95. "Guess a sieve rule based on RFC822 article in buffer.
  96. Return nil if no rule could be guessed."
  97. (when (message-fetch-field "sender")
  98. `(sieve address "sender" ,(message-fetch-field "sender"))))
  99. ;;;###autoload
  100. (defun gnus-sieve-article-add-rule ()
  101. (interactive)
  102. (gnus-summary-select-article nil 'force)
  103. (with-current-buffer gnus-original-article-buffer
  104. (let ((rule (gnus-sieve-guess-rule-for-article))
  105. (info (gnus-get-info gnus-newsgroup-name)))
  106. (if (null rule)
  107. (error "Could not guess rule for article")
  108. (gnus-info-set-params info (cons rule (gnus-info-params info)))
  109. (message "Added rule in group %s for article: %s" gnus-newsgroup-name
  110. rule)))))
  111. ;; Internals
  112. ;; FIXME: do proper quoting of " etc
  113. (defun gnus-sieve-string-list (list)
  114. "Convert an elisp string list to a Sieve string list.
  115. For example:
  116. \(gnus-sieve-string-list '(\"to\" \"cc\"))
  117. => \"[\\\"to\\\", \\\"cc\\\"]\"
  118. "
  119. (concat "[\"" (mapconcat 'identity list "\", \"") "\"]"))
  120. (defun gnus-sieve-test-list (list)
  121. "Convert an elisp test list to a Sieve test list.
  122. For example:
  123. \(gnus-sieve-test-list '((address \"sender\" \"boss@company.com\") (size :over 4K)))
  124. => \"(address \\\"sender\\\" \\\"boss@company.com\\\", size :over 4K)\""
  125. (concat "(" (mapconcat 'gnus-sieve-test list ", ") ")"))
  126. ;; FIXME: do proper quoting
  127. (defun gnus-sieve-test-token (token)
  128. "Convert an elisp test token to a Sieve test token.
  129. For example:
  130. \(gnus-sieve-test-token 'address)
  131. => \"address\"
  132. \(gnus-sieve-test-token \"sender\")
  133. => \"\\\"sender\\\"\"
  134. \(gnus-sieve-test-token '(\"to\" \"cc\"))
  135. => \"[\\\"to\\\", \\\"cc\\\"]\""
  136. (cond
  137. ((symbolp token) ;; Keyword
  138. (symbol-name token))
  139. ((stringp token) ;; String
  140. (concat "\"" token "\""))
  141. ((and (listp token) ;; String list
  142. (stringp (car token)))
  143. (gnus-sieve-string-list token))
  144. ((and (listp token) ;; Test list
  145. (listp (car token)))
  146. (gnus-sieve-test-list token))))
  147. (defun gnus-sieve-test (test)
  148. "Convert an elisp test to a Sieve test.
  149. For example:
  150. \(gnus-sieve-test '(address \"sender\" \"sieve-admin@extundo.com\"))
  151. => \"address \\\"sender\\\" \\\"sieve-admin@extundo.com\\\"\"
  152. \(gnus-sieve-test '(anyof ((header :contains (\"to\" \"cc\") \"my@address.com\")
  153. (size :over 100K))))
  154. => \"anyof (header :contains [\\\"to\\\", \\\"cc\\\"] \\\"my@address.com\\\",
  155. size :over 100K)\""
  156. (mapconcat 'gnus-sieve-test-token test " "))
  157. (defun gnus-sieve-script (&optional method crosspost)
  158. "Generate a Sieve script based on groups with select method METHOD
  159. \(or all groups if nil\). Only groups having a `sieve' parameter are
  160. considered. This parameter should contain an elisp test
  161. \(see the documentation of gnus-sieve-test for details\). For each
  162. such group, a Sieve IF control structure is generated, having the
  163. test as the condition and { fileinto \"group.name\"; } as the body.
  164. If CROSSPOST is nil, each conditional body contains a \"stop\" command
  165. which stops execution after a match is found.
  166. For example: If the INBOX.list.sieve group has the
  167. (sieve address \"sender\" \"sieve-admin@extundo.com\")
  168. group parameter, (gnus-sieve-script) results in:
  169. if address \"sender\" \"sieve-admin@extundo.com\" {
  170. fileinto \"INBOX.list.sieve\";
  171. }
  172. This is returned as a string."
  173. (let* ((newsrc (cdr gnus-newsrc-alist))
  174. script)
  175. (dolist (info newsrc)
  176. (when (or (not method)
  177. (gnus-server-equal method (gnus-info-method info)))
  178. (let* ((group (gnus-info-group info))
  179. (spec (gnus-group-find-parameter group 'sieve t)))
  180. (when spec
  181. (push (concat "if " (gnus-sieve-test spec) " {\n"
  182. "\tfileinto \"" (gnus-group-real-name group) "\";\n"
  183. (if crosspost
  184. ""
  185. "\tstop;\n")
  186. "}")
  187. script)))))
  188. (mapconcat 'identity script "\n")))
  189. (provide 'gnus-sieve)
  190. ;;; gnus-sieve.el ends here