31717
|
1 ;;; gnus-mlspl.el --- a group params-based mail splitting mechanism
|
|
2
|
|
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Alexandre Oliva <oliva@lsd.ic.unicamp.br>
|
|
6 ;; Keywords: news, mail
|
|
7
|
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
11 ;; it under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 ;; GNU General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
|
24
|
|
25 (require 'gnus)
|
|
26 (require 'gnus-sum)
|
|
27 (require 'gnus-group)
|
|
28 (require 'nnmail)
|
|
29
|
|
30 (defvar gnus-group-split-updated-hook nil
|
|
31 "Hook called just after nnmail-split-fancy is updated by gnus-group-split-update.")
|
|
32
|
|
33 (defvar gnus-group-split-default-catch-all-group "mail.misc"
|
|
34 "Group used by gnus-group-split and gnus-group-split-update as default catch-all group.")
|
|
35
|
|
36 ;;;###autoload
|
|
37 (defun gnus-group-split-setup (&optional auto-update catch-all)
|
|
38 "Set up the split for nnmail-split-fancy.
|
|
39 Sets things up so that nnmail-split-fancy is used for mail
|
|
40 splitting, and defines the variable nnmail-split-fancy according with
|
|
41 group parameters.
|
|
42
|
|
43 If AUTO-UPDATE is non-nil (prefix argument accepted, if called
|
|
44 interactively), it makes sure nnmail-split-fancy is re-computed before
|
|
45 getting new mail, by adding gnus-group-split-update to
|
|
46 nnmail-pre-get-new-mail-hook."
|
|
47 (interactive "P")
|
|
48 (setq nnmail-split-methods 'nnmail-split-fancy)
|
|
49 (when catch-all
|
|
50 (setq gnus-group-split-default-catch-all-group catch-all))
|
|
51 (gnus-group-split-update)
|
|
52 (when auto-update
|
|
53 (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-group-split-update)))
|
|
54
|
|
55 ;;;###autoload
|
|
56 (defun gnus-group-split-update (&optional catch-all)
|
|
57 "Computes nnmail-split-fancy from group params.
|
|
58 It does this by calling \(gnus-group-split-fancy nil nil DEFAULTGROUP)."
|
|
59 (interactive)
|
|
60 (setq nnmail-split-fancy
|
|
61 (gnus-group-split-fancy
|
|
62 nil nil (or catch-all gnus-group-split-default-catch-all-group)))
|
|
63 (run-hooks 'gnus-group-split-updated-hook))
|
|
64
|
|
65 ;;;###autoload
|
|
66 (defun gnus-group-split ()
|
|
67 "Uses information from group parameters in order to split mail.
|
|
68 See gnus-group-split-fancy for more information.
|
|
69
|
|
70 If no group is defined as catch-all, the value of
|
|
71 gnus-group-split-default-catch-all-group is used.
|
|
72
|
|
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)))
|
|
78
|
|
79 ;;;###autoload
|
|
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
|
|
84
|
|
85 \(: gnus-group-split-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
|
|
86
|
|
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.
|
|
90
|
|
91 if NO-CROSSPOST is ommitted or nil, a & split will be returned,
|
|
92 otherwise, a | split, that does not allow crossposting, will be
|
|
93 returned.
|
|
94
|
|
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.
|
|
99
|
|
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.
|
|
108
|
|
109 For example, given the following group parameters:
|
|
110
|
|
111 nnml:mail.bar:
|
|
112 \((to-address . \"bar@femail.com\")
|
|
113 (split-regexp . \".*@femail\\\\.com\"))
|
|
114 nnml:mail.foo:
|
|
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\"))
|
|
119 nnml:mail.others:
|
|
120 \((split-spec . catch-all))
|
|
121
|
|
122 Calling (gnus-group-split-fancy nil nil \"mail.misc\") returns:
|
|
123
|
|
124 \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
|
|
125 \"mail.bar\")
|
|
126 (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\"
|
|
127 - \"bugs-foo\" - \"rambling-foo\" \"mail.foo\"))
|
|
128 \"mail.others\")"
|
|
129 (let* ((newsrc (cdr gnus-newsrc-alist))
|
|
130 split)
|
|
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)
|
|
136 (and (listp groups)
|
|
137 (memq group groups))
|
|
138 (and (stringp groups)
|
|
139 (string-match groups group)))
|
|
140 (let ((split-spec (assoc 'split-spec params)) group-clean)
|
|
141 ;; Remove backend from group name
|
|
142 (setq group-clean (string-match ":" group))
|
|
143 (setq group-clean
|
|
144 (if group-clean
|
|
145 (substring group (1+ group-clean))
|
|
146 group))
|
|
147 (if split-spec
|
|
148 (when (setq split-spec (cdr split-spec))
|
|
149 (if (eq split-spec 'catch-all)
|
|
150 ;; Emit catch-all only when requested
|
|
151 (when catch-all
|
|
152 (setq catch-all group-clean))
|
|
153 ;; Append split-spec to the main split
|
|
154 (push split-spec split)))
|
|
155 ;; Let's deduce split-spec from other params
|
|
156 (let ((to-address (cdr (assoc 'to-address params)))
|
|
157 (to-list (cdr (assoc 'to-list params)))
|
|
158 (extra-aliases (cdr (assoc 'extra-aliases params)))
|
|
159 (split-regexp (cdr (assoc 'split-regexp params)))
|
|
160 (split-exclude (cdr (assoc 'split-exclude params))))
|
|
161 (when (or to-address to-list extra-aliases split-regexp)
|
|
162 ;; regexp-quote to-address, to-list and extra-aliases
|
|
163 ;; and add them all to split-regexp
|
|
164 (setq split-regexp
|
|
165 (concat
|
|
166 "\\("
|
|
167 (mapconcat
|
|
168 'identity
|
|
169 (append
|
|
170 (and to-address (list (regexp-quote to-address)))
|
|
171 (and to-list (list (regexp-quote to-list)))
|
|
172 (and extra-aliases
|
|
173 (if (listp extra-aliases)
|
|
174 (mapcar 'regexp-quote extra-aliases)
|
|
175 (list extra-aliases)))
|
|
176 (and split-regexp (list split-regexp)))
|
|
177 "\\|")
|
|
178 "\\)"))
|
|
179 ;; Now create the new SPLIT
|
|
180 (push (append
|
|
181 (list 'any split-regexp)
|
|
182 ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
|
|
183 (if (listp split-exclude)
|
|
184 (let ((seq split-exclude)
|
|
185 res)
|
|
186 (while seq
|
|
187 (push (cons '- (pop seq))
|
|
188 res))
|
|
189 (apply #'nconc (nreverse res)))
|
|
190 (list '- split-exclude))
|
|
191 (list group-clean))
|
|
192 split)
|
|
193 ;; If it matches the empty string, it is a catch-all
|
|
194 (when (string-match split-regexp "")
|
|
195 (setq catch-all nil)))))))))
|
|
196 ;; Add catch-all if not crossposting
|
|
197 (if (and catch-all no-crosspost)
|
|
198 (push split catch-all))
|
|
199 ;; Move it to the tail, while arranging that SPLITs appear in the
|
|
200 ;; same order as groups.
|
|
201 (setq split (reverse split))
|
|
202 ;; Decide whether to accept cross-postings or not.
|
|
203 (push (if no-crosspost '| '&) split)
|
|
204 ;; Even if we can cross-post, catch-all should not get
|
|
205 ;; cross-posts.
|
|
206 (if (and catch-all (not no-crosspost))
|
|
207 (setq split (list '| split catch-all)))
|
|
208 split))
|
|
209
|
|
210 (provide 'gnus-mlspl)
|