*** empty log message ***
[gnus] / lisp / gnus-mlspl.el
1 ;;; gnus-mlsplt.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-mlsplt-updated-hook nil
28   "Hook called just after nnmail-split-fancy is updated by
29 gnus-mlsplt-update")
30
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")
34
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.
38
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."
42   (interactive "P")
43   (setq nnmail-split-methods 'nnmail-split-fancy)
44   (when catch-all
45     (setq gnus-mlsplt-default-catch-all-group catch-all))
46   (gnus-mlsplt-update)
47   (when auto-update
48     (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-mlsplt-update)))
49
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)"
53   (interactive)
54   (setq nnmail-split-fancy
55         (gnus-mlsplt-fancy
56          nil nil (or catch-all gnus-mlsplt-default-catch-all-group)))
57   (run-hooks 'gnus-mlsplt-updated-hook)
58   )
59
60 (defun gnus-mlsplt ()
61   "Uses information from group parameters in order to split mail.
62 See gnus-mlsplt-fancy for more information.
63
64 If no group is defined as catch-all, the value of
65 gnus-mlsplt-default-catch-all-group is used.
66
67 gnus-mlsplt is a valid value for nnmail-split-methods."
68   (let (nnmail-split-fancy)
69     (gnus-mlsplt-update
70      gnus-mlsplt-default-catch-all-group)
71     (nnmail-split-fancy)))
72
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
77
78 \(: gnus-mlsplt-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
79
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.
83
84 if NO-CROSSPOST is ommitted or nil, a & split will be returned,
85 otherwise, a | split, that does not allow crossposting, will be
86 returned.
87
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.
92
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.
101
102 For example, given the following group parameters:
103
104 nnml:mail.bar:
105 \((to-address . \"bar@femail.com\")
106  (split-regexp . \".*@femail\\\\.com\"))
107 nnml:mail.foo:
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\"))
112 nnml:mail.others:
113 \((split-spec . catch-all))
114
115 Calling (gnus-mlsplt-fancy nil nil \"mail.misc\") returns:
116
117 \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
118            \"nnml:mail.bar\")
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))
123          split)
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)
129                   (and (listp groups)
130                        (memq group 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))
136             (setq group-clean
137                   (if group-clean
138                       (substring group (1+ group-clean))
139                     group))
140             (if split-spec
141                 (if (eq split-spec 'catch-all)
142                     ;; Emit catch-all only when requested
143                     (when catch-all
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
156                   (setq split-regexp
157                         (concat
158                          "\\("
159                          (mapconcat
160                           'identity
161                           (append
162                            (and to-address (list (regexp-quote to-address)))
163                            (and to-list (list (regexp-quote to-list)))
164                            (and extra-aliases
165                                 (if (listp extra-aliases)
166                                     (mapcar 'regexp-quote extra-aliases)
167                                   (list extra-aliases)))
168                            (and split-regexp (list split-regexp)))
169                           "\\|")
170                          "\\)"))
171                   ;; Now create the new SPLIT
172                   (push (append
173                          (list 'any split-regexp)
174                          ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
175                          (if (listp split-exclude)
176                              (mapcon (lambda (arg) (cons '- arg))
177                                      split-exclude)
178                            (list '- split-exclude))
179                          (list group-clean))
180                         split)
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
193     ;; cross-posts.
194     (if (and catch-all (not no-crosspost))
195         (setq split (list '| split catch-all)))
196     split))
197
198 (provide 'gnus-mlsplt)