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