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