*** empty log message ***
[gnus] / lisp / nnmail.el
1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news, mail
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'nnheader)
28 (require 'rmail)
29 (require 'timezone)
30 (require 'sendmail)
31
32 (defvar nnmail-split-methods
33   '(("mail.misc" ""))
34   "*Incoming mail will be split according to this variable.
35
36 If you'd like, for instance, one mail group for mail from the
37 \"4ad-l\" mailing list, one group for junk mail and one for everything
38 else, you could do something like this:
39
40  (setq nnmail-split-methods
41        '((\"mail.4ad\" \"From:.*4ad\")
42          (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
43          (\"mail.misc\" \"\")))
44
45 As you can see, this variable is a list of lists, where the first
46 element in each \"rule\" is the name of the group (which, by the way,
47 does not have to be called anything beginning with \"mail\",
48 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
49 nnmail will try to match on the header to find a fit.
50
51 The second element can also be a function.  In that case, it will be
52 called narrowed to the headers with the first element of the rule as
53 the argument.  It should return a non-nil value if it thinks that the
54 mail belongs in that group.
55
56 The last element should always have \"\" as the regexp.")
57
58 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
59 (defvar nnmail-crosspost t
60   "*If non-nil, do crossposting if several split methods match the mail.
61 If nil, the first match found will be used.")
62
63 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
64 (defvar nnmail-keep-last-article nil
65   "*If non-nil, nnmail will never delete the last expired article in a
66 directory.  You may need to set this variable if other programs are putting
67 new mail into folder numbers that Gnus has marked as expired.")
68
69 (defvar nnmail-expiry-wait 7
70   "*Articles that are older than `nnmail-expiry-wait' days will be expired.")
71
72 (defvar nnmail-expiry-wait-function nil
73   "*Variable that holds funtion to specify how old articles should be before they are expired.
74   The function will be called with the name of the group that the
75 expiry is to be performed in, and it should return an integer that
76 says how many days an article can be stored before it is considered
77 'old'. 
78
79 Eg.:
80
81 (setq nnmail-expiry-wait-function
82       (lambda (newsgroup)
83         (cond ((string-match \"private\" newsgroup) 31)
84               ((string-match \"junk\" newsgroup) 1)
85               (t 7))))")
86
87 (defvar nnmail-spool-file 
88   (or (getenv "MAIL")
89       (concat "/usr/spool/mail/" (user-login-name)))
90   "Where the mail backends will look for incoming mail.
91 This variable is \"/usr/spool/mail/$user\" by default.
92 If this variable is nil, no mail backends will read incoming mail.
93 If this variable is `procmail', the mail backends will look in
94 `nnmail-procmail-directory' for spool files.")
95
96 (defvar nnmail-procmail-directory "~/incoming/"
97   "*When using procmail (and the like), incoming mail is put in this directory.
98 The Gnus mail backends will read the mail from this directory.")
99
100 (defvar nnmail-procmail-suffix ".spool"
101   "*Suffix of files created by procmail (and the like).")
102
103 (defvar nnmail-read-incoming-hook nil
104   "*Hook that will be run after the incoming mail has been transferred.
105 The incoming mail is moved from `nnmail-spool-file' (which normally is
106 something like \"/usr/spool/mail/$user\") to the user's home
107 directory. This hook is called after the incoming mail box has been
108 emptied, and can be used to call any mail box programs you have
109 running (\"xwatch\", etc.)
110
111 Eg.
112
113 (add-hook 'nnmail-read-incoming-hook 
114            (lambda () 
115              (start-process \"mailsend\" nil 
116                             \"/local/bin/mailsend\" \"read\" \"mbox\")))")
117
118 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
119 (defvar nnmail-prepare-incoming-hook nil
120   "*Hook called before treating incoming mail.
121 The hook is run in a buffer with all the new, incoming mail.")
122
123 ;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
124 (defvar nnmail-tmp-directory nil
125   "*If non-nil, use this directory for temporary storage when reading incoming mail.")
126
127 (defvar nnmail-large-newsgroup 50
128   "*The number of the articles which indicates a large newsgroup.
129 If the number of the articles is greater than the value, verbose
130 messages will be shown to indicate the current status.")
131
132 (defvar nnmail-split-fancy "mail.misc"
133   "*Incoming mail can be split according to this fancy variable.
134 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
135
136 The format is this variable is SPLIT, where SPLIT can be one of
137 the following:
138
139 GROUP: Mail will be stored in GROUP (a string).
140
141 \(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
142   VALUE (a regexp), store the messages as specified by SPLIT.
143
144 \(| SPLIT...): Process each SPLIT expression until one of them matches.
145   A SPLIT expression is said to match if it will cause the mail
146   message to be stored in one or more groups.  
147
148 \(& SPLIT...): Process each SPLIT expression.
149
150 FIELD must match a complete field name.  VALUE must match a complete
151 word according to the fundamental mode syntax table.  You can use .*
152 in the regexps to match partial field names or words.
153
154 FIELD and VALUE can also be lisp symbols, in that case they are expanded
155 as specified in `nnmail-split-abbrev-alist'.
156
157 Example:
158
159 \(setq nnmail-split-methods 'nnmail-split-fancy
160       nnmail-split-fancy
161       ;; Messages from the mailer deamon are not crossposted to any of
162       ;; the ordinary groups.  Warnings are put in a separate group
163       ;; from real errors.
164       '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
165                           \"mail.misc\"))
166           ;; Non-error messages are crossposted to all relevant
167           ;; groups, but we don't crosspost between the group for the
168           ;; (ding) list and the group for other (ding) related mail.
169           (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
170                 (\"subject\" \"ding\" \"ding.misc\"))
171              ;; Other mailing lists...
172              (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
173              (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
174              ;; People...
175              (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
176           ;; Unmatched mail goes to the catch all group.
177           \"misc.misc\"))")
178
179 (defvar nnmail-split-abbrev-alist
180   '((any . "from\\|to\\|cc\\|sender\\|apparently-to")
181     (mail . "mailer-daemon\\|postmaster"))
182   "*Alist of abbrevations allowed in `nnmail-split-fancy'.")
183
184 \f
185
186 (defconst nnmail-version "nnml 0.0"
187   "nnmail version.")
188
189 \f
190
191 (defun nnmail-request-post-buffer (post group subject header article-buffer
192                                         info follow-to respect-poster)
193   (let ((method-address (cdr (assq 'to-address (nth 5 info))))
194         from date to reply-to message-of
195         references message-id sender cc sendto elt)
196     (setq method-address
197           (if (and (stringp method-address) 
198                    (string= method-address ""))
199               nil method-address))
200     (save-excursion
201       (set-buffer (get-buffer-create "*mail*"))
202       (mail-mode)
203       (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
204       (if (and (buffer-modified-p)
205                (> (buffer-size) 0)
206                (not (y-or-n-p "Unsent mail being composed; erase it? ")))
207           ()
208         (erase-buffer)
209         (if post
210             (mail-setup method-address subject nil nil nil nil)
211           (save-excursion
212             (set-buffer article-buffer)
213             (goto-char (point-min))
214             (narrow-to-region (point-min)
215                               (progn (search-forward "\n\n") (point)))
216             (let ((buffer-read-only nil))
217               (set-text-properties (point-min) (point-max) nil))
218             (setq from (header-from header))
219             (setq date (header-date header))
220             (and from
221                  (let ((stop-pos 
222                         (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
223                    (setq message-of
224                          (concat (if stop-pos (substring from 0 stop-pos) from)
225                                  "'s message of " date))))
226             (setq sender (mail-fetch-field "sender"))
227             (setq cc (mail-fetch-field "cc"))
228             (setq to (mail-fetch-field "to"))
229             (setq subject (header-subject header))
230             (or (string-match "^[Rr][Ee]:" subject)
231                 (setq subject (concat "Re: " subject)))
232             (setq reply-to (mail-fetch-field "reply-to"))
233             (setq references (header-references header))
234             (setq message-id (header-id header))
235             (widen))
236           (setq news-reply-yank-from from)
237           (setq news-reply-yank-message-id message-id)
238           
239           ;; Gather the "to" addresses out of the follow-to list and remove
240           ;; them as we go.
241           (if (and follow-to (listp follow-to))
242               (while (setq elt (assoc "To" follow-to))
243                 (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
244                 (setq follow-to (delq elt follow-to))))
245           (mail-setup (if (and follow-to (listp follow-to)) sendto
246                         (or method-address 
247                             (concat (or sender reply-to from "")
248                                     (if to (concat ", " to) "")
249                                     (if cc (concat ", " cc) ""))))
250                       subject message-of nil article-buffer nil)
251           ;; Note that "To" elements should already be in the message.
252           (if (and follow-to (listp follow-to))
253               (progn
254                 (goto-char (point-min))
255                 (re-search-forward "^To:" nil t)
256                 (beginning-of-line)
257                 (forward-line 1)
258                 (while follow-to
259                   (insert 
260                    (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
261                   (setq follow-to (cdr follow-to)))))
262           ;; Fold long references line to follow RFC1036.
263           (mail-position-on-field "References")
264           (let ((begin (- (point) (length "References: ")))
265                 (fill-column 78)
266                 (fill-prefix "\t"))
267             (if references (insert references))
268             (if (and references message-id) (insert " "))
269             (if message-id (insert message-id))
270             ;; The region must end with a newline to fill the region
271             ;; without inserting extra newline.
272             (fill-region-as-paragraph begin (1+ (point))))))
273       (current-buffer))))
274
275 (defun nnmail-find-file (file)
276   "Insert FILE in server buffer safely."
277   (set-buffer nntp-server-buffer)
278   (erase-buffer)
279   (condition-case ()
280       (progn (insert-file-contents file) t)
281     (file-error nil)))
282
283 (defun nnmail-article-pathname (group mail-dir)
284   "Make pathname for GROUP."
285   (concat (file-name-as-directory (expand-file-name mail-dir))
286           (nnmail-replace-chars-in-string group ?. ?/) "/"))
287
288 (defun nnmail-replace-chars-in-string (string from to)
289   "Replace characters in STRING from FROM to TO."
290   (let ((string (substring string 0))   ;Copy string.
291         (len (length string))
292         (idx 0))
293     ;; Replace all occurrences of FROM with TO.
294     (while (< idx len)
295       (if (= (aref string idx) from)
296           (aset string idx to))
297       (setq idx (1+ idx)))
298     string))
299
300 (defun nnmail-days-between (date1 date2)
301   ;; Return the number of days between date1 and date2.
302   (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
303                     (timezone-parse-date date1)))
304         (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
305                     (timezone-parse-date date2))))
306     (- (timezone-absolute-from-gregorian 
307         (nth 1 d1) (nth 2 d1) (car d1))
308        (timezone-absolute-from-gregorian 
309         (nth 1 d2) (nth 2 d2) (car d2)))))
310
311 ;; Function taken from rmail.el.
312 (defun nnmail-move-inbox (inbox tofile)
313   (let ((inbox (file-truename
314                 (expand-file-name (substitute-in-file-name inbox))))
315         (tofile (nnmail-make-complex-temp-name (expand-file-name tofile)))
316         movemail popmail errors)
317     ;; Check whether the inbox is to be moved to the special tmp dir. 
318     (if nnmail-tmp-directory
319         (setq tofile (concat (file-name-as-directory nnmail-tmp-directory)
320                              (file-name-nondirectory tofile))))
321     ;; If getting from mail spool directory,
322     ;; use movemail to move rather than just renaming,
323     ;; so as to interlock with the mailer.
324     (setq movemail (string= (file-name-directory inbox)
325                             (file-truename rmail-spool-directory))
326           popmail (string-match "^po:" (file-name-nondirectory inbox)))
327     (if popmail (setq inbox (file-name-nondirectory inbox)))
328     (if movemail
329         ;; On some systems, /usr/spool/mail/foo is a directory
330         ;; and the actual inbox is /usr/spool/mail/foo/foo.
331         (if (file-directory-p inbox)
332             (setq inbox (expand-file-name (user-login-name) inbox))))
333     (if popmail
334         (message "Getting mail from post office ...")
335       (if (or (and (file-exists-p tofile)
336                    (/= 0 (nth 7 (file-attributes tofile))))
337               (and (file-exists-p inbox)
338                    (/= 0 (nth 7 (file-attributes inbox)))))
339           (message "Getting mail from %s..." inbox)))
340     ;; Set TOFILE if have not already done so, and
341     ;; rename or copy the file INBOX to TOFILE if and as appropriate.
342     (cond ((or (file-exists-p tofile) (and (not popmail)
343                                            (not (file-exists-p inbox))))
344            nil)
345           ((and (not movemail) (not popmail))
346            ;; Try copying.  If that fails (perhaps no space),
347            ;; rename instead.
348            (condition-case nil
349                (copy-file inbox tofile nil)
350              (error
351               ;; Third arg is t so we can replace existing file TOFILE.
352               (rename-file inbox tofile t)))
353            ;; Make the real inbox file empty.
354            ;; Leaving it deleted could cause lossage
355            ;; because mailers often won't create the file.
356            (condition-case ()
357                (write-region (point) (point) inbox)
358              (file-error nil)))
359           (t
360            (unwind-protect
361                (save-excursion
362                  (setq errors (generate-new-buffer " *nnmail loss*"))
363                  (buffer-disable-undo errors)
364                  (call-process
365                   (expand-file-name "movemail" exec-directory)
366                   nil errors nil inbox tofile)
367                  (if (not (buffer-modified-p errors))
368                      ;; No output => movemail won
369                      nil
370                    (set-buffer errors)
371                    (subst-char-in-region (point-min) (point-max)
372                                          ?\n ?\  )
373                    (goto-char (point-max))
374                    (skip-chars-backward " \t")
375                    (delete-region (point) (point-max))
376                    (goto-char (point-min))
377                    (if (looking-at "movemail: ")
378                        (delete-region (point-min) (match-end 0)))
379                    (beep t)
380                    (message (concat "movemail: "
381                                     (buffer-substring (point-min)
382                                                       (point-max))))
383                    (sit-for 3)
384                    nil)))))
385     (and errors
386          (buffer-name errors)
387          (kill-buffer errors))
388     tofile))
389
390
391 (defun nnmail-get-active ()
392   "Returns an assoc of group names and active ranges.
393 nn*-request-list should have been called before calling this function."
394   (let (group-assoc)
395     ;; Go through all groups from the active list.
396     (save-excursion
397       (set-buffer nntp-server-buffer)
398       (goto-char (point-min))
399       (while (re-search-forward 
400               "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
401         (setq group-assoc
402               (cons (list (buffer-substring (match-beginning 1) 
403                                             (match-end 1))
404                           (cons (string-to-int 
405                                  (buffer-substring (match-beginning 3)
406                                                    (match-end 3)))
407                                 (string-to-int 
408                                  (buffer-substring (match-beginning 2)
409                                                    (match-end 2)))))
410                     group-assoc))))
411     ;; In addition, add all groups mentioned in `nnmail-split-methods'.
412     (let ((methods (and (not (symbolp nnmail-split-methods))
413                         nnmail-split-methods)))
414       (while methods
415         (if (not (assoc (car (car methods)) group-assoc))
416             (setq group-assoc
417                   (cons (list (car (car methods)) (cons 1 0)) 
418                         group-assoc)))
419         (setq methods (cdr methods))))
420     group-assoc))
421
422 (defun nnmail-save-active (group-assoc file-name)
423   (let (group)
424     (save-excursion
425       (set-buffer (get-buffer-create " *nnmail active*"))
426       (buffer-disable-undo (current-buffer))
427       (erase-buffer)
428       (while group-assoc
429         (setq group (car group-assoc))
430         (insert (format "%s %d %d y\n" (car group) (cdr (car (cdr group)) )
431                         (car (car (cdr group)))))
432         (setq group-assoc (cdr group-assoc)))
433       (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
434       (kill-buffer (current-buffer)))))
435
436 (defun nnmail-split-incoming (incoming func &optional dont-kill)
437   "Go through the entire INCOMING file and pick out each individual mail.
438 FUNC will be called with the buffer narrowed to each mail."
439   (let ((delim (concat "^" rmail-unix-mail-delimiter))
440         start end content-length do-search)
441     (save-excursion
442       (set-buffer (get-buffer-create " *nnmail incoming*"))
443       (buffer-disable-undo (current-buffer))
444       (erase-buffer)
445       (insert-file-contents incoming)
446       (goto-char (point-min))
447       (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
448       ;; Go to the beginning of the first mail...
449       (if (and (re-search-forward delim nil t)
450                (goto-char (match-beginning 0)))
451           ;; and then carry on until the bitter end.
452           (while (not (eobp))
453             (setq start (point))
454             ;; Skip all the headers in case there are more "From "s...
455             (if (not (search-forward "\n\n" nil t))
456                 (forward-line 1))
457             ;; Look for a Content-Length header.
458             (if (not (save-excursion
459                        (and (re-search-backward 
460                              "^Content-Length: \\([0-9]+\\)" nil t)
461                             (setq content-length (string-to-int
462                                                   (buffer-substring 
463                                                    (match-beginning 1)
464                                                    (match-end 1))))
465                             ;; We destroy the header, since none of
466                             ;; the backends ever use it, and we do not
467                             ;; want to confuse other mailers by having
468                             ;; a (possibly) faulty header.
469                             (progn (insert "X-") t))))
470                 (setq do-search t)
471               (if (save-excursion
472                     (forward-char content-length)
473                     (looking-at delim))
474                   (progn
475                     (forward-char content-length)
476                     (setq do-search nil))
477                 (setq do-search t)))
478             ;; Go to the beginning of the next article - or to the end
479             ;; of the buffer.  
480             (if do-search
481                 (if (re-search-forward delim nil t)
482                     (goto-char (match-beginning 0))
483                   (goto-char (point-max))))
484             (save-excursion
485               (save-restriction
486                 (narrow-to-region start (point))
487                 (goto-char (point-min))
488                 (funcall func)
489                 (setq end (point-max))))
490             (goto-char end)))
491       (if dont-kill
492           (current-buffer)
493         (kill-buffer (current-buffer))))))
494
495 ;; Mail crossposts syggested by Brian Edmonds <edmonds@cs.ubc.ca>. 
496 (defun nnmail-article-group (func)
497   "Look at the headers and return an alist of groups that match.
498 FUNC will be called with the group name to determine the article number."
499   (let ((methods nnmail-split-methods)
500         (obuf (current-buffer))
501         (beg (point-min))
502         end found group-art)
503     (save-excursion
504       ;; Find headers.
505       (goto-char beg)
506       (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
507       (set-buffer (get-buffer-create " *nnmail work*"))
508       (buffer-disable-undo (current-buffer))
509       (erase-buffer)
510       ;; Copy the headers into the work buffer.
511       (insert-buffer-substring obuf beg end)
512       ;; Fold continuation lines.
513       (goto-char (point-min))
514       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
515         (replace-match " " t t))
516       (if (and (symbolp nnmail-split-methods)
517                (fboundp nnmail-split-methods))
518           (setq group-art
519                 (mapcar
520                  (lambda (group) (cons group (funcall func group)))
521                  (funcall nnmail-split-methods)))
522         ;; Go throught the split methods to find a match.
523         (while (and methods (or nnmail-crosspost (not group-art)))
524           (goto-char (point-max))
525           (if (or (cdr methods)
526                   (not (equal "" (nth 1 (car methods)))))
527               (if (and (condition-case () 
528                            (if (stringp (nth 1 (car methods)))
529                                (re-search-backward
530                                 (car (cdr (car methods))) nil t)
531                              ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
532                              (funcall (nth 1 (car methods)) 
533                                       (car (car methods))))
534                          (error nil))
535                        ;; Don't enter the article into the same group twice.
536                        (not (assoc (car (car methods)) group-art)))
537                   (setq group-art
538                         (cons (cons (car (car methods))
539                                     (funcall func (car (car methods)))) 
540                               group-art)))
541             (or group-art
542                 (setq group-art 
543                       (list (cons (car (car methods)) 
544                                   (funcall func (car (car methods))))))))
545           (setq methods (cdr methods))))
546       (kill-buffer (current-buffer))
547       group-art)))
548
549 (defun nnmail-insert-lines ()
550   "Insert how many lines and chars there are in the body of the mail."
551   (let (lines chars)
552     (save-excursion
553       (goto-char (point-min))
554       (if (search-forward "\n\n" nil t) 
555           (progn
556             (setq chars (- (point-max) (point)))
557             (setq lines (- (count-lines (point) (point-max)) 1))
558             (forward-char -1)
559             (save-excursion
560               (if (re-search-backward "^Lines: " nil t)
561                   (delete-region (point) (progn (forward-line 1) (point)))))
562             (insert (format "Lines: %d\n" lines))
563             chars)))))
564
565 (defun nnmail-insert-xref (group-alist)
566   "Insert an Xref line based on the (group . article) alist."
567   (save-excursion
568     (goto-char (point-min))
569     (if (search-forward "\n\n" nil t) 
570         (progn
571           (forward-char -1)
572           (if (re-search-backward "^Xref: " nil t)
573               (delete-region (match-beginning 0) 
574                              (progn (forward-line 1) (point))))
575           (insert (format "Xref: %s" (system-name)))
576           (while group-alist
577             (insert (format " %s:%d" (car (car group-alist)) 
578                             (cdr (car group-alist))))
579             (setq group-alist (cdr group-alist)))
580           (insert "\n")))))
581
582 ;; Written by byer@mv.us.adobe.com (Scott Byer).
583 (defun nnmail-make-complex-temp-name (prefix)
584   (let ((newname (make-temp-name prefix))
585         (newprefix prefix))
586     (while (file-exists-p newname)
587       (setq newprefix (concat newprefix "x"))
588       (setq newname (make-temp-name newprefix)))
589     newname))
590
591 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
592
593 (defun nnmail-split-fancy ()
594   "Fancy splitting method.
595 See the documentation for the variable `nnmail-split-fancy' for documentation."
596   (nnmail-split-it nnmail-split-fancy))
597
598 (defvar nnmail-split-cache nil)
599 ;; Alist of split expresions their equivalent regexps.
600
601 (defun nnmail-split-it (split)
602   ;; Return a list of groups matching SPLIT.
603   (cond ((stringp split)
604          ;; A group.
605          (list split))
606         ((eq (car split) '&)
607          (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
608         ((eq (car split) '|)
609          (let (done)
610            (while (and (not done) (cdr split))
611              (setq split (cdr split)
612                    done (nnmail-split-it (car split))))
613            done))       ((assq split nnmail-split-cache)
614          ;; A compiled match expression.
615          (goto-char (point-max))
616          (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
617              (nnmail-split-it (nth 2 split))))
618         (t
619          ;; An uncompiled match.
620          (let* ((field (nth 0 split))
621                 (value (nth 1 split))
622                 (regexp (concat "^\\(" 
623                                  (if (symbolp field)
624                                      (cdr (assq field 
625                                                 nnmail-split-abbrev-alist))
626                                    field)
627                                  "\\):.*\\<\\("
628                                  (if (symbolp value)
629                                      (cdr (assq value
630                                                 nnmail-split-abbrev-alist))
631                                    value)
632                                  "\\>\\)")))
633            (setq nnmail-split-cache 
634                  (cons (cons split regexp) nnmail-split-cache))
635            (goto-char (point-max))
636            (if (re-search-backward regexp nil t)
637                (nnmail-split-it (nth 2 split)))))))
638
639 ;; Get a list of spool files to read.
640 (defun nnmail-get-spool-files (&optional group)
641   (if (null nnmail-spool-file)
642       ;; No spool file whatsoever.
643       nil)
644   (let ((procmails 
645          ;; If procmail is used to get incoming mail, the files
646          ;; are stored in this directory.
647          (and (file-exists-p nnmail-procmail-directory)
648               (directory-files 
649                nnmail-procmail-directory 
650                t (concat (if group group "")
651                          nnmail-procmail-suffix "$") t))))
652     (cond ((listp nnmail-spool-file)
653            (append nnmail-spool-file procmails))
654           ((stringp nnmail-spool-file)
655            (cons nnmail-spool-file procmails))
656           (t
657            procmails))))
658
659 (provide 'nnmail)
660
661 ;;; nnml.el ends here