Merge flim-1_12_6.
[elisp/flim.git] / mime.el
diff --git a/mime.el b/mime.el
index 0ba74c9..ce23631 100644 (file)
--- a/mime.el
+++ b/mime.el
@@ -1,6 +1,6 @@
 ;;; mime.el --- MIME library module
 
-;; Copyright (C) 1998 Free Software Foundation, Inc.
+;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
 
 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
 ;; Keywords: MIME, multimedia, mail, news
 (require 'mime-def)
 (require 'eword-decode)
 
-(autoload 'eword-encode-field "eword-encode"
-  "Encode header field STRING, and return the result.")
+(eval-and-compile
+
 (autoload 'eword-encode-header "eword-encode"
   "Encode header fields to network representation, such as MIME encoded-word.")
 
-
 (autoload 'mime-parse-Content-Type "mime-parse"
   "Parse STRING as field-body of Content-Type field.")
 (autoload 'mime-read-Content-Type "mime-parse"
@@ -53,21 +52,78 @@ and return parsed it.")
   "Read field-body of Content-Transfer-Encoding field from
 current-buffer, and return it.")
 
-(autoload 'mime-parse-message "mime-parse"
-  "Parse current-buffer as a MIME message.")
+(autoload 'mime-parse-msg-id "mime-parse"
+  "Parse TOKENS as msg-id of Content-Id or Message-Id field.")
+
+(autoload 'mime-uri-parse-cid "mime-parse"
+  "Parse STRING as cid URI.")
 
 (autoload 'mime-parse-buffer "mime-parse"
   "Parse BUFFER as a MIME message.")
 
+)
+
+;;; @ Entity Representation and Implementation
+;;;
+
+(defsubst mime-find-function (service type)
+  (let ((imps (cdr (assq type mime-entity-implementation-alist))))
+    (if imps
+       (cdr (assq service imps))
+      (require (intern (format "mm%s" type)))
+      (cdr (assq service
+                (cdr (assq type mime-entity-implementation-alist))))
+      )))
+
+(defsubst mime-entity-function (entity service)
+  (mime-find-function service
+                     (mime-entity-representation-type-internal entity)))
+
+(defsubst mime-entity-send (entity message &rest args)
+  "Send MESSAGE to ENTITY with ARGS, and return the result."
+  (apply (mime-find-function
+         message (mime-entity-representation-type-internal entity))
+        entity
+        args))
+
+(defmacro mm-define-generic (name args &optional doc)
+  (if doc
+      `(defun ,(intern (format "mime-%s" name)) ,args
+        ,doc
+        (mime-entity-send ,(car args) ',name
+                          ,@(mm-arglist-to-arguments (cdr args)))
+        )
+    `(defun ,(intern (format "mime-%s" name)) ,args
+       (mime-entity-send ,(car args) ',name
+                        ,@(mm-arglist-to-arguments (cdr args)))
+       )))
+
+(put 'mm-define-generic 'lisp-indent-function 'defun)
+
+(defun mime-open-entity (type location)
+  "Open an entity and return it.
+TYPE is representation-type.
+LOCATION is location of entity.  Specification of it is depended on
+representation-type."
+  (let ((entity (make-mime-entity-internal type location)))
+    (mime-entity-send entity 'initialize-instance)
+    entity))
+
+(mm-define-generic entity-cooked-p (entity)
+  "Return non-nil if contents of ENTITY has been already code-converted.")
+
 
 ;;; @ Entity as node of message
 ;;;
 
-(defalias 'mime-entity-children        'mime-entity-children-internal)
+(defun mime-entity-children (entity)
+  (or (mime-entity-children-internal entity)
+      (mime-entity-send entity 'entity-children)))
 
 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
 
-(defsubst mime-entity-number (entity)
+(defun mime-entity-number (entity)
+  "Return entity-number of ENTITY."
   (reverse (mime-entity-node-id-internal entity)))
 
 (defun mime-find-entity-from-number (entity-number &optional message)
@@ -84,83 +140,159 @@ If MESSAGE is not specified, `mime-message-structure' is used."
          ))
       )))
 
-(defsubst mime-find-entity-from-node-id (entity-node-id &optional message)
+(defun mime-find-entity-from-node-id (entity-node-id &optional message)
   "Return entity from ENTITY-NODE-ID in MESSAGE.
 If MESSAGE is not specified, `mime-message-structure' is used."
   (mime-find-entity-from-number (reverse entity-node-id) message))
 
-(defsubst mime-entity-parent (entity &optional message)
+(defun mime-find-entity-from-content-id (cid &optional message)
+  "Return entity from CID in MESSAGE.
+If MESSAGE is not specified, `mime-message-structure' is used."
+  (or message
+      (setq message mime-message-structure))
+  (if (equal cid (mime-read-field 'Content-Id message))
+      message
+    (let ((children (mime-entity-children message))
+         ret)
+      (while (and children
+                 (null (setq ret (mime-find-entity-from-content-id
+                                  cid (car children)))))
+       (setq children (cdr children)))
+      ret)))
+
+(defun mime-entity-parent (entity &optional message)
   "Return mother entity of ENTITY.
-If MESSAGE is not specified, `mime-message-structure' in the buffer of
-ENTITY is used."
-  (mime-find-entity-from-node-id
-   (cdr (mime-entity-node-id entity))
-   (or message
-       (save-excursion
-        (set-buffer (mime-entity-buffer entity))
-        mime-message-structure))))
+If MESSAGE is specified, it is regarded as root entity."
+  (if (equal entity message)
+      nil
+    (mime-entity-parent-internal entity)))
 
-(defsubst mime-root-entity-p (entity)
-  "Return t if ENTITY is root-entity (message)."
-  (null (mime-entity-node-id entity)))
+(defun mime-root-entity-p (entity &optional message)
+  "Return t if ENTITY is root-entity (message).
+If MESSAGE is specified, it is regarded as root entity."
+  (null (mime-entity-parent entity message)))
 
 
 ;;; @ Entity Buffer
 ;;;
 
-(defalias 'mime-entity-buffer    'mime-entity-buffer-internal)
-(defalias 'mime-entity-point-min 'mime-entity-header-start-internal)
-(defalias 'mime-entity-point-max 'mime-entity-body-end-internal)
+(defun mime-entity-buffer (entity)
+  (or (mime-entity-buffer-internal entity)
+      (mime-entity-send entity 'entity-buffer)))
+
+(mm-define-generic entity-point-min (entity)
+  "Return the start point of ENTITY in the buffer which contains ENTITY.")
+
+(mm-define-generic entity-point-max (entity)
+  "Return the end point of ENTITY in the buffer which contains ENTITY.")
+
+(defun mime-entity-header-start (entity)
+  (or (mime-entity-header-start-internal entity)
+      (mime-entity-send entity 'entity-header-start)))
+
+(defun mime-entity-header-end (entity)
+  (or (mime-entity-header-end-internal entity)
+      (mime-entity-send entity 'entity-header-end)))
+
+(defun mime-entity-body-start (entity)
+  (or (mime-entity-body-start-internal entity)
+      (mime-entity-send entity 'entity-body-start)))
+
+(defun mime-entity-body-end (entity)
+  (or (mime-entity-body-end-internal entity)
+      (mime-entity-send entity 'entity-body-end)))
 
 
 ;;; @ Entity Header
 ;;;
 
-(defalias 'mime-entity-header-start 'mime-entity-header-start-internal)
-(defalias 'mime-entity-header-end   'mime-entity-header-end-internal)
-
 (defun mime-fetch-field (field-name &optional entity)
   (or (symbolp field-name)
       (setq field-name (intern (capitalize (capitalize field-name)))))
   (or entity
       (setq entity mime-message-structure))
-  (let* ((header (mime-entity-original-header-internal entity))
-        (field-body (cdr (assq field-name header))))
-    (or field-body
-       (progn
-         (if (save-excursion
-               (set-buffer (mime-entity-buffer entity))
-               (save-restriction
-                 (narrow-to-region (mime-entity-header-start entity)
-                                   (mime-entity-header-end entity))
-                 (setq field-body
-                       (std11-fetch-field (symbol-name field-name)))
-                 ))
-             (mime-entity-set-original-header-internal
-              entity (put-alist field-name field-body header))
-           )
-         field-body))))
-
-(defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
+  (cond ((eq field-name 'Date)
+        (or (mime-entity-date-internal entity)
+            (mime-entity-set-date-internal
+             entity (mime-entity-send entity 'fetch-field "Date"))
+            ))
+       ((eq field-name 'Message-Id)
+        (or (mime-entity-message-id-internal entity)
+            (mime-entity-set-message-id-internal
+             entity (mime-entity-send entity 'fetch-field "Message-Id"))
+            ))
+       ((eq field-name 'References)
+        (or (mime-entity-references-internal entity)
+            (mime-entity-set-references-internal
+             entity (mime-entity-send entity 'fetch-field "References"))
+            ))
+       (t
+        (let* ((header (mime-entity-original-header-internal entity))
+               (field-body (cdr (assq field-name header))))
+          (or field-body
+              (progn
+                (if (setq field-body
+                          (mime-entity-send entity 'fetch-field
+                                            (symbol-name field-name)))
+                    (mime-entity-set-original-header-internal
+                     entity (put-alist field-name field-body header))
+                  )
+                field-body))
+          ))))
+
+(defun mime-entity-content-type (entity)
+  (or (mime-entity-content-type-internal entity)
+      (let ((ret (mime-fetch-field 'Content-Type entity)))
+       (if ret
+           (mime-entity-set-content-type-internal
+            entity (mime-parse-Content-Type ret))
+         ))))
 
 (defun mime-entity-content-disposition (entity)
   (or (mime-entity-content-disposition-internal entity)
       (let ((ret (mime-fetch-field 'Content-Disposition entity)))
        (if ret
-           (let ((disposition (mime-parse-Content-Disposition ret)))
-             (when disposition
-               (mime-entity-set-content-disposition-internal
-                entity disposition)
-               disposition))))))
+           (mime-entity-set-content-disposition-internal
+            entity (mime-parse-Content-Disposition ret))
+         ))))
 
-(defun mime-entity-encoding (entity)
+(defun mime-entity-encoding (entity &optional default-encoding)
   (or (mime-entity-encoding-internal entity)
       (let ((ret (mime-fetch-field 'Content-Transfer-Encoding entity)))
-       (if ret
-           (let ((encoding (mime-parse-Content-Transfer-Encoding ret)))
-             (when encoding
-               (mime-entity-set-encoding-internal entity encoding)
-               encoding))))))
+       (mime-entity-set-encoding-internal
+        entity
+        (or (and ret (mime-parse-Content-Transfer-Encoding ret))
+            default-encoding "7bit"))
+       )))
+
+(defvar mime-field-parser-alist
+  '((Return-Path       . std11-parse-route-addr)
+    
+    (Reply-To          . std11-parse-addresses)
+    
+    (Sender            . std11-parse-mailbox)
+    (From              . std11-parse-addresses)
+
+    (Resent-Reply-To   . std11-parse-addresses)
+    
+    (Resent-Sender     . std11-parse-mailbox)
+    (Resent-From       . std11-parse-addresses)
+
+    (To                        . std11-parse-addresses)
+    (Resent-To         . std11-parse-addresses)
+    (Cc                        . std11-parse-addresses)
+    (Resent-Cc         . std11-parse-addresses)
+    (Bcc               . std11-parse-addresses)
+    (Resent-Bcc                . std11-parse-addresses)
+    
+    (Message-Id                . mime-parse-msg-id)
+    (Recent-Message-Id . mime-parse-msg-id)
+    
+    (In-Reply-To       . std11-parse-msg-ids)
+    (References                . std11-parse-msg-ids)
+    
+    (Content-Id                . mime-parse-msg-id)
+    ))
 
 (defun mime-read-field (field-name &optional entity)
   (or (symbolp field-name)
@@ -180,126 +312,31 @@ ENTITY is used."
         (let* ((header (mime-entity-parsed-header-internal entity))
                (field (cdr (assq field-name header))))
           (or field
-              (let ((field-body (mime-fetch-field field-name entity)))
+              (let ((field-body (mime-fetch-field field-name entity))
+                    parser)
                 (when field-body
-                  (cond ((memq field-name '(From Resent-From
-                                            To Resent-To
-                                            Cc Resent-Cc
-                                            Bcc Resent-Bcc
-                                            Reply-To Resent-Reply-To))
-                         (setq field (std11-parse-addresses
-                                      (eword-lexical-analyze field-body)))
-                         )
-                        ((memq field-name '(Sender Resent-Sender))
-                         (setq field (std11-parse-address
-                                      (eword-lexical-analyze field-body)))
-                         )
-                        ((memq field-name eword-decode-ignored-field-list)
-                         (setq field field-body))
-                        ((memq field-name eword-decode-structured-field-list)
-                         (setq field (eword-decode-structured-field-body
-                                      field-body)))
-                        (t
-                         (setq field (eword-decode-unstructured-field-body
-                                      field-body))
-                         ))
+                  (setq parser
+                        (cdr (assq field-name mime-field-parser-alist)))
+                  (setq field
+                        (if parser
+                            (funcall parser
+                                     (eword-lexical-analyze field-body))
+                          (mime-decode-field-body
+                           field-body field-name 'plain)
+                          ))
                   (mime-entity-set-parsed-header-internal
                    entity (put-alist field-name field header))
                   field)))))))
 
-(defun eword-visible-field-p (field-name visible-fields invisible-fields)
-  (or (catch 'found
-       (while visible-fields
-         (let ((regexp (car visible-fields)))
-           (if (string-match regexp field-name)
-               (throw 'found t)
-             ))
-         (setq visible-fields (cdr visible-fields))
-         ))
-      (catch 'found
-       (while invisible-fields
-         (let ((regexp (car invisible-fields)))
-           (if (string-match regexp field-name)
-               (throw 'found nil)
-             ))
-         (setq invisible-fields (cdr invisible-fields))
-         )
-       t)))
-               
-(defun mime-insert-decoded-header (entity
-                                  &optional invisible-fields visible-fields
-                                  code-conversion)
-  "Insert before point a decoded header of ENTITY."
-  (let ((default-charset
-         (if code-conversion
-             (if (mime-charset-to-coding-system code-conversion)
-                 code-conversion
-               default-mime-charset))))
-    (save-restriction
-      (narrow-to-region (point)(point))
-      (let ((the-buf (current-buffer))
-           (src-buf (mime-entity-buffer entity))
-           (h-end (mime-entity-header-end entity))
-           beg p end field-name len field)
-       (save-excursion
-         (set-buffer src-buf)
-         (goto-char (mime-entity-header-start entity))
-         (save-restriction
-           (narrow-to-region (point) h-end)
-           (while (re-search-forward std11-field-head-regexp nil t)
-             (setq beg (match-beginning 0)
-                   p (match-end 0)
-                   field-name (buffer-substring beg (1- p))
-                   len (string-width field-name)
-                   end (std11-field-end))
-             (when (eword-visible-field-p field-name
-                                          visible-fields invisible-fields)
-               (setq field (intern (capitalize field-name)))
-               (save-excursion
-                 (set-buffer the-buf)
-                 (insert field-name)
-                 (insert ":")
-                 (cond ((memq field eword-decode-ignored-field-list)
-                        ;; Don't decode
-                        (insert-buffer-substring src-buf p end)
-                        )
-                       ((memq field eword-decode-structured-field-list)
-                        ;; Decode as structured field
-                        (let ((body (save-excursion
-                                      (set-buffer src-buf)
-                                      (buffer-substring p end)))
-                              (default-mime-charset default-charset))
-                          (insert (eword-decode-and-fold-structured-field
-                                   body (1+ len)))
-                          ))
-                       (t
-                        ;; Decode as unstructured field
-                        (let ((body (save-excursion
-                                      (set-buffer src-buf)
-                                      (buffer-substring p end)))
-                              (default-mime-charset default-charset))
-                          (insert (eword-decode-unstructured-field-body
-                                   body (1+ len)))
-                          )))
-                 (insert "\n")
-                 )))))))))
-
-
-;;; @ Entity Content
-;;;
-
-(defalias 'mime-entity-body-start 'mime-entity-body-start-internal)
-(defalias 'mime-entity-body-end   'mime-entity-body-end-internal)
+(mm-define-generic insert-header (entity &optional invisible-fields
+                                        visible-fields)
+  "Insert before point a decoded header of ENTITY.")
 
-(defun mime-entity-content (entity)
-  (save-excursion
-    (set-buffer (mime-entity-buffer entity))
-    (mime-decode-string (buffer-substring (mime-entity-body-start entity)
-                                         (mime-entity-body-end entity))
-                       (mime-entity-encoding entity))))
+(define-obsolete-function-alias
+  'mime-insert-decoded-header 'mime-insert-header)
 
 
-;;; @ Another Entity Information
+;;; @ Entity Attributes
 ;;;
 
 (defun mime-entity-uu-filename (entity)
@@ -336,6 +373,31 @@ ENTITY is used."
                            (mime-entity-media-subtype entity-info)))
 
 
+;;; @ Entity Content
+;;;
+
+(mm-define-generic entity-content (entity)
+  "Return content of ENTITY as byte sequence (string).")
+
+(mm-define-generic insert-entity-content (entity)
+  "Insert content of ENTITY at point.")
+
+(mm-define-generic write-entity-content (entity filename)
+  "Write content of ENTITY into FILENAME.")
+
+(mm-define-generic insert-text-content (entity)
+  "Insert decoded text body of ENTITY.")
+
+(mm-define-generic insert-entity (entity)
+  "Insert header and body of ENTITY at point.")
+
+(mm-define-generic write-entity (entity filename)
+  "Write header and body of ENTITY into FILENAME.")
+
+(mm-define-generic write-entity-body (entity filename)
+  "Write body of ENTITY into FILENAME.")
+
+
 ;;; @ end
 ;;;