*** empty log message ***
[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                     (set-file-modes inbox nnmail-default-file-modes)
444                     (push inbox nnmail-moved-inboxes))
445                 (set-buffer errors)
446                 ;; There may be a warning about older revisions.  We
447                 ;; ignore those.
448                 (goto-char (point-min))
449                 (if (search-forward "older revision" nil t)
450                     (progn
451                       (set-file-modes inbox nnmail-default-file-modes)
452                       (push inbox nnmail-moved-inboxes))
453                   ;; Probably a real error.
454                   (subst-char-in-region (point-min) (point-max) ?\n ?\  )
455                   (goto-char (point-max))
456                   (skip-chars-backward " \t")
457                   (delete-region (point) (point-max))
458                   (goto-char (point-min))
459                   (when (looking-at "movemail: ")
460                     (delete-region (point-min) (match-end 0)))
461                   (unless (yes-or-no-p
462                            (format "movemail: %s.  Continue? "
463                                    (buffer-string)))
464                     (error "%s" (buffer-string)))
465                   (setq tofile nil)))))))
466       (and errors
467            (buffer-name errors)
468            (kill-buffer errors))
469       tofile)))
470
471 (defun nnmail-get-active ()
472   "Returns an assoc of group names and active ranges.
473 nn*-request-list should have been called before calling this function."
474   (let (group-assoc)
475     ;; Go through all groups from the active list.
476     (save-excursion
477       (set-buffer nntp-server-buffer)
478       (goto-char (point-min))
479       (while (re-search-forward 
480               "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
481         ;; We create an alist with `(GROUP (LOW . HIGH))' elements.
482         (push (list (match-string 1)
483                     (cons (string-to-int (match-string 3))
484                           (string-to-int (match-string 2))))
485               group-assoc)))
486     group-assoc))
487
488 (defun nnmail-save-active (group-assoc file-name)
489   "Save GROUP-ASSOC in ACTIVE-FILE."
490   (when file-name
491     (let (group)
492       (save-excursion
493         (set-buffer (get-buffer-create " *nnmail active*"))
494         (buffer-disable-undo (current-buffer))
495         (erase-buffer)
496         (while group-assoc
497           (setq group (pop group-assoc))
498           (insert (format "%s %d %d y\n" (car group) (cdadr group) 
499                           (caadr group))))
500         (unless (file-exists-p (file-name-directory file-name))
501           (make-directory (file-name-directory file-name) t))
502         (nnmail-write-region
503          1 (point-max) (expand-file-name file-name) nil 'nomesg)
504         (kill-buffer (current-buffer))))))
505
506 (defun nnmail-get-split-group (file group)
507   (if (or (eq nnmail-spool-file 'procmail)
508           nnmail-use-procmail)
509       (cond (group group)
510             ((string-match (concat "^" (expand-file-name
511                                         (file-name-as-directory
512                                          nnmail-procmail-directory))
513                                    "\\([^/]*\\)" nnmail-procmail-suffix "$")
514                            (expand-file-name file))
515              (substring (expand-file-name file)
516                         (match-beginning 1) (match-end 1)))
517             (t
518              group))
519     group))
520
521 (defun nnmail-process-babyl-mail-format (func artnum-func)
522   (let ((case-fold-search t)
523         start message-id content-length do-search end)
524     (while (not (eobp))
525       (goto-char (point-min))
526       (re-search-forward
527        "\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
528       (goto-char (match-end 0))
529       (delete-region (match-beginning 0) (match-end 0))
530       (setq start (point))
531       ;; Skip all the headers in case there are more "From "s...
532       (or (search-forward "\n\n" nil t)
533           (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
534           (search-forward "\1f\f"))
535       ;; Find the Message-ID header.
536       (save-excursion
537         (if (re-search-backward "^Message-ID:[ \t]*\\(<[^>]*>\\)" nil t)
538             (setq message-id (buffer-substring (match-beginning 1)
539                                                (match-end 1)))
540           ;; There is no Message-ID here, so we create one.
541           (save-excursion
542             (when (re-search-backward "^Message-ID:" nil t)
543               (beginning-of-line)
544               (insert "Original-")))
545           (forward-line -1)
546           (insert "Message-ID: " (setq message-id (nnmail-message-id))
547                   "\n")))
548       ;; Look for a Content-Length header.
549       (if (not (save-excursion
550                  (and (re-search-backward 
551                        "^Content-Length:[ \t]*\\([0-9]+\\)" start t)
552                       (setq content-length (string-to-int
553                                             (buffer-substring 
554                                              (match-beginning 1)
555                                              (match-end 1))))
556                       ;; We destroy the header, since none of
557                       ;; the backends ever use it, and we do not
558                       ;; want to confuse other mailers by having
559                       ;; a (possibly) faulty header.
560                       (progn (insert "X-") t))))
561           (setq do-search t)
562         (if (or (= (+ (point) content-length) (point-max))
563                 (save-excursion
564                   (goto-char (+ (point) content-length))
565                   (looking-at "\1f")))
566             (progn
567               (goto-char (+ (point) content-length))
568               (setq do-search nil))
569           (setq do-search t)))
570       ;; Go to the beginning of the next article - or to the end
571       ;; of the buffer.  
572       (if do-search
573           (if (re-search-forward "^\1f" nil t)
574               (goto-char (match-beginning 0))
575             (goto-char (1- (point-max)))))
576       (delete-char 1)                   ; delete ^_
577       (save-excursion
578         (save-restriction
579           (narrow-to-region start (point))
580           (goto-char (point-min))
581           (nnmail-check-duplication message-id func artnum-func)
582           (setq end (point-max))))
583       (goto-char end))))
584
585 (defun nnmail-search-unix-mail-delim ()
586   "Put point at the beginning of the next message."
587   (let ((case-fold-search t)
588         (delim (concat "^" message-unix-mail-delimiter))
589         found)
590     (while (not found)
591       (if (re-search-forward delim nil t)
592           (when (or (looking-at "[^\n :]+ *:")
593                     (looking-at delim)
594                     (looking-at (concat ">" message-unix-mail-delimiter)))
595             (forward-line -1)
596             (setq found 'yes))
597         (setq found 'no)))
598     (eq found 'yes)))
599
600 (defun nnmail-process-unix-mail-format (func artnum-func)
601   (let ((case-fold-search t)
602         (delim (concat "^" message-unix-mail-delimiter))
603         start message-id content-length end skip head-end)
604     (goto-char (point-min))
605     (if (not (and (re-search-forward delim nil t)
606                   (goto-char (match-beginning 0))))
607         ;; Possibly wrong format?
608         (error "Error, unknown mail format! (Possibly corrupted.)")
609       ;; Carry on until the bitter end.
610       (while (not (eobp))
611         (setq start (point)
612               end nil)
613         ;; Find the end of the head.
614         (narrow-to-region
615          start 
616          (if (search-forward "\n\n" nil t)
617              (1- (point))
618            ;; This will never happen, but just to be on the safe side --
619            ;; if there is no head-body delimiter, we search a bit manually.
620            (while (and (looking-at "From \\|[^ \t]+:")
621                        (not (eobp)))
622              (forward-line 1)
623              (point))))
624         ;; Find the Message-ID header.
625         (goto-char (point-min))
626         (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
627             (setq message-id (match-string 1))
628           (save-excursion
629             (when (re-search-forward "^Message-ID:" nil t)
630               (beginning-of-line)
631               (insert "Original-")))
632           ;; There is no Message-ID here, so we create one.
633           (forward-line 1)
634           (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
635         ;; Look for a Content-Length header.
636         (goto-char (point-min))
637         (if (not (re-search-forward
638                   "^Content-Length:[ \t]*\\([0-9]+\\)" nil t))
639             (setq content-length nil)
640           (setq content-length (string-to-int (match-string 1)))
641           ;; We destroy the header, since none of the backends ever 
642           ;; use it, and we do not want to confuse other mailers by
643           ;; having a (possibly) faulty header.
644           (beginning-of-line)
645           (insert "X-"))
646         ;; Find the end of this article.
647         (goto-char (point-max))
648         (widen)
649         (setq head-end (point))
650         ;; We try the Content-Length value.  The idea: skip over the header
651         ;; separator, then check what happens content-length bytes into the
652         ;; message body.  This should be either the end ot the buffer, the
653         ;; message separator or a blank line followed by the separator.
654         ;; The blank line should probably be deleted.  If neither of the
655         ;; three is met, the content-length header is probably invalid.
656         (when content-length
657           (forward-line 1)
658           (setq skip (+ (point) content-length))
659           (goto-char skip)
660           (cond ((or (= skip (point-max))
661                      (= (1+ skip) (point-max)))
662                  (setq end (point-max)))
663                 ((looking-at delim)
664                  (setq end skip))
665                 ((looking-at
666                   (concat "[ \t]*\n\\(" delim "\\)"))
667                  (setq end (match-beginning 1)))
668                 (t (setq end nil))))
669         (if end
670             (goto-char end)
671           ;; No Content-Length, so we find the beginning of the next 
672           ;; article or the end of the buffer.
673           (goto-char head-end)
674           (or (nnmail-search-unix-mail-delim)
675               (goto-char (point-max))))
676         ;; Allow the backend to save the article.
677         (save-excursion
678           (save-restriction
679             (narrow-to-region start (point))
680             (goto-char (point-min))
681             (nnmail-check-duplication message-id func artnum-func)
682             (setq end (point-max))))
683         (goto-char end)))))
684
685 (defun nnmail-process-mmdf-mail-format (func artnum-func)
686   (let ((delim "^\^A\^A\^A\^A$")
687         (case-fold-search t)
688         start message-id end)
689     (goto-char (point-min))
690     (if (not (and (re-search-forward delim nil t)
691                   (forward-line 1)))
692         ;; Possibly wrong format?
693         (error "Error, unknown mail format! (Possibly corrupted.)")
694       ;; Carry on until the bitter end.
695       (while (not (eobp))
696         (setq start (point))
697         ;; Find the end of the head.
698         (narrow-to-region
699          start 
700          (if (search-forward "\n\n" nil t)
701              (1- (point))
702            ;; This will never happen, but just to be on the safe side --
703            ;; if there is no head-body delimiter, we search a bit manually.
704            (while (and (looking-at "From \\|[^ \t]+:")
705                        (not (eobp)))
706              (forward-line 1)
707              (point))))
708         ;; Find the Message-ID header.
709         (goto-char (point-min))
710         (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
711             (setq message-id (match-string 1))
712           ;; There is no Message-ID here, so we create one.
713           (save-excursion
714             (when (re-search-backward "^Message-ID:" nil t)
715               (beginning-of-line)
716               (insert "Original-")))
717           (forward-line 1)
718           (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
719         ;; Find the end of this article.
720         (goto-char (point-max))
721         (widen)
722         (if (re-search-forward delim nil t)
723             (beginning-of-line)
724           (goto-char (point-max)))
725         ;; Allow the backend to save the article.
726         (save-excursion
727           (save-restriction
728             (narrow-to-region start (point))
729             (goto-char (point-min))
730             (nnmail-check-duplication message-id func artnum-func)
731             (setq end (point-max))))
732         (goto-char end)
733         (forward-line 2)))))
734
735 (defun nnmail-split-incoming (incoming func &optional exit-func
736                                        group artnum-func)
737   "Go through the entire INCOMING file and pick out each individual mail.
738 FUNC will be called with the buffer narrowed to each mail."
739   (let (;; If this is a group-specific split, we bind the split
740         ;; methods to just this group.
741         (nnmail-split-methods (if (and group
742                                        (or (eq nnmail-spool-file 'procmail)
743                                            nnmail-use-procmail)
744                                        (not nnmail-resplit-incoming))
745                                   (list (list group ""))
746                                 nnmail-split-methods)))
747     (save-excursion
748       ;; Insert the incoming file.
749       (set-buffer (get-buffer-create " *nnmail incoming*"))
750       (buffer-disable-undo (current-buffer))
751       (erase-buffer)
752       (nnheader-insert-file-contents-literally incoming)
753       (unless (zerop (buffer-size))
754         (goto-char (point-min))
755         (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
756         ;; Handle both babyl, MMDF and unix mail formats, since movemail will
757         ;; use the former when fetching from a mailbox, the latter when
758         ;; fetches from a file.
759         (cond ((or (looking-at "\^L")
760                    (looking-at "BABYL OPTIONS:"))
761                (nnmail-process-babyl-mail-format func artnum-func))
762               ((looking-at "\^A\^A\^A\^A")
763                (nnmail-process-mmdf-mail-format func artnum-func))
764               (t
765                (nnmail-process-unix-mail-format func artnum-func))))
766       (if exit-func (funcall exit-func))
767       (kill-buffer (current-buffer)))))
768
769 ;; Mail crossposts suggested by Brian Edmonds <edmonds@cs.ubc.ca>. 
770 (defun nnmail-article-group (func)
771   "Look at the headers and return an alist of groups that match.
772 FUNC will be called with the group name to determine the article number."
773   (let ((methods nnmail-split-methods)
774         (obuf (current-buffer))
775         (beg (point-min))
776         end group-art method)
777     (if (and (sequencep methods) (= (length methods) 1))
778         ;; If there is only just one group to put everything in, we
779         ;; just return a list with just this one method in.
780         (setq group-art
781               (list (cons (caar methods) (funcall func (caar methods)))))
782       ;; We do actual comparison.
783       (save-excursion
784         ;; Find headers.
785         (goto-char beg)
786         (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
787         (set-buffer nntp-server-buffer)
788         (erase-buffer)
789         ;; Copy the headers into the work buffer.
790         (insert-buffer-substring obuf beg end)
791         ;; Fold continuation lines.
792         (goto-char (point-min))
793         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
794           (replace-match " " t t))
795         ;; Allow washing.
796         (run-hooks 'nnmail-split-hook)
797         (if (and (symbolp nnmail-split-methods)
798                  (fboundp nnmail-split-methods))
799             ;; `nnmail-split-methods' is a function, so we just call 
800             ;; this function here and use the result.
801             (setq group-art
802                   (mapcar
803                    (lambda (group) (cons group (funcall func group)))
804                    (condition-case nil
805                        (or (funcall nnmail-split-methods)
806                            '("bogus"))
807                      (error
808                       (message 
809                        "Error in `nnmail-split-methods'; using `bogus' mail group")
810                       (sit-for 1)
811                       '("bogus")))))
812           ;; Go through the split methods to find a match.
813           (while (and methods (or nnmail-crosspost (not group-art)))
814             (goto-char (point-max))
815             (setq method (pop methods))
816             (if (or methods
817                     (not (equal "" (nth 1 method))))
818                 (when (and
819                        (condition-case () 
820                            (if (stringp (nth 1 method))
821                                (re-search-backward (cadr method) nil t)
822                              ;; Function to say whether this is a match.
823                              (funcall (nth 1 method) (car method)))
824                          (error nil))
825                        ;; Don't enter the article into the same 
826                        ;; group twice.
827                        (not (assoc (car method) group-art)))
828                   (push (cons (car method) (funcall func (car method))) 
829                         group-art))
830               ;; This is the final group, which is used as a 
831               ;; catch-all.
832               (unless group-art
833                 (setq group-art 
834                       (list (cons (car method) 
835                                   (funcall func (car method)))))))))
836         ;; See whether the split methods returned `junk'.
837         (if (equal group-art '(junk))
838             nil
839           (nreverse (delq 'junk group-art)))))))
840
841 (defun nnmail-insert-lines ()
842   "Insert how many lines there are in the body of the mail.
843 Return the number of characters in the body."
844   (let (lines chars)
845     (save-excursion
846       (goto-char (point-min))
847       (when (search-forward "\n\n" nil t) 
848         (setq chars (- (point-max) (point)))
849         (setq lines (count-lines (point) (point-max)))
850         (forward-char -1)
851         (save-excursion
852           (when (re-search-backward "^Lines: " nil t)
853             (delete-region (point) (progn (forward-line 1) (point)))))
854         (beginning-of-line)
855         (insert (format "Lines: %d\n" (max lines 0)))
856         chars))))
857
858 (defun nnmail-insert-xref (group-alist)
859   "Insert an Xref line based on the (group . article) alist."
860   (save-excursion
861     (goto-char (point-min))
862     (when (search-forward "\n\n" nil t) 
863       (forward-char -1)
864       (when (re-search-backward "^Xref: " nil t)
865         (delete-region (match-beginning 0) 
866                        (progn (forward-line 1) (point))))
867       (insert (format "Xref: %s" (system-name)))
868       (while group-alist
869         (insert (format " %s:%d" (caar group-alist) (cdar group-alist)))
870         (setq group-alist (cdr group-alist)))
871       (insert "\n"))))
872
873 ;; Written by byer@mv.us.adobe.com (Scott Byer).
874 (defun nnmail-make-complex-temp-name (prefix)
875   (let ((newname (make-temp-name prefix))
876         (newprefix prefix))
877     (while (file-exists-p newname)
878       (setq newprefix (concat newprefix "x"))
879       (setq newname (make-temp-name newprefix)))
880     newname))
881
882 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
883
884 (defun nnmail-split-fancy ()
885   "Fancy splitting method.
886 See the documentation for the variable `nnmail-split-fancy' for documentation."
887   (let ((syntab (syntax-table)))
888     (unwind-protect
889         (progn
890           (set-syntax-table nnmail-split-fancy-syntax-table)
891           (nnmail-split-it nnmail-split-fancy))
892       (set-syntax-table syntab))))
893
894 (defvar nnmail-split-cache nil)
895 ;; Alist of split expressions their equivalent regexps.
896
897 (defun nnmail-split-it (split)
898   ;; Return a list of groups matching SPLIT.
899   (cond
900    ((stringp split)
901     ;; A group.
902     (list split))
903    ((eq split 'junk)
904     ;; Junk this.
905     (list 'junk))
906    ((eq (car split) '&)
907     (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
908    ((eq (car split) '|)
909     (let (done)
910       (while (and (not done) (cdr split))
911         (setq split (cdr split)
912               done (nnmail-split-it (car split))))
913       done))
914    ((assq split nnmail-split-cache)
915     ;; A compiled match expression.
916     (goto-char (point-max))
917     (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
918         (nnmail-split-it (nth 2 split))))
919    (t
920     ;; An uncompiled match.
921     (let* ((field (nth 0 split))
922            (value (nth 1 split))
923            (regexp (concat "^\\(" 
924                            (if (symbolp field)
925                                (cdr (assq field nnmail-split-abbrev-alist))
926                              field)
927                            "\\):.*\\<\\("
928                            (if (symbolp value)
929                                (cdr (assq value nnmail-split-abbrev-alist))
930                              value)
931                            "\\)\\>")))
932       (setq nnmail-split-cache 
933             (cons (cons split regexp) nnmail-split-cache))
934       (goto-char (point-max))
935       (if (re-search-backward regexp nil t)
936           (nnmail-split-it (nth 2 split)))))))
937
938 ;; Get a list of spool files to read.
939 (defun nnmail-get-spool-files (&optional group)
940   (if (null nnmail-spool-file)
941       ;; No spool file whatsoever.
942       nil
943     (let* ((procmails 
944             ;; If procmail is used to get incoming mail, the files
945             ;; are stored in this directory.
946             (and (file-exists-p nnmail-procmail-directory)
947                  (or (eq nnmail-spool-file 'procmail)
948                      nnmail-use-procmail)
949                  (directory-files 
950                   nnmail-procmail-directory 
951                   t (concat (if group (concat "^" group) "")
952                             nnmail-procmail-suffix "$"))))
953            (p procmails)
954            (crash (when (and (file-exists-p nnmail-crash-box)
955                              (> (nnheader-file-size
956                                  (file-truename nnmail-crash-box)) 0))
957                     (list nnmail-crash-box))))
958       ;; Remove any directories that inadvertantly match the procmail
959       ;; suffix, which might happen if the suffix is "". 
960       (while p
961         (when (file-directory-p (car p))
962           (setq procmails (delete (car p) procmails)))
963         (setq p (cdr p)))
964       ;; Return the list of spools.
965       (append 
966        crash
967        (cond ((and group
968                    (or (eq nnmail-spool-file 'procmail)
969                        nnmail-use-procmail)
970                    procmails)
971               procmails)
972              ((and group
973                    (eq nnmail-spool-file 'procmail))
974               nil)
975              ((listp nnmail-spool-file)
976               (append nnmail-spool-file procmails))
977              ((stringp nnmail-spool-file)
978               (cons nnmail-spool-file procmails))
979              ((eq nnmail-spool-file 'pop)
980               (cons (format "po:%s" (user-login-name)) procmails))
981              (t
982               procmails))))))
983
984 ;; Activate a backend only if it isn't already activated. 
985 ;; If FORCE, re-read the active file even if the backend is 
986 ;; already activated.
987 (defun nnmail-activate (backend &optional force)
988   (let (file timestamp file-time)
989     (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
990             force
991             (and (setq file (condition-case ()
992                                 (symbol-value (intern (format "%s-active-file" 
993                                                               backend)))
994                               (error nil)))
995                  (setq file-time (nth 5 (file-attributes file)))
996                  (or (not
997                       (setq timestamp
998                             (condition-case ()
999                                 (symbol-value (intern
1000                                                (format "%s-active-timestamp" 
1001                                                        backend)))
1002                               (error 'none))))
1003                      (not (consp timestamp))
1004                      (equal timestamp '(0 0))
1005                      (> (nth 0 file-time) (nth 0 timestamp))
1006                      (and (= (nth 0 file-time) (nth 0 timestamp))
1007                           (> (nth 1 file-time) (nth 1 timestamp))))))
1008         (save-excursion
1009           (or (eq timestamp 'none)
1010               (set (intern (format "%s-active-timestamp" backend)) 
1011                    (current-time)))
1012           (funcall (intern (format "%s-request-list" backend)))
1013           (set (intern (format "%s-group-alist" backend)) 
1014                (nnmail-get-active))))
1015     t))
1016
1017 (defun nnmail-message-id ()
1018   (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1019
1020 ;;;
1021 ;;; nnmail duplicate handling
1022 ;;;
1023
1024 (defvar nnmail-cache-buffer nil)
1025
1026 (defun nnmail-cache-open ()
1027   (if (or (not nnmail-treat-duplicates)
1028           (and nnmail-cache-buffer
1029                (buffer-name nnmail-cache-buffer)))
1030       ()                                ; The buffer is open.
1031     (save-excursion
1032       (set-buffer 
1033        (setq nnmail-cache-buffer 
1034              (get-buffer-create " *nnmail message-id cache*")))
1035       (buffer-disable-undo (current-buffer))
1036       (and (file-exists-p nnmail-message-id-cache-file)
1037            (insert-file-contents nnmail-message-id-cache-file))
1038       (set-buffer-modified-p nil)
1039       (current-buffer))))
1040
1041 (defun nnmail-cache-close ()
1042   (when (and nnmail-cache-buffer
1043              nnmail-treat-duplicates
1044              (buffer-name nnmail-cache-buffer)
1045              (buffer-modified-p nnmail-cache-buffer))
1046     (save-excursion
1047       (set-buffer nnmail-cache-buffer)
1048       ;; Weed out the excess number of Message-IDs.
1049       (goto-char (point-max))
1050       (and (search-backward "\n" nil t nnmail-message-id-cache-length)
1051            (progn
1052              (beginning-of-line)
1053              (delete-region (point-min) (point))))
1054       ;; Save the buffer.
1055       (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1056           (make-directory (file-name-directory nnmail-message-id-cache-file)
1057                           t))
1058       (nnmail-write-region (point-min) (point-max)
1059                            nnmail-message-id-cache-file nil 'silent)
1060       (set-buffer-modified-p nil)
1061       (setq nnmail-cache-buffer nil)
1062       ;;(kill-buffer (current-buffer))
1063       )))
1064
1065 (defun nnmail-cache-insert (id)
1066   (when nnmail-treat-duplicates
1067     (save-excursion
1068       (set-buffer nnmail-cache-buffer)
1069       (goto-char (point-max))
1070       (insert id "\n"))))
1071
1072 (defun nnmail-cache-id-exists-p (id)
1073   (when nnmail-treat-duplicates
1074     (save-excursion
1075       (set-buffer nnmail-cache-buffer)
1076       (goto-char (point-max))
1077       (search-backward id nil t))))
1078
1079 (defun nnmail-check-duplication (message-id func artnum-func)
1080   ;; If this is a duplicate message, then we do not save it.
1081   (let* ((duplication (nnmail-cache-id-exists-p message-id))
1082          (action (when duplication
1083                    (cond
1084                     ((memq nnmail-treat-duplicates '(warn delete))
1085                      nnmail-treat-duplicates)
1086                     ((nnheader-functionp nnmail-treat-duplicates)
1087                      (funcall nnmail-treat-duplicates message-id))
1088                     (t
1089                      nnmail-treat-duplicates))))
1090          (group-art (nreverse (nnmail-article-group artnum-func))))
1091     (cond
1092      ((null group-art)
1093       (delete-region (point-min) (point-max)))
1094      ((not duplication)
1095       (nnmail-cache-insert message-id)
1096       (funcall func group-art))
1097      ((eq action 'delete)
1098       (delete-region (point-min) (point-max)))
1099      ((eq action 'warn)
1100       ;; We insert a warning.
1101       (let ((case-fold-search t)
1102             (newid (nnmail-message-id)))
1103         (goto-char (point-min))
1104         (when (re-search-forward "^message-id:" nil t)
1105           (beginning-of-line)
1106           (insert "Original-"))
1107         (beginning-of-line)
1108         (insert 
1109          "Message-ID: " newid "\n"
1110          "Gnus-Warning: This is a duplicate of message " message-id "\n")
1111         (nnmail-cache-insert newid)
1112         (funcall func group-art)))
1113      (t
1114       (funcall func group-art)))))
1115
1116 ;;; Get new mail.
1117
1118 (defun nnmail-get-value (&rest args)
1119   (let ((sym (intern (apply 'format args))))
1120     (when (boundp sym)
1121       (symbol-value sym))))
1122
1123 (defun nnmail-get-new-mail (method exit-func temp
1124                                    &optional group spool-func)
1125   "Read new incoming mail."
1126   (let* ((spools (nnmail-get-spool-files group))
1127          (group-in group)
1128          incoming incomings spool)
1129     (when (and (nnmail-get-value "%s-get-new-mail" method)
1130                nnmail-spool-file)
1131       ;; We first activate all the groups.
1132       (nnmail-activate method)
1133       ;; Allow the user to hook.
1134       (run-hooks 'nnmail-pre-get-new-mail-hook)
1135       ;; Open the message-id cache.
1136       (nnmail-cache-open)
1137       ;; The we go through all the existing spool files and split the
1138       ;; mail from each.
1139       (while spools
1140         (setq spool (pop spools))
1141         ;; We read each spool file if either the spool is a POP-mail
1142         ;; spool, or the file exists.  We can't check for the
1143         ;; existance of POPped mail.
1144         (when (or (string-match "^po:" spool)
1145                   (and (file-exists-p spool)
1146                        (> (nnheader-file-size (file-truename spool)) 0)))
1147           (nnheader-message 3 "%s: Reading incoming mail..." method)
1148           (when (and (nnmail-move-inbox spool)
1149                      (file-exists-p nnmail-crash-box))
1150             ;; There is new mail.  We first find out if all this mail
1151             ;; is supposed to go to some specific group.
1152             (setq group (nnmail-get-split-group spool group-in))
1153             ;; We split the mail
1154             (nnmail-split-incoming 
1155              nnmail-crash-box (intern (format "%s-save-mail" method)) 
1156              spool-func group (intern (format "%s-active-number" method)))
1157             ;; Check whether the inbox is to be moved to the special tmp dir. 
1158             (setq incoming
1159                   (nnmail-make-complex-temp-name 
1160                    (expand-file-name 
1161                     (if nnmail-tmp-directory
1162                         (concat 
1163                          (file-name-as-directory nnmail-tmp-directory)
1164                          (file-name-nondirectory (concat temp "Incoming")))
1165                       (concat temp "Incoming")))))
1166             (rename-file nnmail-crash-box incoming t)
1167             (push incoming incomings))))
1168       ;; If we did indeed read any incoming spools, we save all info. 
1169       (when incomings
1170         (nnmail-save-active 
1171          (nnmail-get-value "%s-group-alist" method)
1172          (nnmail-get-value "%s-active-file" method))
1173         (when exit-func
1174           (funcall exit-func))
1175         (run-hooks 'nnmail-read-incoming-hook)
1176         (nnheader-message 3 "%s: Reading incoming mail...done" method))
1177       ;; Close the message-id cache.
1178       (nnmail-cache-close)
1179       ;; Allow the user to hook.
1180       (run-hooks 'nnmail-post-get-new-mail-hook)
1181       ;; Delete all the temporary files.
1182       (while incomings
1183         (setq incoming (pop incomings))
1184         (and nnmail-delete-incoming
1185              (file-exists-p incoming)
1186              (file-writable-p incoming)
1187              (delete-file incoming))))))
1188
1189 (defun nnmail-expired-article-p (group time force &optional inhibit)
1190   "Say whether an article that is TIME old in GROUP should be expired."
1191   (if force
1192       t
1193     (let ((days (or (and nnmail-expiry-wait-function
1194                          (funcall nnmail-expiry-wait-function group))
1195                     nnmail-expiry-wait)))
1196       (cond ((or (eq days 'never)
1197                  (and (not force)
1198                       inhibit))
1199              ;; This isn't an expirable group.
1200              nil)
1201             ((eq days 'immediate)
1202              ;; We expire all articles on sight.
1203              t)
1204             ((equal time '(0 0))
1205              ;; This is an ange-ftp group, and we don't have any dates.
1206              nil)
1207             ((numberp days)
1208              (setq days (nnmail-days-to-time days))
1209              ;; Compare the time with the current time.
1210              (nnmail-time-less days (nnmail-time-since time)))))))
1211
1212 (defvar nnmail-read-passwd nil)
1213 (defun nnmail-read-passwd (prompt)
1214   (unless nnmail-read-passwd
1215     (if (load "passwd" t)
1216         (setq nnmail-read-passwd 'read-passwd)
1217       (autoload 'ange-ftp-read-passwd "ange-ftp")
1218       (setq nnmail-read-passwd 'ange-ftp-read-passwd)))
1219   (funcall nnmail-read-passwd prompt))
1220
1221 (defun nnmail-check-syntax ()
1222   "Check (and modify) the syntax of the message in the current buffer."
1223   (save-restriction
1224     (message-narrow-to-head)
1225     (let ((case-fold-search t))
1226       (unless (re-search-forward "^Message-Id:" nil t)
1227         (insert "Message-ID: " (nnmail-message-id) "\n")))))
1228
1229 (defun nnmail-write-region (start end filename &optional append visit lockname)
1230   "Do a `write-region', and then set the file modes."
1231   (write-region start end filename append visit lockname)
1232   (set-file-modes filename nnmail-default-file-modes))
1233
1234 ;;;
1235 ;;; Status functions
1236 ;;;
1237
1238 (defun nnmail-replace-status (name value)
1239   "Make status NAME and VALUE part of the current status line."
1240   (save-restriction
1241     (message-narrow-to-head)
1242     (let ((status (nnmail-decode-status)))
1243       (setq status (delq (member name status) status))
1244       (when value
1245         (push (cons name value) status))
1246       (message-remove-header "status")
1247       (goto-char (point-max))
1248       (insert "Status: " (nnmail-encode-status status) "\n"))))
1249
1250 (defun nnmail-decode-status ()
1251   "Return a status-value alist from STATUS."
1252   (goto-char (point-min))
1253   (when (re-search-forward "^Status: " nil t)
1254     (let (name value status)
1255       (save-restriction
1256         ;; Narrow to the status.
1257         (narrow-to-region
1258          (point)
1259          (if (re-search-forward "^[^ \t]" nil t)
1260              (1- (point))
1261            (point-max)))
1262         ;; Go through all elements and add them to the list.
1263         (goto-char (point-min))
1264         (while (re-search-forward "[^ \t=]+" nil t)
1265           (setq name (match-string 0))
1266           (if (not (= (following-char) ?=))
1267               ;; Implied "yes".
1268               (setq value "yes")
1269             (forward-char 1)
1270             (if (not (= (following-char) ?\"))
1271                 (if (not (looking-at "[^ \t]"))
1272                     ;; Implied "no".
1273                     (setq value "no")
1274                   ;; Unquoted value.
1275                   (setq value (match-string 0))
1276                   (goto-char (match-end 0)))
1277               ;; Quoted value.
1278               (setq value (read (current-buffer)))))
1279           (push (cons name value) status)))
1280       status)))
1281
1282 (defun nnmail-encode-status (status)
1283   "Return a status string from STATUS."
1284   (mapconcat
1285    (lambda (elem)
1286      (concat
1287       (car elem) "="
1288       (if (string-match "[ \t]" (cdr elem))
1289           (prin1-to-string (cdr elem))
1290         (cdr elem))))
1291    status " "))
1292
1293 (run-hooks 'nnmail-load-hook)
1294             
1295 (provide 'nnmail)
1296
1297 ;;; nnmail.el ends here