* many files: Remove trailing whitespaces, replace spc+tab with
[gnus] / lisp / nndoc.el
index 0092ac0..843dabf 100644 (file)
@@ -1,8 +1,9 @@
 ;;; nndoc.el --- single file access for Gnus
-;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
+;;        Free Software Foundation, Inc.
 
 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
-;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
+;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
 
 ;;; Commentary:
 
+;; For Outlook mail boxes format, see http://mbx2mbox.sourceforge.net/
+
 ;;; Code:
 
 (require 'nnheader)
 (require 'message)
 (require 'nnmail)
 (require 'nnoo)
+(require 'gnus-util)
+(require 'mm-util)
 (eval-when-compile (require 'cl))
 
 (nnoo-declare nndoc)
@@ -37,8 +42,9 @@
 (defvoo nndoc-article-type 'guess
   "*Type of the file.
 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
-`rfc934', `rfc822-forward', `mime-digest', `standard-digest',
-`slack-digest', `clari-briefs' or `guess'.")
+`rfc934', `rfc822-forward', `mime-parts', `standard-digest',
+`slack-digest', `clari-briefs', `nsmail', `outlook', `oe-dbx',
+`mailman', `exim-bounce', or `guess'.")
 
 (defvoo nndoc-post-type 'mail
   "*Whether the nndoc group is `mail' or `post'.")
@@ -46,12 +52,17 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 (defvoo nndoc-open-document-hook 'nnheader-ms-strip-cr
   "Hook run after opening a document.
 The default function removes all trailing carriage returns
-from the document.")  
+from the document.")
 
 (defvar nndoc-type-alist
   `((mmdf
      (article-begin .  "^\^A\^A\^A\^A\n")
      (body-end .  "^\^A\^A\^A\^A\n"))
+    (exim-bounce
+     (article-begin . "^------ This is a copy of the message, including all the headers. ------\n\n")
+     (body-end-function . nndoc-exim-bounce-body-end-function))
+    (nsmail
+     (article-begin .  "^From - "))
     (news
      (article-begin . "^Path:"))
     (rnews
@@ -65,14 +76,14 @@ from the document.")
      (body-end . "\^_")
      (body-begin-function . nndoc-babyl-body-begin)
      (head-begin-function . nndoc-babyl-head-begin))
-    (forward
-     (article-begin . "^-+ Start of forwarded message -+\n+")
-     (body-end . "^-+ End of forwarded message -+$")
-     (prepare-body-function . nndoc-unquote-dashes))
     (rfc934
      (article-begin . "^--.*\n+")
      (body-end . "^--.*$")
      (prepare-body-function . nndoc-unquote-dashes))
+    (mailman
+     (article-begin . "^--__--__--\n\nMessage:")
+     (body-end . "^--__--__--$")
+     (prepare-body-function . nndoc-unquote-dashes))
     (clari-briefs
      (article-begin . "^ \\*")
      (body-end . "^\t------*[ \t]^*\n^ \\*")
@@ -82,10 +93,14 @@ from the document.")
      (article-transform-function . nndoc-transform-clari-briefs))
     (mime-digest
      (article-begin . "")
+     (head-begin . "^ ?\n")
      (head-end . "^ ?$")
      (body-end . "")
      (file-end . "")
      (subtype digest guess))
+    (mime-parts
+     (generate-head-function . nndoc-generate-mime-parts-head)
+     (article-transform-function . nndoc-transform-mime-parts))
     (standard-digest
      (first-article . ,(concat "^" (make-string 70 ?-) "\n *\n+"))
      (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n *\n+"))
@@ -116,6 +131,19 @@ from the document.")
     (rfc822-forward
      (article-begin . "^\n")
      (body-end-function . nndoc-rfc822-forward-body-end-function))
+    (outlook
+     (article-begin-function . nndoc-outlook-article-begin)
+     (body-end .  "\0"))
+    (oe-dbx  ;; Outlook Express DBX format
+     (dissection-function . nndoc-oe-dbx-dissection)
+     (generate-head-function . nndoc-oe-dbx-generate-head)
+     (generate-article-function . nndoc-oe-dbx-generate-article))
+    (forward
+     (article-begin . "^-+ \\(Start of \\)?forwarded message.*\n+")
+     (body-end . "^-+ End \\(of \\)?forwarded message.*$")
+     (prepare-body-function . nndoc-unquote-dashes))
+    (mail-in-mail ;; Wild guess on mailer daemon's messages or others
+     (article-begin-function . nndoc-mail-in-mail-article-begin))
     (guess
      (guess . t)
      (subtype nil))
@@ -126,11 +154,12 @@ from the document.")
      (guess . t)
      (subtype nil))))
 
-\f
+(defvar nndoc-binary-file-names ".[Dd][Bb][Xx]$"
+  "Regexp for binary nndoc file names.")
 
+\f
 (defvoo nndoc-file-begin nil)
 (defvoo nndoc-first-article nil)
-(defvoo nndoc-article-end nil)
 (defvoo nndoc-article-begin nil)
 (defvoo nndoc-head-begin nil)
 (defvoo nndoc-head-end nil)
@@ -140,11 +169,21 @@ from the document.")
 (defvoo nndoc-body-begin-function nil)
 (defvoo nndoc-head-begin-function nil)
 (defvoo nndoc-body-end nil)
+;; nndoc-dissection-alist is a list of sublists.  Each sublist holds the
+;; following items.  ARTICLE acts as the association key and is an ordinal
+;; starting at 1.  HEAD-BEGIN [0], HEAD-END [1], BODY-BEGIN [2] and BODY-END
+;; [3] are positions in the `nndoc' buffer.  LINE-COUNT [4] is a count of
+;; lines in the body.  For MIME dissections only, ARTICLE-INSERT [5] and
+;; SUMMARY-INSERT [6] give headers to insert for full article or summary line
+;; generation, respectively.  Other headers usually follow directly from the
+;; buffer.  Value `nil' means no insert.
 (defvoo nndoc-dissection-alist nil)
 (defvoo nndoc-prepare-body-function nil)
 (defvoo nndoc-generate-head-function nil)
 (defvoo nndoc-article-transform-function nil)
 (defvoo nndoc-article-begin-function nil)
+(defvoo nndoc-generate-article-function nil)
+(defvoo nndoc-dissection-function nil)
 
 (defvoo nndoc-status-string "")
 (defvoo nndoc-group-alist nil)
@@ -178,7 +217,7 @@ from the document.")
                (insert-buffer-substring
                 nndoc-current-buffer (car entry) (nth 1 entry)))
              (goto-char (point-max))
-             (unless (= (char-after (1- (point))) ?\n)
+             (unless (eq (char-after (1- (point))) ?\n)
                (insert "\n"))
              (insert (format "Lines: %d\n" (nth 4 entry)))
              (insert ".\n")))
@@ -195,8 +234,11 @@ from the document.")
       (set-buffer buffer)
       (erase-buffer)
       (when entry
-       (if (stringp article)
-           nil
+       (cond
+        ((stringp article) nil)
+        (nndoc-generate-article-function
+         (funcall nndoc-generate-article-function article))
+        (t
          (insert-buffer-substring
           nndoc-current-buffer (car entry) (nth 1 entry))
          (insert "\n")
@@ -208,7 +250,7 @@ from the document.")
            (funcall nndoc-prepare-body-function))
          (when nndoc-article-transform-function
            (funcall nndoc-article-transform-function article))
-         t)))))
+         t))))))
 
 (deffoo nndoc-request-group (group &optional server dont-check)
   "Select news GROUP."
@@ -228,8 +270,8 @@ from the document.")
 
 (deffoo nndoc-request-type (group &optional article)
   (cond ((not article) 'unknown)
-        (nndoc-post-type nndoc-post-type)
-        (t 'unknown)))
+       (nndoc-post-type nndoc-post-type)
+       (t 'unknown)))
 
 (deffoo nndoc-close-group (group &optional server)
   (nndoc-possibly-change-buffer group server)
@@ -280,19 +322,24 @@ from the document.")
       (setq nndoc-dissection-alist nil)
       (save-excursion
        (set-buffer nndoc-current-buffer)
-       (buffer-disable-undo (current-buffer))
        (erase-buffer)
-       (if (stringp nndoc-address)
-           (nnheader-insert-file-contents nndoc-address)
-         (insert-buffer-substring nndoc-address))
-       (run-hooks 'nndoc-open-document-hook))))
+       (if (and (stringp nndoc-address)
+                (string-match nndoc-binary-file-names nndoc-address))
+           (let ((coding-system-for-read 'binary))
+             (mm-insert-file-contents nndoc-address))
+         (if (stringp nndoc-address)
+             (nnheader-insert-file-contents nndoc-address)
+           (insert-buffer-substring nndoc-address))
+         (run-hooks 'nndoc-open-document-hook)))))
     ;; Initialize the nndoc structures according to this new document.
     (when (and nndoc-current-buffer
               (not nndoc-dissection-alist))
       (save-excursion
        (set-buffer nndoc-current-buffer)
        (nndoc-set-delims)
-       (nndoc-dissect-buffer)))
+       (if (eq nndoc-article-type 'mime-parts)
+           (nndoc-dissect-mime-parts)
+         (nndoc-dissect-buffer))))
     (unless nndoc-current-buffer
       (nndoc-close-server))
     ;; Return whether we managed to select a file.
@@ -306,12 +353,15 @@ from the document.")
   "Set the nndoc delimiter variables according to the type of the document."
   (let ((vars '(nndoc-file-begin
                nndoc-first-article
-               nndoc-article-end nndoc-head-begin nndoc-head-end
+               nndoc-article-begin-function
+               nndoc-head-begin nndoc-head-end
                nndoc-file-end nndoc-article-begin
                nndoc-body-begin nndoc-body-end-function nndoc-body-end
                nndoc-prepare-body-function nndoc-article-transform-function
                nndoc-generate-head-function nndoc-body-begin-function
-               nndoc-head-begin-function)))
+               nndoc-head-begin-function
+               nndoc-generate-article-function
+               nndoc-dissection-function)))
     (while vars
       (set (pop vars) nil)))
   (let (defs)
@@ -331,6 +381,9 @@ from the document.")
                (setq entry (pop alist)))
       (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
        (goto-char (point-min))
+       ;; Remove blank lines.
+       (while (eq (following-char) ?\n)
+         (delete-char 1))
        (when (numberp (setq result (funcall (intern
                                              (format "nndoc-%s-type-p"
                                                      (car entry))))))
@@ -413,10 +466,9 @@ from the document.")
     t))
 
 (defun nndoc-forward-type-p ()
-  (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
-            (not (re-search-forward "^Subject:.*digest" nil t))
-            (not (re-search-backward "^From:" nil t 2))
-            (not (re-search-forward "^From:" nil t 2)))
+  (when (and (re-search-forward "^-+ \\(Start of \\)?forwarded message.*\n+"
+                               nil t)
+            (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:"))
     t))
 
 (defun nndoc-rfc934-type-p ()
@@ -426,6 +478,10 @@ from the document.")
             (not (re-search-forward "^From:" nil t 2)))
     t))
 
+(defun nndoc-mailman-type-p ()
+  (when (re-search-forward "^--__--__--\n+" nil t)
+    t))
+
 (defun nndoc-rfc822-forward-type-p ()
   (save-restriction
     (message-narrow-to-head)
@@ -435,6 +491,36 @@ from the document.")
 (defun nndoc-rfc822-forward-body-end-function ()
   (goto-char (point-max)))
 
+(defun nndoc-mime-parts-type-p ()
+  (let ((case-fold-search t)
+       (limit (search-forward "\n\n" nil t)))
+    (goto-char (point-min))
+    (when (and