*** empty log message ***
[gnus] / lisp / gnus-mlspl.el
1 ;;; gnus-mlspl.el --- a group params-based mail splitting mechanism
2 ;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
3
4 ;; Author: Alexandre Oliva <oliva@dcc.unicamp.br>
5 ;; Keywords: news, mail
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 (require 'gnus)
23 (require 'gnus-sum)
24 (require 'gnus-group)
25 (require 'nnmail)
26
27 (defvar gnus-mlsplit-updated-hook nil
28   "Hook called just after nnmail-split-fancy is updated by
29 gnus-mlsplit-update")
30
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")
34
35 ;;;###autoload
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.
39
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."
43   (interactive "P")
44   (setq nnmail-split-methods 'nnmail-split-fancy)
45   (when catch-all
46     (setq gnus-mlsplit-default-catch-all-group catch-all))
47   (gnus-mlsplit-update)
48   (when auto-update
49     (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-mlsplit-update)))
50
51 ;;;###autoload
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)"
55   (interactive)
56   (setq nnmail-split-fancy
57         (gnus-mlsplit-fancy
58          nil nil (or catch-all gnus-mlsplit-default-catch-all-group)))
59   (run-hooks 'gnus-mlsplit-updated-hook)
60   )
61
62 ;;;###autoload
63 (defun gnus-mlsplit ()
64   "Uses information from group parameters in order to split mail.
65 See gnus-mlsplit-fancy for more information.
66
67 If no group is defined as catch-all, the value of
68 gnus-mlsplit-default-catch-all-group is used.
69
70 gnus-mlsplit is a valid value for nnmail-split-methods."
71   (let (nnmail-split-fancy)
72     (gnus-mlsplit-update
73      gnus-mlsplit-default-catch-all-group)
74     (nnmail-split-fancy)))
75
76 ;;;###autoload
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
81
82 \(: gnus-mlsplit-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
83
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.
87
88 if NO-CROSSPOST is ommitted or nil, a & split will be returned,
89 otherwise, a | split, that does not allow crossposting, will be
90 returned.
91
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.
96
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.
105
106 For example, given the following group parameters:
107
108 nnml:mail.bar:
109 \((to-address . \"bar@femail.com\")
110  (split-regexp . \".*@femail\\\\.com\"))
111 nnml:mail.foo:
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\"))
116 nnml:mail.others:
117 \((split-spec . catch-all))
118
119 Calling (gnus-mlsplit-fancy nil nil \"mail.misc\") returns:
120
121 \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
122            \"nnml:mail.bar\")
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))
127          split)
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)
133                   (and (listp groups)
134                        (memq group 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))
140             (setq group-clean
141                   (if group-clean
142                       (substring group (1+ group-clean))
143                     group))
144             (if split-spec
145                 (if (eq split-spec 'catch-all)
146                     ;; Emit catch-all only when requested
147                     (when catch-all
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
160                   (setq split-regexp
161                         (concat
162                          "\\("
163                          (mapconcat
164                           'identity
165                           (append
166                            (and to-address (list (regexp-quote to-address)))
167                            (and to-list (list (regexp-quote to-list)))
168                            (and extra-aliases
169                                 (if (listp extra-aliases)
170                                     (mapcar 'regexp-quote extra-aliases)
171                                   (list extra-aliases)))
172                            (and split-regexp (list split-regexp)))
173                           "\\|")
174                          "\\)"))
175                   ;; Now create the new SPLIT
176                   (push (append
177                          (list 'any split-regexp)
178                          ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
179                          (if (listp split-exclude)
180                              (mapcon (lambda (arg) (cons '- arg))
181                                      split-exclude)
182                            (list '- split-exclude))
183                          (list group-clean))
184                         split)
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
197     ;; cross-posts.
198     (if (and catch-all (not no-crosspost))
199         (setq split (list '| split catch-all)))
200     split))
201
202 (provide 'gnus-mlspl)