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