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