1 ;;; nnmail.el --- mail support functions for the Gnus mail backends
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news, mail
7 ;; This file is part of GNU Emacs.
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)
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.
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.
31 (eval-when-compile (require 'cl))
33 (defvar nnmail-split-methods
35 "*Incoming mail will be split according to this variable.
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:
41 (setq nnmail-split-methods
42 '((\"mail.4ad\" \"From:.*4ad\")
43 (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
44 (\"mail.misc\" \"\")))
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.
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.
57 The last element should always have \"\" as the regexp.
59 This variable can also have a function as its value.")
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.")
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.")
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
77 (defvar nnmail-default-file-modes 384
78 "Set the mode bits of all new mail files to this integer.")
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'.")
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'.
95 (setq nnmail-expiry-wait-function
97 (cond ((string-match \"private\" newsgroup) 31)
98 ((string-match \"junk\" newsgroup) 1)
99 ((string-match \"important\" newsgroup) 'never)
102 (defvar nnmail-spool-file
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.")
111 (defvar nnmail-crash-box "~/.gnus-crash-box"
112 "*File where Gnus will store mail while processing it.")
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.")
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.")
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]*\".")
127 (defvar nnmail-resplit-incoming nil
128 "*If non-nil, re-split incoming procmail sorted mail.")
130 (defvar nnmail-delete-file-function 'delete-file
131 "Function called to delete files in some mail backends.")
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.")
139 (defvar nnmail-movemail-program "movemail"
140 "*A command to be executed to move mail from the inbox.
141 The default is \"movemail\".")
143 (defvar nnmail-pop-password-required nil
144 "*Non-nil if a password is required when reading mail using POP.")
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.)
156 \(add-hook 'nnmail-read-incoming-hook
158 (start-process \"mailsend\" nil
159 \"/local/bin/mailsend\" \"read\" \"mbox\")))
161 If you have xwatch running, this will alert it that mail has been
164 If you use `display-time', you could use something like this:
166 \(add-hook 'nnmail-read-incoming-hook
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 \"\"))))")
173 (when (eq system-type 'windows-nt)
174 (add-hook 'nnmail-prepare-incoming-hook 'nnheader-ms-strip-cr))
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.")
181 (defvar nnmail-pre-get-new-mail-hook nil
182 "Hook called just before starting to handle new incoming mail.")
184 (defvar nnmail-post-get-new-mail-hook nil
185 "Hook called just after finishing handling new incoming mail.")
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.")
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.")
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.")
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'.
206 The format is this variable is SPLIT, where SPLIT can be one of
209 GROUP: Mail will be stored in GROUP (a string).
211 \(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
212 VALUE (a regexp), store the messages as specified by SPLIT.
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.
218 \(& SPLIT...): Process each SPLIT expression.
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.
224 FIELD and VALUE can also be lisp symbols, in that case they are expanded
225 as specified in `nnmail-split-abbrev-alist'.
229 \(setq nnmail-split-methods '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
234 '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
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\")
245 (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
246 ;; Unmatched mail goes to the catch all group.
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'.")
254 (defvar nnmail-delete-incoming nil
255 "*If non-nil, the mail backends will delete incoming files after splitting.")
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
262 (defvar nnmail-message-id-cache-file "~/.nnmail-cache"
263 "*The file name of the nnmail Message-ID cache.")
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.
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'.")
276 ;;; Internal variables.
278 (defvar nnmail-pop-password nil
279 "*Password to use when reading mail from a POP server, if required.")
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))
289 (defvar nnmail-prepare-save-mail-hook nil
290 "Hook called before saving mail.")
292 (defvar nnmail-moved-inboxes nil
293 "List of inboxes that have been moved.")
295 (defvar nnmail-internal-password nil)
299 (defconst nnmail-version "nnmail 1.0"
304 (defun nnmail-request-post (&optional server)
305 (mail-send-and-exit nil))
307 (defun nnmail-find-file (file)
308 "Insert FILE in server buffer safely."
309 (set-buffer nntp-server-buffer)
311 (let ((format-alist nil)
312 (after-insert-file-functions nil))
314 (progn (insert-file-contents file) t)
317 (defun nnmail-group-pathname (group dir &optional file)
318 "Make pathname for GROUP."
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 ?. ?/) "/")))
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))))
335 (and el (string-to-number el)))
337 (aref t1 2) (aref t1 1) (aref t1 0)
338 (aref d1 2) (aref d1 1) (aref d1 0)
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)))))
347 (defun nnmail-days-to-time (days)
348 "Convert DAYS into time."
349 (let* ((seconds (* 1.0 days 60 60 24))
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))))))
356 (defun nnmail-time-since (time)
357 "Return the time since TIME, which is either an internal time or a date."
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)))))
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)))
378 (setq inbox (file-name-nondirectory inbox)))
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)
388 (setq nnmail-internal-password nnmail-pop-password)
389 (when (and nnmail-pop-password-required (not nnmail-pop-password))
390 (setq nnmail-internal-password
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.
403 ((file-exists-p tofile)
404 ;; The crash box exists already.
407 (not (file-exists-p inbox)))
408 ;; There is no inbox.
410 ((and (not movemail) (not popmail))
411 ;; Try copying. If that fails (perhaps no space),
414 (copy-file inbox tofile nil)
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.
423 (write-region (point) (point) inbox)
429 (setq errors (generate-new-buffer " *nnmail loss*"))
430 (buffer-disable-undo errors)
431 (let ((default-directory "/"))
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
444 (set-file-modes inbox nnmail-default-file-modes))
445 (push inbox nnmail-moved-inboxes))
447 ;; There may be a warning about older revisions. We
449 (goto-char (point-min))
450 (if (search-forward "older revision" nil t)
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)))
464 (format "movemail: %s. Continue? "
466 (error "%s" (buffer-string)))
467 (setq tofile nil)))))))
470 (kill-buffer errors))
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."
477 ;; Go through all groups from the active list.
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))))
490 (defun nnmail-save-active (group-assoc file-name)
491 "Save GROUP-ASSOC in ACTIVE-FILE."
495 (set-buffer (get-buffer-create " *nnmail active*"))
496 (buffer-disable-undo (current-buffer))
499 (setq group (pop group-assoc))
500 (insert (format "%s %d %d y\n" (car group) (cdadr group)
502 (unless (file-exists-p (file-name-directory file-name))
503 (make-directory (file-name-directory file-name) t))
505 1 (point-max) (expand-file-name file-name) nil 'nomesg)
506 (kill-buffer (current-buffer))))))
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)
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)
525 (if (string-equal group procmail-group)
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)
536 (goto-char (point-min))
538 "
\f\n0, *unseen,+\n\\(\\*\\*\\* EOOH \\*\\*\\*\n\\)?" nil t)
539 (goto-char (match-end 0))
540 (delete-region (match-beginning 0) (match-end 0))
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.
548 (if (re-search-backward "^Message-ID:[ \t]*\\(<[^>]*>\\)" nil t)
549 (setq message-id (buffer-substring (match-beginning 1)
551 ;; There is no Message-ID here, so we create one.
553 (when (re-search-backward "^Message-ID:" nil t)
555 (insert "Original-")))
557 (insert "Message-ID: " (setq message-id (nnmail-message-id))
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
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))))
573 (if (or (= (+ (point) content-length) (point-max))
575 (goto-char (+ (point) content-length))
578 (goto-char (+ (point) content-length))
579 (setq do-search nil))
581 ;; Go to the beginning of the next article - or to the end
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 ^_
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))))
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))
602 (if (re-search-forward delim nil t)
603 (when (or (looking-at "[^\n :]+ *:")
605 (looking-at (concat ">" message-unix-mail-delimiter)))
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.
624 ;; Find the end of the head.
627 (if (search-forward "\n\n" nil t)
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]+:")
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))
640 (when (re-search-forward "^Message-ID:" nil t)
642 (insert "Original-")))
643 ;; There is no Message-ID here, so we create one.
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.
657 ;; Find the end of this article.
658 (goto-char (point-max))
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.
669 (setq skip (+ (point) content-length))
671 (cond ((or (= skip (point-max))
672 (= (1+ skip) (point-max)))
673 (setq end (point-max)))
677 (concat "[ \t]*\n\\(" delim "\\)"))
678 (setq end (match-beginning 1)))
682 ;; No Content-Length, so we find the beginning of the next
683 ;; article or the end of the buffer.
685 (or (nnmail-search-unix-mail-delim)
686 (goto-char (point-max))))
687 ;; Allow the backend to save the article.
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))))
696 (defun nnmail-process-mmdf-mail-format (func artnum-func)
697 (let ((delim "^\^A\^A\^A\^A$")
699 start message-id end)
700 (goto-char (point-min))
701 (if (not (and (re-search-forward delim nil t)
703 ;; Possibly wrong format?
704 (error "Error, unknown mail format! (Possibly corrupted.)")
705 ;; Carry on until the bitter end.
708 ;; Find the end of the head.
711 (if (search-forward "\n\n" nil t)
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]+:")
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.
725 (when (re-search-backward "^Message-ID:" nil t)
727 (insert "Original-")))
729 (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
730 ;; Find the end of this article.
731 (goto-char (point-max))
733 (if (re-search-forward delim nil t)
735 (goto-char (point-max)))
736 ;; Allow the backend to save the article.
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))))
746 (defun nnmail-split-incoming (incoming func &optional exit-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)
755 (not nnmail-resplit-incoming))
756 (list (list group ""))
757 nnmail-split-methods)))
759 ;; Insert the incoming file.
760 (set-buffer (get-buffer-create " *nnmail incoming*"))
761 (buffer-disable-undo (current-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))
776 (nnmail-process-unix-mail-format func artnum-func))))
777 (if exit-func (funcall exit-func))
778 (kill-buffer (current-buffer)))))
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))
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.
792 (list (cons (caar methods) (funcall func (caar methods)))))
793 ;; We do actual comparison.
797 (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
798 (set-buffer nntp-server-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))
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.
814 (lambda (group) (cons group (funcall func group)))
816 (or (funcall nnmail-split-methods)
820 "Error in `nnmail-split-methods'; using `bogus' mail group")
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))
828 (not (equal "" (nth 1 method))))
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)))
836 ;; Don't enter the article into the same
838 (not (assoc (car method) group-art)))
839 (push (cons (car method) (funcall func (car method)))
841 ;; This is the final group, which is used as a
845 (list (cons (car method)
846 (funcall func (car method)))))))))
847 ;; See whether the split methods returned `junk'.
848 (if (equal group-art '(junk))
850 (nreverse (delq 'junk group-art)))))))
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."
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)))
863 (when (re-search-backward "^Lines: " nil t)
864 (delete-region (point) (progn (forward-line 1) (point)))))
866 (insert (format "Lines: %d\n" (max lines 0)))
869 (defun nnmail-insert-xref (group-alist)
870 "Insert an Xref line based on the (group . article) alist."
872 (goto-char (point-min))
873 (when (search-forward "\n\n" nil t)
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)))
880 (insert (format " %s:%d" (caar group-alist) (cdar group-alist)))
881 (setq group-alist (cdr group-alist)))
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))
888 (while (file-exists-p newname)
889 (setq newprefix (concat newprefix "x"))
890 (setq newname (make-temp-name newprefix)))
893 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
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)))
901 (set-syntax-table nnmail-split-fancy-syntax-table)
902 (nnmail-split-it nnmail-split-fancy))
903 (set-syntax-table syntab))))
905 (defvar nnmail-split-cache nil)
906 ;; Alist of split expressions their equivalent regexps.
908 (defun nnmail-split-it (split)
909 ;; Return a list of groups matching SPLIT.
918 (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
921 (while (and (not done) (cdr split))
922 (setq split (cdr split)
923 done (nnmail-split-it (car split))))
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))))
931 ;; An uncompiled match.
932 (let* ((field (nth 0 split))
933 (value (nth 1 split))
934 (regexp (concat "^\\("
936 (cdr (assq field nnmail-split-abbrev-alist))
940 (cdr (assq value nnmail-split-abbrev-alist))
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)))))))
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.
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)
961 nnmail-procmail-directory
962 t (concat (if group (concat "^" group) "")
963 nnmail-procmail-suffix "$"))))
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 "".
972 (when (file-directory-p (car p))
973 (setq procmails (delete (car p) procmails)))
975 ;; Return the list of spools.
979 (or (eq nnmail-spool-file 'procmail)
984 (eq nnmail-spool-file 'procmail))
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))
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))))
1002 (and (setq file (condition-case ()
1003 (symbol-value (intern (format "%s-active-file"
1006 (setq file-time (nth 5 (file-attributes file)))
1010 (symbol-value (intern
1011 (format "%s-active-timestamp"
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))))))
1020 (or (eq timestamp 'none)
1021 (set (intern (format "%s-active-timestamp" backend))
1023 (funcall (intern (format "%s-request-list" backend)))
1024 (set (intern (format "%s-group-alist" backend))
1025 (nnmail-get-active))))
1028 (defun nnmail-message-id ()
1029 (concat "<" (message-unique-id) "@totally-fudged-out-message-id>"))
1032 ;;; nnmail duplicate handling
1035 (defvar nnmail-cache-buffer nil)
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.
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)
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))
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)
1064 (delete-region (point-min) (point))))
1066 (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
1067 (make-directory (file-name-directory nnmail-message-id-cache-file)
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))
1076 (defun nnmail-cache-insert (id)
1077 (when nnmail-treat-duplicates
1079 (set-buffer nnmail-cache-buffer)
1080 (goto-char (point-max))
1083 (defun nnmail-cache-id-exists-p (id)
1084 (when nnmail-treat-duplicates
1086 (set-buffer nnmail-cache-buffer)
1087 (goto-char (point-max))
1088 (search-backward id nil t))))
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
1095 ((memq nnmail-treat-duplicates '(warn delete))
1096 nnmail-treat-duplicates)
1097 ((nnheader-functionp nnmail-treat-duplicates)
1098 (funcall nnmail-treat-duplicates message-id))
1100 nnmail-treat-duplicates))))
1101 (group-art (nreverse (nnmail-article-group artnum-func))))
1104 (delete-region (point-min) (point-max)))
1106 (nnmail-cache-insert message-id)
1107 (funcall func group-art))
1108 ((eq action 'delete)
1109 (delete-region (point-min) (point-max)))
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)
1117 (insert "Original-"))
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)))
1125 (funcall func group-art)))))
1129 (defun nnmail-get-value (&rest args)
1130 (let ((sym (intern (apply 'format args))))
1132 (symbol-value sym))))
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))
1139 incoming incomings spool)
1140 (when (and (nnmail-get-value "%s-get-new-mail" method)
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.
1148 ;; The we go through all the existing spool files and split the
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.
1170 (nnmail-make-complex-temp-name
1172 (if nnmail-tmp-directory
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.
1182 (nnmail-get-value "%s-group-alist" method)
1183 (nnmail-get-value "%s-active-file" method))
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.
1194 (setq incoming (pop incomings))
1195 (and nnmail-delete-incoming
1196 (file-exists-p incoming)
1197 (file-writable-p incoming)
1198 (delete-file incoming))))))
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."
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)
1210 ;; This isn't an expirable group.
1212 ((eq days 'immediate)
1213 ;; We expire all articles on sight.
1215 ((equal time '(0 0))
1216 ;; This is an ange-ftp group, and we don't have any dates.
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)))))))
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))
1232 (defun nnmail-check-syntax ()
1233 "Check (and modify) the syntax of the message in the current buffer."
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")))))
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))
1246 ;;; Status functions
1249 (defun nnmail-replace-status (name value)
1250 "Make status NAME and VALUE part of the current status line."
1252 (message-narrow-to-head)
1253 (let ((status (nnmail-decode-status)))
1254 (setq status (delq (member name status) status))
1256 (push (cons name value) status))
1257 (message-remove-header "status")
1258 (goto-char (point-max))
1259 (insert "Status: " (nnmail-encode-status status) "\n"))))
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)
1267 ;; Narrow to the status.
1270 (if (re-search-forward "^[^ \t]" nil t)
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) ?=))
1281 (if (not (= (following-char) ?\"))
1282 (if (not (looking-at "[^ \t]"))
1286 (setq value (match-string 0))
1287 (goto-char (match-end 0)))
1289 (setq value (read (current-buffer)))))
1290 (push (cons name value) status)))
1293 (defun nnmail-encode-status (status)
1294 "Return a status string from STATUS."
1299 (if (string-match "[ \t]" (cdr elem))
1300 (prin1-to-string (cdr elem))
1304 (run-hooks 'nnmail-load-hook)
1308 ;;; nnmail.el ends here