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