1 ;;; gnus-mlspl.el --- a group params-based mail splitting mechanism
3 ;; Copyright (C) 1998-2018 Free Software Foundation, Inc.
5 ;; Author: Alexandre Oliva <oliva@lsd.ic.unicamp.br>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
32 (defvar gnus-group-split-updated-hook nil
33 "Hook called just after `nnmail-split-fancy' is updated by
34 `gnus-group-split-update'.")
36 (defvar gnus-group-split-default-catch-all-group
"mail.misc"
37 "Group name (or arbitrary fancy split) with default splitting rules.
38 Used by `gnus-group-split' and `gnus-group-split-update' as a fallback
39 split, in case none of the group-based splits matches.")
42 (defun gnus-group-split-setup (&optional auto-update catch-all
)
43 "Set up the split for `nnmail-split-fancy'.
44 Sets things up so that nnmail-split-fancy is used for mail
45 splitting, and defines the variable nnmail-split-fancy according with
48 If AUTO-UPDATE is non-nil (prefix argument accepted, if called
49 interactively), it makes sure nnmail-split-fancy is re-computed before
50 getting new mail, by adding `gnus-group-split-update' to
51 `nnmail-pre-get-new-mail-hook'.
53 A non-nil CATCH-ALL replaces the current value of
54 `gnus-group-split-default-catch-all-group'. This variable is only used
55 by gnus-group-split-update, and only when its CATCH-ALL argument is
56 nil. This argument may contain any fancy split, that will be added as
57 the last split in a `|' split produced by `gnus-group-split-fancy',
58 unless overridden by any group marked as a catch-all group. Typical
59 uses are as simple as the name of a default mail group, but more
60 elaborate fancy splits may also be useful to split mail that doesn't
61 match any of the group-specified splitting rules. See
62 `gnus-group-split-fancy' for details."
64 (setq nnmail-split-methods
'nnmail-split-fancy
)
66 (setq gnus-group-split-default-catch-all-group catch-all
))
67 (gnus-group-split-update)
69 (add-hook 'nnmail-pre-get-new-mail-hook
'gnus-group-split-update
)))
72 (defun gnus-group-split-update (&optional catch-all
)
73 "Computes nnmail-split-fancy from group params and CATCH-ALL.
74 It does this by calling (gnus-group-split-fancy nil nil CATCH-ALL).
76 If CATCH-ALL is nil, `gnus-group-split-default-catch-all-group' is used
77 instead. This variable is set by `gnus-group-split-setup'."
79 (setq nnmail-split-fancy
80 (gnus-group-split-fancy
81 nil
(null nnmail-crosspost
)
82 (or catch-all gnus-group-split-default-catch-all-group
)))
83 (run-hooks 'gnus-group-split-updated-hook
))
86 (defun gnus-group-split ()
87 "Use information from group parameters in order to split mail.
88 See `gnus-group-split-fancy' for more information.
90 `gnus-group-split' is a valid value for `nnmail-split-methods'."
91 (let (nnmail-split-fancy)
92 (gnus-group-split-update)
93 (nnmail-split-fancy)))
96 (defun gnus-group-split-fancy
97 (&optional groups no-crosspost catch-all
)
98 "Uses information from group parameters in order to split mail.
99 It can be embedded into `nnmail-split-fancy' lists with the SPLIT
101 \(: gnus-group-split-fancy GROUPS NO-CROSSPOST CATCH-ALL)
103 GROUPS may be a regular expression or a list of group names, that will
104 be used to select candidate groups. If it is omitted or nil, all
105 existing groups are considered.
107 if NO-CROSSPOST is omitted or nil, a & split will be returned,
108 otherwise, a | split, that does not allow crossposting, will be
111 For each selected group, a SPLIT is composed like this: if SPLIT-SPEC
112 is specified, this split is returned as-is (unless it is nil: in this
113 case, the group is ignored). Otherwise, if TO-ADDRESS, TO-LIST and/or
114 EXTRA-ALIASES are specified, a regexp that matches any of them is
115 constructed (extra-aliases may be a list). Additionally, if
116 SPLIT-REGEXP is specified, the regexp will be extended so that it
117 matches this regexp too, and if SPLIT-EXCLUDE is specified, RESTRICT
118 clauses will be generated.
120 If CATCH-ALL is nil, no catch-all handling is performed, regardless of
121 catch-all marks in group parameters. Otherwise, if there is no
122 selected group whose SPLIT-REGEXP matches the empty string, nor is
123 there a selected group whose SPLIT-SPEC is `catch-all', this fancy
124 split (say, a group name) will be appended to the returned SPLIT list,
125 as the last element of a `|' SPLIT.
127 For example, given the following group parameters:
130 \((to-address . \"bar@femail.com\")
131 (split-regexp . \".*@femail\\\\.com\"))
133 \((to-list . \"foo@nowhere.gov\")
134 (extra-aliases \"foo@localhost\" \"foo-redist@home\")
135 (split-exclude \"bugs-foo\" \"rambling-foo\")
136 (admin-address . \"foo-request@nowhere.gov\"))
138 \((split-spec . catch-all))
140 Calling (gnus-group-split-fancy nil nil \"mail.others\") returns:
142 \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
144 (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\"
145 - \"bugs-foo\" - \"rambling-foo\" \"mail.foo\"))
147 (let ((group-names (if (and (listp groups
)
154 (let ((group (gnus-info-group info
)))
156 (and (stringp groups
)
157 (string-match groups group
)))
159 (append gnus-newsrc-alist gnus-parameters
))))))
161 (dolist (group group-names
)
162 (let ((params (gnus-group-find-parameter group
)))
163 ;; Skip groups without param (or nonexistent)
164 (when (not (null params
))
165 (let ((split-spec (assoc 'split-spec params
)) group-clean
)
166 ;; Remove backend from group name
167 (setq group-clean
(string-match ":" group
))
170 (substring group
(1+ group-clean
))
173 (when (setq split-spec
(cdr split-spec
))
174 (if (eq split-spec
'catch-all
)
175 ;; Emit catch-all only when requested
177 (setq catch-all group-clean
))
178 ;; Append split-spec to the main split
179 (push split-spec split
)))
180 ;; Let's deduce split-spec from other params
181 (let ((to-address (cdr (assoc 'to-address params
)))
182 (to-list (cdr (assoc 'to-list params
)))
183 (extra-aliases (cdr (assoc 'extra-aliases params
)))
184 (split-regexp (cdr (assoc 'split-regexp params
)))
185 (split-exclude (cdr (assoc 'split-exclude params
)))
186 (match-list (cdr (assoc 'match-list params
))))
187 (when (or to-address to-list extra-aliases split-regexp
)
188 ;; regexp-quote to-address, to-list and extra-aliases
189 ;; and add them all to split-regexp
196 (and to-address
(list (regexp-quote to-address
)))
197 (and to-list
(list (regexp-quote to-list
)))
199 (if (listp extra-aliases
)
200 (mapcar 'regexp-quote extra-aliases
)
201 (list extra-aliases
)))
202 (and split-regexp
(list split-regexp
)))
205 ;; Now create the new SPLIT
206 (let ((split-regexp-with-list-ids
207 (replace-regexp-in-string "@" "[@.]" split-regexp t t
))
209 ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
210 (if (listp split-exclude
)
212 (mapcar (lambda (arg) (list '- arg
))
214 (list '- split-exclude
))))
217 ;; Match RFC2919 IDs or mail addresses
219 (list 'list split-regexp-with-list-ids
)
224 (list 'any split-regexp
)
228 ;; If it matches the empty string, it is a catch-all
229 (when (string-match split-regexp
"")
230 (setq catch-all nil
)))))))))
231 ;; Add catch-all if not crossposting
232 (if (and catch-all no-crosspost
)
233 (push catch-all split
))
234 ;; Move it to the tail, while arranging that SPLITs appear in the
235 ;; same order as groups.
236 (setq split
(reverse split
))
237 ;; Decide whether to accept cross-postings or not.
238 (push (if no-crosspost
'|
'&) split
)
239 ;; Even if we can cross-post, catch-all should not get
241 (if (and catch-all
(not no-crosspost
))
242 (setq split
(list '| split catch-all
)))
245 (provide 'gnus-mlspl
)
247 ;;; gnus-mlspl.el ends here