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