Gnus: fix last change
[bpt/emacs.git] / lisp / gnus / gnus-mlspl.el
CommitLineData
c113de23 1;;; gnus-mlspl.el --- a group params-based mail splitting mechanism
23f87bed 2
ba318903 3;; Copyright (C) 1998-2014 Free Software Foundation, Inc.
c113de23
GM
4
5;; Author: Alexandre Oliva <oliva@lsd.ic.unicamp.br>
6;; Keywords: news, mail
7
2be7dabc
GM
8;; This file is part of GNU Emacs.
9
5e809f55
GM
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.
c113de23 14
5e809f55
GM
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.
c113de23
GM
19
20;; You should have received a copy of the GNU General Public License
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c113de23 22
715a2ca2
PJ
23;;; Commentary:
24
25;;; Code:
26
d8cc10e1 27(eval-when-compile (require 'cl))
c113de23
GM
28(require 'gnus)
29(require 'gnus-sum)
30(require 'gnus-group)
31(require 'nnmail)
32
33(defvar gnus-group-split-updated-hook nil
01c52d31
MB
34 "Hook called just after `nnmail-split-fancy' is updated by
35`gnus-group-split-update'.")
c113de23
GM
36
37(defvar gnus-group-split-default-catch-all-group "mail.misc"
10b3dd25 38 "Group name (or arbitrary fancy split) with default splitting rules.
01c52d31 39Used by `gnus-group-split' and `gnus-group-split-update' as a fallback
10b3dd25 40split, in case none of the group-based splits matches.")
c113de23
GM
41
42;;;###autoload
43(defun gnus-group-split-setup (&optional auto-update catch-all)
01c52d31 44 "Set up the split for `nnmail-split-fancy'.
c113de23
GM
45Sets things up so that nnmail-split-fancy is used for mail
46splitting, and defines the variable nnmail-split-fancy according with
47group parameters.
48
49If AUTO-UPDATE is non-nil (prefix argument accepted, if called
50interactively), it makes sure nnmail-split-fancy is re-computed before
01c52d31
MB
51getting new mail, by adding `gnus-group-split-update' to
52`nnmail-pre-get-new-mail-hook'.
10b3dd25
DL
53
54A non-nil CATCH-ALL replaces the current value of
01c52d31 55`gnus-group-split-default-catch-all-group'. This variable is only used
10b3dd25
DL
56by gnus-group-split-update, and only when its CATCH-ALL argument is
57nil. This argument may contain any fancy split, that will be added as
01c52d31 58the last split in a `|' split produced by `gnus-group-split-fancy',
10b3dd25
DL
59unless overridden by any group marked as a catch-all group. Typical
60uses are as simple as the name of a default mail group, but more
61elaborate fancy splits may also be useful to split mail that doesn't
62match any of the group-specified splitting rules. See
23f87bed 63`gnus-group-split-fancy' for details."
c113de23
GM
64 (interactive "P")
65 (setq nnmail-split-methods 'nnmail-split-fancy)
66 (when catch-all
67 (setq gnus-group-split-default-catch-all-group catch-all))
68 (gnus-group-split-update)
69 (when auto-update
70 (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-group-split-update)))
71
72;;;###autoload
73(defun gnus-group-split-update (&optional catch-all)
23f87bed
MB
74 "Computes nnmail-split-fancy from group params and CATCH-ALL.
75It does this by calling by calling (gnus-group-split-fancy nil
76nil CATCH-ALL).
10b3dd25 77
01c52d31
MB
78If CATCH-ALL is nil, `gnus-group-split-default-catch-all-group' is used
79instead. This variable is set by `gnus-group-split-setup'."
c113de23
GM
80 (interactive)
81 (setq nnmail-split-fancy
82 (gnus-group-split-fancy
d8cc10e1
DL
83 nil (null nnmail-crosspost)
84 (or catch-all gnus-group-split-default-catch-all-group)))
c113de23
GM
85 (run-hooks 'gnus-group-split-updated-hook))
86
87;;;###autoload
88(defun gnus-group-split ()
01c52d31 89 "Use information from group parameters in order to split mail.
23f87bed 90See `gnus-group-split-fancy' for more information.
c113de23 91
01c52d31 92`gnus-group-split' is a valid value for `nnmail-split-methods'."
c113de23 93 (let (nnmail-split-fancy)
10b3dd25 94 (gnus-group-split-update)
c113de23
GM
95 (nnmail-split-fancy)))
96
97;;;###autoload
98(defun gnus-group-split-fancy
99 (&optional groups no-crosspost catch-all)
11f6edcc
PJ
100 "Uses information from group parameters in order to split mail.
101It can be embedded into `nnmail-split-fancy' lists with the SPLIT
c113de23
GM
102
103\(: gnus-group-split-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
104
105GROUPS may be a regular expression or a list of group names, that will
8f688cb0 106be used to select candidate groups. If it is omitted or nil, all
c113de23
GM
107existing groups are considered.
108
8f688cb0 109if NO-CROSSPOST is omitted or nil, a & split will be returned,
c113de23
GM
110otherwise, a | split, that does not allow crossposting, will be
111returned.
112
c113de23
GM
113For each selected group, a SPLIT is composed like this: if SPLIT-SPEC
114is specified, this split is returned as-is (unless it is nil: in this
115case, the group is ignored). Otherwise, if TO-ADDRESS, TO-LIST and/or
116EXTRA-ALIASES are specified, a regexp that matches any of them is
117constructed (extra-aliases may be a list). Additionally, if
118SPLIT-REGEXP is specified, the regexp will be extended so that it
119matches this regexp too, and if SPLIT-EXCLUDE is specified, RESTRICT
120clauses will be generated.
121
10b3dd25
DL
122If CATCH-ALL is nil, no catch-all handling is performed, regardless of
123catch-all marks in group parameters. Otherwise, if there is no
124selected group whose SPLIT-REGEXP matches the empty string, nor is
125there a selected group whose SPLIT-SPEC is 'catch-all, this fancy
126split (say, a group name) will be appended to the returned SPLIT list,
127as the last element of a '| SPLIT.
128
c113de23
GM
129For example, given the following group parameters:
130
131nnml:mail.bar:
132\((to-address . \"bar@femail.com\")
133 (split-regexp . \".*@femail\\\\.com\"))
134nnml:mail.foo:
135\((to-list . \"foo@nowhere.gov\")
136 (extra-aliases \"foo@localhost\" \"foo-redist@home\")
137 (split-exclude \"bugs-foo\" \"rambling-foo\")
138 (admin-address . \"foo-request@nowhere.gov\"))
139nnml:mail.others:
140\((split-spec . catch-all))
141
23f87bed 142Calling (gnus-group-split-fancy nil nil \"mail.others\") returns:
c113de23
GM
143
144\(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
145 \"mail.bar\")
146 (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\"
23f87bed 147 - \"bugs-foo\" - \"rambling-foo\" \"mail.foo\"))
c113de23 148 \"mail.others\")"
4d2226bf
G
149 (let ((group-names (if (and (listp groups)
150 (not (null groups)))
151 groups
152 (delete-dups
153 (delq nil
154 (mapcar
155 (lambda (info)
156 (let ((group (gnus-info-group info)))
157 (if (or (not groups)
158 (and (stringp groups)
159 (string-match groups group)))
160 group)))
161 (append gnus-newsrc-alist gnus-parameters))))))
162 split)
163 (dolist (group group-names)
164 (let ((params (gnus-group-find-parameter group)))
165 ;; Skip groups without param (or nonexistent)
166 (when (not (null params))
167 (let ((split-spec (assoc 'split-spec params)) group-clean)
168 ;; Remove backend from group name
169 (setq group-clean (string-match ":" group))
c113de23
GM
170 (setq group-clean
171 (if group-clean
172 (substring group (1+ group-clean))
173 group))
174 (if split-spec
175 (when (setq split-spec (cdr split-spec))
176 (if (eq split-spec 'catch-all)
177 ;; Emit catch-all only when requested
178 (when catch-all
179 (setq catch-all group-clean))
180 ;; Append split-spec to the main split
181 (push split-spec split)))
182 ;; Let's deduce split-spec from other params
183 (let ((to-address (cdr (assoc 'to-address params)))
184 (to-list (cdr (assoc 'to-list params)))
185 (extra-aliases (cdr (assoc 'extra-aliases params)))
186 (split-regexp (cdr (assoc 'split-regexp params)))
187 (split-exclude (cdr (assoc 'split-exclude params))))
188 (when (or to-address to-list extra-aliases split-regexp)
189 ;; regexp-quote to-address, to-list and extra-aliases
190 ;; and add them all to split-regexp
191 (setq split-regexp
192 (concat
193 "\\("
194 (mapconcat
195 'identity
196 (append
197 (and to-address (list (regexp-quote to-address)))
198 (and to-list (list (regexp-quote to-list)))
199 (and extra-aliases
200 (if (listp extra-aliases)
201 (mapcar 'regexp-quote extra-aliases)
202 (list extra-aliases)))
203 (and split-regexp (list split-regexp)))
204 "\\|")
205 "\\)"))
206 ;; Now create the new SPLIT
207 (push (append
208 (list 'any split-regexp)
209 ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
210 (if (listp split-exclude)
23f87bed
MB
211 (apply #'append
212 (mapcar (lambda (arg) (list '- arg))
213 split-exclude))
c113de23
GM
214 (list '- split-exclude))
215 (list group-clean))
216 split)
217 ;; If it matches the empty string, it is a catch-all
218 (when (string-match split-regexp "")
219 (setq catch-all nil)))))))))
220 ;; Add catch-all if not crossposting
221 (if (and catch-all no-crosspost)
d8cc10e1 222 (push catch-all split))
c113de23
GM
223 ;; Move it to the tail, while arranging that SPLITs appear in the
224 ;; same order as groups.
225 (setq split (reverse split))
226 ;; Decide whether to accept cross-postings or not.
227 (push (if no-crosspost '| '&) split)
228 ;; Even if we can cross-post, catch-all should not get
229 ;; cross-posts.
230 (if (and catch-all (not no-crosspost))
231 (setq split (list '| split catch-all)))
232 split))
233
234(provide 'gnus-mlspl)
715a2ca2
PJ
235
236;;; gnus-mlspl.el ends here