Remove <a name...> tags, which confuses the rest of the function.
[gnus] / lisp / nndoc.el
index eecfd61..ddeac7f 100644 (file)
@@ -1,16 +1,18 @@
 ;;; nndoc.el --- single file 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,
+;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+
+;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
+;;     Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, 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
 ;; 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, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; 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)
 (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'.")
 
-(defvar nndoc-type-alist 
-  `((mmdf 
+(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"))
+    (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
      (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
      (body-end-function . nndoc-rnews-body-end))
-    (mbox 
+    (mbox
      (article-begin-function . nndoc-mbox-article-begin)
      (body-end-function . nndoc-mbox-body-end))
-    (babyl 
+    (babyl
      (article-begin . "\^_\^L *\n")
      (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 . "^-.*$")
+     (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 . "^ \\*")
@@ -75,19 +100,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))
-    (mime-digest
-     (article-begin . "")
-     (head-end . "^ ?$")
-     (body-end . "")
-     (file-end . "")
-     (subtype digest guess))
+
     (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)
-     (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
@@ -98,17 +118,45 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
      (file-end . "^End of")
      (prepare-body-function . nndoc-unquote-dashes)
      (subtype digest guess))
+    (google
+     (pre-dissection-function . nndoc-decode-content-transfer-encoding)
+     (article-begin . "^== [0-9]+ of [0-9]+ ==$")
+     (head-begin . "^Date:")
+     (head-end . "^$")
+     (body-end-function . nndoc-digest-body-end)
+     (body-begin . "^$")
+     (file-end . "^==============================================================================$")
+     (prepare-body-function . nndoc-unquote-dashes)
+     (subtype digest guess))
     (lanl-gov-announce
-      (article-begin . "^\\\\\\\\\n")
-      (head-begin . "^Paper.*:")
-      (head-end   . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
-      (body-begin . "")
-      (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))
-    (guess 
+     (article-begin . "^\\\\\\\\\n")
+     (head-begin . "^\\(Paper.*:\\|arXiv:\\)")
+     (head-end   . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
+     (body-begin . "")
+     (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))
+    (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))
     (digest
@@ -116,14 +164,14 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
      (subtype nil))
     (preprints
      (guess . t)
-     (subtype nil))
-    ))
+     (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)
@@ -133,11 +181,22 @@ 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)
+;; 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-pre-dissection-function nil)
 
 (defvoo nndoc-status-string "")
 (defvoo nndoc-group-alist nil)
@@ -171,7 +230,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))
-             (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")))
@@ -188,25 +247,28 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
       (set-buffer buffer)
       (erase-buffer)
       (when entry
-       (if (stringp article)
-           nil
-         (insert-buffer-substring 
+       (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")
          (setq beg (point))
-         (insert-buffer-substring 
+         (insert-buffer-substring
           nndoc-current-buffer (nth 2 entry) (nth 3 entry))
          (goto-char beg)
          (when nndoc-prepare-body-function
            (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."
   (let (number)
-    (cond 
+    (cond
      ((not (nndoc-possibly-change-buffer group server))
       (nnheader-report 'nndoc "No such file or buffer: %s"
                       nndoc-address))
@@ -221,8 +283,8 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 
 (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)
@@ -250,41 +312,47 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 
 (defun nndoc-possibly-change-buffer (group source)
   (let (buf)
-    (cond 
+    (cond
      ;; The current buffer is this group's buffer.
      ((and nndoc-current-buffer
           (buffer-name nndoc-current-buffer)
-          (eq nndoc-current-buffer 
+          (eq nndoc-current-buffer
               (setq buf (cdr (assoc group nndoc-group-alist))))))
      ;; We change buffers by taking an old from the group alist.
-     ;; `source' is either a string (a file name) or a buffer object. 
+     ;; `source' is either a string (a file name) or a buffer object.
      (buf
       (setq nndoc-current-buffer buf))
-     ;; It's a totally new group.    
+     ;; It's a totally new group.
      ((or (and (bufferp nndoc-address)
               (buffer-name nndoc-address))
          (and (stringp nndoc-address)
               (file-exists-p nndoc-address)
               (not (file-directory-p nndoc-address))))
-      (push (cons group (setq nndoc-current-buffer 
-                             (get-buffer-create 
+      (push (cons group (setq nndoc-current-buffer
+                             (get-buffer-create
                               (concat " *nndoc " group "*"))))
            nndoc-group-alist)
       (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)))))
+       (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.
@@ -296,19 +364,23 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 
 (defun nndoc-set-delims ()
   "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
+  (let ((vars '(nndoc-file-begin
+               nndoc-first-article
+               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
+               nndoc-pre-dissection-function)))
     (while vars
       (set (pop vars) nil)))
   (let (defs)
     ;; Guess away until we find the real file type.
-    (while (assq 'guess (setq defs (cdr (assq nndoc-article-type 
+    (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
                                              nndoc-type-alist))))
       (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
     ;; Set the nndoc variables.
@@ -323,8 +395,11 @@ 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))
+       ;; Remove blank lines.
+       (while (eq (following-char) ?\n)
+         (delete-char 1))
        (when (numberp (setq result (funcall (intern
-                                             (format "nndoc-%s-type-p" 
+                                             (format "nndoc-%s-type-p"
                                                      (car entry))))))
          (push (cons result entry) results)
          (setq result nil))))
@@ -332,9 +407,9 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
       (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
 ;;;
 
@@ -351,13 +426,13 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
        len end)
     (when
        (save-excursion
-         (and (re-search-backward 
+         (and (re-search-backward
                (concat "^" message-unix-mail-delimiter) nil t)
               (setq end (point))
               (search-forward "\n\n" beg t)
               (re-search-backward
                "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
-              (setq len (string-to-int (match-string 1)))
+              (setq len (string-to-number (match-string 1)))
               (search-forward "\n\n" beg t)
               (unless (= (setq len (+ (point) len)) (point-max))
                 (and (< len (point-max))
@@ -380,7 +455,23 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 (defun nndoc-rnews-body-end ()
   (and (re-search-backward nndoc-article-begin nil t)
        (forward-line 1)
-       (goto-char (+ (point) (string-to-int (match-string 1))))))
+       (goto-char (+ (point) (string-to-number (match-string 1))))))
+
+(defun nndoc-google-type-p ()
+  (when (re-search-forward "^=3D=3D 1 of [0-9]+ =3D=3D$" nil t)
+    t))
+
+(defun nndoc-decode-content-transfer-encoding ()
+  (let ((encoding
+        (save-restriction
+          (message-narrow-to-head)
+          (message-fetch-field "content-transfer-encoding"))))
+    (when (and encoding
+              (search-forward "\n\n" nil t))
+      (save-restriction
+       (narrow-to-region (point) (point-max))
+       (mm-decode-content-transfer-encoding
+        (intern (downcase (mail-header-strip encoding))))))))
 
 (defun nndoc-babyl-type-p ()
   (when (re-search-forward "\^_\^L *\n" nil t)
@@ -388,7 +479,7 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 
 (defun nndoc-babyl-body-begin ()
   (re-search-forward "^\n" nil t)
-  (when (looking-at "\*\*\* EOOH \*\*\*")
+  (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
     (let ((next (or (save-excursion
                      (re-search-forward nndoc-article-begin nil t))
                    (point-max))))
@@ -400,15 +491,14 @@ One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
 
 (defun nndoc-babyl-head-begin ()
   (when (re-search-forward "^[0-9].*\n" nil t)
-    (when (looking-at "\*\*\* EOOH \*\*\*")
+    (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
       (forward-line 1))
     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)))
+  (whe