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