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