comment
[bpt/emacs.git] / lisp / gnus / gnus-mlspl.el
1 ;;; gnus-mlspl.el --- a group params-based mail splitting mechanism
2
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4
5 ;; Author: Alexandre Oliva <oliva@lsd.ic.unicamp.br>
6 ;; Keywords: news, mail
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 2, or (at your option)
13 ;; 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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 (eval-when-compile (require 'cl))
26 (require 'gnus)
27 (require 'gnus-sum)
28 (require 'gnus-group)
29 (require 'nnmail)
30
31 (defvar gnus-group-split-updated-hook nil
32 "Hook called just after nnmail-split-fancy is updated by gnus-group-split-update.")
33
34 (defvar gnus-group-split-default-catch-all-group "mail.misc"
35 "Group used by gnus-group-split and gnus-group-split-update as default catch-all group.")
36
37 ;;;###autoload
38 (defun gnus-group-split-setup (&optional auto-update catch-all)
39 "Set up the split for nnmail-split-fancy.
40 Sets things up so that nnmail-split-fancy is used for mail
41 splitting, and defines the variable nnmail-split-fancy according with
42 group parameters.
43
44 If AUTO-UPDATE is non-nil (prefix argument accepted, if called
45 interactively), it makes sure nnmail-split-fancy is re-computed before
46 getting new mail, by adding gnus-group-split-update to
47 nnmail-pre-get-new-mail-hook."
48 (interactive "P")
49 (setq nnmail-split-methods 'nnmail-split-fancy)
50 (when catch-all
51 (setq gnus-group-split-default-catch-all-group catch-all))
52 (gnus-group-split-update)
53 (when auto-update
54 (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-group-split-update)))
55
56 ;;;###autoload
57 (defun gnus-group-split-update (&optional catch-all)
58 "Computes nnmail-split-fancy from group params.
59 It does this by calling \(gnus-group-split-fancy nil CROSSPOST DEFAULTGROUP)."
60 (interactive)
61 (setq nnmail-split-fancy
62 (gnus-group-split-fancy
63 nil (null nnmail-crosspost)
64 (or catch-all gnus-group-split-default-catch-all-group)))
65 (run-hooks 'gnus-group-split-updated-hook))
66
67 ;;;###autoload
68 (defun gnus-group-split ()
69 "Uses information from group parameters in order to split mail.
70 See gnus-group-split-fancy for more information.
71
72 If no group is defined as catch-all, the value of
73 gnus-group-split-default-catch-all-group is used.
74
75 gnus-group-split is a valid value for nnmail-split-methods."
76 (let (nnmail-split-fancy)
77 (gnus-group-split-update
78 gnus-group-split-default-catch-all-group)
79 (nnmail-split-fancy)))
80
81 ;;;###autoload
82 (defun gnus-group-split-fancy
83 (&optional groups no-crosspost catch-all)
84 "Uses information from group parameters in order to split mail. It
85 can be embedded into nnmail-split-fancy lists with the SPLIT
86
87 \(: gnus-group-split-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
88
89 GROUPS may be a regular expression or a list of group names, that will
90 be used to select candidate groups. If it is ommited or nil, all
91 existing groups are considered.
92
93 if NO-CROSSPOST is ommitted or nil, a & split will be returned,
94 otherwise, a | split, that does not allow crossposting, will be
95 returned.
96
97 if CATCH-ALL is not nil, and there is no selected group whose
98 SPLIT-REGEXP matches the empty string, nor is there a selected group
99 whose SPLIT-SPEC is 'catch-all, this group name will be appended to
100 the returned SPLIT list, as the last element in a '| SPLIT.
101
102 For each selected group, a SPLIT is composed like this: if SPLIT-SPEC
103 is specified, this split is returned as-is (unless it is nil: in this
104 case, the group is ignored). Otherwise, if TO-ADDRESS, TO-LIST and/or
105 EXTRA-ALIASES are specified, a regexp that matches any of them is
106 constructed (extra-aliases may be a list). Additionally, if
107 SPLIT-REGEXP is specified, the regexp will be extended so that it
108 matches this regexp too, and if SPLIT-EXCLUDE is specified, RESTRICT
109 clauses will be generated.
110
111 For example, given the following group parameters:
112
113 nnml:mail.bar:
114 \((to-address . \"bar@femail.com\")
115 (split-regexp . \".*@femail\\\\.com\"))
116 nnml:mail.foo:
117 \((to-list . \"foo@nowhere.gov\")
118 (extra-aliases \"foo@localhost\" \"foo-redist@home\")
119 (split-exclude \"bugs-foo\" \"rambling-foo\")
120 (admin-address . \"foo-request@nowhere.gov\"))
121 nnml:mail.others:
122 \((split-spec . catch-all))
123
124 Calling (gnus-group-split-fancy nil nil \"mail.misc\") returns:
125
126 \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
127 \"mail.bar\")
128 (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\"
129 - \"bugs-foo\" - \"rambling-foo\" \"mail.foo\"))
130 \"mail.others\")"
131 (let* ((newsrc (cdr gnus-newsrc-alist))
132 split)
133 (dolist (info newsrc)
134 (let ((group (gnus-info-group info))
135 (params (gnus-info-params info)))
136 ;; For all GROUPs that match the specified GROUPS
137 (when (or (not groups)
138 (and (listp groups)
139 (memq group groups))
140 (and (stringp groups)
141 (string-match groups group)))
142 (let ((split-spec (assoc 'split-spec params)) group-clean)
143 ;; Remove backend from group name
144 (setq group-clean (string-match ":" group))
145 (setq group-clean
146 (if group-clean
147 (substring group (1+ group-clean))
148 group))
149 (if split-spec
150 (when (setq split-spec (cdr split-spec))
151 (if (eq split-spec 'catch-all)
152 ;; Emit catch-all only when requested
153 (when catch-all
154 (setq catch-all group-clean))
155 ;; Append split-spec to the main split
156 (push split-spec split)))
157 ;; Let's deduce split-spec from other params
158 (let ((to-address (cdr (assoc 'to-address params)))
159 (to-list (cdr (assoc 'to-list params)))
160 (extra-aliases (cdr (assoc 'extra-aliases params)))
161 (split-regexp (cdr (assoc 'split-regexp params)))
162 (split-exclude (cdr (assoc 'split-exclude params))))
163 (when (or to-address to-list extra-aliases split-regexp)
164 ;; regexp-quote to-address, to-list and extra-aliases
165 ;; and add them all to split-regexp
166 (setq split-regexp
167 (concat
168 "\\("
169 (mapconcat
170 'identity
171 (append
172 (and to-address (list (regexp-quote to-address)))
173 (and to-list (list (regexp-quote to-list)))
174 (and extra-aliases
175 (if (listp extra-aliases)
176 (mapcar 'regexp-quote extra-aliases)
177 (list extra-aliases)))
178 (and split-regexp (list split-regexp)))
179 "\\|")
180 "\\)"))
181 ;; Now create the new SPLIT
182 (push (append
183 (list 'any split-regexp)
184 ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
185 (if (listp split-exclude)
186 (let ((seq split-exclude)
187 res)
188 (while seq
189 (push (cons '- (pop seq))
190 res))
191 (apply #'nconc (nreverse res)))
192 (list '- split-exclude))
193 (list group-clean))
194 split)
195 ;; If it matches the empty string, it is a catch-all
196 (when (string-match split-regexp "")
197 (setq catch-all nil)))))))))
198 ;; Add catch-all if not crossposting
199 (if (and catch-all no-crosspost)
200 (push catch-all split))
201 ;; Move it to the tail, while arranging that SPLITs appear in the
202 ;; same order as groups.
203 (setq split (reverse split))
204 ;; Decide whether to accept cross-postings or not.
205 (push (if no-crosspost '| '&) split)
206 ;; Even if we can cross-post, catch-all should not get
207 ;; cross-posts.
208 (if (and catch-all (not no-crosspost))
209 (setq split (list '| split catch-all)))
210 split))
211
212 (provide 'gnus-mlspl)