tm 7.52.1.
[elisp/tm.git] / tm-parse.el
index e1d2c30..d6fe8ca 100644 (file)
@@ -2,15 +2,30 @@
 ;;; tm-parse.el --- MIME message parser
 ;;;
 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
-;;; Copyright (C) 1994,1995 MORIOKA Tomohiko
+;;; Copyright (C) 1994 .. 1996 MORIOKA Tomohiko
 ;;;
 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
 ;;; Version:
-;;;    $Id: tm-parse.el,v 3.0 1995/10/05 11:28:40 morioka Exp $
+;;;    $Id: tm-parse.el,v 7.8 1996/04/20 12:17:27 morioka Exp $
 ;;; Keywords: mail, news, MIME, multimedia
 ;;;
 ;;; This file is part of tm (Tools for MIME).
 ;;;
+;;; This program 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.
+;;;
+;;; This program 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 This program.  If not, write to the Free Software
+;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;;;
+;;; Code:
 
 (require 'tl-822)
 (require 'tl-misc)
@@ -35,8 +50,7 @@
         (substring str e)
         ))))
 
-(defconst mime::ctype-regexp
-  (concat "^" mime/content-type-subtype-regexp))
+(defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
 
 (defun mime/parse-Content-Type (str)
   "Parse STR as field-body of Content-Type field. [tm-parse.el]"
             ret dest)
        (setq str (substring str e))
        (while (setq ret (mime/parse-parameter str))
-         (setq dest (cons (car ret) dest))
-         (setq str (cdr ret))
+         (setq dest (cons (car ret) dest)
+               str (cdr ret))
+         )
+       (cons ctype (nreverse dest))
+       )))
+
+(defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
+
+(defun mime/parse-Content-Disposition (str)
+  "Parse STR as field-body of Content-Disposition field. [tm-parse.el]"
+  (setq str (rfc822/unfolding-string str))
+  (if (string-match mime::dtype-regexp str)
+      (let* ((e (match-end 0))
+            (ctype (downcase (substring str 0 e)))
+            ret dest)
+       (setq str (substring str e))
+       (while (setq ret (mime/parse-parameter str))
+         (setq dest (cons (car ret) dest)
+               str (cdr ret))
          )
-       (cons ctype (reverse dest))
+       (cons ctype (nreverse dest))
        )))
 
 
@@ -75,27 +106,41 @@ If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
       default-encoding)
     ))
 
+(defun mime/Content-Disposition ()
+  "Read field-body of Content-Disposition field from current-buffer,
+and return parsed it. [tm-parse.el]"
+  (let ((str (rfc822/get-field-body "Content-Disposition")))
+    (if str
+       (mime/parse-Content-Disposition str)
+      )))
+
 
 ;;; @ message parser
 ;;;
 
 (define-structure mime::content-info
-  point-min point-max type parameters encoding children)
+  rcnum point-min point-max type parameters encoding children)
 
 
-(defun mime/parse-multipart (boundary ctype params encoding)
+(defun mime/parse-multipart (boundary ctype params encoding rcnum)
   (goto-char (point-min))
-  (let ((beg (point-min))
-       (end (if (re-search-forward
-                 (concat "^--" (regexp-quote boundary) "--$") nil t)
-                (match-beginning 0)
-              (point-max)
-              ))
-       (rsep (concat "^--" (regexp-quote boundary) "\n"))
-       (dc-ctl
-        (cond ((string= ctype "multipart/digest") '("message/rfc822"))
-              (t '("text/plain"))))
-       cb ce ct ret ncb children)
+  (let* ((dash-boundary   (concat "--" boundary))
+        (delimiter       (concat "\n" (regexp-quote dash-boundary)))
+        (close-delimiter (concat delimiter "--[ \t]*$"))
+        (beg (point-min))
+        (end (progn
+               (goto-char (point-max))
+               (if (re-search-backward close-delimiter nil t)
+                   (match-beginning 0)
+                 (point-max)
+                 )))
+        (rsep (concat delimiter "[ \t]*\n"))
+        (dc-ctl
+         (if (string-equal ctype "multipart/digest")
+             '("message/rfc822")
+           '("text/plain")
+           ))
+        cb ce ct ret ncb children (i 0))
     (save-restriction
       (narrow-to-region beg end)
       (goto-char beg)
@@ -106,24 +151,26 @@ If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
        (setq ncb (match-end 0))
        (save-restriction
          (narrow-to-region cb ce)
-         (setq ret (mime/parse-message dc-ctl "7bit"))
+         (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
          )
        (setq children (cons ret children))
        (goto-char (mime::content-info/point-max ret))
        (goto-char (setq cb ncb))
+       (setq i (1+ i))
        )
       (setq ce (point-max))
       (save-restriction
        (narrow-to-region cb ce)
-       (setq ret (mime/parse-message dc-ctl "7bit"))
+       (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
        )
       (setq children (cons ret children))
       )
-    (mime::content-info/create beg end ctype params encoding
-                              (reverse children))
+    (mime::content-info/create rcnum beg (point-max)
+                              ctype params encoding
+                              (nreverse children))
     ))
 
-(defun mime/parse-message (&optional ctl encoding)
+(defun mime/parse-message (&optional ctl encoding rcnum)
   "Parse current-buffer as a MIME message. [tm-parse.el]"
   (setq ctl (or (mime/Content-Type) ctl))
   (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
@@ -133,25 +180,27 @@ If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
     (let ((boundary (assoc "boundary" params)))
       (cond (boundary
             (setq boundary (rfc822/strip-quoted-string (cdr boundary)))
-            (mime/parse-multipart boundary ctype params encoding)
+            (mime/parse-multipart boundary ctype params encoding rcnum)
             )
-           ((string= ctype "message/rfc822")
+           ((string-equal ctype "message/rfc822")
             (goto-char (point-min))
-            (mime::content-info/create (point-min) (point-max)
+            (mime::content-info/create rcnum
+                                       (point-min) (point-max)
                                        ctype params encoding
                                        (save-restriction
                                          (narrow-to-region
                                           (if (re-search-forward "^$" nil t)
-                                              (+ (match-end 0) 1)
+                                              (1+ (match-end 0))
                                             (point-min)
                                             )
                                           (point-max))
-                                         (list (mime/parse-message))
+                                         (list (mime/parse-message
+                                                nil nil (cons 0 rcnum)))
                                          )
                                        )
             )
            (t 
-            (mime::content-info/create (point-min) (point-max)
+            (mime::content-info/create rcnum (point-min) (point-max)
                                        ctype params encoding nil)
             ))
       )))
@@ -161,3 +210,5 @@ If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
 ;;;
 
 (provide 'tm-parse)
+
+;;; tm-parse.el ends here