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