* gnus-art.el (gnus-mime-copy-part): Don't decode compressed parts.
[gnus] / lisp / nndoc.el
index 016c35d..47a3cbd 100644 (file)
@@ -1,8 +1,9 @@
 ;;; nndoc.el --- single file access for Gnus
 ;;; nndoc.el --- single file access for Gnus
-;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
+;;        Free Software Foundation, Inc.
 
 
-;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
-;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
+;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
+;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
 
 ;;; Commentary:
 
 
 ;;; Commentary:
 
+;; For Outlook mail boxes format, see http://mbx2mbox.sourceforge.net/
+
 ;;; Code:
 
 (require 'nnheader)
 (require 'message)
 (require 'nnmail)
 (require 'nnoo)
 ;;; Code:
 
 (require 'nnheader)
 (require 'message)
 (require 'nnmail)
 (require 'nnoo)
+(require 'gnus-util)
+(require 'mm-util)
 (eval-when-compile (require 'cl))
 
 (nnoo-declare nndoc)
 (eval-when-compile (require 'cl))
 
 (nnoo-declare nndoc)
 (defvoo nndoc-article-type 'guess
   "*Type of the file.
 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 (defvoo nndoc-article-type 'guess
   "*Type of the file.
 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
-`rfc934', `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'.")
 
 
 (defvoo nndoc-post-type 'mail
   "*Whether the nndoc group is `mail' or `post'.")
 
+(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.")
+
 (defvar nndoc-type-alist
   `((mmdf
      (article-begin .  "^\^A\^A\^A\^A\n")
      (body-end .  "^\^A\^A\^A\^A\n"))
 (defvar nndoc-type-alist
   `((mmdf
      (article-begin .  "^\^A\^A\^A\^A\n")
      (body-end .  "^\^A\^A\^A\^A\n"))
+    (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))
+    (nsmail
+     (article-begin .  "^From - "))
     (news
      (article-begin . "^Path:"))
     (rnews
     (news
      (article-begin . "^Path:"))
     (rnews
@@ -60,14 +83,17 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
      (body-end . "\^_")
      (body-begin-function . nndoc-babyl-body-begin)
      (head-begin-function . nndoc-babyl-head-begin))
      (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))
+    (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))
     (rfc934
      (article-begin . "^--.*\n+")
      (body-end . "^--.*$")
      (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^ \\*")
     (clari-briefs
      (article-begin . "^ \\*")
      (body-end . "^\t------*[ \t]^*\n^ \\*")
@@ -75,19 +101,14 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
      (head-end . "^\t")
      (generate-head-function . nndoc-generate-clari-briefs-head)
      (article-transform-function . nndoc-transform-clari-briefs))
      (head-end . "^\t")
      (generate-head-function . nndoc-generate-clari-briefs-head)
      (article-transform-function . nndoc-transform-clari-briefs))
-    (mime-digest
-     (article-begin . "")
-     (head-end . "^ ?$")
-     (body-end . "")
-     (file-end . "")
-     (subtype digest guess))
+    
     (standard-digest
     (standard-digest
-     (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
-     (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n\n+"))
+     (first-article . ,(concat "^" (make-string 70 ?-) "\n *\n+"))
+     (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n *\n+"))
      (prepare-body-function . nndoc-unquote-dashes)
      (body-end-function . nndoc-digest-body-end)
      (prepare-body-function . nndoc-unquote-dashes)
      (body-end-function . nndoc-digest-body-end)
-     (head-end . "^ ?$")
-     (body-begin . "^ ?\n")
+     (head-end . "^ *$")
+     (body-begin . "^ *\n")
      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
      (subtype digest guess))
     (slack-digest
      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
      (subtype digest guess))
     (slack-digest
@@ -103,14 +124,29 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
      (head-begin . "^Paper.*:")
      (head-end   . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
      (body-begin . "")
      (head-begin . "^Paper.*:")
      (head-end   . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
      (body-begin . "")
-     (body-end   . "-------------------------------------------------")
-     (file-end   . "^Title: Recent Seminal")
+     (body-end   . "\\(-------------------------------------------------\\|%-%-%-%-%-%-%-%-%-%-%-%-%-%-\\|%%--%%--%%--%%--%%--%%--%%--%%--\\|%%%---%%%---%%%---%%%---\\)")
+     (file-end   . "\\(^Title: Recent Seminal\\|%%%---%%%---%%%---%%%---\\)")
      (generate-head-function . nndoc-generate-lanl-gov-head)
      (article-transform-function . nndoc-transform-lanl-gov-announce)
      (subtype preprints guess))
      (generate-head-function . nndoc-generate-lanl-gov-head)
      (article-transform-function . nndoc-transform-lanl-gov-announce)
      (subtype preprints guess))
-    (x400-forward
-     (article-begin . "^\n")
-     (body-end-function . nndoc-x400-forward-body-end-function))
+    (rfc822-forward
+     (article-begin . "^\n+")
+     (body-end-function . nndoc-rfc822-forward-body-end-function)
+     (generate-head-function . nndoc-rfc822-forward-generate-head)
+     (generate-article-function . nndoc-rfc822-forward-generate-article))
+    (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))
     (guess
      (guess . t)
      (subtype nil))
@@ -121,11 +157,12 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
      (guess . t)
      (subtype nil))))
 
      (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-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)
 (defvoo nndoc-article-begin nil)
 (defvoo nndoc-head-begin nil)
 (defvoo nndoc-head-end nil)
@@ -135,11 +172,21 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 (defvoo nndoc-body-begin-function nil)
 (defvoo nndoc-head-begin-function nil)
 (defvoo nndoc-body-end nil)
 (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-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)
 
 (defvoo nndoc-status-string "")
 (defvoo nndoc-group-alist nil)
@@ -173,7 +220,7 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
                (insert-buffer-substring
                 nndoc-current-buffer (car entry) (nth 1 entry)))
              (goto-char (point-max))
                (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")))
                (insert "\n"))
              (insert (format "Lines: %d\n" (nth 4 entry)))
              (insert ".\n")))
@@ -190,8 +237,11 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
       (set-buffer buffer)
       (erase-buffer)
       (when entry
       (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")
          (insert-buffer-substring
           nndoc-current-buffer (car entry) (nth 1 entry))
          (insert "\n")
@@ -203,7 +253,7 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
            (funcall nndoc-prepare-body-function))
          (when nndoc-article-transform-function
            (funcall nndoc-article-transform-function article))
            (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."
 
 (deffoo nndoc-request-group (group &optional server dont-check)
   "Select news GROUP."
@@ -223,8 +273,8 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 
 (deffoo nndoc-request-type (group &optional article)
   (cond ((not article) 'unknown)
 
 (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)
 
 (deffoo nndoc-close-group (group &optional server)
   (nndoc-possibly-change-buffer group server)
@@ -275,18 +325,24 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
       (setq nndoc-dissection-alist nil)
       (save-excursion
        (set-buffer nndoc-current-buffer)
       (setq nndoc-dissection-alist nil)
       (save-excursion
        (set-buffer nndoc-current-buffer)
-       (buffer-disable-undo (current-buffer))
        (erase-buffer)
        (erase-buffer)
-       (if (stringp nndoc-address)
-           (nnheader-insert-file-contents nndoc-address)
-         (insert-buffer-substring nndoc-address)))))
+       (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)
     ;; 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.
     (unless nndoc-current-buffer
       (nndoc-close-server))
     ;; Return whether we managed to select a file.
@@ -300,12 +356,15 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
   "Set the nndoc delimiter variables according to the type of the document."
   (let ((vars '(nndoc-file-begin
                nndoc-first-article
   "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-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)
     (while vars
       (set (pop vars) nil)))
   (let (defs)
@@ -325,6 +384,9 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
                (setq entry (pop alist)))
       (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
        (goto-char (point-min))
                (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))))))
        (when (numberp (setq result (funcall (intern
                                              (format "nndoc-%s-type-p"
                                                      (car entry))))))
@@ -334,7 +396,7 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
       (error "Document is not of any recognized type"))
     (if result
        (car entry)
       (error "Document is not of any recognized type"))
     (if result
        (car entry)
-      (cadar (sort results (lambda (r1 r2) (< (car r1) (car r2))))))))
+      (cadar (last (sort results 'car-less-than-car))))))
 
 ;;;
 ;;; Built-in type predicates and functions
 
 ;;;
 ;;; Built-in type predicates and functions
@@ -390,7 +452,7 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 
 (defun nndoc-babyl-body-begin ()
   (re-search-forward "^\n" nil t)
 
 (defun nndoc-babyl-body-begin ()
   (re-search-forward "^\n" nil t)
-  (when (looking-at "\*\*\* EOOH \*\*\*")
+  (when (looking-at "\