1 ;;; gnus-sieve.el --- Utilities to manage sieve scripts for Gnus
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: NAGY Andras <nagya@inf.elte.hu>,
6 ;; Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Gnus glue to generate complete Sieve scripts from Gnus Group
28 ;; Parameters with "if" test predicates.
34 (require 'format-spec
)
35 (autoload 'sieve-mode
"sieve-mode")
41 (defgroup gnus-sieve nil
42 "Manage sieve scripts in Gnus."
45 (defcustom gnus-sieve-file
"~/.sieve"
46 "Path to your Sieve script."
50 (defcustom gnus-sieve-region-start
"\n## Begin Gnus Sieve Script\n"
51 "Line indicating the start of the autogenerated region in
56 (defcustom gnus-sieve-region-end
"\n## End Gnus Sieve Script\n"
57 "Line indicating the end of the autogenerated region in
62 (defcustom gnus-sieve-select-method nil
63 "Which select method we generate the Sieve script for.
65 For example: \"nnimap:mailbox\""
68 (defcustom gnus-sieve-crosspost t
69 "Whether the generated Sieve script should do crossposting."
73 (defcustom gnus-sieve-update-shell-command
"echo put %f | sieveshell %s"
74 "Shell command to execute after updating your Sieve script. The following
75 formatting characters are recognized:
77 %f Script's file name (gnus-sieve-file)
78 %s Server name (from gnus-sieve-select-method)"
83 (defun gnus-sieve-update ()
84 "Update the Sieve script in gnus-sieve-file, by replacing the region
85 between gnus-sieve-region-start and gnus-sieve-region-end with
86 \(gnus-sieve-script gnus-sieve-select-method gnus-sieve-crosspost\), then
87 execute gnus-sieve-update-shell-command.
88 See the documentation for these variables and functions for details."
93 (format-spec gnus-sieve-update-shell-command
94 (format-spec-make ?f gnus-sieve-file
95 ?s
(or (cadr (gnus-server-get-method
96 nil gnus-sieve-select-method
))
100 (defun gnus-sieve-generate ()
101 "Generate the Sieve script in gnus-sieve-file, by replacing the region
102 between gnus-sieve-region-start and gnus-sieve-region-end with
103 \(gnus-sieve-script gnus-sieve-select-method gnus-sieve-crosspost\).
104 See the documentation for these variables and functions for details."
107 (find-file gnus-sieve-file
)
108 (goto-char (point-min))
109 (if (re-search-forward (regexp-quote gnus-sieve-region-start
) nil t
)
110 (delete-region (match-end 0)
111 (or (re-search-forward (regexp-quote
112 gnus-sieve-region-end
) nil t
)
114 (insert sieve-template
))
115 (insert gnus-sieve-region-start
116 (gnus-sieve-script gnus-sieve-select-method gnus-sieve-crosspost
)
117 gnus-sieve-region-end
))
119 (defun gnus-sieve-guess-rule-for-article ()
120 "Guess a sieve rule based on RFC822 article in buffer.
121 Return nil if no rule could be guessed."
122 (when (message-fetch-field "sender")
123 `(sieve address
"sender" ,(message-fetch-field "sender"))))
126 (defun gnus-sieve-article-add-rule ()
128 (gnus-summary-select-article nil
'force
)
129 (with-current-buffer gnus-original-article-buffer
130 (let ((rule (gnus-sieve-guess-rule-for-article))
131 (info (gnus-get-info gnus-newsgroup-name
)))
133 (error "Could not guess rule for article")
134 (gnus-info-set-params info
(cons rule
(gnus-info-params info
)))
135 (message "Added rule in group %s for article: %s" gnus-newsgroup-name
140 ;; FIXME: do proper quoting of " etc
141 (defun gnus-sieve-string-list (list)
142 "Convert an elisp string list to a Sieve string list.
145 \(gnus-sieve-string-list '(\"to\" \"cc\"))
146 => \"[\\\"to\\\", \\\"cc\\\"]\"
148 (concat "[\"" (mapconcat 'identity list
"\", \"") "\"]"))
150 (defun gnus-sieve-test-list (list)
151 "Convert an elisp test list to a Sieve test list.
154 \(gnus-sieve-test-list '((address \"sender\" \"boss@company.com\") (size :over 4K)))
155 => \"(address \\\"sender\\\" \\\"boss@company.com\\\", size :over 4K)\""
156 (concat "(" (mapconcat 'gnus-sieve-test list
", ") ")"))
158 ;; FIXME: do proper quoting
159 (defun gnus-sieve-test-token (token)
160 "Convert an elisp test token to a Sieve test token.
163 \(gnus-sieve-test-token 'address)
166 \(gnus-sieve-test-token \"sender\")
167 => \"\\\"sender\\\"\"
169 \(gnus-sieve-test-token '(\"to\" \"cc\"))
170 => \"[\\\"to\\\", \\\"cc\\\"]\""
172 ((symbolp token
) ;; Keyword
175 ((stringp token
) ;; String
176 (concat "\"" token
"\""))
178 ((and (listp token
) ;; String list
179 (stringp (car token
)))
180 (gnus-sieve-string-list token
))
182 ((and (listp token
) ;; Test list
184 (gnus-sieve-test-list token
))))
186 (defun gnus-sieve-test (test)
187 "Convert an elisp test to a Sieve test.
190 \(gnus-sieve-test '(address \"sender\" \"sieve-admin@extundo.com\"))
191 => \"address \\\"sender\\\" \\\"sieve-admin@extundo.com\\\"\"
193 \(gnus-sieve-test '(anyof ((header :contains (\"to\" \"cc\") \"my@address.com\")
195 => \"anyof (header :contains [\\\"to\\\", \\\"cc\\\"] \\\"my@address.com\\\",
197 (mapconcat 'gnus-sieve-test-token test
" "))
199 (defun gnus-sieve-script (&optional method crosspost
)
200 "Generate a Sieve script based on groups with select method METHOD
201 \(or all groups if nil\). Only groups having a `sieve' parameter are
202 considered. This parameter should contain an elisp test
203 \(see the documentation of gnus-sieve-test for details\). For each
204 such group, a Sieve IF control structure is generated, having the
205 test as the condition and { fileinto \"group.name\"; } as the body.
207 If CROSSPOST is nil, each conditional body contains a \"stop\" command
208 which stops execution after a match is found.
210 For example: If the INBOX.list.sieve group has the
212 (sieve address \"sender\" \"sieve-admin@extundo.com\")
214 group parameter, (gnus-sieve-script) results in:
216 if address \"sender\" \"sieve-admin@extundo.com\" {
217 fileinto \"INBOX.list.sieve\";
220 This is returned as a string."
221 (let* ((newsrc (cdr gnus-newsrc-alist
))
223 (dolist (info newsrc
)
224 (when (or (not method
)
225 (gnus-server-equal method
(gnus-info-method info
)))
226 (let* ((group (gnus-info-group info
))
227 (spec (gnus-group-find-parameter group
'sieve t
)))
229 (push (concat "if " (gnus-sieve-test spec
) " {\n"
230 "\tfileinto \"" (gnus-group-real-name group
) "\";\n"
236 (mapconcat 'identity script
"\n")))
238 (provide 'gnus-sieve
)
240 ;;; arch-tag: 3b906527-c7f3-4c86-9e82-62e2697998a3
241 ;;; gnus-sieve.el ends here