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