* nntp.el (nntp-accept-process-output): Use new function.
[gnus] / lisp / nnsoup.el
index e5e1ad4..009a1d3 100644 (file)
@@ -1,8 +1,10 @@
 ;;; nnsoup.el --- SOUP access for Gnus
-;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
 
-;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
-;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+;;     Free Software Foundation, Inc.
+
+;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
+;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
 ;; Keywords: news, mail
 
 ;; This file is part of GNU Emacs.
 (defvoo nnsoup-directory "~/SOUP/"
   "*SOUP packet directory.")
 
-(defvoo nnsoup-tmp-directory "/tmp/"
+(defvoo nnsoup-tmp-directory
+    (cond ((fboundp 'temp-directory) (temp-directory))
+         ((boundp 'temporary-file-directory) temporary-file-directory)
+         ("/tmp/"))
   "*Where nnsoup will store temporary files.")
 
-(defvoo nnsoup-replies-directory (concat nnsoup-directory "replies/")
+(defvoo nnsoup-replies-directory (expand-file-name "replies/" nnsoup-directory)
   "*Directory where outgoing packets will be composed.")
 
-(defvoo nnsoup-replies-format-type ?n
+(defvoo nnsoup-replies-format-type ?u  ;; u is USENET news format.
   "*Format of the replies packages.")
 
 (defvoo nnsoup-replies-index-type ?n
   "*Index type of the replies packages.")
 
-(defvoo nnsoup-active-file (concat nnsoup-directory "active")
+(defvoo nnsoup-active-file (expand-file-name "active" nnsoup-directory)
   "Active file.")
 
 (defvoo nnsoup-packer "tar cf - %s | gzip > $HOME/Soupin%d.tgz"
@@ -69,6 +74,11 @@ The SOUP packet file name will be inserted at the %s.")
 (defvoo nnsoup-packet-regexp "Soupout"
   "*Regular expression matching SOUP packets in `nnsoup-packet-directory'.")
 
+(defvoo nnsoup-always-save t
+  "If non nil commit the reply buffer on each message send.
+This is necessary if using message mode outside Gnus with nnsoup as a
+backend for the messages.")
+
 \f
 
 (defconst nnsoup-version "nnsoup 0.0"
@@ -81,7 +91,7 @@ The SOUP packet file name will be inserted at the %s.")
 (defvoo nnsoup-buffers nil)
 (defvoo nnsoup-current-group nil)
 (defvoo nnsoup-group-alist-touched nil)
-
+(defvoo nnsoup-article-alist nil)
 \f
 
 ;;; Interface functions.
@@ -104,7 +114,7 @@ The SOUP packet file name will be inserted at the %s.")
        ;; articles in SEQUENCE come from.
        (while (and areas sequence)
          ;; Peel off areas that are below sequence.
-         (while (and areas (< (cdaar areas) (car sequence)))
+         (while (and areas (< (cdar (car areas)) (car sequence)))
            (setq areas (cdr areas)))
          (when areas
            ;; This is a useful area.
@@ -112,7 +122,7 @@ The SOUP packet file name will be inserted at the %s.")
            (setq this-area-seq nil)
            ;; We take note whether this MSG has a corresponding IDX
            ;; for later use.
-           (when (or (= (gnus-soup-encoding-index 
+           (when (or (= (gnus-soup-encoding-index
                          (gnus-soup-area-encoding (nth 1 (car areas)))) ?n)
                      (not (file-exists-p
                            (nnsoup-file
@@ -120,14 +130,14 @@ The SOUP packet file name will be inserted at the %s.")
              (setq use-nov nil))
            ;; We assign the portion of `sequence' that is relevant to
            ;; this MSG packet to this packet.
-           (while (and sequence (<= (car sequence) (cdaar areas)))
+           (while (and sequence (<= (car sequence) (cdar (car areas))))
              (push (car sequence) this-area-seq)
              (setq sequence (cdr sequence)))
            (setcar useful-areas (cons (nreverse this-area-seq)
                                       (car useful-areas)))))
 
        ;; We now have a list of article numbers and corresponding
-       ;; areas. 
+       ;; areas.
        (setq useful-areas (nreverse useful-areas))
 
        ;; Two different approaches depending on whether all the MSG
@@ -148,7 +158,7 @@ The SOUP packet file name will be inserted at the %s.")
                  (when index-buffer
                    (insert-buffer-substring index-buffer)
                    (goto-char b)
-                   ;; We have to remove the index number entires and
+                   ;; We have to remove the index number entries and
                    ;; insert article numbers instead.
                    (while (looking-at "[0-9]+")
                      (replace-match (int-to-string number) t t)
@@ -162,7 +172,7 @@ The SOUP packet file name will be inserted at the %s.")
                  useful-areas (cdr useful-areas))
            (while articles
              (when (setq msg-buf
-                         (nnsoup-narrow-to-article 
+                         (nnsoup-narrow-to-article
                           (car articles) (cdar useful-areas) 'head))
                (goto-char (point-max))
                (insert (format "221 %d Article retrieved.\n" (car articles)))
@@ -180,7 +190,7 @@ The SOUP packet file name will be inserted at the %s.")
     (condition-case ()
        (make-directory nnsoup-directory t)
       (error t)))
-  (cond 
+  (cond
    ((not (file-exists-p nnsoup-directory))
     (nnsoup-close-server)
     (nnheader-report 'nnsoup "Couldn't create directory: %s" nnsoup-directory))
@@ -224,25 +234,25 @@ The SOUP packet file name will be inserted at the %s.")
 
 (deffoo nnsoup-request-group (group &optional server dont-check)
   (nnsoup-possibly-change-group group)
-  (if dont-check 
+  (if dont-check
       t
     (let ((active (cadr (assoc group nnsoup-group-alist))))
       (if (not active)
          (nnheader-report 'nnsoup "No such group: %s" group)
-       (nnheader-insert 
-        "211 %d %d %d %s\n" 
+       (nnheader-insert
+        "211 %d %d %d %s\n"
         (max (1+ (- (cdr active) (car active))) 0)
         (car active) (cdr active) group)))))
 
 (deffoo nnsoup-request-type (group &optional article)
   (nnsoup-possibly-change-group group)
-  ;; Try to guess the type based on the first articl ein the group.
+  ;; Try to guess the type based on the first articlin the group.
   (when (not article)
     (setq article
-         (cdaar (cddr (assoc group nnsoup-group-alist)))))
+         (cdar (car (cddr (assoc group nnsoup-group-alist))))))
   (if (not article)
       'unknown
-    (let ((kind (gnus-soup-encoding-kind 
+    (let ((kind (gnus-soup-encoding-kind
                 (gnus-soup-area-encoding
                  (nth 1 (nnsoup-article-to-area
                          article nnsoup-current-group))))))
@@ -305,29 +315,29 @@ The SOUP packet file name will be inserted at the %s.")
       (setq info (pop infolist)
            range-list (gnus-uncompress-range (car info))
            prefix (gnus-soup-area-prefix (nth 1 info)))
-      (when ;; All the articles in this file are marked for expiry.
+      (when;; All the articles in this file are marked for expiry.
          (and (or (setq mod-time (nth 5 (file-attributes
                                          (nnsoup-file prefix))))
                   (setq mod-time (nth 5 (file-attributes
                                          (nnsoup-file prefix t)))))
               (gnus-sublist-p articles range-list)
-              ;; This file is old enough. 
+              ;; This file is old enough.
               (nnmail-expired-article-p group mod-time force))
        ;; Ok, we delete this file.
        (when (ignore-errors
-               (nnheader-message 
+               (nnheader-message
                 5 "Deleting %s in group %s..." (nnsoup-file prefix)
                 group)
                (when (file-exists-p (nnsoup-file prefix))
                  (delete-file (nnsoup-file prefix)))
-               (nnheader-message 
+               (nnheader-message
                 5 "Deleting %s in group %s..." (nnsoup-file prefix t)
                 group)
                (when (file-exists-p (nnsoup-file prefix t))
                  (delete-file (nnsoup-file prefix t)))
                t)
          (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))
-         (setq articles (gnus-sorted-complement articles range-list))))
+         (setq articles (gnus-sorted-difference articles range-list))))
       (when (not mod-time)
        (setcdr (cdr total-infolist) (delq info (cddr total-infolist)))))
     (if (cddr total-infolist)
@@ -341,9 +351,11 @@ The SOUP packet file name will be inserted at the %s.")
 ;;; Internal functions
 
 (defun nnsoup-possibly-change-group (group &optional force)
-  (if group
-      (setq nnsoup-current-group group)
-    t))
+  (when (and group
+            (not (equal nnsoup-current-group group)))
+    (setq nnsoup-article-alist nil)
+    (setq nnsoup-current-group group))
+  t)
 
 (defun nnsoup-read-active-file ()
   (setq nnsoup-group-alist nil)
@@ -359,17 +371,17 @@ The SOUP packet file name will be inserted at the %s.")
          (setq min (caaar e))
          (while (cdr e)
            (setq e (cdr e)))
-         (setq max (cdaar e))
+         (setq max (cdar (car e)))
          (setcdr entry (cons (cons min max) (cdr entry)))))
       (setq nnsoup-group-alist-touched t))
     nnsoup-group-alist))
 
 (defun nnsoup-write-active-file (&optional force)
   (when (and nnsoup-group-alist
-            (or force 
+            (or force
                 nnsoup-group-alist-touched))
     (setq nnsoup-group-alist-touched nil)
-    (nnheader-temp-write nnsoup-active-file
+    (with-temp-file nnsoup-active-file
       (gnus-prin1 `(setq nnsoup-group-alist ',nnsoup-group-alist))
       (insert "\n")
       (gnus-prin1 `(setq nnsoup-current-prefix ,nnsoup-current-prefix))
@@ -378,7 +390,7 @@ The SOUP packet file name will be inserted at the %s.")
 (defun nnsoup-next-prefix ()
   "Return the next free prefix."
   (let (prefix)
-    (while (or (file-exists-p 
+    (while (or (file-exists-p
                (nnsoup-file (setq prefix (int-to-string
                                           nnsoup-current-prefix))))
               (file-exists-p (nnsoup-file prefix t)))
@@ -386,49 +398,65 @@ The SOUP packet file name will be inserted at the %s.")
     (incf nnsoup-current-prefix)
     prefix))
 
+(defun nnsoup-file-name (dir file)
+  "Return the full name of FILE (in any case) in DIR."
+  (let* ((case-fold-search t)
+        (files (directory-files dir t))
+        (regexp (concat (regexp-quote file) "$")))
+    (car (delq nil
+              (mapcar
+               (lambda (file)
+                 (if (string-match regexp file)
+                     file
+                   nil))
+               files)))))
+
 (defun nnsoup-read-areas ()
-  (when (file-exists-p (concat nnsoup-tmp-directory "AREAS"))
-    (save-excursion
-      (set-buffer nntp-server-buffer)
-      (let ((areas (gnus-soup-parse-areas 
-                   (concat nnsoup-tmp-directory "AREAS")))
-           entry number area lnum cur-prefix file)
-       ;; Go through all areas in the new AREAS file.
-       (while (setq area (pop areas))
-         ;; Change the name to the permanent name and move the files.
-         (setq cur-prefix (nnsoup-next-prefix))
-         (message "Incorporating file %s..." cur-prefix)
-         (when (file-exists-p 
-                (setq file (concat nnsoup-tmp-directory
-                                   (gnus-soup-area-prefix area) ".IDX")))
-           (rename-file file (nnsoup-file cur-prefix)))
-         (when (file-exists-p 
-                (setq file (concat nnsoup-tmp-directory 
-                                   (gnus-soup-area-prefix area) ".MSG")))
-           (rename-file file (nnsoup-file cur-prefix t))
-           (gnus-soup-set-area-prefix area cur-prefix)
-           ;; Find the number of new articles in this area.
-           (setq number (nnsoup-number-of-articles area))
-           (if (not (setq entry (assoc (gnus-soup-area-name area)
-                                       nnsoup-group-alist)))
-               ;; If this is a new area (group), we just add this info to
-               ;; the group alist. 
-               (push (list (gnus-soup-area-name area)
-                           (cons 1 number)
-                           (list (cons 1 number) area))
-                     nnsoup-group-alist)
-             ;; There are already articles in this group, so we add this
-             ;; info to the end of the entry.
-             (nconc entry (list (list (cons (1+ (setq lnum (cdadr entry)))
-                                            (+ lnum number))
-                                      area)))
-             (setcdr (cadr entry) (+ lnum number))))))
-      (nnsoup-write-active-file t)
-      (delete-file (concat nnsoup-tmp-directory "AREAS")))))
+  (let ((areas-file (nnsoup-file-name nnsoup-tmp-directory "areas")))
+    (when areas-file
+      (save-excursion
+       (set-buffer nntp-server-buffer)
+       (let ((areas (gnus-soup-parse-areas areas-file))
+             entry number area lnum cur-prefix file)
+         ;; Go through all areas in the new AREAS file.
+         (while (setq area (pop areas))
+           ;; Change the name to the permanent name and move the files.
+           (setq cur-prefix (nnsoup-next-prefix))
+           (nnheader-message 5 "Incorporating file %s..." cur-prefix)
+           (when (file-exists-p
+                  (setq file
+                        (expand-file-name
+                         (concat (gnus-soup-area-prefix area) ".IDX")
+                         nnsoup-tmp-directory)))
+             (rename-file file (nnsoup-file cur-prefix)))
+           (when (file-exists-p
+                  (setq file (expand-file-name
+                              (concat (gnus-soup-area-prefix area) ".MSG")
+                              nnsoup-tmp-directory)))
+             (rename-file file (nnsoup-file cur-prefix t))
+             (gnus-soup-set-area-prefix area cur-prefix)
+             ;; Find the number of new articles in this area.
+             (setq number (nnsoup-number-of-articles area))
+             (if (not (setq entry (assoc (gnus-soup-area-name area)
+                                         nnsoup-group-alist)))
+                 ;; If this is a new area (group), we just add this info to
+                 ;; the group alist.
+                 (push (list (gnus-soup-area-name area)
+                             (cons 1 number)
+                             (list (cons 1 number) area))
+                       nnsoup-group-alist)
+               ;; There are already articles in this group, so we add this
+               ;; info to the end of the entry.
+               (nconc entry (list (list (cons (1+ (setq lnum (cdadr entry)))
+                                              (+ lnum number))
+                                        area)))
+               (setcdr (cadr entry) (+ lnum number))))))
+       (nnsoup-write-active-file t)
+       (delete-file areas-file)))))
 
 (defun nnsoup-number-of-articles (area)
   (save-excursion
-    (cond 
+    (cond
      ;; If the number is in the area info, we just return it.
      ((gnus-soup-area-number area)
       (gnus-soup-area-number area))
@@ -437,32 +465,90 @@ The SOUP packet file name will be inserted at the %s.")
       (set-buffer (nnsoup-index-buffer (gnus-soup-area-prefix area)))
       (count-lines (point-min) (point-max)))
      ;; We do it the hard way - re-searching through the message
-     ;; buffer. 
+     ;; buffer.
      (t
       (set-buffer (nnsoup-message-buffer (gnus-soup-area-prefix area)))
-      (goto-char (point-min))
-      (let ((regexp (nnsoup-header (gnus-soup-encoding-format 
-                                   (gnus-soup-area-encoding area))))
-           (num 0))
-       (while (re-search-forward regexp nil t)
-         (setq num (1+ num)))
-       num)))))
+      (unless (assoc (gnus-soup-area-prefix area) nnsoup-article-alist)
+       (nnsoup-dissect-buffer area))
+      (length (cdr (assoc (gnus-soup-area-prefix area)
+                         nnsoup-article-alist)))))))
+
+(defun nnsoup-dissect-buffer (area)
+  (let ((mbox-delim (concat "^" message-unix-mail-delimiter))
+       (format (gnus-soup-encoding-format (gnus-soup-area-encoding area)))
+       (i 0)
+       alist len)
+    (goto-char (point-min))
+    (cond
+     ;; rnews batch format
+     ((or (= format ?u)
+         (= format ?n)) ;; Gnus back compatibility.
+      (while (looking-at "^#! *rnews \\(+[0-9]+\\) *$")
+       (forward-line 1)
+       (push (list
+              (incf i) (point)
+              (progn
+                (forward-char (string-to-number (match-string 1)))
+                (point)))
+             alist)))
+     ;; Unix mbox format
+     ((= format ?m)
+      (while (looking-at mbox-delim)
+       (forward-line 1)
+       (push (list
+              (incf i) (point)
+              (progn
+                (if (re-search-forward mbox-delim nil t)
+                    (beginning-of-line)
+                  (goto-char (point-max)))
+                (point)))
+             alist)))
+     ;; MMDF format
+     ((= format ?M)
+      (while (looking-at "\^A\^A\^A\^A\n")
+       (forward-line 1)
+       (push (list
+              (incf i) (point)
+              (progn
+                (if (search-forward "\n\^A\^A\^A\^A\n" nil t)
+                    (beginning-of-line)
+                  (goto-char (point-max)))
+                (point)))
+             alist)))
+     ;; Binary format
+     ((or (= format ?B) (= format ?b))
+      (while (not (eobp))
+       (setq len (+ (* (char-after (point)) (expt 2.0 24))
+                    (* (char-after (+ (point) 1)) (expt 2 16))
+                    (* (char-after (+ (point) 2)) (expt 2 8))
+                    (char-after (+ (point) 3))))
+       (push (list
+              (incf i) (+ (point) 4)
+              (progn
+                (forward-char (floor (+ len 4)))
+                (point)))
+             alist)))
+     (t
+      (error "Unknown format: %c" format)))
+    (push (cons (gnus-soup-area-prefix area) alist) nnsoup-article-alist)))
 
 (defun nnsoup-index-buffer (prefix &optional message)
   (let* ((file (concat prefix (if message ".MSG" ".IDX")))
         (buffer-name (concat " *nnsoup " file "*")))
     (or (get-buffer buffer-name)       ; File already loaded.
-       (when (file-exists-p (concat nnsoup-directory file))
+       (when (file-exists-p (expand-file-name file nnsoup-directory))
          (save-excursion               ; Load the file.
            (set-buffer (get-buffer-create buffer-name))
-           (buffer-disable-undo (current-buffer))
+           (buffer-disable-undo)
            (push (cons nnsoup-current-group (current-buffer)) nnsoup-buffers)
-           (nnheader-insert-file-contents (concat nnsoup-directory file))
+           (nnheader-insert-file-contents
+            (expand-file-name file nnsoup-directory))
            (current-buffer))))))
 
 (defun nnsoup-file (prefix &optional message)
   (expand-file-name
-   (concat nnsoup-directory prefix (if message ".MSG" ".IDX"))))
+   (concat prefix (if message ".MSG" ".IDX"))
+   nnsoup-directory))
 
 (defun nnsoup-message-buffer (prefix)
   (nnsoup-index-buffer prefix 'msg))
@@ -473,13 +559,13 @@ The SOUP packet file name will be inserted at the %s.")
                  nnsoup-packet-directory t nnsoup-packet-regexp))
        packet)
     (while (setq packet (pop packets))
-      (message "nnsoup: unpacking %s..." packet)
-      (if (not (gnus-soup-unpack-packet 
+      (nnheader-message 5 "nnsoup: unpacking %s..." packet)
+      (if (not (gnus-soup-unpack-packet
                nnsoup-tmp-directory nnsoup-unpacker packet))
-         (message "Couldn't unpack %s" packet)
+         (nnheader-message 5 "Couldn't unpack %s" packet)
        (delete-file packet)
        (nnsoup-read-areas)
-       (message "Unpacking...done")))))
+       (nnheader-message 5 "Unpacking...done")))))
 
 (defun nnsoup-narrow-to-article (article &optional area head)
   (let* ((area (or area (nnsoup-article-to-area article nnsoup-current-group)))
@@ -492,9 +578,9 @@ The SOUP packet file name will be inserted at the %s.")
         ;; There is no MSG file.
         ((null msg-buf)
          nil)
-        ;; We use the index file to find out where the article 
-        ;; begins and ends. 
-        ((and (= (gnus-soup-encoding-index 
+        ;; We use the index file to find out where the article
+        ;; begins and ends.
+        ((and (= (gnus-soup-encoding-index
                   (gnus-soup-area-encoding (nth 1 area)))
                  ?c)
               (file-exists-p (nnsoup-file prefix)))
@@ -512,7 +598,7 @@ The SOUP packet file name will be inserted at the %s.")
                (let ((format (gnus-soup-encoding-format
                               (gnus-soup-area-encoding (nth 1 area)))))
                  (goto-char end)
-                 (when (or (= format ?n) (= format ?m))
+                 (when (or (= format ?u) (= format ?n) (= format ?m))
                    (setq end (progn (forward-line -1) (point))))))
            (set-buffer msg-buf))
          (widen)
@@ -520,16 +606,14 @@ The SOUP packet file name will be inserted at the %s.")
         (t
          (set-buffer msg-buf)
          (widen)
-         (goto-char (point-min))
-         (let ((header (nnsoup-header 
-                        (gnus-soup-encoding-format 
-                         (gnus-soup-area-encoding (nth 1 area))))))
-           (re-search-forward header nil t (- article (caar area)))
-           (narrow-to-region
-            (match-beginning 0)
-            (if (re-search-forward header nil t)
-                (match-beginning 0)
-              (point-max))))))
+         (unless (assoc (gnus-soup-area-prefix (nth 1 area))
+                        nnsoup-article-alist)
+           (nnsoup-dissect-buffer (nth 1 area)))
+         (let ((entry (assq article (cdr (assoc (gnus-soup-area-prefix
+                                                 (nth 1 area))
+                                                nnsoup-article-alist)))))
+           (when entry
+             (narrow-to-region (cadr entry) (caddr entry))))))
 &nb