*** empty log message ***
[gnus] / lisp / nnmail.el
index 9862eef..1869372 100644 (file)
@@ -1,8 +1,7 @@
-;;; nnmail.el --- mail mbox access for Gnus
+;;; nnmail.el --- mail support functions for the Gnus mail backends
 ;; Copyright (C) 1995 Free Software Foundation, Inc.
 
-;; Author: Lars Ingebrigtsen <larsi@ifi.uio.no>
-;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
+;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
 ;; Keywords: news, mail
 
 ;; This file is part of GNU Emacs.
 
 (require 'nnheader)
 (require 'rmail)
+(require 'timezone)
+(require 'sendmail)
+(eval-when-compile (require 'cl))
 
 (defvar nnmail-split-methods
   '(("mail.misc" ""))
-  "nnmail will split incoming mail into the groups detailed in this variable.")
+  "*Incoming mail will be split according to this variable.
+
+If you'd like, for instance, one mail group for mail from the
+\"4ad-l\" mailing list, one group for junk mail and one for everything
+else, you could do something like this:
+
+ (setq nnmail-split-methods
+       '((\"mail.4ad\" \"From:.*4ad\")
+         (\"mail.junk\" \"From:.*Lars\\\\|Subject:.*buy\")
+         (\"mail.misc\" \"\")))
+
+As you can see, this variable is a list of lists, where the first
+element in each \"rule\" is the name of the group (which, by the way,
+does not have to be called anything beginning with \"mail\",
+\"yonka.zow\" is a fine, fine name), and the second is a regexp that
+nnmail will try to match on the header to find a fit.
+
+The second element can also be a function.  In that case, it will be
+called narrowed to the headers with the first element of the rule as
+the argument.  It should return a non-nil value if it thinks that the
+mail belongs in that group.
+
+The last element should always have \"\" as the regexp.
+
+This variable can also have a function as its value.")
 
 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
 (defvar nnmail-crosspost t
-  "If non-nil, do crossposting if several split methods match the mail.
+  "*If non-nil, do crossposting if several split methods match the mail.
 If nil, the first match found will be used.")
 
-(defvar nnmail-mbox-file (expand-file-name "~/mbox")
-  "The name of the mail box file in the users home directory.")
+;; Added by gord@enci.ucalgary.ca (Gordon Matzigkeit).
+(defvar nnmail-keep-last-article nil
+  "*If non-nil, nnmail will never delete the last expired article in a
+directory.  You may need to set this variable if other programs are putting
+new mail into folder numbers that Gnus has marked as expired.")
 
-(defvar nnmail-active-file (expand-file-name "~/.mbox-active")
-  "The name of the active file for the mail box.")
+(defvar nnmail-use-long-file-names nil
+  "*If non-nil the mail backends will use long file and directory names.
+If nil, groups like \"mail.misc\" will end up in directories like
+\"mail/misc/\".")
 
 (defvar nnmail-expiry-wait 7
-  "Articles that are older than `nnmail-expiry-wait' days will be expired.")
+  "*Articles that are older than `nnmail-expiry-wait' days will be expired.")
 
-;; Quote fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
 (defvar nnmail-expiry-wait-function nil
-  "Variable that holds funtion to specify how old articles should be before they are expired.
+  "*Variable that holds function to specify how old articles should be before they are expired.
   The function will be called with the name of the group that the
 expiry is to be performed in, and it should return an integer that
 says how many days an article can be stored before it is considered
@@ -57,18 +87,48 @@ says how many days an article can be stored before it is considered
 Eg.:
 
 (setq nnmail-expiry-wait-function
-  (function
-    (lambda (newsgroup)
-      (cond ((string-match \"private\" newsgroup) 31)
-            ((string-match \"junk\" newsgroup) 1)
-           (t 7)))))")
+      (lambda (newsgroup)
+        (cond ((string-match \"private\" newsgroup) 31)
+              ((string-match \"junk\" newsgroup) 1)
+             (t 7))))")
 
 (defvar nnmail-spool-file 
   (or (getenv "MAIL")
-      (concat "/usr/spool/mail/" (user-login-name))))
+      (concat "/usr/spool/mail/" (user-login-name)))
+  "Where the mail backends will look for incoming mail.
+This variable is \"/usr/spool/mail/$user\" by default.
+If this variable is nil, no mail backends will read incoming mail.
+If this variable is a list, all files mentioned in this list will be
+used as incoming mailboxes.")
+
+(defvar nnmail-crash-box "~/.gnus-crash-box"
+  "*File where Gnus will store mail while processing it.")
+
+(defvar nnmail-use-procmail nil
+  "*If non-nil, the mail backends will look in `nnmail-procmail-directory' for spool files.
+The file(s) in `nnmail-spool-file' will also be read.")
+
+(defvar nnmail-procmail-directory "~/incoming/"
+  "*When using procmail (and the like), incoming mail is put in this directory.
+The Gnus mail backends will read the mail from this directory.")
+
+(defvar nnmail-procmail-suffix ".spool"
+  "*Suffix of files created by procmail (and the like).
+This variable might be a suffix-regexp to match the suffixes of
+several files - eg. \".spool[0-9]*\".")
+
+(defvar nnmail-resplit-incoming nil
+  "*If non-nil, re-split incoming procmail sorted mail.")
+
+(defvar nnmail-delete-file-function 'delete-file
+  "Function called to delete files in some mail backends.")
+
+(defvar nnmail-movemail-program "movemail"
+  "*A command to be executed to move mail from the inbox.
+The default is \"movemail\".")
 
 (defvar nnmail-read-incoming-hook nil
-  "Hook that will be run after the incoming mail has been transferred.
+  "*Hook that will be run after the incoming mail has been transferred.
 The incoming mail is moved from `nnmail-spool-file' (which normally is
 something like \"/usr/spool/mail/$user\") to the user's home
 directory. This hook is called after the incoming mail box has been
@@ -77,557 +137,873 @@ running (\"xwatch\", etc.)
 
 Eg.
 
-(add-hook 'nnmail-read-incoming-hook 
-         (function
+\(add-hook 'nnmail-read-incoming-hook 
           (lambda () 
             (start-process \"mailsend\" nil 
-                           \"/local/bin/mailsend\" \"read\" \"mbox\"))))")
+                           \"/local/bin/mailsend\" \"read\" \"mbox\")))
+
+If you have xwatch running, this will alert it that mail has been
+read.  
+
+If you use `display-time', you could use something like this:
+
+\(add-hook 'nnmail-read-incoming-hook
+         (lambda ()
+           ;; Update the displayed time, since that will clear out
+           ;; the flag that says you have mail.
+           (if (eq (process-status \"display-time\") 'run)
+               (display-time-filter display-time-process \"\"))))") 
 
 ;; Suggested by Erik Selberg <speed@cs.washington.edu>.
 (defvar nnmail-prepare-incoming-hook nil
-  "Hook called before treating incoming mail.
+  "*Hook called before treating incoming mail.
 The hook is run in a buffer with all the new, incoming mail.")
 
+;; Suggested by Mejia Pablo J <pjm9806@usl.edu>.
+(defvar nnmail-tmp-directory nil
+  "*If non-nil, use this directory for temporary storage when reading incoming mail.")
+
 (defvar nnmail-large-newsgroup 50
   "*The number of the articles which indicates a large newsgroup.
 If the number of the articles is greater than the value, verbose
 messages will be shown to indicate the current status.")
 
-\f
+(defvar nnmail-split-fancy "mail.misc"
+  "*Incoming mail can be split according to this fancy variable.
+To enable this, set `nnmail-split-methods' to `nnmail-split-fancy'.
 
-(defconst nnmail-version "nnmail 0.1"
-  "nnmail version.")
+The format is this variable is SPLIT, where SPLIT can be one of
+the following:
 
-(defvar nnmail-current-group nil
-  "Current nnmail news group directory.")
+GROUP: Mail will be stored in GROUP (a string).
 
-(defconst nnmail-mbox-buffer "*nnmail mbox buffer*")
+\(FIELD VALUE SPLIT): If the message field FIELD (a regexp) contains
+  VALUE (a regexp), store the messages as specified by SPLIT.
 
-(defvar nnmail-active-alist nil)
+\(| SPLIT...): Process each SPLIT expression until one of them matches.
+  A SPLIT expression is said to match if it will cause the mail
+  message to be stored in one or more groups.  
 
-(defvar nnmail-status-string "")
+\(& SPLIT...): Process each SPLIT expression.
 
-;;; Interface functions
+FIELD must match a complete field name.  VALUE must match a complete
+word according to the fundamental mode syntax table.  You can use .*
+in the regexps to match partial field names or words.
 
-(defun nnmail-retrieve-headers (sequence &optional newsgroup server)
-  "Retrieve the headers for the articles in SEQUENCE.
-Newsgroup must be selected before calling this function."
-  (save-excursion
-    (set-buffer nntp-server-buffer)
-    (erase-buffer)
-    (let ((file nil)
-         (number (length sequence))
-         (count 0)
-         beg article art-string start stop)
-      (nnmail-possibly-change-newsgroup newsgroup)
-      (while sequence
-       (setq article (car sequence))
-       (setq art-string (nnmail-article-string article))
-       (set-buffer nnmail-mbox-buffer)
-       (if (or (search-forward art-string nil t)
-               (progn (goto-char 1)
-                      (search-forward art-string nil t)))
-           (progn
-             (setq start 
-                   (save-excursion
-                     (re-search-backward 
-                      (concat "^" rmail-unix-mail-delimiter) nil t)
-                     (point)))
-             (search-forward "\n\n" nil t)
-             (setq stop (1- (point)))
-             (set-buffer nntp-server-buffer)
-             (insert (format "221 %d Article retrieved.\n" article))
-             (setq beg (point))
-             (insert-buffer-substring nnmail-mbox-buffer start stop)
-             (goto-char (point-max))
-             (insert ".\n")))
-       (setq sequence (cdr sequence))
-       (setq count (1+ count))
-       (and (numberp nnmail-large-newsgroup)
-            (> number nnmail-large-newsgroup)
-            (zerop (% count 20))
-            (message "NNMAIL: Receiving headers... %d%%"
-                     (/ (* count 100) number))))
-
-      (and (numberp nnmail-large-newsgroup)
-          (> number nnmail-large-newsgroup)
-          (message "NNMAIL: Receiving headers... done"))
-
-      ;; Fold continuation lines.
-      (goto-char 1)
-      (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
-       (replace-match " " t t))
-      'headers)))
-
-(defun nnmail-open-server (host &optional service)
-  "Open mbox backend."
-  (setq nnmail-status-string "")
-  (nnmail-open-server-internal host service))
-
-(defun nnmail-close-server (&optional server)
-  "Close news server."
-  (nnmail-close-server-internal))
-
-(fset 'nnmail-request-quit (symbol-function 'nnmail-close-server))
-
-(defun nnmail-server-opened (&optional server)
-  "Return server process status, T or NIL.
-If the stream is opened, return T, otherwise return NIL."
-  (and nntp-server-buffer
-       (get-buffer nntp-server-buffer)))
-
-(defun nnmail-status-message ()
-  "Return server status response as string."
-  nnmail-status-string)
-
-(defun nnmail-request-article (article &optional newsgroup server buffer)
-  "Select ARTICLE by number."
-  (nnmail-possibly-change-newsgroup newsgroup)
-  (if (stringp article)
-      nil
-    (save-excursion
-      (set-buffer nnmail-mbox-buffer)
-      (goto-char 1)
-      (if (search-forward (nnmail-article-string article) nil t)
-         (let (start stop)
-           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
-           (setq start (point))
-           (forward-line 1)
-           (or (and (re-search-forward 
-                     (concat "^" rmail-unix-mail-delimiter) nil t)
-                    (forward-line -1))
-               (goto-char (point-max)))
-           (setq stop (point))
-           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
-             (set-buffer nntp-server-buffer)
-             (erase-buffer)
-             (insert-buffer-substring nnmail-mbox-buffer start stop)
-             t))))))
-
-(defun nnmail-request-group (group &optional server dont-check)
-  "Select news GROUP."
-  (if (nnmail-possibly-change-newsgroup group)
-      (if dont-check
-         t
-       (nnmail-get-new-mail)
-       (save-excursion
-         (set-buffer nntp-server-buffer)
-         (erase-buffer)
-         (let ((active (assoc group nnmail-active-alist)))
-           (insert (format "211 %d %d %d %s\n" 
-                           (1+ (- (cdr (car (cdr active)))
-                                  (car (car (cdr active)))))
-                           (car (car (cdr active)))
-                           (cdr (car (cdr active)))
-                           (car active))))
-         t))))
-
-(defun nnmail-request-list (&optional server)
-  "List active newsgoups."
-  (nnmail-find-file nnmail-active-file))
-
-(defun nnmail-request-list-newsgroups (&optional server)
-  "List newsgroups (defined in NNTP2)."
-  (setq nnmail-status-string "NNMAIL: LIST NEWSGROUPS is not implemented.")
-  nil)
+FIELD and VALUE can also be lisp symbols, in that case they are expanded
+as specified in `nnmail-split-abbrev-alist'.
 
-(defun nnmail-request-post (&optional server)
-  "Post a new news in current buffer."
-  (mail-send-and-exit nil))
+Example:
 
-(defun nnmail-request-post-buffer (method header article-buffer group info)
-  (let ((method-address (nth 1 (nth 4 info)))
-       from subject date to reply-to message-of
-       references message-id sender follow-to)
-    (setq method-address
-         (if (and (stringp method-address) 
-                  (string= method-address ""))
-             nil method-address))
-    (save-excursion
-      (set-buffer (get-buffer-create "*mail*"))
-      (mail-mode)
-      (local-set-key "\C-c\C-c" 'gnus-mail-send-and-exit)
-      (local-set-key "\C-c\C-y" 'gnus-mail-yank-original)
-      (if (and (buffer-modified-p)
-              (> (buffer-size) 0)
-              (not (y-or-n-p "Unsent mail being composed; erase it? ")))
-         ()
-       (erase-buffer)
-       (if (eq method 'post)
-           (mail-setup method-address nil nil nil nil nil)
-         (save-excursion
-           (set-buffer article-buffer)
-           (goto-char (point-min))
-           (narrow-to-region (point-min)
-                             (progn (search-forward "\n\n") (point)))
-           (set-text-properties (point-min) (point-max) nil)
-           (if (and (boundp 'gnus-followup-to-function)
-                    gnus-followup-to-function)
-               (setq follow-to (funcall gnus-followup-to-function group)))
-           (setq from (header-from header))
-           (setq date (header-date header))
-           (and from
-                (let ((stop-pos 
-                       (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
-                  (setq message-of
-                        (concat (if stop-pos (substring from 0 stop-pos) from)
-                                "'s message of " date))))
-           (setq sender (mail-fetch-field "sender"))
-           (setq subject (header-subject header))
-           (or (string-match "^[Rr][Ee]:" subject)
-               (setq subject (concat "Re: " subject)))
-           (setq reply-to (mail-fetch-field "reply-to"))
-           (setq references (header-references header))
-           (setq message-id (header-id header))
-           (widen))
-         (setq news-reply-yank-from from)
-         (setq news-reply-yank-message-id message-id)
-         (mail-setup (or follow-to method-address sender reply-to from)
-                     subject message-of nil article-buffer nil)
-         ;; Fold long references line to follow RFC1036.
-         (mail-position-on-field "References")
-         (let ((begin (- (point) (length "References: ")))
-               (fill-column 78)
-               (fill-prefix "\t"))
-           (if references (insert references))
-           (if (and references message-id) (insert " "))
-           (if message-id (insert message-id))
-           ;; The region must end with a newline to fill the region
-           ;; without inserting extra newline.
-           (fill-region-as-paragraph begin (1+ (point))))
-         ))
-      (current-buffer))))
+\(setq nnmail-split-methods 'nnmail-split-fancy
+      nnmail-split-fancy
+      ;; Messages from the mailer deamon are not crossposted to any of
+      ;; the ordinary groups.  Warnings are put in a separate group
+      ;; from real errors.
+      '(| (\"from\" mail (| (\"subject\" \"warn.*\" \"mail.warning\")
+                         \"mail.misc\"))
+         ;; Non-error messages are crossposted to all relevant
+         ;; groups, but we don't crosspost between the group for the
+         ;; (ding) list and the group for other (ding) related mail.
+         (& (| (any \"ding@ifi\\\\.uio\\\\.no\" \"ding.list\")
+               (\"subject\" \"ding\" \"ding.misc\"))
+            ;; Other mailing lists...
+            (any \"procmail@informatik\\\\.rwth-aachen\\\\.de\" \"procmail.list\")
+            (any \"SmartList@informatik\\\\.rwth-aachen\\\\.de\" \"SmartList.list\")
+            ;; People...
+            (any \"larsi@ifi\\\\.uio\\\\.no\" \"people.Lars Magne Ingebrigtsen\"))
+         ;; Unmatched mail goes to the catch all group.
+         \"misc.misc\"))")
 
-(defun nnmail-request-expire-articles (articles newsgroup &optional server force)
-  "Expire all articles in the ARTICLES list in group GROUP.
-The list of unexpired articles will be returned (ie. all articles that
-were too fresh to be expired).
-If FORCE is non-nil, the ARTICLES will be deleted without looking at
-the date."
-  (nnmail-possibly-change-newsgroup newsgroup)
-  (let* ((days (or (and nnmail-expiry-wait-function
-                       (funcall nnmail-expiry-wait-function newsgroup))
-                  nnmail-expiry-wait))
-        (cur-time (current-time))
-        (day-sec (* 24 60 60 days))
-        (day-time (list nil nil))
-        mod-time article rest)
-    (setcar day-time (/ day-sec 65536))
-    (setcar (cdr day-time) (- day-sec (* (car day-time) 65536)))
-    (if (< (car (cdr cur-time)) (car (cdr day-time)))
-       (progn
-         (setcar day-time (+ 1 (- (car cur-time) (car day-time))))
-         (setcar (cdr day-time) (- (+ 65536 (car (cdr cur-time)))
-                                   (car (cdr day-time)))))
-      (setcar day-time (- (car cur-time) (car day-time)))
-      (setcar (cdr day-time) (- (car (cdr cur-time)) (car (cdr day-time)))))
-    (save-excursion 
-      (set-buffer nnmail-mbox-buffer)
-      (while articles
-       (goto-char 1)
-       (if (and (search-forward (nnmail-article-string (car articles)) nil t)
-                (or force
-                    (setq mod-time (read (current-buffer)))
-                    (or (< (car mod-time) (car day-time))
-                        (and (= (car mod-time) (car day-time))
-                             (< (car (cdr mod-time)) (car (cdr day-time)))))))
-           (progn
-             (message "Deleting: %s" article)
-             (nnmail-delete-mail))
-         (setq rest (cons (car articles) rest)))
-       (setq articles (cdr articles)))
-      (save-buffer)
-      rest)))
-
-(defun nnmail-request-move-article (article group server accept-form)
-  (let ((buf (get-buffer-create " *nnmail move*"))
-       result)
-    (and 
-     (nnmail-request-article article group server)
-     (save-excursion
-       (set-buffer buf)
-       (insert-buffer-substring nntp-server-buffer)
-       (goto-char (point-min))
-       (if (re-search-forward 
-           "^X-Gnus-Newsgroup:" 
-           (save-excursion (search-forward "\n\n" nil t) (point)) t)
-          (delete-region (progn (beginning-of-line) (point))
-                         (progn (forward-line 1) (point))))
-       (setq result (eval accept-form))
-       (kill-buffer (current-buffer))
-       result)
-     (save-excursion
-       (set-buffer nnmail-mbox-buffer)
-       (goto-char 1)
-       (if (search-forward (nnmail-article-string article) nil t)
-          (nnmail-delete-mail))
-       (save-buffer)))
-    result))
-
-(defun nnmail-request-accept-article (group)
-  (let ((buf (current-buffer))
-       result beg)
-    (and 
-     (nnmail-get-active)
-     (save-excursion
-       (set-buffer nnmail-mbox-buffer)
-       (setq beg (goto-char (point-max)))
-       (insert-buffer-substring buf)
-       (goto-char beg)
-       (if (stringp group)
-          (progn
-            (search-forward "\n\n" nil t)
-            (forward-line -1)
-            (save-excursion
-              (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
-                (delete-region
-                 (point)
-                 (progn
-                   (forward-line 1)
-                   (point)))))
-            (setq result (nnmail-insert-newsgroup-line group beg (point))))
-        (setq result (nnmail-choose-mail beg (point-max))))
-       (save-buffer)
-       result)
-     (nnmail-save-active))
-    result))
+(defvar nnmail-split-abbrev-alist
+  '((any . "from\\|to\\|cc\\|sender\\|apparently-to")
+    (mail . "mailer-daemon\\|postmaster"))
+  "*Alist of abbreviations allowed in `nnmail-split-fancy'.")
+
+(defvar nnmail-delete-incoming nil
+  "*If non-nil, the mail backends will delete incoming files after splitting.")
+
+(defvar nnmail-message-id-cache-length 1000
+  "*The approximate number of Message-IDs nnmail will keep in its cache.
+If this variable is nil, no checking on duplicate messages will be
+perfomed.")
+
+(defvar nnmail-message-id-cache-file "~/.nnmail-cache"
+  "*The file name of the nnmail Message-ID cache.")
+
+(defvar nnmail-delete-duplicates nil
+  "*If non-nil, nnmail will delete any duplicate mails it sees.")
 
 \f
-;;; Low-Level Interface
-
-(defun nnmail-delete-mail ()
-  (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
-  (delete-region 
-   (point)
-   (progn
-     (forward-line 1)
-     (or (and (re-search-forward 
-              (concat "^" rmail-unix-mail-delimiter) nil t)
-             (forward-line -1)
-             (point))
-        (point-max)))))
-
-(defun nnmail-open-server-internal (host &optional service)
-  "Open connection to news server on HOST by SERVICE (default is nntp)."
-  (save-excursion
-    (if (not (string-equal host (system-name)))
-       (error "NNMAIL: cannot talk to %s." host))
-    ;; Initialize communication buffer.
-    (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
-    (set-buffer nntp-server-buffer)
-    (buffer-disable-undo (current-buffer))
-    (erase-buffer)
-    (kill-all-local-variables)
-    (setq case-fold-search t)          ;Should ignore case.
-    t))
 
-(defun nnmail-close-server-internal ()
-  "Close connection to news server."
-  nil)
+(defconst nnmail-version "nnmail 1.0"
+  "nnmail version.")
+
+\f
+
+(defun nnmail-request-post (&optional server)
+  (mail-send-and-exit nil))
 
 (defun nnmail-find-file (file)
   "Insert FILE in server buffer safely."
-  (save-excursion
-    (set-buffer nntp-server-buffer)
-    (erase-buffer)
-    (condition-case ()
-       (progn (insert-file-contents file) t)
-      (file-error nil))))
-
-(defun nnmail-possibly-change-newsgroup (newsgroup)
-  (if (not (get-buffer nnmail-mbox-buffer))
-      (save-excursion
-       (set-buffer (setq nnmail-mbox-buffer 
-                         (find-file-noselect nnmail-mbox-file)))
-       (buffer-disable-undo (current-buffer))))
-  (if (not nnmail-active-alist)
-      (nnmail-get-active))
-  (if newsgroup
-      (if (assoc newsgroup nnmail-active-alist)
-         (setq nnmail-current-group newsgroup))))
-
-;; Most of this function was taken from rmail.el
-(defun nnmail-move-inbox ()
-  (let ((inbox (expand-file-name nnmail-spool-file))
-       tofile errors)
-    (setq tofile (make-temp-name
-                 (expand-file-name (concat nnmail-mbox-file "-Incoming"))))
-    (unwind-protect
-       (save-excursion
-         (setq errors (generate-new-buffer " *nnmail loss*"))
-         (buffer-disable-undo errors)
-         (call-process
-          (expand-file-name "movemail" exec-directory)
-          nil errors nil inbox tofile)
-         (if (not (buffer-modified-p errors))
-             ;; No output => movemail won
-             nil
-           (set-buffer errors)
-           (subst-char-in-region (point-min) (point-max) ?\n ?\  )
-           (goto-char (point-max))
-           (skip-chars-backward " \t")
-           (delete-region (point) (point-max))
-           (goto-char (point-min))
-           (if (looking-at "movemail: ")
-               (delete-region (point-min) (match-end 0)))
-           (error (concat "movemail: "
-                          (buffer-substring (point-min)
-                                            (point-max)))))))
+  (set-buffer nntp-server-buffer)
+  (erase-buffer)
+  (condition-case ()
+      (progn (insert-file-contents file) t)
+    (file-error nil)))
+
+(defun nnmail-group-pathname (group mail-dir)
+  "Make pathname for GROUP."
+  (let ((mail-dir (file-name-as-directory (expand-file-name mail-dir))))
+    ;; If this directory exists, we use it directly.
+    (if (or nnmail-use-long-file-names 
+           (file-directory-p (concat mail-dir group)))
+       (concat mail-dir group "/")
+      ;; If not, we translate dots into slashes.
+      (concat mail-dir (nnmail-replace-chars-in-string group ?. ?/) "/"))))
+
+(defun nnmail-replace-chars-in-string (string from to)
+  "Replace characters in STRING from FROM to TO."
+  (let ((string (substring string 0))  ;Copy string.
+       (len (length string))
+       (idx 0))
+    ;; Replace all occurrences of FROM with TO.
+    (while (< idx len)
+      (if (= (aref string idx) from)
+         (aset string idx to))
+      (setq idx (1+ idx)))
+    string))
+
+(defun nnmail-days-between (date1 date2)
+  ;; Return the number of days between date1 and date2.
+  (let ((d1 (mapcar (lambda (s) (and s (string-to-int s)) )
+                   (timezone-parse-date date1)))
+       (d2 (mapcar (lambda (s) (and s (string-to-int s)) )
+                   (timezone-parse-date date2))))
+    (- (timezone-absolute-from-gregorian 
+       (nth 1 d1) (nth 2 d1) (car d1))
+       (timezone-absolute-from-gregorian 
+       (nth 1 d2) (nth 2 d2) (car d2)))))
+
+;; Function taken from rmail.el.
+(defun nnmail-move-inbox (inbox)
+  "Move INBOX to `nnmail-crash-box'."
+  (let ((inbox (file-truename
+               (expand-file-name (substitute-in-file-name inbox))))
+       (tofile (file-truename (expand-file-name 
+                               (substitute-in-file-name nnmail-crash-box))))
+       movemail popmail errors)
+    ;; If getting from mail spool directory,
+    ;; use movemail to move rather than just renaming,
+    ;; so as to interlock with the mailer.
+    (unless (setq popmail (string-match "^po:" (file-name-nondirectory inbox)))
+      (setq movemail t))
+    (when popmail 
+      (setq inbox (file-name-nondirectory inbox)))
+    (when (and movemail
+              ;; On some systems, /usr/spool/mail/foo is a directory
+              ;; and the actual inbox is /usr/spool/mail/foo/foo.
+              (file-directory-p inbox))
+      (setq inbox (expand-file-name (user-login-name) inbox)))
+    (if popmail
+       (message "Getting mail from post office ...")
+      (when (or (and (file-exists-p tofile)
+                    (/= 0 (nth 7 (file-attributes tofile))))
+               (and (file-exists-p inbox)
+                    (/= 0 (nth 7 (file-attributes inbox)))))
+       (message "Getting mail from %s..." inbox)))
+    ;; Set TOFILE if have not already done so, and
+    ;; rename or copy the file INBOX to TOFILE if and as appropriate.
+    (cond ((file-exists-p tofile)
+          ;; The crash box exists already.
+          t)
+         ((and (not popmail)
+               (not (file-exists-p inbox)))
+          ;; There is no inbox.
+          (setq tofile nil))
+         ((and (not movemail) (not popmail))
+          ;; Try copying.  If that fails (perhaps no space),
+          ;; rename instead.
+          (condition-case nil
+              (copy-file inbox tofile nil)
+            (error
+             ;; Third arg is t so we can replace existing file TOFILE.
+             (rename-file inbox tofile t)))
+          ;; Make the real inbox file empty.
+          ;; Leaving it deleted could cause lossage
+          ;; because mailers often won't create the file.
+          (condition-case ()
+              (write-region (point) (point) inbox)
+            (file-error nil)))
+         (t
+          (unwind-protect
+              (save-excursion
+                (setq errors (generate-new-buffer " *nnmail loss*"))
+                (buffer-disable-undo errors)
+                (let ((default-directory "/"))
+                  (call-process
+                   (expand-file-name nnmail-movemail-program exec-directory)
+                   nil errors nil inbox tofile))
+                (if (not (buffer-modified-p errors))
+                    ;; No output => movemail won
+                    nil
+                  (set-buffer errors)
+                  (subst-char-in-region (point-min) (point-max) ?\n ?\  )
+                  (goto-char (point-max))
+                  (skip-chars-backward " \t")
+                  (delete-region (point) (point-max))
+                  (goto-char (point-min))
+                  (if (looking-at "movemail: ")
+                      (delete-region (point-min) (match-end 0)))
+                  (beep t)
+                  (message (concat "movemail: "
+                                   (buffer-substring (point-min)
+                                                     (point-max))))
+                  (sit-for 3)
+                  (setq tofile nil))))))
+    (and errors
+        (buffer-name errors)
+        (kill-buffer errors))
     tofile))
 
-(defun nnmail-article-string (article)
-  (concat "\nX-Gnus-Newsgroup: " nnmail-current-group ":" 
-         (int-to-string article) " ("))
-
-(defun nnmail-choose-mail (beg end)
-  (let (result)
+(defun nnmail-get-active ()
+  "Returns an assoc of group names and active ranges.
+nn*-request-list should have been called before calling this function."
+  (let (group-assoc)
+    ;; Go through all groups from the active list.
     (save-excursion
-      (goto-char end)
-      (let ((methods nnmail-split-methods)
-           found)
-       (while (and (not found) methods)
-         (if (re-search-backward (car (cdr (car methods))) beg t)
-             (progn
-               (goto-char end)
-               (setq result (nnmail-insert-newsgroup-line 
-                             (car (car methods)) beg end))
-               (setq found t))
-           (setq methods (cdr methods))))
-       (if (not found)
-           (progn
-             (goto-char end)
-             (setq result (nnmail-insert-newsgroup-line 
-                           (car (car nnmail-split-methods)) beg end))))))
-    result))
-
-(defun nnmail-insert-newsgroup-line (group beg end)
-  (let ((active (car (cdr (assoc group nnmail-active-alist))))
-       (time (current-time)))
-    (if (not active)
-       (progn
-         (setq nnmail-active-alist 
-               (cons (list group (cons 1 0)) nnmail-active-alist))
-         (setq active (car (cdr (car nnmail-active-alist))))))
-    (setcdr active (1+ (cdr active)))
-    (insert (format "X-Gnus-Newsgroup: %s:%d (%d %d)\n" group (cdr active)
-                   (car time) (car (cdr time))))
-    (cons group (cdr active))))
-
-(defun nnmail-split-region (beg end)
-  (save-excursion
-    (save-restriction
-      (goto-char beg)
-      (narrow-to-region beg end)
-      (let ((delim (concat "^" rmail-unix-mail-delimiter))
-           start)
-       (while (re-search-forward delim nil t)
-         (setq start (match-beginning 0))
-         (search-forward "\n\n" nil t)
-         (forward-char -1)
-         (save-excursion
-           (if (not (save-excursion (re-search-backward "^Lines:" start t)))
-               (insert 
-                (format "Lines: %d\n" 
-                        (- (count-lines 
-                            (point) 
-                            (or (save-excursion
-                                  (and (re-search-forward
-                                        rmail-unix-mail-delimiter nil t)
-                                       (match-beginning 0)))
-                                (point-max)))
-                           2)))))
-         (if (not (search-backward "\nX-Gnus-Newsgroup: " start t))
-             (nnmail-choose-mail start (point))))))))
-
-(defun nnmail-read-mbox ()
-  (if (not (file-exists-p nnmail-mbox-file))
-      (write-region 1 1 nnmail-mbox-file t 'nomesg))
-  (if (and nnmail-mbox-buffer
-          (get-buffer nnmail-mbox-buffer)
-          (buffer-name nnmail-mbox-buffer)
-          (save-excursion
-            (set-buffer nnmail-mbox-buffer)
-            (= (buffer-size) (nth 7 (file-attributes nnmail-mbox-file)))))
-      ()
+      (set-buffer nntp-server-buffer)
+      (goto-char (point-min))
+      (while (re-search-forward 
+             "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
+       ;; We create an alist with `(GROUP (LOW . HIGH))' elements.
+       (push (list (match-string 1)
+                   (cons (string-to-int (match-string 3))
+                         (string-to-int (match-string 2))))
+             group-assoc)))
+    group-assoc))
+
+(defun nnmail-save-active (group-assoc file-name)
+  (let (group)
     (save-excursion
-      (set-buffer (setq nnmail-mbox-buffer 
-                       (find-file-noselect nnmail-mbox-file)))
+      (set-buffer (get-buffer-create " *nnmail active*"))
       (buffer-disable-undo (current-buffer))
-      (nnmail-split-region (point-min) (point-max)))))
+      (erase-buffer)
+      (while group-assoc
+       (setq group (car group-assoc))
+       (insert (format "%s %d %d y\n" (car group) (cdr (car (cdr group)) )
+                       (car (car (cdr group)))))
+       (setq group-assoc (cdr group-assoc)))
+      (write-region 1 (point-max) (expand-file-name file-name) nil 'nomesg)
+      (kill-buffer (current-buffer)))))
 
-(defun nnmail-split-incoming (incoming)
-  (save-excursion
-    (set-buffer nnmail-mbox-buffer)
-    (goto-char (point-max))
-    (let ((start (point)))
-      (insert-file-contents incoming)
+(defun nnmail-get-split-group (file group)
+  (if (or (eq nnmail-spool-file 'procmail)
+         nnmail-use-procmail)
+      (cond (group group)
+           ((string-match (concat "^" (expand-file-name
+                                       (file-name-as-directory
+                                        nnmail-procmail-directory))
+                                  "\\(.*\\)" nnmail-procmail-suffix "$")
+                          (expand-file-name file))
+            (substring (expand-file-name file)
+                       (match-beginning 1) (match-end 1)))
+           (t
+            group))
+    group))
+
+(defun nnmail-process-babyl-mail-format (func)
+  (let (start message-id content-length do-search end)
+    (while (not (eobp))
+      (goto-char (point-min))
+      (re-search-forward "\f\n0, *unseen,+\n\\*\\*\\* EOOH \\*\\*\\*\n" nil t)
+      (goto-char (match-end 0))
+      (delete-region (match-beginning 0) (match-end 0))
+      (setq start (point))
+      ;; Skip all the headers in case there are more "From "s...
+      (or (search-forward "\n\n" nil t)
+         (search-forward-regexp "^[^:]*\\( .*\\|\\)$" nil t)
+         (search-forward "\1f\f"))
+      ;; Find the Message-ID header.
+      (save-excursion
+       (if (re-search-backward "^Message-ID:[ \t]*\\(<[^>]*>\\)" nil t)
+           (setq message-id (buffer-substring (match-beginning 1)
+                                              (match-end 1)))
+         ;; There is no Message-ID here, so we create one.
+         (forward-line -1)
+         (insert "Message-ID: " (setq message-id (nnmail-message-id))
+                 "\n")))
+      ;; Look for a Content-Length header.
+      (if (not (save-excursion
+                (and (re-search-backward 
+                      "^Content-Length: \\([0-9]+\\)" start t)
+                     (setq content-length (string-to-int
+                                           (buffer-substring 
+                                            (match-beginning 1)
+                                            (match-end 1))))
+                     ;; We destroy the header, since none of
+                     ;; the backends ever use it, and we do not
+                     ;; want to confuse other mailers by having
+                     ;; a (possibly) faulty header.
+                     (progn (insert "X-") t))))
+         (setq do-search t)
+       (if (or (= (+ (point) content-length) (point-max))
+               (save-excursion
+                 (goto-char (+ (point) content-length))
+                 (looking-at "\1f")))
+           (progn
+             (goto-char (+ (point) content-length))
+             (setq do-search nil))
+         (setq do-search t)))
+      ;; Go to the beginning of the next article - or to the end
+      ;; of the buffer.  
+      (if do-search
+         (if (re-search-forward "\n\1f" nil t)
+             (goto-char (+ 1 (match-beginning 0)))
+           (goto-char (- (point-max) 1))))
+      (delete-char 1)                  ; delete ^_
       (save-excursion
        (save-restriction
-         (narrow-to-region start (point-max))
-         (run-hooks 'nnmail-prepare-incoming-hook)))
-      (nnmail-split-region start (point-max)))))
+         (narrow-to-region start (point))
+         (goto-char (point-min))
+         ;; If this is a duplicate message, then we do not save it.
+         (if (nnmail-cache-id-exists-p message-id)
+             (delete-region (point-min) (point-max))
+           (nnmail-cache-insert message-id)
+           (funcall func))
+         (setq end (point-max))))
+      (goto-char end))))
+
+(defun nnmail-process-unix-mail-format (func)
+  (let ((delim (concat "^" rmail-unix-mail-delimiter))
+       start message-id content-length end skip head-end)
+    (goto-char (point-min))
+    (if (not (and (re-search-forward delim nil t)
+                 (goto-char (match-beginning 0))))
+       ;; Possibly wrong format?
+       ()
+      ;; Carry on until the bitter end.
+      (while (not (eobp))
+       (setq start (point)
+             end nil)
+       ;; Find the end of the head.
+       (narrow-to-region
+        start 
+        (if (search-forward "\n\n" nil t)
+            (1- (point))
+          ;; This will never happen, but just to be on the safe side --
+          ;; if there is no head-body delimiter, we search a bit manually.
+          (while (and (looking-at "From \\|[^ \t]+:")
+                      (not (eobp)))
+            (forward-line 1)
+            (point))))
+       ;; Find the Message-ID header.
+       (goto-char (point-min))
+       (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
+           (setq message-id (match-string 1))
+         ;; There is no Message-ID here, so we create one.
+         (forward-line 1)
+         (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
+       ;; Look for a Content-Length header.
+       (goto-char (point-min))
+       (if (not (re-search-forward "^Content-Length: \\([0-9]+\\)" nil t))
+           (setq content-length nil)
+         (setq content-length (string-to-int (match-string 1)))
+         ;; We destroy the header, since none of the backends ever 
+         ;; use it, and we do not want to confuse other mailers by
+         ;; having a (possibly) faulty header.
+         (beginning-of-line)
+         (insert "X-"))
+       ;; Find the end of this article.
+       (goto-char (point-max))
+       (widen)
+       (setq head-end (point))
+       ;; We try the Content-Length value.
+       (when content-length
+         (forward-line 1)
+         (setq skip (+ (point) content-length))
+         (when (or (= skip (point-max))
+                   (and (< skip (point-max))
+                        (goto-char skip)
+                        (looking-at delim)))
+           (setq end skip)))
+       (if end
+           (goto-char end)
+         ;; No Content-Length, so we find the beginning of the next 
+         ;; article or the end of the buffer.
+         (goto-char head-end)
+         (if (re-search-forward delim nil t)
+             (goto-char (match-beginning 0))
+           (goto-char (point-max))))
+       ;; Allow the backend to save the article.
+       (save-excursion
+         (save-restriction
+           (narrow-to-region start (point))
+           (goto-char (point-min))
+           ;; If this is a duplicate message, then we do not save it.
+           (if (nnmail-cache-id-exists-p message-id)
+               (delete-region (point-min) (point-max))
+             (nnmail-cache-insert message-id)
+             (funcall func))
+           (setq end (point-max))))
+       (goto-char end)))))
+
+(defun nnmail-process-mmfd-mail-format (func)
+  (let ((delim "^\^A\^A\^A\^A$")
+       start message-id end)
+    (goto-char (point-min))
+    (if (not (and (re-search-forward delim nil t)
+                 (forward-line 1)))
+       ;; Possibly wrong format?
+       ()
+      ;; Carry on until the bitter end.
+      (while (not (eobp))
+       (setq start (point))
+       ;; Find the end of the head.
+       (narrow-to-region
+        start 
+        (if (search-forward "\n\n" nil t)
+            (1- (point))
+          ;; This will never happen, but just to be on the safe side --
+          ;; if there is no head-body delimiter, we search a bit manually.
+          (while (and (looking-at "From \\|[^ \t]+:")
+                      (not (eobp)))
+            (forward-line 1)
+            (point))))
+       ;; Find the Message-ID header.
+       (goto-char (point-min))
+       (if (re-search-forward "^Message-ID:[ \t]*\\(<[^>]+>\\)" nil t)
+           (setq message-id (match-string 1))
+         ;; There is no Message-ID here, so we create one.
+         (forward-line 1)
+         (insert "Message-ID: " (setq message-id (nnmail-message-id)) "\n"))
+       ;; Find the end of this article.
+       (goto-char (point-max))
+       (widen)
+       (if (re-search-forward delim nil t)
+           (beginning-of-line)
+         (goto-char (point-max)))
+       ;; Allow the backend to save the article.
+       (save-excursion
+         (save-restriction
+           (narrow-to-region start (point))
+           (goto-char (point-min))
+           ;; If this is a duplicate message, then we do not save it.
+           (if (nnmail-cache-id-exists-p message-id)
+               (delete-region (point-min) (point-max))
+             (nnmail-cache-insert message-id)
+             (funcall func))
+           (setq end (point-max))))
+       (goto-char end)))))
+
+(defun nnmail-split-incoming (incoming func &optional exit-func group)
+  "Go through the entire INCOMING file and pick out each individual mail.
+FUNC will be called with the buffer narrowed to each mail."
+  (let (;; If this is a group-specific split, we bind the split
+       ;; methods to just this group.
+       (nnmail-split-methods (if (and group
+                                      (or (eq nnmail-spool-file 'procmail)
+                                          nnmail-use-procmail)
+                                      (not nnmail-resplit-incoming))
+                                 (list (list group ""))
+                               nnmail-split-methods))
+       start end do-search message-id)
+    (save-excursion
+      ;; Open the message-id cache.
+      (nnmail-cache-open)
+      ;; Insert the incoming file.
+      (set-buffer (get-buffer-create " *nnmail incoming*"))
+      (buffer-disable-undo (current-buffer))
+      (erase-buffer)
+      (insert-file-contents incoming)
+      (goto-char (point-min))
+      (save-excursion (run-hooks 'nnmail-prepare-incoming-hook))
+      ;; Handle both babyl, MMFD and unix mail formats, since movemail will
+      ;; use the former when fetching from a mailbox, the latter when
+      ;; fetches from a file.
+      (cond ((looking-at "\^L")
+            (nnmail-process-babyl-mail-format func))
+           ((looking-at "\^A\^A\^A\^A")
+            (nnmail-process-mmfd-mail-format func))
+           (t
+            (nnmail-process-unix-mail-format func)))
+      ;; Close the message-id cache.
+      (nnmail-cache-close)
+      (if exit-func (funcall exit-func))
+      (kill-buffer (current-buffer)))))
 
-(defun nnmail-get-active ()
-  (let ((methods nnmail-split-methods))
-    (setq nnmail-active-alist nil)
-    (if (nnmail-request-list)
+;; Mail crossposts syggested by Brian Edmonds <edmonds@cs.ubc.ca>. 
+(defun nnmail-article-group (func)
+  "Look at the headers and return an alist of groups that match.
+FUNC will be called with the group name to determine the article number."
+  (let ((methods nnmail-split-methods)
+       (obuf (current-buffer))
+       (beg (point-min))
+       end group-art method)
+    (if (and (sequencep methods) (= (length methods) 1))
+       ;; If there is only just one group to put everything in, we
+       ;; just return a list with just this one method in.
+       (setq group-art
+             (list (cons (car (car methods))
+                         (funcall func (car (car methods))))))
+      ;; We do actual comparison.
+      (save-excursion
+       ;; Find headers.
+       (goto-char beg)
+       (setq end (if (search-forward "\n\n" nil t) (point) (point-max)))
+       (set-buffer nntp-server-buffer)
+       (erase-buffer)
+       ;; Copy the headers into the work buffer.
+       (insert-buffer-substring obuf beg end)
+       ;; Fold continuation lines.
+       (goto-char (point-min))
+       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
+         (replace-match " " t t))
+       (if (and (symbolp nnmail-split-methods)
+                (fboundp nnmail-split-methods))
+           ;; `nnmail-split-methods' is a function, so we just call 
+           ;; this function here and use the result.
+           (setq group-art
+                 (mapcar
+                  (lambda (group) (cons group (funcall func group)))
+                  (condition-case nil
+                      (funcall nnmail-split-methods)
+                    (error
+                     (message 
+                      "Error in `nnmail-split-methods'; using `bogus' mail group")
+                     (sit-for 1)
+                     '("bogus")))))
+         ;; Go throught the split methods to find a match.
+         (while (and methods (or nnmail-crosspost (not group-art)))
+           (goto-char (point-max))
+           (setq method (pop methods))
+           (if (or methods
+                   (not (equal "" (nth 1 method))))
+               (when (and
+                      (condition-case () 
+                          (if (stringp (nth 1 method))
+                              (re-search-backward (car (cdr method)) nil t)
+                            ;; Function to say whether this is a match.
+                            (funcall (nth 1 method) (car method)))
+                        (error nil))
+                      ;; Don't enter the article into the same 
+                      ;; group twice.
+                      (not (assoc (car method) group-art)))
+                 (push (cons (car method) (funcall func (car method))) 
+                       group-art))
+             ;; This is the final group, which is used as a 
+             ;; catch-all.
+             (unless group-art
+               (setq group-art 
+                     (list (cons (car method) 
+                                 (funcall func (car method)))))))))
+       group-art))))
+
+(defun nnmail-insert-lines ()
+  "Insert how many lines there are in the body of the mail.
+Return the number of characters in the body."
+  (let (lines chars)
+    (save-excursion
+      (goto-char (point-min))
+      (when (search-forward "\n\n" nil t) 
+       (setq chars (- (point-max) (point)))
+       (setq lines (- (count-lines (point) (point-max)) 1))
+       (forward-char -1)
        (save-excursion
-         (set-buffer (get-buffer-create " *nntpd*"))
-         (goto-char 1)
-         (while (re-search-forward 
-                 "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)" nil t)
-           (setq nnmail-active-alist 
-                 (cons (list (buffer-substring (match-beginning 1) 
-                                               (match-end 1))
-                             (cons (string-to-int 
-                                    (buffer-substring (match-beginning 3)
-                                                      (match-end 3)))
-                                   (string-to-int 
-                                    (buffer-substring (match-beginning 2)
-                                                      (match-end 2)))))
-                       nnmail-active-alist)))))
-    (while methods
-      (if (not (assoc (car (car methods)) nnmail-active-alist))
-         (setq nnmail-active-alist
-               (cons (list (car (car methods)) (cons 1 0)) 
-                     nnmail-active-alist)))
-      (setq methods (cdr methods)))
+         (when (re-search-backward "^Lines: " nil t)
+           (delete-region (point) (progn (forward-line 1) (point)))))
+       (insert (format "Lines: %d\n" lines))
+       chars))))
+
+(defun nnmail-insert-xref (group-alist)
+  "Insert an Xref line based on the (group . article) alist."
+  (save-excursion
+    (goto-char (point-min))
+    (when (search-forward "\n\n" nil t) 
+      (forward-char -1)
+      (if (re-search-backward "^Xref: " nil t)
+         (delete-region (match-beginning 0) 
+                        (progn (forward-line 1) (point))))
+      (insert (format "Xref: %s" (system-name)))
+      (while group-alist
+       (insert (format " %s:%d" (car (car group-alist)) 
+                       (cdr (car group-alist))))
+       (setq group-alist (cdr group-alist)))
+      (insert "\n"))))
+
+;; Written by byer@mv.us.adobe.com (Scott Byer).
+(defun nnmail-make-complex-temp-name (prefix)
+  (let ((newname (make-temp-name prefix))
+       (newprefix prefix))
+    (while (file-exists-p newname)
+      (setq newprefix (concat newprefix "x"))
+      (setq newname (make-temp-name newprefix)))
+    newname))
+
+;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
+
+(defun nnmail-split-fancy ()
+  "Fancy splitting method.
+See the documentation for the variable `nnmail-split-fancy' for documentation."
+  (nnmail-split-it nnmail-split-fancy))
+
+(defvar nnmail-split-cache nil)
+;; Alist of split expresions their equivalent regexps.
+
+(defun nnmail-split-it (split)
+  ;; Return a list of groups matching SPLIT.
+  (cond ((stringp split)
+        ;; A group.
+        (list split))
+       ((eq (car split) '&)
+        (apply 'nconc (mapcar 'nnmail-split-it (cdr split))))
+       ((eq (car split) '|)
+        (let (done)
+          (while (and (not done) (cdr split))
+            (setq split (cdr split)
+                  done (nnmail-split-it (car split))))
+          done))
+       ((assq split nnmail-split-cache)
+        ;; A compiled match expression.
+        (goto-char (point-max))
+        (if (re-search-backward (cdr (assq split nnmail-split-cache)) nil t)
+            (nnmail-split-it (nth 2 split))))
+       (t
+        ;; An uncompiled match.
+        (let* ((field (nth 0 split))
+               (value (nth 1 split))
+               (regexp (concat "^\\(" 
+                               (if (symbolp field)
+                                   (cdr (assq field 
+                                              nnmail-split-abbrev-alist))
+                                 field)
+                               "\\):.*\\<\\("
+                               (if (symbolp value)
+                                   (cdr (assq value
+                                              nnmail-split-abbrev-alist))
+                                 value)
+                               "\\>\\)")))
+          (setq nnmail-split-cache 
+                (cons (cons split regexp) nnmail-split-cache))
+          (goto-char (point-max))
+          (if (re-search-backward regexp nil t)
+              (nnmail-split-it (nth 2 split)))))))
+
+;; Get a list of spool files to read.
+(defun nnmail-get-spool-files (&optional group)
+  (if (null nnmail-spool-file)
+      ;; No spool file whatsoever.
+      nil
+    (let* ((procmails 
+           ;; If procmail is used to get incoming mail, the files
+           ;; are stored in this directory.
+           (and (file-exists-p nnmail-procmail-directory)
+                (directory-files 
+                 nnmail-procmail-directory 
+                 t (concat (if group group "")
+                           nnmail-procmail-suffix "$") t)))
+          (p procmails)
+          (crash (when (and (file-exists-p nnmail-crash-box)
+                            (> (nth 7 (file-attributes nnmail-crash-box)) 0))
+                   (list nnmail-crash-box))))
+      ;; Remove any directories that inadvertantly match the procmail
+      ;; suffix, which might happen if the suffix is "".
+      (while p
+       (and (or (file-directory-p (car p))
+                (file-symlink-p (car p)))
+            (setq procmails (delete (car p) procmails)))
+       (setq p (cdr p)))
+      ;; Return the list of spools.
+      (append 
+       crash
+       (cond ((listp nnmail-spool-file)
+             (append nnmail-spool-file procmails))
+            ((stringp nnmail-spool-file)
+             (cons nnmail-spool-file procmails))
+            ((eq nnmail-spool-file 'pop)
+             (cons (format "po:%s" (user-login-name)) procmails))
+            (t
+             procmails))))))
+
+;; Activate a backend only if it isn't already activated. 
+;; If FORCE, re-read the active file even if the backend is 
+;; already activated.
+(defun nnmail-activate (backend &optional force)
+  (let (file timestamp file-time)
+    (if (or (not (symbol-value (intern (format "%s-group-alist" backend))))
+           force
+           (and (setq file (condition-case ()
+                               (symbol-value (intern (format "%s-active-file" 
+                                                             backend)))
+                             (error nil)))
+                (setq file-time (nth 5 (file-attributes file)))
+                (or (not
+                     (setq timestamp
+                           (condition-case ()
+                               (symbol-value (intern
+                                              (format "%s-active-timestamp" 
+                                                      backend)))
+                             (error 'none))))
+                    (not (consp timestamp))
+                    (equal timestamp '(0 0))
+                    (> (nth 0 file-time) (nth 0 timestamp))
+                    (and (= (nth 0 file-time) (nth 0 timestamp))
+                         (> (nth 1 file-time) (nth 1 timestamp))))))
+       (save-excursion
+         (or (eq timestamp 'none)
+             (set (intern (format "%s-active-timestamp" backend)) 
+                  (current-time)))
+         (funcall (intern (format "%s-request-list" backend)))
+         (set (intern (format "%s-group-alist" backend)) 
+              (nnmail-get-active))))
     t))
 
-(defun nnmail-save-active ()
-  (let ((groups nnmail-active-alist)
-       group)
+(defun nnmail-message-id ()
+  (concat "<" (nnmail-unique-id) "@totally-fudged-out-message-id>"))
+
+(defvar nnmail-unique-id-char nil)
+
+(defun nnmail-number-base36 (num len)
+  (if (if (< len 0) (<= num 0) (= len 0))
+      ""
+    (concat (nnmail-number-base36 (/ num 36) (1- len))
+           (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
+                                 (% num 36))))))
+
+(defun nnmail-unique-id ()
+  (setq nnmail-unique-id-char
+       (% (1+ (or nnmail-unique-id-char (logand (random t) (1- (lsh 1 20)))))
+          ;; (current-time) returns 16-bit ints,
+          ;; and 2^16*25 just fits into 4 digits i base 36.
+          (* 25 25)))
+  (let ((tm (if (fboundp 'current-time)
+               (current-time) '(12191 46742 287898))))
+    (concat
+     (nnmail-number-base36 (+ (car   tm) 
+                             (lsh (% nnmail-unique-id-char 25) 16)) 4)
+     (nnmail-number-base36 (+ (nth 1 tm) 
+                             (lsh (/ nnmail-unique-id-char 25) 16)) 4))))
+
+;;;
+;;; nnmail duplicate handling
+;;;
+
+(defvar nnmail-cache-buffer nil)
+
+(defun nnmail-cache-open ()
+  (if (or (not nnmail-delete-duplicates)
+         (and nnmail-cache-buffer
+              (buffer-name nnmail-cache-buffer)))
+      ()                               ; The buffer is open.
     (save-excursion
-      (set-buffer (get-buffer-create " *nnmail*"))
+      (set-buffer 
+       (setq nnmail-cache-buffer 
+            (get-buffer-create " *nnmail message-id cache*")))
       (buffer-disable-undo (current-buffer))
-      (erase-buffer)
-      (while groups
-       (setq group (car groups))
-       (insert (format "%s %d %d y\n" (car group) (cdr (car (cdr group)) )
-                       (car (car (cdr group)))))
-       (setq groups (cdr groups)))
-      (write-region 1 (point-max) (expand-file-name nnmail-active-file) nil 
-                   'nomesg)
-      (kill-buffer (current-buffer)))))
+      (and (file-exists-p nnmail-message-id-cache-file)
+          (insert-file-contents nnmail-message-id-cache-file))
+      (current-buffer))))
 
-(defun nnmail-get-new-mail ()
-  (let (incoming)
-    (nnmail-get-active)
-    (nnmail-read-mbox)
-    (if (and (file-exists-p nnmail-spool-file)
-            (> (nth 7 (file-attributes nnmail-spool-file)) 0))
-       (progn
-         (setq incoming (nnmail-move-inbox))
-         (nnmail-split-incoming incoming)
-         (run-hooks 'nnmail-read-incoming-hook)))
-    (and (buffer-modified-p nnmail-mbox-buffer) 
-        (save-excursion
-          (nnmail-save-active)
-          (set-buffer nnmail-mbox-buffer)
-          (save-buffer)))
-;    (if incoming
-;      (delete-file incoming))
-    ))
+(defun nnmail-cache-close ()
+  (if (or (not nnmail-cache-buffer)
+         (not nnmail-delete-duplicates)
+         (not (buffer-name nnmail-cache-buffer))
+         (not (buffer-modified-p nnmail-cache-buffer)))
+      ()                               ; The buffer is closed.
+    (save-excursion
+      (set-buffer nnmail-cache-buffer)
+      ;; Weed out the excess number of Message-IDs.
+      (goto-char (point-max))
+      (and (search-backward "\n" nil t nnmail-message-id-cache-length)
+          (progn
+            (beginning-of-line)
+            (delete-region (point-min) (point))))
+      ;; Save the buffer.
+      (or (file-exists-p (file-name-directory nnmail-message-id-cache-file))
+         (make-directory (file-name-directory nnmail-message-id-cache-file)
+                         t))
+      (write-region (point-min) (point-max)
+                   nnmail-message-id-cache-file nil 'silent)
+      (set-buffer-modified-p nil))))
+
+(defun nnmail-cache-insert (id)
+  (and nnmail-delete-duplicates
+       (save-excursion
+        (set-buffer nnmail-cache-buffer)
+        (goto-char (point-max))
+        (insert id "\n"))))
+
+(defun nnmail-cache-id-exists-p (id)
+  (and nnmail-delete-duplicates
+       (save-excursion
+        (set-buffer nnmail-cache-buffer)
+        (goto-char (point-max))
+        (search-backward id nil t))))
+
+(defun nnmail-get-value (&rest args)
+  (let ((sym (intern (apply 'format args))))
+    (and (boundp sym)
+        (symbol-value sym))))
+
+(defun nnmail-get-new-mail (method exit-func temp
+                                  &optional group spool-func)
+  "Read new incoming mail."
+  (let* ((spools (nnmail-get-spool-files group))
+        (group-in group)
+        incoming incomings spool)
+    (when (and (nnmail-get-value "%s-get-new-mail" method)
+              nnmail-spool-file)
+      ;; We first activate all the groups.
+      (nnmail-activate method)
+      ;; The we go through all the existing spool files and split the
+      ;; mail from each.
+      (while spools
+       (setq spool (pop spools))
+       ;; We read each spool file if either the spool is a POP-mail
+       ;; spool, or the file exists.  We can't check for the
+       ;; existance of POPped mail.
+       (when (or (string-match "^po:" spool)
+                 (and (file-exists-p spool)
+                      (> (nth 7 (file-attributes spool)) 0)))
+         (when gnus-verbose-backends 
+           (message "%s: Reading incoming mail..." method))
+         (when (and (nnmail-move-inbox spool)
+                    (file-exists-p nnmail-crash-box))
+           ;; There is new mail.  We first find out if all this mail
+           ;; is supposed to go to some specific group.
+           (setq group (nnmail-get-split-group spool group-in))
+           ;; We split the mail
+           (nnmail-split-incoming 
+            nnmail-crash-box (intern (format "%s-save-mail" method)) 
+            spool-func group)
+           ;; Check whether the inbox is to be moved to the special tmp dir. 
+           (setq incoming
+                 (nnmail-make-complex-temp-name 
+                  (expand-file-name 
+                   (if nnmail-tmp-directory
+                       (concat 
+                        (file-name-as-directory nnmail-tmp-directory)
+                        (file-name-nondirectory (concat temp "Incoming")))
+                     (concat temp "Incoming")))))
+           (rename-file nnmail-crash-box incoming t)
+           (push incoming incomings))))
+      ;; If we did indeed read any incoming spools, we save all info. 
+      (when incomings
+       (nnmail-save-active 
+        (nnmail-get-value "%s-group-alist" method)
+        (nnmail-get-value "%s-active-file" method))
+       (when exit-func
+         (funcall exit-func))
+       (run-hooks 'nnmail-read-incoming-hook)
+       (when gnus-verbose-backends
+         (message "%s: Reading incoming mail...done" method)))
+      ;; Delete all the temporary files.
+      (while incomings
+       (setq incoming (pop incomings))
+       (and nnmail-delete-incoming
+            (file-exists-p incoming)
+            (file-writable-p incoming)
+            (delete-file incoming))))))
 
 (provide 'nnmail)