1 ;;; gnus-mlspl.el --- a group params-based mail splitting mechanism
2 ;; Copyright (C) 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Alexandre Oliva <oliva@lsd.ic.unicamp.br>
6 ;; Keywords: news, mail
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)
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.
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.
23 (eval-when-compile (require 'cl))
24 (eval-when-compile (require 'gnus-clfns))
31 (defvar gnus-group-split-updated-hook nil
32 "Hook called just after nnmail-split-fancy is updated by gnus-group-split-update.")
34 (defvar gnus-group-split-default-catch-all-group "mail.misc"
35 "Group used by gnus-group-split and gnus-group-split-update as default catch-all group.")
38 (defun gnus-group-split-setup (&optional auto-update catch-all)
39 "Set up the split for nnmail-split-fancy.
40 Sets things up so that nnmail-split-fancy is used for mail
41 splitting, and defines the variable nnmail-split-fancy according with
44 If AUTO-UPDATE is non-nil (prefix argument accepted, if called
45 interactively), it makes sure nnmail-split-fancy is re-computed before
46 getting new mail, by adding gnus-group-split-update to
47 nnmail-pre-get-new-mail-hook."
49 (setq nnmail-split-methods 'nnmail-split-fancy)
51 (setq gnus-group-split-default-catch-all-group catch-all))
52 (gnus-group-split-update)
54 (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-group-split-update)))
57 (defun gnus-group-split-update (&optional catch-all)
58 "Computes nnmail-split-fancy from group params.
59 It does this by calling \(gnus-group-split-fancy nil nil DEFAULTGROUP)."
61 (setq nnmail-split-fancy
62 (gnus-group-split-fancy
63 nil nil (or catch-all gnus-group-split-default-catch-all-group)))
64 (run-hooks 'gnus-group-split-updated-hook))
67 (defun gnus-group-split ()
68 "Uses information from group parameters in order to split mail.
69 See gnus-group-split-fancy for more information.
71 If no group is defined as catch-all, the value of
72 gnus-group-split-default-catch-all-group is used.
74 gnus-group-split is a valid value for nnmail-split-methods."
75 (let (nnmail-split-fancy)
76 (gnus-group-split-update
77 gnus-group-split-default-catch-all-group)
78 (nnmail-split-fancy)))
81 (defun gnus-group-split-fancy
82 (&optional groups no-crosspost catch-all)
83 "Uses information from group parameters in order to split mail. It
84 can be embedded into nnmail-split-fancy lists with the SPLIT
86 \(: gnus-group-split-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
88 GROUPS may be a regular expression or a list of group names, that will
89 be used to select candidate groups. If it is ommited or nil, all
90 existing groups are considered.
92 if NO-CROSSPOST is ommitted or nil, a & split will be returned,
93 otherwise, a | split, that does not allow crossposting, will be
96 if CATCH-ALL is not nil, and there is no selected group whose
97 SPLIT-REGEXP matches the empty string, nor is there a selected group
98 whose SPLIT-SPEC is 'catch-all, this group name will be appended to
99 the returned SPLIT list, as the last element in a '| SPLIT.
101 For each selected group, a SPLIT is composed like this: if SPLIT-SPEC
102 is specified, this split is returned as-is (unless it is nil: in this
103 case, the group is ignored). Otherwise, if TO-ADDRESS, TO-LIST and/or
104 EXTRA-ALIASES are specified, a regexp that matches any of them is
105 constructed (extra-aliases may be a list). Additionally, if
106 SPLIT-REGEXP is specified, the regexp will be extended so that it
107 matches this regexp too, and if SPLIT-EXCLUDE is specified, RESTRICT
108 clauses will be generated.
110 For example, given the following group parameters:
113 \((to-address . \"bar@femail.com\")
114 (split-regexp . \".*@femail\\\\.com\"))
116 \((to-list . \"foo@nowhere.gov\")
117 (extra-aliases \"foo@localhost\" \"foo-redist@home\")
118 (split-exclude \"bugs-foo\" \"rambling-foo\")
119 (admin-address . \"foo-request@nowhere.gov\"))
121 \((split-spec . catch-all))
123 Calling (gnus-group-split-fancy nil nil \"mail.misc\") returns:
125 \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
127 (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\"
128 - \"bugs-foo\" - \"rambling-foo\" \"mail.foo\"))
130 (let* ((newsrc (cdr gnus-newsrc-alist))
132 (dolist (info newsrc)
133 (let ((group (gnus-info-group info))
134 (params (gnus-info-params info)))
135 ;; For all GROUPs that match the specified GROUPS
136 (when (or (not groups)
139 (and (stringp groups)
140 (string-match groups group)))
141 (let ((split-spec (assoc 'split-spec params)) group-clean)
142 ;; Remove backend from group name
143 (setq group-clean (string-match ":" group))
146 (substring group (1+ group-clean))
149 (when (setq split-spec (cdr split-spec))
150 (if (eq split-spec 'catch-all)
151 ;; Emit catch-all only when requested
153 (setq catch-all group-clean))
154 ;; Append split-spec to the main split
155 (push split-spec split)))
156 ;; Let's deduce split-spec from other params
157 (let ((to-address (cdr (assoc 'to-address params)))
158 (to-list (cdr (assoc 'to-list params)))
159 (extra-aliases (cdr (assoc 'extra-aliases params)))
160 (split-regexp (cdr (assoc 'split-regexp params)))
161 (split-exclude (cdr (assoc 'split-exclude params))))
162 (when (or to-address to-list extra-aliases split-regexp)
163 ;; regexp-quote to-address, to-list and extra-aliases
164 ;; and add them all to split-regexp
171 (and to-address (list (regexp-quote to-address)))
172 (and to-list (list (regexp-quote to-list)))
174 (if (listp extra-aliases)
175 (mapcar 'regexp-quote extra-aliases)
176 (list extra-aliases)))
177 (and split-regexp (list split-regexp)))
180 ;; Now create the new SPLIT
182 (list 'any split-regexp)
183 ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
184 (if (listp split-exclude)
185 (mapcon (lambda (arg) (cons '- arg))
187 (list '- split-exclude))
190 ;; If it matches the empty string, it is a catch-all
191 (when (string-match split-regexp "")
192 (setq catch-all nil)))))))))
193 ;; Add catch-all if not crossposting
194 (if (and catch-all no-crosspost)
195 (push split catch-all))
196 ;; Move it to the tail, while arranging that SPLITs appear in the
197 ;; same order as groups.
198 (setq split (reverse split))
199 ;; Decide whether to accept cross-postings or not.
200 (push (if no-crosspost '| '&) split)
201 ;; Even if we can cross-post, catch-all should not get
203 (if (and catch-all (not no-crosspost))
204 (setq split (list '| split catch-all)))
207 (provide 'gnus-mlspl)