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.
27 (defvar gnus-mlsplit-updated-hook nil
28 "Hook called just after nnmail-split-fancy is updated by
31 (defvar gnus-mlsplit-default-catch-all-group "mail.misc"
32 "Group used by gnus-mlsplit and
33 gnus-mlsplit-update as default catch-all group")
36 (defun gnus-mlsplit-setup (&optional auto-update catch-all)
37 "Sets things up so that nnmail-split-fancy is used for mail splitting,
38 and defines the variable nnmail-split-fancy according with group parameters.
40 if AUTO-UPDATE is non-nil (prefix argument accepted, if called interactive),
41 makes sure nnmail-split-fancy is re-computed before getting new mail,
42 by adding gnus-mlsplit-update to nnmail-pre-get-new-mail-hook."
44 (setq nnmail-split-methods 'nnmail-split-fancy)
46 (setq gnus-mlsplit-default-catch-all-group catch-all))
49 (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-mlsplit-update)))
52 (defun gnus-mlsplit-update (&optional catch-all)
53 "Computes nnmail-split-fancy from group params, by calling
54 \(gnus-mlsplit-fancy nil nil DEFAULTGROUP)"
56 (setq nnmail-split-fancy
58 nil nil (or catch-all gnus-mlsplit-default-catch-all-group)))
59 (run-hooks 'gnus-mlsplit-updated-hook)
63 (defun gnus-mlsplit ()
64 "Uses information from group parameters in order to split mail.
65 See gnus-mlsplit-fancy for more information.
67 If no group is defined as catch-all, the value of
68 gnus-mlsplit-default-catch-all-group is used.
70 gnus-mlsplit is a valid value for nnmail-split-methods."
71 (let (nnmail-split-fancy)
73 gnus-mlsplit-default-catch-all-group)
74 (nnmail-split-fancy)))
77 (defun gnus-mlsplit-fancy
78 (&optional groups no-crosspost catch-all)
79 "Uses information from group parameters in order to split mail.
80 It can be embedded into nnmail-split-fancy lists with the SPLIT
82 \(: gnus-mlsplit-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
84 GROUPS may be a regular expression or a list of group names, that will
85 be used to select candidate groups. If it is ommited or nil, all
86 existing groups are considered.
88 if NO-CROSSPOST is ommitted or nil, a & split will be returned,
89 otherwise, a | split, that does not allow crossposting, will be
92 if CATCH-ALL is not nil, and there is no selected group whose
93 split-regexp matches the empty string, nor is there a selected group
94 whose SPLIT-SPEC is 'catch-all, this group name will be appended to
95 the returned SPLIT list, as the last element in a '| SPLIT.
97 For each selected group, a SPLIT is composed like this: if split-spec
98 is specified, this split is returned as-is (unless it is nil: in this
99 case, the group is ignored). Otherwise, if TO-ADDRESS, TO-LIST and/or
100 EXTRA-ALIASES are specified, a regexp that matches any of them is
101 constructed (extra-aliases may be a list). Additionally, if
102 SPLIT-REGEXP is specified, the regexp will be extended so that it
103 matches this regexp too, and if SPLIT-EXCLUDE is specified, RESTRICT
104 clauses will be generated.
106 For example, given the following group parameters:
109 \((to-address . \"bar@femail.com\")
110 (split-regexp . \".*@femail\\\\.com\"))
112 \((to-list . \"foo@nowhere.gov\")
113 (extra-aliases \"foo@localhost\" \"foo-redist@home\")
114 (split-exclude \"bugs-foo\" \"rambling-foo\")
115 (admin-address . \"foo-request@nowhere.gov\"))
117 \((split-spec . catch-all))
119 Calling (gnus-mlsplit-fancy nil nil \"mail.misc\") returns:
121 \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
123 (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\"
124 - \"bugs-foo\" - \"rambling-foo\" \"nnml:mail.foo\"))
125 \"nnml:mail.others\")"
126 (let* ((newsrc (cdr gnus-newsrc-alist))
128 (dolist (info newsrc)
129 (let ((group (gnus-info-group info))
130 (params (gnus-info-params info)))
131 ;; For all GROUPs that match the specified GROUPS
132 (when (or (not groups)
135 (and (stringp groups)
136 (string-match groups group)))
137 (let ((split-spec (cdr (assoc 'split-spec params))) group-clean)
138 ;; Remove backend from group name
139 (setq group-clean (string-match ":" group))
142 (substring group (1+ group-clean))
145 (if (eq split-spec 'catch-all)
146 ;; Emit catch-all only when requested
148 (setq catch-all group-clean))
149 ;; Append split-spec to the main split
150 (push split-spec split))
151 ;; Let's deduce split-spec from other params
152 (let ((to-address (cdr (assoc 'to-address params)))
153 (to-list (cdr (assoc 'to-list params)))
154 (extra-aliases (cdr (assoc 'extra-aliases params)))
155 (split-regexp (cdr (assoc 'split-regexp params)))
156 (split-exclude (cdr (assoc 'split-exclude params))))
157 (when (or to-address to-list extra-aliases split-regexp)
158 ;; regexp-quote to-address, to-list and extra-aliases
159 ;; and add them all to split-regexp
166 (and to-address (list (regexp-quote to-address)))
167 (and to-list (list (regexp-quote to-list)))
169 (if (listp extra-aliases)
170 (mapcar 'regexp-quote extra-aliases)
171 (list extra-aliases)))
172 (and split-regexp (list split-regexp)))
175 ;; Now create the new SPLIT
177 (list 'any split-regexp)
178 ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
179 (if (listp split-exclude)
180 (mapcon (lambda (arg) (cons '- arg))
182 (list '- split-exclude))
185 ;; If it matches the empty string, it is a catch-all
186 (when (string-match split-regexp "")
187 (setq catch-all nil)))))))))
188 ;; Add catch-all if not crossposting
189 (if (and catch-all no-crosspost)
190 (push split catch-all))
191 ;; Move it to the tail, while arranging that SPLITs appear in the
192 ;; same order as groups.
193 (setq split (reverse split))
194 ;; Decide whether to accept cross-postings or not.
195 (push (if no-crosspost '| '&) split)
196 ;; Even if we can cross-post, catch-all should not get
198 (if (and catch-all (not no-crosspost))
199 (setq split (list '| split catch-all)))
202 (provide 'gnus-mlspl)