627d4795075e726a9c0a68a8a3d98c5967390f7b
[gnus] / lisp / nnmail.el
1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995,96 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 the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'nnheader)
29 (require 'timezone)
30 (require 'message)
31 (eval-when-compile (require 'cl))
32
33 (defvar nnmail-split-methods
34   '(("mail.misc" ""))
35   "*Incoming mail will be split according to this variable.
36
37 If you'd like, for instance, one mail group for mail from the
38 \"4ad-l\" mailing list, one group for junk mail and one for everything
39 else, you could do something like this:
40
41  (setq nnmail-split-methods
42        '((\"mail.4ad\" \"From:.*4ad\")
43          (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
44          (\"mail.misc\" \"\")))
45
46 As you can see, this variable is a list of lists, where the first
47 element in each \"rule\" is the name of the group (which, by the way,
48 does not have to be called anything beginning with \"mail\",
49 \"yonka.zow\" is a fine, fine name), and the second is a regexp that
50 nnmail will try to match on the header to find a fit.
51
52 The second element can also be a function.  In that case, it will be
53 called narrowed to the headers with the first element of the rule as
54 the argument.  It should return a non-nil value if it thinks that the
55 mail belongs in that group.
56
57 The last element should always have \"\" as the regexp.
58
59 This variable can also have a function as its value.")
60
61 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
62 (defvar nnmail-crosspost t
63   "*If non-nil, do crossposting if several split methods match the mail.
64 If nil, the first match found will be used.")
65
66 ;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
67 (defvar nnmail-keep-last-article nil
68   "*If non-nil, nnmail will never delete the last expired article in a directory.  
69 You may need to set this variable if other programs are putting
70 new mail into folder numbers that Gnus has marked as expired.")
71
72 (defvar nnmail-use-long-file-names nil
73   "*If non-nil the mail backends will use long file and directory names.
74 If nil, groups like \"mail.misc\" will end up in directories like
75 \"mail/misc/\".")
76
77 (defvar nnmail-default-file-modes 384
78   "Set the mode bits of all new mail files to this integer.")
79
80 (defvar nnmail-expiry-wait 7
81   "*Expirable articles that are older than this will be expired.
82 This variable can either be a number (which will be interpreted as a
83 number of days) -- this doesn't have to be an integer.  This variable
84 can also be `immediate' and `never'.")
85
86 (defvar nnmail-expiry-wait-function nil
87   "*Variable that holds function to specify how old articles should be before they are expired.
88   The function will be called with the name of the group that the
89 expiry is to be performed in, and it should return an integer that
90 says how many days an article can be stored before it is considered
91 \"old\".  It can also return the values `never' and `immediate'.
92
93 Eg.:
94
95 (setq nnmail-expiry-wait-function
96       (lambda (newsgroup)
97         (cond ((string-match \"private\" newsgroup) 31)
98               ((string-match \"junk\" newsgroup) 1)
99               ((string-match \"important\" newsgroup) 'never)
100               (t 7))))")
101
102 (defvar nnmail-spool-file 
103   (or (getenv "MAIL")
104       (concat "/usr/spool/mail/" (user-login-name)))
105   "Where the mail backends will look for incoming mail.
106 This variable is \"/usr/spool/mail/$user\" by default.
107 If this variable is nil, no mail backends will read incoming mail.
108 If this variable is a list, all files mentioned in this list will be
109 used as incoming mailboxes.
110 If this variable is a directory (i. e., it's name ends with a \"/\"),
111 treat all files in that directory as incoming spool files.")
112
113 (defvar nnmail-crash-box "~/.gnus-crash-box"
114   "*File where Gnus will store mail while processing it.")
115
116 (defvar nnmail-use-procmail nil
117   "*If non-nil, the mail backends will look in `nnmail-procmail-directory' for spool files.
118 The file(s) in `nnmail-spool-file' will also be read.")
119
120 (defvar nnmail-procmail-directory "~/incoming/"
121   "*When using procmail (and the like), incoming mail is put in this directory.
122 The Gnus mail backends will read the mail from this directory.")
123
124 (defvar nnmail-procmail-suffix "\\.spool"
125   "*Suffix of files created by procmail (and the like).
126 This variable might be a suffix-regexp to match the suffixes of
127 several files - eg. \".spool[0-9]*\".")
128
129 (defvar nnmail-resplit-incoming nil
130   "*If non-nil, re-split incoming procmail sorted mail.")
131
132 (defvar nnmail-delete-file-function 'delete-file
133   "Function called to delete files in some mail backends.")
134
135 (defvar nnmail-crosspost-link-function 'add-name-to-file
136   "Function called to create a copy of a file.
137 This is `add-name-to-file' by default, which means that crossposts
138 will use hard links.  If your file system doesn't allow hard
139 links, you could set this variable to `copy-file' instead.")
140
141 (defvar nnmail-movemail-program "movemail"
142   "*A command to be executed to move mail from the inbox.
143 The default is \"movemail\".
144
145 This can also be a function.  In that case, the function will be
146 called with two parameters -- the name of the INBOX file, and the file
147 to be moved to.")
148
149 (defvar nnmail-pop-password-required nil
150   "*Non-nil if a password is required when reading mail using POP.")
151
152 (defvar nnmail-read-incoming-hook nil
153   "*Hook that will be run after the incoming mail has been transferred.
154 The incoming mail is moved from `nnmail-spool-file' (which normally is
155 something like \"/usr/spool/mail/$user\") to the user's home
156 directory.  This hook is called after the incoming mail box has been
157 emptied, and can be used to call any mail box programs you have
158 running (\"xwatch\", etc.)
159
160 Eg.
161
162 \(add-hook 'nnmail-read-incoming-hook 
163            (lambda ()
164              (start-process \"mailsend\" nil 
165                             \"/local/bin/mailsend\" \"read\" \"mbox\")))
166
167 If you have xwatch running, this will alert it that mail has been
168 read.  
169
170 If you use `display-time', you could use something like this:
171
172 \(add-hook 'nnmail-read-incoming-hook
173           (lambda ()
174             ;; Update the displayed time, since that will clear out
175             ;; the flag that says you have mail.
176             (when (eq (process-status \"display-time\") 'run)
177               (display-time-filter display-time-process \"\"))))")
178
179 (when (eq system-type 'windows-nt)
180   (add-hook 'nnmail-prepare-incoming-hook 'nnheader-ms-strip-cr))
181
182 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
183 (defvar nnmail-prepare-incoming-hook nil
184   "*Hook called before treating incoming mail.
185 The hook is run in a buffer with all the new, incoming mail.")
186
187 (defvar nnmail-prepare-incoming-header-hook nil
188   "*Hook called narrowed to the headers of each message.
189 This can be used to remove excessive spaces (and stuff like
190 that) from the headers before splitting and saving the messages.")
191
192 (defvar nnmail-prepare-incoming-message-hook nil
193   "*Hook called narrowed to each message.")
194
195 (defvar nnmail-list-identifiers nil
196   "Regexp that match list identifiers to be removed.
197 This can also be a list of regexps.")
198
199 (defvar nnmail-pre-get-new-mail-hook nil
200   "Hook called just before starting to handle new incoming mail.")
201
202 (defvar nnmail-post-get-new-mail-hook nil
203   "Hook called just after finishing handling new incoming mail.")
204
205 (defvar nnmail-split-hook nil
206   "Hook called before deciding where to split an article.
207 The functions in this hook are free to modify the buffer
208 contents in any way they choose -- the buffer contents are
209 discarded after running the split process.")
210
211 ;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
212 (defvar nnmail-tmp-directory nil
213   "*If non-nil, use this directory for temporary storage when reading incoming mail.")
214
215 (defvar nnmail-large-newsgroup 50
216   "*The number of the articles which indicates a large newsgroup.
217 If the number of the articles is greater than the value, verbose
218 messages will be shown to indicate the current status.")
219
220 (defvar nnmail-split-fancy "mail.misc"
221   "*Incoming mail can be split according to this fancy variable.
222 To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
223
224 The format is this variable is SPLIT, where SPLIT can be one of
225 the following:
226
227 GROUP: Mail will be stored in GROUP (a string).
228
229 \(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
230   VALUE (a regexp), store the messages as specified by SPLIT.
231
232 \(| SPLIT...): Process each SPLIT expression until one of them matches.
233   A SPLIT expression is said to match if it will cause the mail
234   message to be stored in one or more groups.  
235
236 \(& SPLIT...): Process each SPLIT expression.
237
238 FIELD must match a complete field name.  VALUE must match a complete
239 word according to the `nnmail-split-fancy-syntax-table' syntax table.
240 You can use .* in the regexps to match partial field names or words.
241
242 FIELD and VALUE can also be lisp symbols, in that case they are expanded
243 as specified in `nnmail-split-abbrev-alist'.
244
245 Example:
246
247 \(setq nnmail-split-methods 'nnmail-split-fancy
248       nnmail-split-fancy
249       ;; Messages from the mailer daemon are not crossposted to any of
250       ;; the ordinary groups.  Warnings are put in a separate group
251       ;; from real errors.
252       '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
253                           \"mail.misc\"))
254           ;; Non-error messages are crossposted to all relevant
255           ;; groups, but we don't crosspost between the group for the
256           ;; (ding) list and the group for other (ding) related mail.
257           (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
258                 (\"subject\" \"ding\" \"ding.misc\"))
259              ;; Other mailing lists...
260              (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
261              (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
262              ;; People...
263              (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
264           ;; Unmatched mail goes to the catch all group.
265           \"misc.misc\"))")
266
267 (defvar nnmail-split-abbrev-alist
268   '((any . "from\\|to\\|cc\\|sender\\|apparently-to\\|resent-from\\|resent-to\\|resent-cc")
269     (mail . "mailer-daemon\\|postmaster"))
270   "*Alist of abbreviations allowed in `nnmail-split-fancy'.")
271
272 (defvar nnmail-delete-incoming nil
273   "*If non-nil, the mail backends will delete incoming files after splitting.")
274
275 (defvar nnmail-message-id-cache-length 1000
276   "*The approximate number of Message-IDs nnmail will keep in its cache.
277 If this variable is nil, no checking on duplicate messages will be
278 performed.")
279
280 (defvar nnmail-message-id-cache-file "~/.nnmail-cache"
281   "*The file name of the nnmail Message-ID cache.")
282
283 (defvar nnmail-treat-duplicates 'warn
284   "*If non-nil, nnmail keep a cache of Message-IDs to discover mail duplicates.
285 Three values are legal: nil, which means that nnmail is not to keep a
286 Message-ID cache; `warn', which means that nnmail should insert extra
287 headers to warn the user about the duplication (this is the default);
288 and `delete', which means that nnmail will delete duplicated mails.
289
290 This variable can also be a function.  It will be called from a buffer
291 narrowed to the article in question with the Message-ID as a
292 parameter.  It should return nil, `warn' or `delete'.")
293
294 ;;; Internal variables.
295
296 (defvar nnmail-split-history nil
297   "List of group/article elements that say where the previous split put messages.")
298
299 (defvar nnmail-pop-password nil
300   "*Password to use when reading mail from a POP server, if required.")
301
302 (defvar nnmail-split-fancy-syntax-table nil
303   "Syntax table used by `nnmail-split-fancy'.")
304 (unless (syntax-table-p nnmail-split-fancy-syntax-table)
305   (setq nnmail-split-fancy-syntax-table
306         (copy-syntax-table (standard-syntax-table)))
307   ;; support the %-hack
308   (modify-syntax-entry ?\% "." nnmail-split-fancy-syntax-table))
309
310 (defvar nnmail-prepare-save-mail-hook nil
311   "Hook called before saving mail.")
312
313 (defvar nnmail-moved-inboxes nil
314   "List of inboxes that have been moved.")
315
316 (defvar nnmail-internal-password nil)
317
318 \f
319
320 (defconst nnmail-version "nnmail 1.0"
321   "nnmail version.")
322
323 \f
324
325 (defun nnmail-request-post (&optional server)
326   (mail-send-and-exit nil))
327
328 (defun nnmail-find-file (file)
329   "Insert FILE in server buffer safely."
330   (set-buffer nntp-server-buffer)
331   (erase-buffer)
332   (let ((format-alist nil)
333         (after-insert-file-functions nil))
334     (condition-case ()
335         (progn (insert-file-contents file) t)
336       (file-error nil))))
337
338 (defun nnmail-group-pathname (group dir &optional file)
339   "Make pathname for GROUP."
340   (concat
341    (let ((dir (file-name-as-directory (expand-file-name dir))))
342      ;; If this directory exists, we use it directly.
343      (if (or nnmail-use-long-file-names 
344              (file-directory-p (concat dir group)))
345          (concat dir group "/")
346        ;; If not, we translate dots into slashes.
347        (concat dir (nnheader-replace-chars-in-string group ?. ?/) "/")))
348    (or file "")))
349   
350 (defun nnmail-date-to-time (date)
351   "Convert DATE into time."
352   (let* ((d1 (timezone-parse-date date))
353          (t1 (timezone-parse-time (aref d1 3))))
354     (apply 'encode-time
355            (mapcar (lambda (el)
356                      (and el (string-to-number el)))
357                    (list
358                     (aref t1 2) (aref t1 1) (aref t1 0)
359                     (aref d1 2) (aref d1 1) (aref d1 0)
360                     (aref d1 4))))))
361
362 (defun nnmail-time-less (t1 t2)
363   "Say whether time T1 is less than time T2."
364   (or (< (car t1) (car t2))
365       (and (= (car t1) (car t2))
366            (< (nth 1 t1) (nth 1 t2)))))
367
368 (defun nnmail-days-to-time (days)
369   "Convert DAYS into time."
370   (let* ((seconds (* 1.0 days 60 60 24))
371          (rest (expt 2 16))
372          (ms (condition-case nil (round (/ seconds rest))
373                (range-error (expt 2 16)))))
374     (list ms (condition-case nil (round (- seconds (* ms rest)))
375                (range-error (expt 2 16))))))
376
377 (defun nnmail-time-since (time)
378   "Return the time since TIME, which is either an internal time or a date."
379   (when (stringp time)
380     ;; Convert date strings to internal time.
381     (setq time (nnmail-date-to-time time)))
382   (let* ((current (current-time))
383          (rest (when (< (nth 1 current) (nth 1 time))
384                  (expt 2 16))))
385     (list (- (+ (car current) (if rest -1 0)) (car time))
386           (- (+ (or rest 0) (nth 1 current)) (nth 1 time)))))
387
388 ;; Function rewritten from rmail.el.
389 (defun nnmail-move-inbox (inbox)
390   "Move INBOX to `nnmail-crash-box'."
391   (if (not (file-writable-p nnmail-crash-box))
392       (gnus-error 1 "Can't write to crash box %s.  Not moving mail."
393                   nnmail-crash-box)
394     (let ((inbox (file-truename (expand-file-name inbox)))
395           (tofile (file-truename (expand-file-name nnmail-crash-box)))
396           movemail popmail errors)
397       ;; If getting from mail spool directory,
398       ;; use movemail to move rather than just renaming,
399       ;; so as to interlock with the mailer.
400       (unless (setq popmail (string-match
401                              "^po:" (file-name-nondirectory inbox)))
402         (setq movemail t))
403       (when popmail 
404         (setq inbox (file-name-nondirectory inbox)))
405       (when (and movemail
406                  ;; On some systems, /usr/spool/mail/foo is a directory
407                  ;; and the actual inbox is /usr/spool/mail/foo/foo.
408                  (file-directory-p inbox))
409         (setq inbox (expand-file-name (user-login-name) inbox)))
410       (if (member inbox nnmail-moved-inboxes)
411           nil
412         (if popmail
413             (progn
414               (setq nnmail-internal-password nnmail-pop-password)
415               (when (and nnmail-pop-password-required
416                          (not nnmail-pop-password))
417                 (setq nnmail-internal-password
418                       (nnmail-read-passwd
419                        (format "Password for %s: "
420                                (substring inbox (+ popmail 3))))))
421               (message "Getting mail from post office ..."))
422           (when (or (and (file-exists-p tofile)
423                          (/= 0 (nnheader-file-size tofile)))
424                     (and (file-exists-p inbox)
425                          (/= 0 (nnheader-file-size inbox))))
426             (message "Getting mail from %s..." inbox)))
427         ;; Set TOFILE if have not already done so, and
428         ;; rename or copy the file INBOX to TOFILE if and as appropriate.
429         (cond 
430          ((file-exists-p tofile)
431           ;; The crash box exists already.
432           t)
433          ((and (not popmail)
434                (not (file-exists-p inbox)))
435           ;; There is no inbox.
436           (setq tofile nil))
437          ((and (not movemail) (not popmail))
438           ;; Try copying.  If that fails (perhaps no space),
439           ;; rename instead.
440           (condition-case nil
441               (copy-file inbox tofile nil)
442             (error
443              ;; Third arg is t so we can replace existing file TOFILE.
444              (rename-file inbox tofile t)))
445           (push inbox nnmail-moved-inboxes)
446           ;; Make the real inbox file empty.
447           ;; Leaving it deleted could cause lossage
448           ;; because mailers often won't create the file.
449           (condition-case ()
450               (write-region (point) (point) inbox)
451             (file-error nil)))
452          (t
453           ;; Use movemail.
454           (unwind-protect
455               (save-excursion
456                 (setq errors (generate-new-buffer " *nnmail loss*"))
457                 (buffer-disable-undo errors)
458                 (let ((default-directory "/"))
459                   (if (nnheader-functionp nnmail-movemail-program)
460                       (funcall nnmail-movemail-program inbox tofile)
461                     (apply 
462                      'call-process
463                      (append
464                       (list
465                        (expand-file-name nnmail-movemail-program
466                                          exec-directory)
467                        nil errors nil inbox tofile)
468                       (when nnmail-internal-password
469                         (list nnmail-internal-password))))))
470                 (if (not (buffer-modified-p errors))
471                     ;; No output => movemail won
472                     (progn
473                       (or popmail
474                           (set-file-modes tofile nnmail-default-file-modes))
475                       (push inbox nnmail-moved-inboxes))
476                   (set-buffer errors)
477                   ;; There may be a warning about older revisions.  We
478                   ;; ignore those.
479                   (goto-char (point-min))
480                   (if (search-forward "older revision" nil t)
481                       (progn
482                         (or popmail
483                             (set-file-modes tofile nnmail-default-file-modes))
484                         (push inbox nnmail-moved-inboxes))
485                     ;; Probably a real error.
486                     (subst-char-in-region (point-min) (point-max) ?\n ?\  )
487                     (goto-char (point-max))
488                     (skip-chars-backward " \t")
489                     (delete-region (point) (point-max))
490                     (goto-char (point-min))
491                     (when (looking-at "movemail: ")
492                       (delete-region (point-min) (match-end 0)))
493                     (unless (yes-or-no-p
494                              (format "movemail: %s.  Continue? "
495                                      (buffer-string)))
496                       (error "%s" (buffer-string)))
497                     (setq tofile nil)))))))
498         (and errors
499              (buffer-name errors)
500              (kill-buffer errors))
501         tofile))))
502
503 (defun nnmail-get-active ()
504   "Returns an assoc of group names and active ranges.
505 nn*-request-list should have been called before calling this function."
506   (let (group-assoc)
507     ;; Go through all groups from the active list.
508     (save-excursion
509       (set-buffer nntp-server-buffer)
510       (goto-char (point-min))
511       (while (re-search-forward 
512               "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
513         ;; We create an alist with `(GROUP (LOW . HIGH))' elements.
514         (push (list (match-string 1)
515                     (cons (string-to-int (match-string 3))
516                           (string-to-int (match-string 2))))
517               group-assoc)))
518     group-assoc))
519
520 (defun nnmail-save-active (group-assoc file-name)
521   "Save GROUP-ASSOC in ACTIVE-FILE."
522   (when file-name
523     (nnheader-temp-write file-name
524       (nnmail-generate-active group-assoc))))
525
526 (defun nnmail-generate-active (alist)
527   "Generate an active file from group-alist ALIST."
528   (erase-buffer)
529   (let (group)
530     (while (setq group (pop alist))
531       (insert (format "%s %d %d y\n" (car group) (cdadr group)
532                       (caadr group))))))
533
534 (defun nnmail-get-split-group (file group)
535   "Find out whether this FILE is to be split into GROUP only.
536 If GROUP is non-nil and we are using procmail, return the group name
537 only when the file is the correct procmail file.  When GROUP is nil,
538 return nil if FILE is a spool file or the procmail group for which it
539 is a spool.  If not using procmail, return GROUP."
540   (if (or (eq nnmail-spool-file 'procmail)
541           nnmail-use-procmail)
542       (if (string-match (concat "^" (expand-file-name
543                                      (file-name-as-directory
544                                       nnmail-procmail-directory))
545                                 "\\([^/]*\\)" nnmail-procmail-suffix "$")
546                         (expand-file-name file))
547           (let ((procmail-group (substring (expand-file-name file)
548                                            (match-beginning 1)
549                                            (match-end 1))))
550             (if group 
551                 (if (string-equal group procmail-group)
552                     group
553                   nil)
554               procmail-group))
555         nil)
556     group))
557
558 (defun nnmail-process-babyl-mail-format (func artnum-func)
559   (let ((case-fold-search t)
560         start message-id content-length do-search end)
561     (while (not (eobp))
562       (goto-char (point-min))
563       (re-search-forward
564        "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
565       (goto-char (match-end 0))
566       (delete-region (match-beginning 0) (match-end 0))
567       (narrow-to-region
568        (setq start (point))
569        (progn
570          ;; Skip all the headers in case there are more "From "s...
571          (or (search-forward "\n\n" nil t)
572              (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
573              (search-forward "\1f\f"))
574          (point)))
575       (run-hooks 'nnmail-prepare-incoming-header-hook)
576       (widen)
577       ;; Find the Message-ID header.
578       (save-excursion
579         (if (re-search-backward "^Message-ID:[ \t]*\\(<[^>]*>\\)" nil t)
580             (setq message-id (buffer-substring (match-beginning 1)
581                                                (match-end 1)))
582           ;; There is no Message-ID here, so we create one.
583           (save-excursion
584             (when (re-search-backward "^Message-ID:" nil t)
585               (beginning-of-line)
586               (insert "Original-")))
587           (forward-line -1)
588           (insert "Message-ID: " (setq message-id (nnmail-message-id))
589                   "\n")))
590       ;; Look for a Content-Length header.
591       (if (not (save-excursion
592                  (and (re-search-backward 
593                        "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
594                       (setq content-length (string-to-int
595                                             (buffer-substring 
596                                              (match-beginning 1)
597                                              (match-end 1))))
598                       ;; We destroy the header, since none of
599                       ;; the backends ever use it, and we do not
600                       ;; want to confuse other mailers by having
601                       ;; a (possibly) faulty header.
602                       (progn (insert "X-") t))))
603           (setq do-search t)
604         (if (or (= (+ (point) content-length) (point-max))
605                 (save-excursion
606                   (goto-char (+ (point) content-length))
607                   (looking-at "\1f")))
608             (progn
609               (goto-char (+ (point) content-length))
610               (setq do-search nil))
611           (setq do-search t)))
612       ;; Go to the beginning of the next article - or to the end
613       ;; of the buffer.  
614       (when do-search
615         (if (re-search-forward "^\1f" nil t)
616             (goto-char (match-beginning 0))
617           (goto-char (1- (point-max)))))
618       (delete-char 1)                   ; delete ^_
619       (save-excursion
620         (save-restriction
621           (narrow-to-region start (point))
622           (goto-char (point-min))
623           (nnmail-check-duplication message-id func artnum-func)
624           (setq end (point-max))))
625       (goto-char end))))
626
627 (defun nnmail-search-unix-mail-delim ()
628   "Put point at the beginning of the next message."
629   (let ((case-fold-search t)
630         (delim (concat "^" message-unix-mail-delimiter))
631         found search )
632     (while (not found)
633       (setq search (condition-case ()
634                        (re-search-forward delim nil t)
635                      (error 'error)))
636       (cond 
637        ((eq search 'error)
638         (setq found 'yes))
639        (search
640         (when (or (looking-at "[^\n :]+ *:")
641                   (looking-at delim)
642                   (looking-at (concat ">" message-unix-mail-delimiter)))
643           (forward-line -1)
644           (setq found 'yes)))
645        (t
646         (setq found 'no))))
647     (eq found 'yes)))
648
649 (defun nnmail-process-unix-mail-format (func artnum-func)
650   (let ((case-fold-search t)
651         (delim (concat "^" message-unix-mail-delimiter))
652         start message-id content-length end skip head-end)
653     (goto-char (point-min))
654     (if (not (and (re-search-forward delim nil t)
655                   (goto-char (match-beginning 0))))
656         ;; Possibly wrong format?
657         (error "Error, unknown mail format! (Possibly corrupted.)")
658       ;; Carry on until the bitter end.
659       (while (not (eobp))
660         (setq start (point)
661               end nil)
662         ;; Find the end of the head.
663         (narrow-to-region
664          start 
665          (if (search-forward "\n\n" nil t)
666              (1- (point))
667            ;; This will never happen, but just to be on the safe side --
668            ;; if there is no head-body delimiter, we search a bit manually.
669            (while (and (looking-at "From \\|[^ \t]+:")
670                        (not (eobp)))
671              (forward-line 1)
672              (point))))
673         ;; Find the Message-ID header.
674         (goto-char (point-min))
675         (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
676             (setq message-id (match-string 1))
677           (save-excursion
678             (when (re-search-forward "^Message-ID:" nil t)
679               (beginning-of-line)
680               (insert "Original-")))
681           ;; There is no Message-ID here, so we create one.
682           (forward-line 1)
683           (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
684         ;; Look for a Content-Length header.
685         (goto-char (point-min))
686         (if (not (re-search-forward
687                   "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
688             (setq content-length nil)
689           (setq content-length (string-to-int (match-string 1)))
690           ;; We destroy the header, since none of the backends ever 
691           ;; use it, and we do not want to confuse other mailers by
692           ;; having a (possibly) faulty header.
693           (beginning-of-line)
694           (insert "X-"))
695         (run-hooks 'nnmail-prepare-incoming-header-hook)
696         ;; Find the end of this article.
697         (goto-char (point-max))
698         (widen)
699         (setq head-end (point))
700         ;; We try the Content-Length value.  The idea: skip over the header
701         ;; separator, then check what happens content-length bytes into the
702         ;; message body.  This should be either the end ot the buffer, the
703         ;; message separator or a blank line followed by the separator.
704         ;; The blank line should probably be deleted.  If neither of the
705         ;; three is met, the content-length header is probably invalid.
706         (when content-length
707           (forward-line 1)
708           (setq skip (+ (point) content-length))
709           (goto-char skip)
710           (cond ((or (= skip (point-max))
711                      (= (1+ skip) (point-max)))
712                  (setq end (point-max)))
713                 ((looking-at delim)
714                  (setq end skip))
715                 ((looking-at
716                   (concat "[ \t]*\n\\(" delim "\\)"))
717                  (setq end (match-beginning 1)))
718                 (t (setq end nil))))
719         (if end
720             (goto-char end)
721           ;; No Content-Length, so we find the beginning of the next 
722           ;; article or the end of the buffer.
723           (goto-char head-end)
724           (or (nnmail-search-unix-mail-delim)
725               (goto-char (point-max))))
726         ;; Allow the backend to save the article.
727         (save-excursion
728           (save-restriction
729             (narrow-to-region start (point))
730             (goto-char (point-min))
731             (nnmail-check-duplication message-id func artnum-func)
732             (setq end (point-max))))
733         (goto-char end)))))
734
735 (defun nnmail-process-mmdf-mail-format (func artnum-func)
736   (let ((delim "^\^A\^A\^A\^A$")
737         (case-fold-search t)
738         start message-id end)
739     (goto-char (point-min))
740     (if (not (and (re-search-forward delim nil t)
741                   (forward-line 1)))
742         ;; Possibly wrong format?
743         (error "Error, unknown mail format! (Possibly corrupted.)")
744       ;; Carry on until the bitter end.
745       (while (not (eobp))
746         (setq start (point))
747         ;; Find the end of the head.
748         (narrow-to-region
749          start 
750          (if (search-forward "\n\n" nil t)
751              (1- (point))
752            ;; This will never happen, but just to be on the safe side --
753            ;; if there is no head-body delimiter, we search a bit manually.
754            (while (and (looking-at "From \\|[^ \t]+:")
755                        (not (eobp)))
756              (forward-line 1)
757              (point))))
758         ;; Find the Message-ID header.
759         (goto-char (point-min))
760         (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
761             (setq message-id (match-string 1))
762           ;; There is no Message-ID here, so we create one.
763           (save-excursion
764             (when (re-search-backward "^Message-ID:" nil t)
765               (beginning-of-line)
766               (insert "Original-")))
767           (forward-line 1)
768           (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
769         (run-hooks 'nnmail-prepare-incoming-header-hook)
770         ;; Find the end of this article.
771         (goto-char (point-max))
772         (widen)
773         (if (re-search-forward delim nil t)
774             (beginning-of-line)
775           (goto-char (point-max)))
776         ;; Allow the backend to save the article.
777         (save-excursion
778           (save-restriction
779             (narrow-to-region start (point))
780             (goto-char (point-min))
781             (nnmail-check-duplication message-id func artnum-func)
782             (setq end (point-max))))
783         (goto-char end)
784         (forward-line 2)))))
785
786 (defun nnmail-split-incoming (incoming func &optional exit-func
787                                        group artnum-func)
788   "Go through the entire INCOMING file and pick out each individual mail.
789 FUNC will be called with the buffer narrowed to each mail."
790   (let (;; If this is a group-specific split, we bind the split
791         ;; methods to just this group.
792         (nnmail-split-methods (if (and group
793                                        (or (eq nnmail-spool-file 'procmail)
794                                            nnmail-use-procmail)
795                                        (not nnmail-resplit-incoming))
796                                   (list (list group ""))
797                                 nnmail-split-methods)))
798     (save-excursion
799       ;; Insert the incoming file.
800       (set-buffer (get-buffer-create " *nnmail incoming*"))
801       (buffer-disable-undo (current-buffer))
802       (erase-buffer)
803       (nnheader-insert-file-contents-literally incoming)
804       (unless (zerop (buffer-size))
805         (goto-char (point-min))
806         (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
807         ;; Handle both babyl, MMDF and unix mail formats, since movemail will
808         ;; use the former when fetching from a mailbox, the latter when
809         ;; fetches from a file.
810         (cond ((or (looking-at "\^L")
811                    (looking-at "BABYL OPTIONS:"))
812                (nnmail-process-babyl-mail-format func artnum-func))
813               ((looking-at "\^A\^A\^A\^A")
814                (nnmail-process-mmdf-mail-format func artnum-func))
815               (t
816                (nnmail-process-unix-mail-format func artnum-func))))
817       (when exit-func
818         (funcall exit-func))
819       (kill-buffer (current-buffer)))))
820
821 ;; Mail crossposts suggested by Brian Edmonds <edmonds@cs.ubc.ca>. 
822 (defun nnmail-article-group (func)
823   "Look at the headers and return an alist of groups that match.
824 FUNC will be called with the group name to determine the article number."
825   (let ((methods nnmail-split-methods)
826         (obuf (current-buffer))
827         (beg (point-min))
828         end group-art method)
829     (if (and (sequencep methods) (= (length methods) 1))
830         ;; If there is only just one group to put everything in, we
831         ;; just return a list with just this one method in.
832         (setq group-art
833               (list (cons (caar methods) (funcall func (caar methods)))))
834       ;; We do actual comparison.
835       (save-excursion
836         ;; Find headers.
837         (goto-char beg)
838         (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
839         (set-buffer nntp-server-buffer)
840         (erase-buffer)
841         ;; Copy the headers into the work buffer.
842         (insert-buffer-substring obuf beg end)
843         ;; Fold continuation lines.
844         (goto-char (point-min))
845         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
846           (replace-match " " t t))
847         ;; Allow washing.
848         (run-hooks 'nnmail-split-hook)
849         (if (and (symbolp nnmail-split-methods)
850                  (fboundp nnmail-split-methods))
851             ;; `nnmail-split-methods' is a function, so we just call 
852             ;; this function here and use the result.
853             (setq group-art
854                   (mapcar
855                    (lambda (group) (cons group (funcall func group)))
856                    (condition-case nil
857                        (or (funcall nnmail-split-methods)
858                            '("bogus"))
859                      (error
860                       (message 
861                        "Error in `nnmail-split-methods'; using `bogus' mail group")
862                       (sit-for 1)
863                       '("bogus")))))
864           ;; Go through the split methods to find a match.
865           (while (and methods (or nnmail-crosspost (not group-art)))
866             (goto-char (point-max))
867             (setq method (pop methods))
868             (if (or methods
869                     (not (equal "" (nth 1 method))))
870                 (when (and
871                        (condition-case ()
872                            (if (stringp (nth 1 method))
873                                (re-search-backward (cadr method) nil t)
874                              ;; Function to say whether this is a match.
875                              (funcall (nth 1 method) (car method)))
876                          (error nil))
877                        ;; Don't enter the article into the same 
878                        ;; group twice.
879                        (not (assoc (car method) group-art)))
880                   (push (cons (car method) (funcall func (car method)))
881                         group-art))
882               ;; This is the final group, which is used as a 
883               ;; catch-all.
884               (unless group-art
885                 (setq group-art 
886                       (list (cons (car method)
887                                   (funcall func (car method)))))))))
888         ;; See whether the split methods returned `junk'.
889         (if (equal group-art '(junk))
890             nil
891           (nreverse (delq 'junk group-art)))))))
892
893 (defun nnmail-insert-lines ()
894   "Insert how many lines there are in the body of the mail.
895 Return the number of characters in the body."
896   (let (lines chars)
897     (save-excursion
898       (goto-char (point-min))
899       (when (search-forward "\n\n" nil t)
900         (setq chars (- (point-max) (point)))
901         (setq lines (count-lines (point) (point-max)))
902         (forward-char -1)
903         (save-excursion
904           (when (re-search-backward "^Lines: " nil t)
905             (delete-region (point) (progn (forward-line 1) (point)))))
906         (beginning-of-line)
907         (insert (format "Lines: %d\n" (max lines 0)))
908         chars))))
909
910 (defun nnmail-insert-xref (group-alist)
911   "Insert an Xref line based on the (group . article) alist."
912   (save-excursion
913     (goto-char (point-min))
914     (when (search-forward "\n\n" nil t)
915       (forward-char -1)
916       (when (re-search-backward "^Xref: " nil t)
917         (delete-region (match-beginning 0)
918                        (progn (forward-line 1) (point))))
919       (insert (format "Xref: %s" (system-name)))
920       (while group-alist
921         (insert (format " %s:%d" (caar group-alist) (cdar group-alist)))
922         (setq group-alist (cdr group-alist)))
923       (insert "\n"))))
924
925 ;;; Message washing functions
926
927 (defun nnmail-remove-leading-whitespace ()
928   "Remove excessive whitespace from all headers."
929   (goto-char (point-min))
930   (while (re-search-forward "^\\([^ :]+: \\) +" nil t)
931     (replace-match "\\1" t t)))
932
933 (defun nnmail-remove-list-identifiers ()
934   "Remove list identifiers from Subject headers."
935   (let ((regexp (if (stringp nnmail-list-identifiers) nnmail-list-identifiers
936                   (mapconcat 'identity nnmail-list-identifiers "\\|"))))
937     (when regexp
938       (goto-char (point-min))
939       (when (re-search-forward
940              (concat "Subject: +\\(Re: +\\)?\\(" regexp "\\) *")
941              nil t)
942         (delete-region (match-beginning 2) (match-end 0))))))
943
944 (defun nnmail-remove-tabs ()
945   "Translate TAB characters into SPACE characters."
946   (subst-char-in-region (point-min) (point-max) ?\t ?  t))
947
948 ;;; Utility functions
949
950 ;; Written by byer@mv.us.adobe.com (Scott Byer).
951 (defun nnmail-make-complex-temp-name (prefix)
952   (let ((newname (make-temp-name prefix))
953         (newprefix prefix))
954     (while (file-exists-p newname)
955       (setq newprefix (concat newprefix "x"))
956       (setq newname (make-temp-name newprefix)))
957     newname))
958
959 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
960
961 (defun nnmail-split-fancy ()
962   "Fancy splitting method.
963 See the documentation for the variable `nnmail-split-fancy' for documentation."
964   (let ((syntab (syntax-table)))
965     (unwind-protect
966         (progn
967           (set-syntax-table nnmail-split-fancy-syntax-table)
968           (nnmail-split-it nnmail-split-fancy))
969       (set-syntax-table syntab))))
970
971 (defvar nnmail-split-cache nil)
972 ;; Alist of split expressions their equivalent regexps.
973
974 (defun nnmail-split-it (split)
975   ;; Return a list of groups matching SPLIT.
976   (cond
977    ((stringp split)
978     ;; A group.
979     (list split))
980    ((eq split 'junk)
981     ;; Junk this.
982     (list 'junk))
983    ((eq (car split) '&)
984     (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
985    ((eq (car split) '|)
986     (let (done)
987       (while (and (not done) (cdr split))
988         (setq split (cdr split)
989               done (nnmail-split-it (car split))))
990       done))
991    ((assq split nnmail-split-cache)
992     ;; A compiled match expression.
993     (goto-char (point-max))
994     (when (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
995       (nnmail-split-it (nth 2 split))))
996    (t
997     ;; An uncompiled match.
998     (let* ((field (nth 0 split))
999            (value (nth 1 split))
1000            (regexp (concat "^\\(" 
1001                            (if (symbolp field)
1002                                (cdr (assq field nnmail-split-abbrev-alist))
1003                              field)
1004                            "\\):.*\\<\\("
1005                            (if (symbolp value)
1006                                (cdr (assq value nnmail-split-abbrev-alist))
1007                              value)
1008                            "\\)\\>")))
1009       (push (cons split regexp) nnmail-split-cache)
1010       (goto-char (point-max))
1011       (when (re-search-backward regexp nil t)
1012         (nnmail-split-it (nth 2 split)))))))
1013
1014 ;; Get a list of spool files to read.
1015 (defun nnmail-get-spool-files (&optional group)
1016   (if (null nnmail-spool-file)
1017       ;; No spool file whatsoever.
1018       nil
1019     (let* ((procmails 
1020             ;; If procmail is used to get incoming mail, the files
1021             ;; are stored in this directory.
1022             (and (file-exists-p nnmail-procmail-directory)
1023                  (or (eq nnmail-spool-file 'procmail)
1024                      nnmail-use-procmail)
1025                  (directory-files 
1026                   nnmail-procmail-directory 
1027                   t (concat (if group (concat "^" group) "")
1028                             nnmail-procmail-suffix "$"))))
1029            (p procmails)
1030            (crash (when (and (file-exists-p nnmail-crash-box)
1031                              (> (nnheader-file-size
1032                                  (file-truename nnmail-crash-box))
1033                                 0))
1034                     (list nnmail-crash-box))))
1035       ;; Remove any directories that inadvertently match the procmail
1036       ;; suffix, which might happen if the suffix is "". 
1037       (while p
1038         (when (file-directory-p (car p))
1039           (setq procmails (delete (car p) procmails)))
1040         (setq p (cdr p)))
1041       ;; Return the list of spools.
1042       (append 
1043        crash
1044        (cond ((and group
1045                    (or (eq nnmail-spool-file 'procmail)
1046                        nnmail-use-procmail)
1047                    procmails)
1048               procmails)
1049              ((and group
1050                    (eq nnmail-spool-file 'procmail))
1051               nil)
1052              ((listp nnmail-spool-file)
1053               (nconc
1054                (apply 
1055                 'nconc
1056                 (mapcar 
1057                  (lambda (file)
1058                    (if (file-directory-p file)
1059                        (nnheader-directory-regular-files file)
1060                      (list file)))
1061                  nnmail-spool-file))
1062                procmails))
1063              ((and (stringp nnmail-spool-file)
1064                    (not (file-directory-p nnmail-spool-file)))
1065               (cons nnmail-spool-file procmails))
1066              ((and (stringp nnmail-spool-file)
1067                    (file-directory-p nnmail-spool-file))
1068               (nconc
1069                (nnheader-directory-regular-files nnmail-spool-file)
1070                procmails))
1071              ((eq nnmail-spool-file 'pop)
1072               (cons (format "po:%s" (user-login-name)) procmails))
1073              (t
1074               procmails))))))
1075
1076 ;; Activate a backend only if it isn't already activated. 
1077 ;; If FORCE, re-read the active file even if the backend is 
1078 ;; already activated.
1079 (defun nnmail-activate (backend &optional force)
1080   (let (file timestamp file-time)
1081     (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
1082             force
1083             (and (setq file (condition-case ()
1084                                 (symbol-value (intern (format "%s-active-file" 
1085                                                               backend)))
1086                               (error nil)))
1087                  (setq file-time (nth 5 (file-attributes file)))
1088                  (or (not
1089                       (setq timestamp
1090                             (condition-case ()
1091                                 (symbol-value (intern
1092                                                (format "%s-active-timestamp" 
1093                                                        backend)))
1094                               (error 'none))))
1095                      (not (consp timestamp))
1096                      (equal timestamp '(0 0))
1097                      (> (nth 0 file-time) (nth 0 timestamp))
1098                      (and (= (nth 0 file-time) (nth 0 timestamp))
1099                           (> (nth 1 file-time) (nth 1 timestamp))))))
1100         (save-excursion
1101           (or (eq timestamp 'none)
1102               (set (intern (format "%s-active-timestamp" backend)) 
1103 ;;; dmoore@ucsd.edu 25.10.96
1104 ;;; it's not always the case that current-time
1105 ;;; does correspond to changes in the file's time.  So just compare
1106 ;;; the file's new time against its own previous time.
1107 ;;;                (current-time)
1108                    file-time
1109                    ))
1110           (funcall (intern (format "%s-request-list" backend)))
1111 ;;; dmoore@ucsd.edu 25.10.96
1112 ;;; BACKEND-request-list already does this itself!
1113 ;;;       (set (intern (format "%s-group-alist" backend)) 
1114 ;;;            (nnmail-get-active))
1115           ))
1116     t))
1117
1118 (defun nnmail-message-id ()
1119   (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1120
1121 ;;;
1122 ;;; nnmail duplicate handling
1123 ;;;
1124
1125 (defvar nnmail-cache-buffer nil)
1126
1127 (defun nnmail-cache-open ()
1128   (if (or (not nnmail-treat-duplicates)
1129           (and nnmail-cache-buffer
1130                (buffer-name nnmail-cache-buffer)))
1131       ()                                ; The buffer is open.
1132     (save-excursion
1133       (set-buffer 
1134        (setq nnmail-cache-buffer 
1135              (get-buffer-create " *nnmail message-id cache*")))
1136       (buffer-disable-undo (current-buffer))
1137       (when (file-exists-p nnmail-message-id-cache-file)
1138         (insert-file-contents nnmail-message-id-cache-file))
1139       (set-buffer-modified-p nil)
1140       (current-buffer))))
1141
1142 (defun nnmail-cache-close ()
1143   (when (and nnmail-cache-buffer
1144              nnmail-treat-duplicates
1145              (buffer-name nnmail-cache-buffer)
1146              (buffer-modified-p nnmail-cache-buffer))
1147     (save-excursion
1148       (set-buffer nnmail-cache-buffer)
1149       ;; Weed out the excess number of Message-IDs.
1150       (goto-char (point-max))
1151       (when (search-backward "\n" nil t nnmail-message-id-cache-length)
1152         (progn
1153           (beginning-of-line)
1154           (delete-region (point-min) (point))))
1155       ;; Save the buffer.
1156       (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1157           (make-directory (file-name-directory nnmail-message-id-cache-file)
1158                           t))
1159       (nnmail-write-region (point-min) (point-max)
1160                            nnmail-message-id-cache-file nil 'silent)
1161       (set-buffer-modified-p nil)
1162       (setq nnmail-cache-buffer nil)
1163       ;;(kill-buffer (current-buffer))
1164       )))
1165
1166 (defun nnmail-cache-insert (id)
1167   (when nnmail-treat-duplicates
1168     (save-excursion
1169       (set-buffer nnmail-cache-buffer)
1170       (goto-char (point-max))
1171       (insert id "\n"))))
1172
1173 (defun nnmail-cache-id-exists-p (id)
1174   (when nnmail-treat-duplicates
1175     (save-excursion
1176       (set-buffer nnmail-cache-buffer)
1177       (goto-char (point-max))
1178       (search-backward id nil t))))
1179
1180 (defun nnmail-check-duplication (message-id func artnum-func)
1181   (run-hooks 'nnmail-prepare-incoming-message-hook)
1182   ;; If this is a duplicate message, then we do not save it.
1183   (let* ((duplication (nnmail-cache-id-exists-p message-id))
1184          (case-fold-search t)
1185          (action (when duplication
1186                    (cond
1187                     ((memq nnmail-treat-duplicates '(warn delete))
1188                      nnmail-treat-duplicates)
1189                     ((nnheader-functionp nnmail-treat-duplicates)
1190                      (funcall nnmail-treat-duplicates message-id))
1191                     (t
1192                      nnmail-treat-duplicates))))
1193          group-art)
1194     ;; Let the backend save the article (or not).
1195     (cond
1196      ((not duplication)
1197       (nnmail-cache-insert message-id)
1198       (funcall func (setq group-art
1199                           (nreverse (nnmail-article-group artnum-func)))))
1200      ((eq action 'delete)
1201       (setq group-art nil))
1202      ((eq action 'warn)
1203       ;; We insert a warning.
1204       (let ((case-fold-search t)
1205             (newid (nnmail-message-id)))
1206         (goto-char (point-min))
1207         (when (re-search-forward "^message-id:" nil t)
1208           (beginning-of-line)
1209           (insert "Original-"))
1210         (beginning-of-line)
1211         (insert 
1212          "Message-ID: " newid "\n"
1213          "Gnus-Warning: This is a duplicate of message " message-id "\n")
1214         (nnmail-cache-insert newid)
1215         (funcall func (setq group-art
1216                             (nreverse (nnmail-article-group artnum-func))))))
1217      (t
1218       (funcall func (setq group-art
1219                           (nreverse (nnmail-article-group artnum-func))))))
1220     ;; Add the group-art list to the history list.
1221     (if group-art
1222         (push group-art nnmail-split-history)
1223       (delete-region (point-min) (point-max)))))
1224
1225 ;;; Get new mail.
1226
1227 (defun nnmail-get-value (&rest args)
1228   (let ((sym (intern (apply 'format args))))
1229     (when (boundp sym)
1230       (symbol-value sym))))
1231
1232 (defun nnmail-get-new-mail (method exit-func temp
1233                                    &optional group spool-func)
1234   "Read new incoming mail."
1235   ;; Nix out the previous split history.
1236   (unless group
1237     (setq nnmail-split-history nil))
1238   (let* ((spools (nnmail-get-spool-files group))
1239          (group-in group)
1240          incoming incomings spool)
1241     (when (and (nnmail-get-value "%s-get-new-mail" method)
1242                nnmail-spool-file)
1243       ;; We first activate all the groups.
1244       (nnmail-activate method)
1245       ;; Allow the user to hook.
1246       (run-hooks 'nnmail-pre-get-new-mail-hook)
1247       ;; Open the message-id cache.
1248       (nnmail-cache-open)
1249       ;; The we go through all the existing spool files and split the
1250       ;; mail from each.
1251       (while spools
1252         (setq spool (pop spools))
1253         ;; We read each spool file if either the spool is a POP-mail
1254         ;; spool, or the file exists.  We can't check for the
1255         ;; existence of POPped mail.
1256         (when (or (string-match "^po:" spool)
1257                   (and (file-exists-p spool)
1258                        (> (nnheader-file-size (file-truename spool)) 0)))
1259           (nnheader-message 3 "%s: Reading incoming mail..." method)
1260           (when (and (nnmail-move-inbox spool)
1261                      (file-exists-p nnmail-crash-box))
1262             ;; There is new mail.  We first find out if all this mail
1263             ;; is supposed to go to some specific group.
1264             (setq group (nnmail-get-split-group spool group-in))
1265             ;; We split the mail
1266             (nnmail-split-incoming 
1267              nnmail-crash-box (intern (format "%s-save-mail" method))
1268              spool-func group (intern (format "%s-active-number" method)))
1269             ;; Check whether the inbox is to be moved to the special tmp dir. 
1270             (setq incoming
1271                   (nnmail-make-complex-temp-name 
1272                    (expand-file-name 
1273                     (if nnmail-tmp-directory
1274                         (concat 
1275                          (file-name-as-directory nnmail-tmp-directory)
1276                          (file-name-nondirectory (concat temp "Incoming")))
1277                       (concat temp "Incoming")))))
1278             (rename-file nnmail-crash-box incoming t)
1279             (push incoming incomings))))
1280       ;; If we did indeed read any incoming spools, we save all info. 
1281       (when incomings
1282         (nnmail-save-active 
1283          (nnmail-get-value "%s-group-alist" method)
1284          (nnmail-get-value "%s-active-file" method))
1285         (when exit-func
1286           (funcall exit-func))
1287         (run-hooks 'nnmail-read-incoming-hook)
1288         (nnheader-message 3 "%s: Reading incoming mail...done" method))
1289       ;; Close the message-id cache.
1290       (nnmail-cache-close)
1291       ;; Allow the user to hook.
1292       (run-hooks 'nnmail-post-get-new-mail-hook)
1293       ;; Delete all the temporary files.
1294       (while incomings
1295         (setq incoming (pop incomings))
1296         (and nnmail-delete-incoming
1297              (file-exists-p incoming)
1298              (file-writable-p incoming)
1299              (delete-file incoming))))))
1300
1301 (defun nnmail-expired-article-p (group time force &optional inhibit)
1302   "Say whether an article that is TIME old in GROUP should be expired."
1303   (if force
1304       t
1305     (let ((days (or (and nnmail-expiry-wait-function
1306                          (funcall nnmail-expiry-wait-function group))
1307                     nnmail-expiry-wait)))
1308       (cond ((or (eq days 'never)
1309                  (and (not force)
1310                       inhibit))
1311              ;; This isn't an expirable group.
1312              nil)
1313             ((eq days 'immediate)
1314              ;; We expire all articles on sight.
1315              t)
1316             ((equal time '(0 0))
1317              ;; This is an ange-ftp group, and we don't have any dates.
1318              nil)
1319             ((numberp days)
1320              (setq days (nnmail-days-to-time days))
1321              ;; Compare the time with the current time.
1322              (nnmail-time-less days (nnmail-time-since time)))))))
1323
1324 (defvar nnmail-read-passwd nil)
1325 (defun nnmail-read-passwd (prompt &rest args)
1326   "Read a password using PROMPT.
1327 If ARGS, PROMPT is used as an argument to `format'."
1328   (let ((prompt
1329          (if args
1330              (apply 'format prompt args)
1331            prompt)))
1332     (unless nnmail-read-passwd
1333       (if (load "passwd" t)
1334           (setq nnmail-read-passwd 'read-passwd)
1335         (autoload 'ange-ftp-read-passwd "ange-ftp")
1336         (setq nnmail-read-passwd 'ange-ftp-read-passwd)))
1337     (funcall nnmail-read-passwd prompt)))
1338
1339 (defun nnmail-check-syntax ()
1340   "Check (and modify) the syntax of the message in the current buffer."
1341   (save-restriction
1342     (message-narrow-to-head)
1343     (let ((case-fold-search t))
1344       (unless (re-search-forward "^Message-ID:" nil t)
1345         (insert "Message-ID: " (nnmail-message-id) "\n")))))
1346
1347 (defun nnmail-write-region (start end filename &optional append visit lockname)
1348   "Do a `write-region', and then set the file modes."
1349   (write-region start end filename append visit lockname)
1350   (set-file-modes filename nnmail-default-file-modes))
1351
1352 ;;;
1353 ;;; Status functions
1354 ;;;
1355
1356 (defun nnmail-replace-status (name value)
1357   "Make status NAME and VALUE part of the current status line."
1358   (save-restriction
1359     (message-narrow-to-head)
1360     (let ((status (nnmail-decode-status)))
1361       (setq status (delq (member name status) status))
1362       (when value
1363         (push (cons name value) status))
1364       (message-remove-header "status")
1365       (goto-char (point-max))
1366       (insert "Status: " (nnmail-encode-status status) "\n"))))
1367
1368 (defun nnmail-decode-status ()
1369   "Return a status-value alist from STATUS."
1370   (goto-char (point-min))
1371   (when (re-search-forward "^Status: " nil t)
1372     (let (name value status)
1373       (save-restriction
1374         ;; Narrow to the status.
1375         (narrow-to-region
1376          (point)
1377          (if (re-search-forward "^[^ \t]" nil t)
1378              (1- (point))
1379            (point-max)))
1380         ;; Go through all elements and add them to the list.
1381         (goto-char (point-min))
1382         (while (re-search-forward "[^ \t=]+" nil t)
1383           (setq name (match-string 0))
1384           (if (not (= (following-char) ?=))
1385               ;; Implied "yes".
1386               (setq value "yes")
1387             (forward-char 1)
1388             (if (not (= (following-char) ?\"))
1389                 (if (not (looking-at "[^ \t]"))
1390                     ;; Implied "no".
1391                     (setq value "no")
1392                   ;; Unquoted value.
1393                   (setq value (match-string 0))
1394                   (goto-char (match-end 0)))
1395               ;; Quoted value.
1396               (setq value (read (current-buffer)))))
1397           (push (cons name value) status)))
1398       status)))
1399
1400 (defun nnmail-encode-status (status)
1401   "Return a status string from STATUS."
1402   (mapconcat
1403    (lambda (elem)
1404      (concat
1405       (car elem) "="
1406       (if (string-match "[ \t]" (cdr elem))
1407           (prin1-to-string (cdr elem))
1408         (cdr elem))))
1409    status " "))
1410
1411 (defun nnmail-split-history ()
1412   "Generate an overview of where the last mail split put articles."
1413   (interactive)
1414   (unless nnmail-split-history
1415     (error "No current split history"))
1416   (pop-to-buffer "*nnmail split history*")
1417   (buffer-disable-undo (current-buffer))
1418   (erase-buffer)
1419   (let ((history nnmail-split-history)
1420         elem ga)
1421     (while (setq elem (pop history))
1422       (insert (mapconcat (lambda (ga)
1423                            (concat (car ga) ":" (int-to-string (cdr ga))))
1424                          elem
1425                          ", ")
1426               "\n"))
1427     (goto-char (point-min))))
1428
1429 (defun nnmail-new-mail-p (group)
1430   "Say whether GROUP has new mail."
1431   (let ((his nnmail-split-history)
1432         found)
1433     (while his
1434       (when (assoc group (pop his))
1435         (setq found t
1436               his nil)))
1437     found))
1438         
1439 (run-hooks 'nnmail-load-hook)
1440             
1441 (provide 'nnmail)
1442
1443 ;;; nnmail.el ends here