Commit | Line | Data |
---|---|---|
c113de23 | 1 | ;;; gnus-mlspl.el --- a group params-based mail splitting mechanism |
23f87bed | 2 | |
e84b4b86 | 3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
5df4f04c | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
c113de23 GM |
5 | |
6 | ;; Author: Alexandre Oliva <oliva@lsd.ic.unicamp.br> | |
7 | ;; Keywords: news, mail | |
8 | ||
2be7dabc GM |
9 | ;; This file is part of GNU Emacs. |
10 | ||
5e809f55 GM |
11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation, either version 3 of the License, or | |
14 | ;; (at your option) any later version. | |
c113de23 | 15 | |
5e809f55 GM |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
c113de23 GM |
20 | |
21 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c113de23 | 23 | |
715a2ca2 PJ |
24 | ;;; Commentary: |
25 | ||
26 | ;;; Code: | |
27 | ||
d8cc10e1 | 28 | (eval-when-compile (require 'cl)) |
c113de23 GM |
29 | (require 'gnus) |
30 | (require 'gnus-sum) | |
31 | (require 'gnus-group) | |
32 | (require 'nnmail) | |
33 | ||
34 | (defvar gnus-group-split-updated-hook nil | |
01c52d31 MB |
35 | "Hook called just after `nnmail-split-fancy' is updated by |
36 | `gnus-group-split-update'.") | |
c113de23 GM |
37 | |
38 | (defvar gnus-group-split-default-catch-all-group "mail.misc" | |
10b3dd25 | 39 | "Group name (or arbitrary fancy split) with default splitting rules. |
01c52d31 | 40 | Used by `gnus-group-split' and `gnus-group-split-update' as a fallback |
10b3dd25 | 41 | split, in case none of the group-based splits matches.") |
c113de23 GM |
42 | |
43 | ;;;###autoload | |
44 | (defun gnus-group-split-setup (&optional auto-update catch-all) | |
01c52d31 | 45 | "Set up the split for `nnmail-split-fancy'. |
c113de23 GM |
46 | Sets things up so that nnmail-split-fancy is used for mail |
47 | splitting, and defines the variable nnmail-split-fancy according with | |
48 | group parameters. | |
49 | ||
50 | If AUTO-UPDATE is non-nil (prefix argument accepted, if called | |
51 | interactively), it makes sure nnmail-split-fancy is re-computed before | |
01c52d31 MB |
52 | getting new mail, by adding `gnus-group-split-update' to |
53 | `nnmail-pre-get-new-mail-hook'. | |
10b3dd25 DL |
54 | |
55 | A non-nil CATCH-ALL replaces the current value of | |
01c52d31 | 56 | `gnus-group-split-default-catch-all-group'. This variable is only used |
10b3dd25 DL |
57 | by gnus-group-split-update, and only when its CATCH-ALL argument is |
58 | nil. This argument may contain any fancy split, that will be added as | |
01c52d31 | 59 | the last split in a `|' split produced by `gnus-group-split-fancy', |
10b3dd25 DL |
60 | unless overridden by any group marked as a catch-all group. Typical |
61 | uses are as simple as the name of a default mail group, but more | |
62 | elaborate fancy splits may also be useful to split mail that doesn't | |
63 | match any of the group-specified splitting rules. See | |
23f87bed | 64 | `gnus-group-split-fancy' for details." |
c113de23 GM |
65 | (interactive "P") |
66 | (setq nnmail-split-methods 'nnmail-split-fancy) | |
67 | (when catch-all | |
68 | (setq gnus-group-split-default-catch-all-group catch-all)) | |
69 | (gnus-group-split-update) | |
70 | (when auto-update | |
71 | (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-group-split-update))) | |
72 | ||
73 | ;;;###autoload | |
74 | (defun gnus-group-split-update (&optional catch-all) | |
23f87bed MB |
75 | "Computes nnmail-split-fancy from group params and CATCH-ALL. |
76 | It does this by calling by calling (gnus-group-split-fancy nil | |
77 | nil CATCH-ALL). | |
10b3dd25 | 78 | |
01c52d31 MB |
79 | If CATCH-ALL is nil, `gnus-group-split-default-catch-all-group' is used |
80 | instead. This variable is set by `gnus-group-split-setup'." | |
c113de23 GM |
81 | (interactive) |
82 | (setq nnmail-split-fancy | |
83 | (gnus-group-split-fancy | |
d8cc10e1 DL |
84 | nil (null nnmail-crosspost) |
85 | (or catch-all gnus-group-split-default-catch-all-group))) | |
c113de23 GM |
86 | (run-hooks 'gnus-group-split-updated-hook)) |
87 | ||
88 | ;;;###autoload | |
89 | (defun gnus-group-split () | |
01c52d31 | 90 | "Use information from group parameters in order to split mail. |
23f87bed | 91 | See `gnus-group-split-fancy' for more information. |
c113de23 | 92 | |
01c52d31 | 93 | `gnus-group-split' is a valid value for `nnmail-split-methods'." |
c113de23 | 94 | (let (nnmail-split-fancy) |
10b3dd25 | 95 | (gnus-group-split-update) |
c113de23 GM |
96 | (nnmail-split-fancy))) |
97 | ||
98 | ;;;###autoload | |
99 | (defun gnus-group-split-fancy | |
100 | (&optional groups no-crosspost catch-all) | |
11f6edcc PJ |
101 | "Uses information from group parameters in order to split mail. |
102 | It can be embedded into `nnmail-split-fancy' lists with the SPLIT | |
c113de23 GM |
103 | |
104 | \(: gnus-group-split-fancy GROUPS NO-CROSSPOST CATCH-ALL\) | |
105 | ||
106 | GROUPS may be a regular expression or a list of group names, that will | |
8f688cb0 | 107 | be used to select candidate groups. If it is omitted or nil, all |
c113de23 GM |
108 | existing groups are considered. |
109 | ||
8f688cb0 | 110 | if NO-CROSSPOST is omitted or nil, a & split will be returned, |
c113de23 GM |
111 | otherwise, a | split, that does not allow crossposting, will be |
112 | returned. | |
113 | ||
c113de23 GM |
114 | For each selected group, a SPLIT is composed like this: if SPLIT-SPEC |
115 | is specified, this split is returned as-is (unless it is nil: in this | |
116 | case, the group is ignored). Otherwise, if TO-ADDRESS, TO-LIST and/or | |
117 | EXTRA-ALIASES are specified, a regexp that matches any of them is | |
118 | constructed (extra-aliases may be a list). Additionally, if | |
119 | SPLIT-REGEXP is specified, the regexp will be extended so that it | |
120 | matches this regexp too, and if SPLIT-EXCLUDE is specified, RESTRICT | |
121 | clauses will be generated. | |
122 | ||
10b3dd25 DL |
123 | If CATCH-ALL is nil, no catch-all handling is performed, regardless of |
124 | catch-all marks in group parameters. Otherwise, if there is no | |
125 | selected group whose SPLIT-REGEXP matches the empty string, nor is | |
126 | there a selected group whose SPLIT-SPEC is 'catch-all, this fancy | |
127 | split (say, a group name) will be appended to the returned SPLIT list, | |
128 | as the last element of a '| SPLIT. | |
129 | ||
c113de23 GM |
130 | For example, given the following group parameters: |
131 | ||
132 | nnml:mail.bar: | |
133 | \((to-address . \"bar@femail.com\") | |
134 | (split-regexp . \".*@femail\\\\.com\")) | |
135 | nnml:mail.foo: | |
136 | \((to-list . \"foo@nowhere.gov\") | |
137 | (extra-aliases \"foo@localhost\" \"foo-redist@home\") | |
138 | (split-exclude \"bugs-foo\" \"rambling-foo\") | |
139 | (admin-address . \"foo-request@nowhere.gov\")) | |
140 | nnml:mail.others: | |
141 | \((split-spec . catch-all)) | |
142 | ||
23f87bed | 143 | Calling (gnus-group-split-fancy nil nil \"mail.others\") returns: |
c113de23 GM |
144 | |
145 | \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\" | |
146 | \"mail.bar\") | |
147 | (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\" | |
23f87bed | 148 | - \"bugs-foo\" - \"rambling-foo\" \"mail.foo\")) |
c113de23 GM |
149 | \"mail.others\")" |
150 | (let* ((newsrc (cdr gnus-newsrc-alist)) | |
151 | split) | |
152 | (dolist (info newsrc) | |
153 | (let ((group (gnus-info-group info)) | |
154 | (params (gnus-info-params info))) | |
155 | ;; For all GROUPs that match the specified GROUPS | |
156 | (when (or (not groups) | |
157 | (and (listp groups) | |
158 | (memq group groups)) | |
159 | (and (stringp groups) | |
160 | (string-match groups group))) | |
161 | (let ((split-spec (assoc 'split-spec params)) group-clean) | |
162 | ;; Remove backend from group name | |
163 | (setq group-clean (string-match ":" group)) | |
164 | (setq group-clean | |
165 | (if group-clean | |
166 | (substring group (1+ group-clean)) | |
167 | group)) | |
168 | (if split-spec | |
169 | (when (setq split-spec (cdr split-spec)) | |
170 | (if (eq split-spec 'catch-all) | |
171 | ;; Emit catch-all only when requested | |
172 | (when catch-all | |
173 | (setq catch-all group-clean)) | |
174 | ;; Append split-spec to the main split | |
175 | (push split-spec split))) | |
176 | ;; Let's deduce split-spec from other params | |
177 | (let ((to-address (cdr (assoc 'to-address params))) | |
178 | (to-list (cdr (assoc 'to-list params))) | |
179 | (extra-aliases (cdr (assoc 'extra-aliases params))) | |
180 | (split-regexp (cdr (assoc 'split-regexp params))) | |
181 | (split-exclude (cdr (assoc 'split-exclude params)))) | |
182 | (when (or to-address to-list extra-aliases split-regexp) | |
183 | ;; regexp-quote to-address, to-list and extra-aliases | |
184 | ;; and add them all to split-regexp | |
185 | (setq split-regexp | |
186 | (concat | |
187 | "\\(" | |
188 | (mapconcat | |
189 | 'identity | |
190 | (append | |
191 | (and to-address (list (regexp-quote to-address))) | |
192 | (and to-list (list (regexp-quote to-list))) | |
193 | (and extra-aliases | |
194 | (if (listp extra-aliases) | |
195 | (mapcar 'regexp-quote extra-aliases) | |
196 | (list extra-aliases))) | |
197 | (and split-regexp (list split-regexp))) | |
198 | "\\|") | |
199 | "\\)")) | |
200 | ;; Now create the new SPLIT | |
201 | (push (append | |
202 | (list 'any split-regexp) | |
203 | ;; Generate RESTRICTs for SPLIT-EXCLUDEs. | |
204 | (if (listp split-exclude) | |
23f87bed MB |
205 | (apply #'append |
206 | (mapcar (lambda (arg) (list '- arg)) | |
207 | split-exclude)) | |
c113de23 GM |
208 | (list '- split-exclude)) |
209 | (list group-clean)) | |
210 | split) | |
211 | ;; If it matches the empty string, it is a catch-all | |
212 | (when (string-match split-regexp "") | |
213 | (setq catch-all nil))))))))) | |
214 | ;; Add catch-all if not crossposting | |
215 | (if (and catch-all no-crosspost) | |
d8cc10e1 | 216 | (push catch-all split)) |
c113de23 GM |
217 | ;; Move it to the tail, while arranging that SPLITs appear in the |
218 | ;; same order as groups. | |
219 | (setq split (reverse split)) | |
220 | ;; Decide whether to accept cross-postings or not. | |
221 | (push (if no-crosspost '| '&) split) | |
222 | ;; Even if we can cross-post, catch-all should not get | |
223 | ;; cross-posts. | |
224 | (if (and catch-all (not no-crosspost)) | |
225 | (setq split (list '| split catch-all))) | |
226 | split)) | |
227 | ||
228 | (provide 'gnus-mlspl) | |
715a2ca2 PJ |
229 | |
230 | ;;; gnus-mlspl.el ends here |