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