*** 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 (defvar nnmail-delete-incoming nil
185   "*If non-nil, the mail backends will delete incoming files after splitting.
186 This is nil by default for reasons of security.")
187
188 \f
189
190 (defconst nnmail-version "nnml 0.0"
191   "nnmail version.")
192
193 \f
194
195 (defun nnmail-request-post (&optional server)
196   (mail-send-and-exit nil))
197
198 (defun nnmail-request-post-buffer (post group subject header article-buffer
199                                         info follow-to respect-poster)
200   (let ((method-address (cdr (assq 'to-address (nth 5 info))))
201         from date to reply-to message-of
202         references message-id sender cc sendto elt)
203     (setq method-address
204           (if (and (stringp method-address) 
205                    (string= method-address ""))
206               nil method-address))
207     (save-excursion
208       (set-buffer (get-buffer-create "*mail*"))
209       (mail-mode)
210       (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
211       (if (and (buffer-modified-p)
212                (> (buffer-size) 0)
213                (not (y-or-n-p "Unsent mail being composed; erase it? ")))
214           ()
215         (erase-buffer)
216         (if post
217             (mail-setup method-address subject nil nil nil nil)
218           (save-excursion
219             (set-buffer article-buffer)
220             (goto-char (point-min))
221             (narrow-to-region (point-min)
222                               (progn (search-forward "\n\n") (point)))
223             (let ((buffer-read-only nil))
224               (set-text-properties (point-min) (point-max) nil))
225             (setq from (header-from header))
226             (setq date (header-date header))
227             (and from
228                  (let ((stop-pos 
229                         (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
230                    (setq message-of
231                          (concat (if stop-pos (substring from 0 stop-pos) from)
232                                  "'s message of " date))))
233             (setq sender (mail-fetch-field "sender"))
234             (setq cc (mail-fetch-field "cc"))
235             (setq to (mail-fetch-field "to"))
236             (setq subject (header-subject header))
237             (or (string-match "^[Rr][Ee]:" subject)
238                 (setq subject (concat "Re: " subject)))
239             (setq reply-to (mail-fetch-field "reply-to"))
240             (setq references (header-references header))
241             (setq message-id (header-id header))
242             (widen))
243           (setq news-reply-yank-from from)
244           (setq news-reply-yank-message-id message-id)
245           
246           ;; Gather the "to" addresses out of the follow-to list and remove
247           ;; them as we go.
248           (if (and follow-to (listp follow-to))
249               (while (setq elt (assoc "To" follow-to))
250                 (setq sendto (concat sendto (and sendto ", ") (cdr elt)))
251                 (setq follow-to (delq elt follow-to))))
252           (mail-setup (if (and follow-to (listp follow-to)) sendto
253                         (or method-address 
254                             (concat (or sender reply-to from "")
255                                     (if to (concat ", " to) "")
256                                     (if cc (concat ", " cc) ""))))
257                       subject message-of nil article-buffer nil)
258           ;; Note that "To" elements should already be in the message.
259           (if (and follow-to (listp follow-to))
260               (progn
261                 (goto-char (point-min))
262                 (re-search-forward "^To:" nil t)
263                 (beginning-of-line)
264                 (forward-line 1)
265                 (while follow-to
266                   (insert 
267                    (car (car follow-to)) ": " (cdr (car follow-to)) "\n")
268                   (setq follow-to (cdr follow-to)))))
269           ;; Fold long references line to follow RFC1036.
270           (mail-position-on-field "References")
271           (let ((begin (- (point) (length "References: ")))
272                 (fill-column 78)
273                 (fill-prefix "\t"))
274             (if references (insert references))
275             (if (and references message-id) (insert " "))
276             (if message-id (insert message-id))
277             ;; The region must end with a newline to fill the region
278             ;; without inserting extra newline.
279             (fill-region-as-paragraph begin (1+ (point))))))
280       (current-buffer))))
281
282 (defun nnmail-find-file (file)
283   "Insert FILE in server buffer safely."
284   (set-buffer nntp-server-buffer)
285   (erase-buffer)
286   (condition-case ()
287       (progn (insert-file-contents file) t)
288     (file-error nil)))
289
290 (defun nnmail-article-pathname (group mail-dir)
291   "Make pathname for GROUP."
292   (concat (file-name-as-directory (expand-file-name mail-dir))
293           (nnmail-replace-chars-in-string group ?. ?/) "/"))
294
295 (defun nnmail-replace-chars-in-string (string from to)
296   "Replace characters in STRING from FROM to TO."
297   (let ((string (substring string 0))   ;Copy string.
298         (len (length string))
299         (idx 0))
300     ;; Replace all occurrences of FROM with TO.
301     (while (< idx len)
302       (if (= (aref string idx) from)
303           (aset string idx to))
304       (setq idx (1+ idx)))
305     string))
306
307 (defun nnmail-days-between (date1 date2)
308   ;; Return the number of days between date1 and date2.
309   (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
310                     (timezone-parse-date date1)))
311         (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
312                     (timezone-parse-date date2))))
313     (- (timezone-absolute-from-gregorian 
314         (nth 1 d1) (nth 2 d1) (car d1))
315        (timezone-absolute-from-gregorian 
316         (nth 1 d2) (nth 2 d2) (car d2)))))
317
318 ;; Function taken from rmail.el.
319 (defun nnmail-move-inbox (inbox tofile)
320   (let ((inbox (file-truename
321                 (expand-file-name (substitute-in-file-name inbox))))
322         movemail popmail errors)
323     ;; Check whether the inbox is to be moved to the special tmp dir. 
324     (if nnmail-tmp-directory
325         (setq tofile (concat (file-name-as-directory nnmail-tmp-directory)
326                              (file-name-nondirectory tofile))))
327     ;; Make the filename unique.
328     (setq tofile (nnmail-make-complex-temp-name (expand-file-name tofile)))
329     ;; If getting from mail spool directory,
330     ;; use movemail to move rather than just renaming,
331     ;; so as to interlock with the mailer.
332     (setq movemail (string= (file-name-directory inbox)
333                             (file-truename rmail-spool-directory))
334           popmail (string-match "^po:" (file-name-nondirectory inbox)))
335     (if popmail (setq inbox (file-name-nondirectory inbox)))
336     (if movemail
337         ;; On some systems, /usr/spool/mail/foo is a directory
338         ;; and the actual inbox is /usr/spool/mail/foo/foo.
339         (if (file-directory-p inbox)
340             (setq inbox (expand-file-name (user-login-name) inbox))))
341     (if popmail
342         (message "Getting mail from post office ...")
343       (if (or (and (file-exists-p tofile)
344                    (/= 0 (nth 7 (file-attributes tofile))))
345               (and (file-exists-p inbox)
346                    (/= 0 (nth 7 (file-attributes inbox)))))
347           (message "Getting mail from %s..." inbox)))
348     ;; Set TOFILE if have not already done so, and
349     ;; rename or copy the file INBOX to TOFILE if and as appropriate.
350     (cond ((or (file-exists-p tofile) (and (not popmail)
351                                            (not (file-exists-p inbox))))
352            nil)
353           ((and (not movemail) (not popmail))
354            ;; Try copying.  If that fails (perhaps no space),
355            ;; rename instead.
356            (condition-case nil
357                (copy-file inbox tofile nil)
358              (error
359               ;; Third arg is t so we can replace existing file TOFILE.
360               (rename-file inbox tofile t)))
361            ;; Make the real inbox file empty.
362            ;; Leaving it deleted could cause lossage
363            ;; because mailers often won't create the file.
364            (condition-case ()
365                (write-region (point) (point) inbox)
366              (file-error nil)))
367           (t
368            (unwind-protect
369                (save-excursion
370                  (setq errors (generate-new-buffer " *nnmail loss*"))
371                  (buffer-disable-undo errors)
372                  (call-process
373                   (expand-file-name "movemail" exec-directory)
374                   nil errors nil inbox tofile)
375                  (if (not (buffer-modified-p errors))
376                      ;; No output => movemail won
377                      nil
378                    (set-buffer errors)
379                    (subst-char-in-region (point-min) (point-max)
380                                          ?\n ?\  )
381                    (goto-char (point-max))
382                    (skip-chars-backward " \t")
383                    (delete-region (point) (point-max))
384                    (goto-char (point-min))
385                    (if (looking-at "movemail: ")
386                        (delete-region (point-min) (match-end 0)))
387                    (beep t)
388                    (message (concat "movemail: "
389                                     (buffer-substring (point-min)
390                                                       (point-max))))
391                    (sit-for 3)
392                    nil)))))
393     (and errors
394          (buffer-name errors)
395          (kill-buffer errors))
396     tofile))
397
398
399 (defun nnmail-get-active ()
400   "Returns an assoc of group names and active ranges.
401 nn*-request-list should have been called before calling this function."
402   (let (group-assoc)
403     ;; Go through all groups from the active list.
404     (save-excursion
405       (set-buffer nntp-server-buffer)
406       (goto-char (point-min))
407       (while (re-search-forward 
408               "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
409         (setq group-assoc
410               (cons (list (buffer-substring (match-beginning 1) 
411                                             (match-end 1))
412                           (cons (string-to-int 
413                                  (buffer-substring (match-beginning 3)
414                                                    (match-end 3)))
415                                 (string-to-int 
416                                  (buffer-substring (match-beginning 2)
417                                                    (match-end 2)))))
418                     group-assoc))))
419     ;; In addition, add all groups mentioned in `nnmail-split-methods'.
420     (let ((methods (and (not (symbolp nnmail-split-methods))
421                         nnmail-split-methods)))
422       (while methods
423         (if (not (assoc (car (car methods)) group-assoc))
424             (setq group-assoc
425                   (cons (list (car (car methods)) (cons 1 0)) 
426                         group-assoc)))
427         (setq methods (cdr methods))))
428     group-assoc))
429
430 (defun nnmail-save-active (group-assoc file-name)
431   (let (group)
432     (save-excursion
433       (set-buffer (get-buffer-create " *nnmail active*"))
434       (buffer-disable-undo (current-buffer))
435       (erase-buffer)
436       (while group-assoc
437         (setq group (car group-assoc))
438         (insert (format "%s %d %d y\n" (car group) (cdr (car (cdr group)) )
439                         (car (car (cdr group)))))
440         (setq group-assoc (cdr group-assoc)))
441       (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
442       (kill-buffer (current-buffer)))))
443
444 (defun nnmail-split-incoming (incoming func &optional dont-kill group)
445   "Go through the entire INCOMING file and pick out each individual mail.
446 FUNC will be called with the buffer narrowed to each mail."
447   (let ((delim (concat "^" rmail-unix-mail-delimiter))
448         ;; If this is a group-specific split, we bind the split
449         ;; methods to just this group.
450         (nnmail-split-methods (if (and group (eq nnmail-spool-file 'procmail))
451                                   (list (list group ""))
452                                 nnmail-split-methods))
453         start end content-length do-search)
454     (save-excursion
455       (set-buffer (get-buffer-create " *nnmail incoming*"))
456       (buffer-disable-undo (current-buffer))
457       (erase-buffer)
458       (insert-file-contents incoming)
459       (goto-char (point-min))
460       (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
461       ;; Go to the beginning of the first mail...
462       (if (and (re-search-forward delim nil t)
463                (goto-char (match-beginning 0)))
464           ;; and then carry on until the bitter end.
465           (while (not (eobp))
466             (setq start (point))
467             ;; Skip all the headers in case there are more "From "s...
468             (if (not (search-forward "\n\n" nil t))
469                 (forward-line 1))
470             ;; Look for a Content-Length header.
471             (if (not (save-excursion
472                        (and (re-search-backward 
473                              "^Content-Length: \\([0-9]+\\)" start t)
474                             (setq content-length (string-to-int
475                                                   (buffer-substring 
476                                                    (match-beginning 1)
477                                                    (match-end 1))))
478                             ;; We destroy the header, since none of
479                             ;; the backends ever use it, and we do not
480                             ;; want to confuse other mailers by having
481                             ;; a (possibly) faulty header.
482                             (progn (insert "X-") t))))
483                 (setq do-search t)
484               (if (save-excursion
485                     (condition-case nil
486                         (forward-char content-length)
487                       (end-of-buffer nil))
488                     (looking-at delim))
489                   (progn
490                     (forward-char content-length)
491                     (setq do-search nil))
492                 (setq do-search t)))
493             ;; Go to the beginning of the next article - or to the end
494             ;; of the buffer.  
495             (if do-search
496                 (if (re-search-forward delim nil t)
497                     (goto-char (match-beginning 0))
498                   (goto-char (point-max))))
499             (save-excursion
500               (save-restriction
501                 (narrow-to-region start (point))
502                 (goto-char (point-min))
503                 (funcall func)
504                 (setq end (point-max))))
505             (goto-char end)))
506       (if dont-kill
507           (current-buffer)
508         (kill-buffer (current-buffer))))))
509
510 ;; Mail crossposts syggested by Brian Edmonds <edmonds@cs.ubc.ca>. 
511 (defun nnmail-article-group (func)
512   "Look at the headers and return an alist of groups that match.
513 FUNC will be called with the group name to determine the article number."
514   (let ((methods nnmail-split-methods)
515         (obuf (current-buffer))
516         (beg (point-min))
517         end found group-art)
518     (if (and (sequencep methods) (= (length methods) 1))
519         ;; If there is only just one group to put everything in, we
520         ;; just return a list with just this one method in.
521         (setq group-art
522               (list (cons (car (car methods))
523                           (funcall func (car (car methods))))))
524       ;; We do actual comparison.
525       (save-excursion
526         ;; Find headers.
527         (goto-char beg)
528         (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
529         (set-buffer (get-buffer-create " *nnmail work*"))
530         (buffer-disable-undo (current-buffer))
531         (erase-buffer)
532         ;; Copy the headers into the work buffer.
533         (insert-buffer-substring obuf beg end)
534         ;; Fold continuation lines.
535         (goto-char (point-min))
536         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
537           (replace-match " " t t))
538         (if (and (symbolp nnmail-split-methods)
539                  (fboundp nnmail-split-methods))
540             (setq group-art
541                   (mapcar
542                    (lambda (group) (cons group (funcall func group)))
543                    (funcall nnmail-split-methods)))
544           ;; Go throught the split methods to find a match.
545           (while (and methods (or nnmail-crosspost (not group-art)))
546             (goto-char (point-max))
547             (if (or (cdr methods)
548                     (not (equal "" (nth 1 (car methods)))))
549                 (if (and (condition-case () 
550                              (if (stringp (nth 1 (car methods)))
551                                  (re-search-backward
552                                   (car (cdr (car methods))) nil t)
553                                ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
554                                (funcall (nth 1 (car methods)) 
555                                         (car (car methods))))
556                            (error nil))
557                          ;; Don't enter the article into the same group twice.
558                          (not (assoc (car (car methods)) group-art)))
559                     (setq group-art
560                           (cons (cons (car (car methods))
561                                       (funcall func (car (car methods)))) 
562                                 group-art)))
563               (or group-art
564                   (setq group-art 
565                         (list (cons (car (car methods)) 
566                                     (funcall func (car (car methods))))))))
567             (setq methods (cdr methods))))
568         (kill-buffer (current-buffer))
569         group-art))))
570
571 (defun nnmail-insert-lines ()
572   "Insert how many lines and chars there are in the body of the mail."
573   (let (lines chars)
574     (save-excursion
575       (goto-char (point-min))
576       (if (search-forward "\n\n" nil t) 
577           (progn
578             (setq chars (- (point-max) (point)))
579             (setq lines (- (count-lines (point) (point-max)) 1))
580             (forward-char -1)
581             (save-excursion
582               (if (re-search-backward "^Lines: " nil t)
583                   (delete-region (point) (progn (forward-line 1) (point)))))
584             (insert (format "Lines: %d\n" lines))
585             chars)))))
586
587 (defun nnmail-insert-xref (group-alist)
588   "Insert an Xref line based on the (group . article) alist."
589   (save-excursion
590     (goto-char (point-min))
591     (if (search-forward "\n\n" nil t) 
592         (progn
593           (forward-char -1)
594           (if (re-search-backward "^Xref: " nil t)
595               (delete-region (match-beginning 0) 
596                              (progn (forward-line 1) (point))))
597           (insert (format "Xref: %s" (system-name)))
598           (while group-alist
599             (insert (format " %s:%d" (car (car group-alist)) 
600                             (cdr (car group-alist))))
601             (setq group-alist (cdr group-alist)))
602           (insert "\n")))))
603
604 ;; Written by byer@mv.us.adobe.com (Scott Byer).
605 (defun nnmail-make-complex-temp-name (prefix)
606   (let ((newname (make-temp-name prefix))
607         (newprefix prefix))
608     (while (file-exists-p newname)
609       (setq newprefix (concat newprefix "x"))
610       (setq newname (make-temp-name newprefix)))
611     newname))
612
613 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
614
615 (defun nnmail-split-fancy ()
616   "Fancy splitting method.
617 See the documentation for the variable `nnmail-split-fancy' for documentation."
618   (nnmail-split-it nnmail-split-fancy))
619
620 (defvar nnmail-split-cache nil)
621 ;; Alist of split expresions their equivalent regexps.
622
623 (defun nnmail-split-it (split)
624   ;; Return a list of groups matching SPLIT.
625   (cond ((stringp split)
626          ;; A group.
627          (list split))
628         ((eq (car split) '&)
629          (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
630         ((eq (car split) '|)
631          (let (done)
632            (while (and (not done) (cdr split))
633              (setq split (cdr split)
634                    done (nnmail-split-it (car split))))
635            done))       ((assq split nnmail-split-cache)
636          ;; A compiled match expression.
637          (goto-char (point-max))
638          (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
639              (nnmail-split-it (nth 2 split))))
640         (t
641          ;; An uncompiled match.
642          (let* ((field (nth 0 split))
643                 (value (nth 1 split))
644                 (regexp (concat "^\\(" 
645                                  (if (symbolp field)
646                                      (cdr (assq field 
647                                                 nnmail-split-abbrev-alist))
648                                    field)
649                                  "\\):.*\\<\\("
650                                  (if (symbolp value)
651                                      (cdr (assq value
652                                                 nnmail-split-abbrev-alist))
653                                    value)
654                                  "\\>\\)")))
655            (setq nnmail-split-cache 
656                  (cons (cons split regexp) nnmail-split-cache))
657            (goto-char (point-max))
658            (if (re-search-backward regexp nil t)
659                (nnmail-split-it (nth 2 split)))))))
660
661 ;; Get a list of spool files to read.
662 (defun nnmail-get-spool-files (&optional group)
663   (if (null nnmail-spool-file)
664       ;; No spool file whatsoever.
665       nil)
666   (let ((procmails 
667          ;; If procmail is used to get incoming mail, the files
668          ;; are stored in this directory.
669          (and (file-exists-p nnmail-procmail-directory)
670               (directory-files 
671                nnmail-procmail-directory 
672                t (concat (if group group "")
673                          nnmail-procmail-suffix "$") t))))
674     (cond ((listp nnmail-spool-file)
675            (append nnmail-spool-file procmails))
676           ((stringp nnmail-spool-file)
677            (cons nnmail-spool-file procmails))
678           (t
679            procmails))))
680
681 (provide 'nnmail)
682
683 ;;; nnml.el ends here