lisp/ChangeLog addition:
[gnus] / lisp / nnmbox.el
index ee7be43..972e20f 100644 (file)
@@ -1,7 +1,9 @@
 ;;; nnmbox.el --- mail mbox access for Gnus
-;; Copyright (C) 1995 Free Software Foundation, Inc.
 
-;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
+;;     Free Software Foundation, Inc.
+
+;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
 ;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
 ;; Keywords: news, mail
 
 ;; the Free Software Foundation; either version 2, or (at your option)
 ;; any later version.
 
-;; GNU Emacs is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-;; GNU General Public License for more details.
-
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
 ;; For an overview of what the interface functions do, please see the
-;; Gnus sources.  
+;; Gnus sources.
 
 ;;; Code:
 
 (require 'nnheader)
-(require 'rmail)
+(require 'message)
 (require 'nnmail)
+(require 'nnoo)
+(require 'gnus-range)
+(eval-when-compile (require 'cl))
+
+(nnoo-declare nnmbox)
+
+(defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
+  "The name of the mail box file in the user's home directory.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
-(defvar nnmbox-mbox-file (expand-file-name "~/mbox")
-  "The name of the mail box file in the user's home directory.")
+(defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
+  "The name of the active file for the mail box.
 
-(defvar nnmbox-active-file (expand-file-name "~/.mbox-active")
-  "The name of the active file for the mail box.")
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
-(defvar nnmbox-get-new-mail t
-  "If non-nil, nnmbox will check the incoming mail file and split the mail.")
+(defvoo nnmbox-get-new-mail t
+  "If non-nil, nnmbox will check the incoming mail file and split the mail.
 
-(defvar nnmbox-prepare-save-mail-hook nil
-  "Hook run narrowed to an article before saving.")
+This variable is a virtual server slot.  See the Gnus manual for details.")
+
+(defvoo nnmbox-prepare-save-mail-hook nil
+  "Hook run narrowed to an article before saving.
+
+This variable is a virtual server slot.  See the Gnus manual for details.")
 
 \f
 
-(defconst nnmbox-version "nnmbox 0.1"
+(defconst nnmbox-version "nnmbox 1.0"
   "nnmbox version.")
 
-(defvar nnmbox-current-group nil
+(defvoo nnmbox-current-group nil
   "Current nnmbox news group directory.")
 
 (defconst nnmbox-mbox-buffer nil)
 
-(defvar nnmbox-status-string "")
+(defvoo nnmbox-status-string "")
 
-(defvar nnmbox-group-alist nil)
-(defvar nnmbox-active-timestamp nil)
-
-\f
+(defvoo nnmbox-group-alist nil)
+(defvoo nnmbox-active-timestamp nil)
 
-(defvar nnmbox-current-server nil)
-(defvar nnmbox-server-alist nil)
-(defvar nnmbox-server-variables 
-  (list
-   (list 'nnmbox-mbox-file nnmbox-mbox-file)
-   (list 'nnmbox-active-file nnmbox-active-file)
-   (list 'nnmbox-get-new-mail nnmbox-get-new-mail)
-   '(nnmbox-current-group nil)
-   '(nnmbox-status-string "")
-   '(nnmbox-group-alist nil)))
+(defvoo nnmbox-file-coding-system mm-binary-coding-system)
+(defvoo nnmbox-file-coding-system-for-write nil)
+(defvoo nnmbox-active-file-coding-system mm-binary-coding-system)
+(defvoo nnmbox-active-file-coding-system-for-write nil)
 
+(defvar nnmbox-group-building-active-articles nil)
+(defvar nnmbox-group-active-articles nil)
 \f
 
 ;;; Interface functions
 
-(defun nnmbox-retrieve-headers (sequence &optional newsgroup server)
+(nnoo-define-basics nnmbox)
+
+(deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
   (save-excursion
     (set-buffer nntp-server-buffer)
     (erase-buffer)
     (let ((number (length sequence))
          (count 0)
-         article art-string start stop)
-      (nnmbox-possibly-change-newsgroup newsgroup)
-      (if (stringp (car sequence))
-         'headers
-       (while sequence
-         (setq article (car sequence))
-         (setq art-string (nnmbox-article-string article))
-         (set-buffer nnmbox-mbox-buffer)
-         (if (or (search-forward art-string nil t)
-                 (progn (goto-char (point-min))
-                        (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))
-               (insert-buffer-substring nnmbox-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))
-              gnus-verbose-backends
-              (message "nnmbox: Receiving headers... %d%%"
-                       (/ (* count 100) number))))
-
+         article start stop)
+      (nnmbox-possibly-change-newsgroup newsgroup server)
+      (while sequence
+       (setq article (car sequence))
+       (set-buffer nnmbox-mbox-buffer)
+       (when (nnmbox-find-article article)
+         (setq start
+               (save-excursion
+                 (re-search-backward
+                  (concat "^" message-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))
+         (insert-buffer-substring nnmbox-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)
-            gnus-verbose-backends
-            (message "nnmbox: Receiving headers...done"))
-
-       ;; Fold continuation lines.
-       (set-buffer nntp-server-buffer)
-       (goto-char (point-min))
-       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
-         (replace-match " " t t))
-       'headers))))
-
-(defun nnmbox-open-server (server &optional defs)
-  (nnheader-init-server-buffer)
-  (if (equal server nnmbox-current-server)
-      t
-    (if nnmbox-current-server
-       (setq nnmbox-server-alist 
-             (cons (list nnmbox-current-server
-                         (nnheader-save-variables nnmbox-server-variables))
-                   nnmbox-server-alist)))
-    (let ((state (assoc server nnmbox-server-alist)))
-      (if state 
-         (progn
-           (nnheader-restore-variables (nth 1 state))
-           (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
-       (nnheader-set-init-variables nnmbox-server-variables defs)))
-    (setq nnmbox-current-server server)))
-
-(defun nnmbox-close-server (&optional server)
+            (zerop (% count 20))
+            (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
+                              (/ (* count 100) number))))
+
+      (and (numberp nnmail-large-newsgroup)
+          (> number nnmail-large-newsgroup)
+          (nnheader-message 5 "nnmbox: Receiving headers...done"))
+
+      (set-buffer nntp-server-buffer)
+      (nnheader-fold-continuation-lines)
+      'headers)))
+
+(deffoo nnmbox-open-server (server &optional defs)
+  (nnoo-change-server 'nnmbox server defs)
+  (nnmbox-create-mbox)
+  (cond
+   ((not (file-exists-p nnmbox-mbox-file))
+    (nnmbox-close-server)
+    (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
+   ((file-directory-p nnmbox-mbox-file)
+    (nnmbox-close-server)
+    (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
+   (t
+    (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
+                    nnmbox-mbox-file)
+    t)))
+
+(deffoo nnmbox-close-server (&optional server)
+  (when (and nnmbox-mbox-buffer
+            (buffer-name nnmbox-mbox-buffer))
+    (kill-buffer nnmbox-mbox-buffer))
+  (nnoo-close-server 'nnmbox server)
   t)
 
-(defun nnmbox-server-opened (&optional server)
-  (and (equal server nnmbox-current-server)
+(deffoo nnmbox-server-opened (&optional server)
+  (and (nnoo-current-server-p 'nnmbox server)
        nnmbox-mbox-buffer
        (buffer-name nnmbox-mbox-buffer)
        nntp-server-buffer
        (buffer-name nntp-server-buffer)))
 
-(defun nnmbox-status-message (&optional server)
-  nnmbox-status-string)
-
-(defun nnmbox-request-article (article &optional newsgroup server buffer)
-  (nnmbox-possibly-change-newsgroup newsgroup)
-  (if (stringp article)
-      nil
-    (save-excursion
-      (set-buffer nnmbox-mbox-buffer)
-      (goto-char (point-min))
-      (if (search-forward (nnmbox-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 nnmbox-mbox-buffer start stop)
-             (goto-char (point-min))
-             (while (looking-at "From ")
-               (delete-char 5)
-               (insert "X-From-Line: ")
-               (forward-line 1))
-             t))))))
-
-(defun nnmbox-request-group (group &optional server dont-check)
+(deffoo nnmbox-request-article (article &optional newsgroup server buffer)
+  (nnmbox-possibly-change-newsgroup newsgroup server)
   (save-excursion
-    (if (nnmbox-possibly-change-newsgroup group)
-       (if dont-check
-           t
-         (nnmbox-get-new-mail group)
-         (save-excursion
-           (set-buffer nntp-server-buffer)
-           (erase-buffer)
-           (let ((active (assoc group nnmbox-group-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 nnmbox-close-group (group &optional server)
+    (set-buffer nnmbox-mbox-buffer)
+    (when (nnmbox-find-article article)
+      (let (start stop)
+       (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
+       (setq start (point))
+       (forward-line 1)
+       (or (and (re-search-forward
+                 (concat "^" message-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 nnmbox-mbox-buffer start stop)
+         (goto-char (point-min))
+         (while (looking-at "From ")
+           (delete-char 5)
+           (insert "X-From-Line: ")
+           (forward-line 1))
+         (if (numberp article)
+             (cons nnmbox-current-group article)
+           (nnmbox-article-group-number nil)))))))
+
+(deffoo nnmbox-request-group (group &optional server dont-check)
+  (nnmbox-possibly-change-newsgroup nil server)
+  (let ((active (cadr (assoc group nnmbox-group-alist))))
+    (cond
+     ((or (null active)
+         (null (nnmbox-possibly-change-newsgroup group server)))
+      (nnheader-report 'nnmbox "No such group: %s" group))
+     (dont-check
+      (nnheader-report 'nnmbox "Selected group %s" group)
+      (nnheader-insert ""))
+     (t
+      (nnheader-report 'nnmbox "Selected group %s" group)
+      (nnheader-insert "211 %d %d %d %s\n"
+                      (1+ (- (cdr active) (car active)))
+                      (car active) (cdr active) group)))))
+
+(defun nnmbox-save-buffer ()
+  (let ((coding-system-for-write
+        (or nnmbox-file-coding-system-for-write
+            nnmbox-file-coding-system)))
+    (save-buffer)))
+
+(defun nnmbox-save-active (group-alist active-file)
+  (let ((nnmail-active-file-coding-system
+        (or nnmbox-active-file-coding-system-for-write
+            nnmbox-active-file-coding-system)))
+    (nnmail-save-active group-alist active-file)))
+
+(deffoo nnmbox-request-scan (&optional group server)
+  (nnmbox-possibly-change-newsgroup group server)
+  (nnmbox-read-mbox)
+  (nnmail-get-new-mail
+   'nnmbox
+   (lambda ()
+     (save-excursion
+       (set-buffer nnmbox-mbox-buffer)
+       (nnmbox-save-buffer)))
+   (file-name-directory nnmbox-mbox-file)
+   group
+   (lambda ()
+     (save-excursion
+       (let ((in-buf (current-buffer)))
+        (set-buffer nnmbox-mbox-buffer)
+        (goto-char (point-max))
+        (insert-buffer-substring in-buf)))
+     (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
+
+(deffoo nnmbox-close-group (group &optional server)
+  t)
+
+(deffoo nnmbox-request-create-group (group &optional server args)
+  (nnmail-activate 'nnmbox)
+  (unless (assoc group nnmbox-group-alist)
+    (push (list group (cons 1 0))
+         nnmbox-group-alist)
+    (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
   t)
 
-(defun nnmbox-request-list (&optional server)
-  (if server (nnmbox-get-new-mail))
+(deffoo nnmbox-request-list (&optional server)
   (save-excursion
-    (or (nnmail-find-file nnmbox-active-file)
-       (progn
-         (setq nnmbox-group-alist (nnmail-get-active))
-         (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
-         (nnmail-find-file nnmbox-active-file)))))
+    (let ((nnmail-file-coding-system
+          nnmbox-active-file-coding-system))
+      (nnmail-find-file nnmbox-active-file))
+    (setq nnmbox-group-alist (nnmail-get-active))
+    t))
 
-(defun nnmbox-request-newgroups (date &optional server)
+(deffoo nnmbox-request-newgroups (date &optional server)
   (nnmbox-request-list server))
 
-(defun nnmbox-request-list-newsgroups (&optional server)
-  (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
-  nil)
-
-(defun nnmbox-request-post (&optional server)
-  (mail-send-and-exit nil))
+(deffoo nnmbox-request-list-newsgroups (&optional server)
+  (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
 
-(defalias 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
-
-(defun nnmbox-request-expire-articles 
-  (articles newsgroup &optional server force)
-  (nnmbox-possibly-change-newsgroup newsgroup)
-  (let* ((days (or (and nnmail-expiry-wait-function
-                       (funcall nnmail-expiry-wait-function newsgroup))
-                  nnmail-expiry-wait))
-        (is-old t)
+(deffoo nnmbox-request-expire-articles
+    (articles newsgroup &optional server force)
+  (nnmbox-possibly-change-newsgroup newsgroup server)
+  (let* ((is-old t)
         rest)
     (nnmail-activate 'nnmbox)
 
-    (save-excursion 
+    (save-excursion
       (set-buffer nnmbox-mbox-buffer)
       (while (and articles is-old)
-       (goto-char (point-min))
-       (if (search-forward (nnmbox-article-string (car articles)) nil t)
-           (if (or force
-                   (setq is-old
-                         (> (nnmail-days-between 
-                             (current-time-string)
-                             (buffer-substring 
-                              (point) (progn (end-of-line) (point))))
-                            days)))
-               (progn
-                 (and gnus-verbose-backends
-                      (message "Deleting article %s..." (car articles)))
-                 (nnmbox-delete-mail))
-             (setq rest (cons (car articles) rest))))
+       (when (nnmbox-find-article (car articles))
+         (if (setq is-old
+                   (nnmail-expired-article-p
+                    newsgroup
+                    (buffer-substring
+                     (point) (progn (end-of-line) (point))) force))
+             (progn
+               (unless (eq nnmail-expiry-target 'delete)
+                 (with-temp-buffer
+                   (nnmbox-request-article (car articles)
+                                            newsgroup server
+                                            (current-buffer))
+                   (let ((nnml-current-directory nil))
+                     (nnmail-expiry-target-group
+                      nnmail-expiry-target newsgroup)))
+                 (nnmbox-possibly-change-newsgroup newsgroup server))
+               (nnheader-message 5 "Deleting article %d in %s..."
+                                 (car articles) newsgroup)
+               (nnmbox-delete-mail))
+           (push (car articles) rest)))
        (setq articles (cdr articles)))
-      (save-buffer)
+      (nnmbox-save-buffer)
       ;; Find the lowest active article in this group.
       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
-       (goto-char (point-min))
-       (while (and (not (search-forward