delete digest-md5-parse-digest-challenge
[elisp/flim.git] / mime.el
diff --git a/mime.el b/mime.el
index 065e739..63af880 100644 (file)
--- a/mime.el
+++ b/mime.el
@@ -1,8 +1,10 @@
 ;;; mime.el --- MIME library module
 
-;; Copyright (C) 1998 Free Software Foundation, Inc.
+;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
+;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
+;; Licensed to the Free Software Foundation.
 
-;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
+;; Author: MORIOKA Tomohiko <tomo@m17n.org>
 ;; Keywords: MIME, multimedia, mail, news
 
 ;; This file is part of FLIM (Faithful Library about Internet Message).
 (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,69 +54,32 @@ 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 service &rest args)
-  (apply (mime-find-function
-         service (mime-entity-representation-type-internal entity))
-        entity
-        args))
-
-(defsubst mm-arglist-to-arguments (arglist)
-  (let (dest)
-    (while arglist
-      (let ((arg (car arglist)))
-       (or (memq arg '(&optional &rest))
-           (setq dest (cons arg dest)))
-       )
-      (setq arglist (cdr arglist)))
-    (nreverse dest)))
-
-(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)
+(defmacro mime-entity-send (entity message &rest args)
+  `(luna-send ,entity ',(intern (format "mime-%s" (eval message))) ,@args))
 
 (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))
+  (require (intern (format "mm%s" type)))
+  (luna-make-entity (mm-expand-class-name type) :location location))
 
-(mm-define-generic entity-cooked-p (entity)
+(luna-define-generic mime-entity-cooked-p (entity)
   "Return non-nil if contents of ENTITY has been already code-converted.")
 
 
@@ -124,7 +88,7 @@ representation-type."
 
 (defun mime-entity-children (entity)
   (or (mime-entity-children-internal entity)
-      (mime-entity-send entity 'entity-children)))
+      (luna-send entity 'mime-entity-children entity)))
 
 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
 
@@ -151,6 +115,21 @@ If MESSAGE is not specified, `mime-message-structure' is used."
 If MESSAGE is not specified, `mime-message-structure' is used."
   (mime-find-entity-from-number (reverse entity-node-id) 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-entity-read-field message "Content-Id"))
+      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 specified, it is regarded as root entity."
@@ -164,138 +143,188 @@ If MESSAGE is specified, it is regarded as root entity."
   (null (mime-entity-parent entity message)))
 
 
-;;; @ Entity Buffer
+;;; @ Header buffer
 ;;;
 
-(defun mime-entity-buffer (entity)
-  (or (mime-entity-buffer-internal entity)
-      (mime-entity-send entity 'entity-buffer)))
+(luna-define-generic mime-entity-header-buffer (entity))
+
+(luna-define-generic mime-goto-header-start-point (entity)
+  "Set buffer and point to header-start-position of ENTITY.")
+
+(luna-define-generic mime-entity-header-start-point (entity)
+  "Return header-start-position of ENTITY.")
+
+(luna-define-generic mime-entity-header-end-point (entity)
+  "Return header-end-position of ENTITY.")
+
+
+;;; @ Body buffer
+;;;
 
-(mm-define-generic entity-point-min (entity)
-  "Return the start point of ENTITY in the buffer which contains ENTITY.")
+(luna-define-generic mime-entity-body-buffer (entity))
 
-(mm-define-generic entity-point-max (entity)
-  "Return the end point of ENTITY in the buffer which contains ENTITY.")
+(luna-define-generic mime-goto-body-start-point (entity)
+  "Set buffer and point to body-start-position of ENTITY.")
 
-(defun mime-entity-header-start (entity)
-  (or (mime-entity-header-start-internal entity)
-      (mime-entity-send entity 'entity-header-start)))
+(luna-define-generic mime-goto-body-end-point (entity)
+  "Set buffer and point to body-end-position of ENTITY.")
 
-(defun mime-entity-header-end (entity)
-  (or (mime-entity-header-end-internal entity)
-      (mime-entity-send entity 'entity-header-end)))
+(luna-define-generic mime-entity-body-start-point (entity)
+  "Return body-start-position of ENTITY.")
 
-(defun mime-entity-body-start (entity)
-  (or (mime-entity-body-start-internal entity)
-      (mime-entity-send entity 'entity-body-start)))
+(define-obsolete-function-alias
+  'mime-entity-body-start 'mime-entity-body-start-point)
 
-(defun mime-entity-body-end (entity)
-  (or (mime-entity-body-end-internal entity)
-      (mime-entity-send entity 'entity-body-end)))
+(luna-define-generic mime-entity-body-end-point (entity)
+  "Return body-end-position of ENTITY.")
+
+(define-obsolete-function-alias
+  'mime-entity-body-end 'mime-entity-body-end-point)
+
+
+;;; @ Entity buffer (obsolete)
+;;;
+
+(luna-define-generic mime-entity-buffer (entity))
+(make-obsolete 'mime-entity-buffer
+ "use mime-entity-header-buffer or mime-entity-body-buffer instead.")
+
+(luna-define-generic mime-entity-point-min (entity))
+(make-obsolete 'mime-entity-point-min 'mime-entity-header-start-point)
+
+(luna-define-generic mime-entity-point-max (entity))
+(make-obsolete 'mime-entity-point-max 'mime-entity-body-end-point)
 
 
 ;;; @ Entity Header
 ;;;
 
+(luna-define-generic mime-entity-fetch-field (entity field-name)
+  "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
+
 (defun mime-fetch-field (field-name &optional entity)
-  (or (symbolp field-name)
-      (setq field-name (intern (capitalize (capitalize field-name)))))
+  "Return the value of the ENTITY's header field whose type is FIELD-NAME."
+  (if (symbolp field-name)
+      (setq field-name (symbol-name 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 (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))))
-
-(defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
+  (mime-entity-fetch-field entity field-name)
+  )
+(make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
+
+(defun mime-entity-content-type (entity)
+  (or (mime-entity-content-type-internal entity)
+      (let ((ret (mime-entity-fetch-field entity "Content-Type")))
+       (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)))
+      (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
        (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 &optional default-encoding)
   (or (mime-entity-encoding-internal entity)
-      (let ((encoding
-            (or (let ((ret (mime-fetch-field
-                            'Content-Transfer-Encoding entity)))
-                  (and ret (mime-parse-Content-Transfer-Encoding ret)))
-                default-encoding "7bit")))
-       (mime-entity-set-encoding-internal entity encoding)
-       encoding)))
+      (let ((ret (mime-entity-fetch-field entity "Content-Transfer-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-entity-read-field (entity field-name)
+  (let ((sym (if (symbolp field-name)
+                (prog1
+                    field-name
+                  (setq field-name (symbol-name field-name)))
+              (capitalize (capitalize field-name)))))
+    (cond ((eq sym 'Content-Type)
+          (mime-entity-content-type entity)
+          )
+         ((eq sym 'Content-Disposition)
+          (mime-entity-content-disposition entity)
+          )
+         ((eq sym 'Content-Transfer-Encoding)
+          (mime-entity-encoding entity)
+          )
+         (t
+          (let* ((header (mime-entity-parsed-header-internal entity))
+                 (field (cdr (assq sym header))))
+            (or field
+                (let ((field-body (mime-entity-fetch-field entity field-name))
+                      parser)
+                  (when field-body
+                    (setq parser
+                          (cdr (assq sym mime-field-parser-alist)))
+                    (setq field
+                          (if parser
+                              (funcall parser
+                                       (eword-lexical-analyze field-body))
+                            (mime-decode-field-body field-body sym 'plain)
+                            ))
+                    (mime-entity-set-parsed-header-internal
+                     entity (put-alist sym field header))
+                    field))))))))
 
 (defun mime-read-field (field-name &optional entity)
-  (or (symbolp field-name)
-      (setq field-name (capitalize (capitalize field-name))))
   (or entity
       (setq entity mime-message-structure))
-  (cond ((eq field-name 'Content-Type)
-        (mime-entity-content-type entity)
-        )
-       ((eq field-name 'Content-Disposition)
-        (mime-entity-content-disposition entity)
-        )
-       ((eq field-name 'Content-Transfer-Encoding)
-        (mime-entity-encoding entity)
-        )
-       (t
-        (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)))
-                (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))
-                         ))
-                  (mime-entity-set-parsed-header-internal
-                   entity (put-alist field-name field header))
-                  field)))))))
-
-(mm-define-generic insert-decoded-header (entity &optional invisible-fields
-                                         visible-fields)
+  (mime-entity-read-field entity field-name)
+  )
+(make-obsolete 'mime-read-field 'mime-entity-read-field)
+
+(luna-define-generic mime-insert-header (entity &optional invisible-fields
+                                               visible-fields)
   "Insert before point a decoded header of ENTITY.")
 
 
 ;;; @ Entity Attributes
 ;;;
 
+(luna-define-generic mime-entity-name (entity)
+  "Return name of the ENTITY.")
+
 (defun mime-entity-uu-filename (entity)
   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
       (save-excursion
-       (set-buffer (mime-entity-buffer entity))
-       (goto-char (mime-entity-body-start entity))
+       (mime-goto-body-start-point entity)
        (if (re-search-forward "^begin [0-9]+ "
-                              (mime-entity-body-end entity) t)
+                              (mime-entity-body-end-point entity) t)
            (if (looking-at ".+$")
                (buffer-substring (match-beginning 0)(match-end 0))
              )))))
@@ -326,16 +355,25 @@ If MESSAGE is specified, it is regarded as root entity."
 ;;; @ Entity Content
 ;;;
 
-(mm-define-generic entity-content (entity)
+(luna-define-generic mime-entity-content (entity)
   "Return content of ENTITY as byte sequence (string).")
 
-(mm-define-generic write-entity-content (entity filename)
+(luna-define-generic mime-insert-entity-content (entity)
+  "Insert content of ENTITY at point.")
+
+(luna-define-generic mime-write-entity-content (entity filename)
   "Write content of ENTITY into FILENAME.")
 
-(mm-define-generic write-entity (entity filename)
+(luna-define-generic mime-insert-text-content (entity)
+  "Insert decoded text body of ENTITY.")
+
+(luna-define-generic mime-insert-entity (entity)
+  "Insert header and body of ENTITY at point.")
+
+(luna-define-generic mime-write-entity (entity filename)
   "Write header and body of ENTITY into FILENAME.")
 
-(mm-define-generic write-entity-body (entity filename)
+(luna-define-generic mime-write-entity-body (entity filename)
   "Write body of ENTITY into FILENAME.")