1 ;;; gnus-mlspl.el --- a group params-based mail splitting mechanism
2 ;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
4 ;; Author: Alexandre Oliva <oliva@dcc.unicamp.br>
5 ;; Keywords: news, mail
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
22 (eval-when-compile (require 'cl))
28 (defvar gnus-group-split-updated-hook nil
29 "Hook called just after nnmail-split-fancy is updated by
30 gnus-group-split-update")
32 (defvar gnus-group-split-default-catch-all-group "mail.misc"
33 "Group used by gnus-group-split and gnus-group-split-update as
34 default catch-all group")
37 (defun gnus-group-split-setup (&optional auto-update catch-all)
38 "Sets things up so that nnmail-split-fancy is used for mail
39 splitting, and defines the variable nnmail-split-fancy according with
42 If AUTO-UPDATE is non-nil (prefix argument accepted, if called
43 interactively), it makes sure nnmail-split-fancy is re-computed before
44 getting new mail, by adding gnus-group-split-update to
45 nnmail-pre-get-new-mail-hook."
47 (setq nnmail-split-methods 'nnmail-split-fancy)
49 (setq gnus-group-split-default-catch-all-group catch-all))
50 (gnus-group-split-update)
52 (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-group-split-update)))
55 (defun gnus-group-split-update (&optional catch-all)
56 "Computes nnmail-split-fancy from group params, by calling
57 \(gnus-group-split-fancy nil nil DEFAULTGROUP)"
59 (setq nnmail-split-fancy
60 (gnus-group-split-fancy
61 nil nil (or catch-all gnus-group-split-default-catch-all-group)))
62 (run-hooks 'gnus-group-split-updated-hook)
66 (defun gnus-group-split ()
67 "Uses information from group parameters in order to split mail. See
68 gnus-group-split-fancy for more information.
70 If no group is defined as catch-all, the value of
71 gnus-group-split-default-catch-all-group is used.
73 gnus-group-split is a valid value for nnmail-split-methods."
74 (let (nnmail-split-fancy)
75 (gnus-group-split-update
76 gnus-group-split-default-catch-all-group)
77 (nnmail-split-fancy)))
80 (defun gnus-group-split-fancy
81 (&optional groups no-crosspost catch-all)
82 "Uses information from group parameters in order to split mail. It
83 can be embedded into nnmail-split-fancy lists with the SPLIT
85 \(: gnus-group-split-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
87 GROUPS may be a regular expression or a list of group names, that will
88 be used to select candidate groups. If it is ommited or nil, all
89 existing groups are considered.
91 if NO-CROSSPOST is ommitted or nil, a & split will be returned,
92 otherwise, a | split, that does not allow crossposting, will be
95 if CATCH-ALL is not nil, and there is no selected group whose
96 SPLIT-REGEXP matches the empty string, nor is there a selected group
97 whose SPLIT-SPEC is 'catch-all, this group name will be appended to
98 the returned SPLIT list, as the last element in a '| SPLIT.
100 For each selected group, a SPLIT is composed like this: if SPLIT-SPEC
101 is specified, this split is returned as-is (unless it is nil: in this
102 case, the group is ignored). Otherwise, if TO-ADDRESS, TO-LIST and/or
103 EXTRA-ALIASES are specified, a regexp that matches any of them is
104 constructed (extra-aliases may be a list). Additionally, if
105 SPLIT-REGEXP is specified, the regexp will be extended so that it
106 matches this regexp too, and if SPLIT-EXCLUDE is specified, RESTRICT
107 clauses will be generated.
109 For example, given the following group parameters:
112 \((to-address . \"bar@femail.com\")
113 (split-regexp . \".*@femail\\\\.com\"))
115 \((to-list . \"foo@nowhere.gov\")
116 (extra-aliases \"foo@localhost\" \"foo-redist@home\")
117 (split-exclude \"bugs-foo\" \"rambling-foo\")
118 (admin-address . \"foo-request@nowhere.gov\"))
120 \((split-spec . catch-all))
122 Calling (gnus-group-split-fancy nil nil \"mail.misc\") returns:
124 \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
126 (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\"
127 - \"bugs-foo\" - \"rambling-foo\" \"mail.foo\"))
129 (let* ((newsrc (cdr gnus-newsrc-alist))
131 (dolist (info newsrc)
132 (let ((group (gnus-info-group info))
133 (params (gnus-info-params info)))
134 ;; For all GROUPs that match the specified GROUPS
135 (when (or (not groups)
138 (and (stringp groups)
139 (string-match groups group)))
140 (let ((split-spec (cdr (assoc 'split-spec params))) group-clean)
141 ;; Remove backend from group name
142 (setq group-clean (string-match ":" group))
145 (substring group (1+ group-clean))
148 (if (eq split-spec 'catch-all)
149 ;; Emit catch-all only when requested
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
169 (and to-address (list (regexp-quote to-address)))
170 (and to-list (list (regexp-quote to-list)))
172 (if (listp extra-aliases)
173 (mapcar 'regexp-quote extra-aliases)
174 (list extra-aliases)))
175 (and split-regexp (list split-regexp)))
178 ;; Now create the new SPLIT
180 (list 'any split-regexp)
181 ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
182 (if (listp split-exclude)
183 (mapcon (lambda (arg) (cons '- arg))
185 (list '- split-exclude))
188 ;; If it matches the empty string, it is a catch-all
189 (when (string-match split-regexp "")
190 (setq catch-all nil)))))))))
191 ;; Add catch-all if not crossposting
192 (if (and catch-all no-crosspost)
193 (push split catch-all))
194 ;; Move it to the tail, while arranging that SPLITs appear in the
195 ;; same order as groups.
196 (setq split (reverse split))
197 ;; Decide whether to accept cross-postings or not.
198 (push (if no-crosspost '| '&) split)
199 ;; Even if we can cross-post, catch-all should not get
201 (if (and catch-all (not no-crosspost))
202 (setq split (list '| split catch-all)))
205 (provide 'gnus-mlspl)