2000-11-07 08:49:36 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / mm-decode.el
index d18f261..b0ff1d8 100644 (file)
   :group 'news
   :group 'multimedia)
 
+(defgroup mime-security ()
+  "MIME security in mail and news articles."
+  :link '(custom-manual "(emacs-mime)Customization")
+  :group 'mail
+  :group 'news
+  :group 'multimedia)
+
 ;;; Convenience macros.
 
 (defmacro mm-handle-buffer (handle)
   `(setcar (nthcdr 6 ,handle) ,contents))
 (defmacro mm-handle-id (handle)
   `(nth 7 ,handle))
+(defmacro mm-handle-multipart-original-buffer (handle)
+  `(get-text-property 0 'buffer (car ,handle)))
+(defmacro mm-handle-multipart-ctl-parameter (handle parameter)
+  `(get-text-property 0 ,parameter (car ,handle)))
+
 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
                                    disposition description cache
                                    id)
        (and (or (featurep 'nas-sound) (featurep 'native-sound))
            (device-sound-enabled-p))))
     ("application/pgp-signature" ignore identity)
+    ("application/x-pkcs7-signature" ignore identity)
+    ("application/pkcs7-signature" ignore identity)
     ("multipart/alternative" ignore identity)
     ("multipart/mixed" ignore identity)
     ("multipart/related" ignore identity))
 (defcustom mm-inlined-types
   '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
     "message/partial" "message/external-body" "application/emacs-lisp"
-    "application/pgp-signature")
+    "application/pgp-signature" "application/x-pkcs7-signature"
+    "application/pkcs7-signature")
   "List of media types that are to be displayed inline."
   :type '(repeat string)
   :group 'mime-display)
 (defcustom mm-automatic-display
   '("text/plain" "text/enriched" "text/richtext" "text/html"
     "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
-    "message/rfc822" "text/x-patch" "application/pgp-signature" 
-    "application/emacs-lisp")
+    "message/rfc822" "text/x-patch" "application/pgp-signature"
+    "application/emacs-lisp" "application/x-pkcs7-signature"
+    "application/pkcs7-signature")
   "A list of MIME types to be displayed automatically."
   :type '(repeat string)
   :group 'mime-display)
@@ -221,11 +237,15 @@ to:
 (defvar mm-dissect-default-type "text/plain")
 
 (autoload 'mml2015-verify "mml2015")
+(autoload 'mml2015-verify-test "mml2015")
+(autoload 'mml-smime-verify "mml-smime")
 
 (defvar mm-verify-function-alist
-  '(("application/pgp-signature" . mml2015-verify)))
+  '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
+    ("application/pkcs7-signature" mml-smime-verify "S/MIME" nil)
+    ("application/x-pkcs7-signature" mml-smime-verify "S/MIME" nil)))
 
-(defcustom mm-verify-option nil
+(defcustom mm-verify-option 'known
   "Option of verifying signed parts.
 `never', not verify; `always', always verify; 
 `known', only verify known protocols. Otherwise, ask user."
@@ -233,14 +253,15 @@ to:
                 (item never)
                 (item :tag "only known protocols" known)
                 (item :tag "ask" nil))
-  :group 'gnus-article)
+  :group 'mime-security)
 
 (autoload 'mml2015-decrypt "mml2015")
+(autoload 'mml2015-decrypt-test "mml2015")
 
 (defvar mm-decrypt-function-alist
-  '(("application/pgp-encrypted" . mml2015-decrypt)))
+  '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)))
 
-(defcustom mm-decrypt-option nil
+(defcustom mm-decrypt-option 'known
   "Option of decrypting signed parts.
 `never', not decrypt; `always', always decrypt; 
 `known', only decrypt known protocols. Otherwise, ask user."
@@ -248,17 +269,7 @@ to:
                 (item never)
                 (item :tag "only known protocols" known)
                 (item :tag "ask" nil))
-  :group 'gnus-article)
-
-(defcustom mm-snarf-option nil
-  "Option of snarfing PGP key.
-`never', not snarf; `always', always snarf; 
-`known', only snarf known protocols. Otherwise, ask user."
-  :type '(choice (item always)
-                (item never)
-                (item :tag "only known protocols" known)
-                (item :tag "ask" nil))
-  :group 'gnus-article)
+  :group 'mime-security)
 
 (defvar mm-viewer-completion-map
   (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
@@ -271,6 +282,24 @@ to:
 
 ;;; The functions.
 
+(defun mm-alist-to-plist (alist)
+  "Convert association list ALIST into the equivalent property-list form.
+The plist is returned.  This converts from
+
+\((a . 1) (b . 2) (c . 3))
+
+into
+
+\(a 1 b 2 c 3)
+
+The original alist is not modified.  See also `destructive-alist-to-plist'."
+  (let (plist)
+    (while alist
+      (let ((el (car alist)))
+       (setq plist (cons (cdr el) (cons (car el) plist))))
+      (setq alist (cdr alist)))
+    (nreverse plist)))
+
 (defun mm-dissect-buffer (&optional no-strict-mime)
   "Dissect the current buffer and return a list of MIME handles."
   (save-excursion
@@ -307,6 +336,17 @@ to:
           (let ((mm-dissect-default-type (if (equal subtype "digest")
                                              "message/rfc822"
                                            "text/plain")))
+             (add-text-properties 0 (length (car ctl))
+                                  (mm-alist-to-plist (cdr ctl)) (car ctl))
+
+            ;; what really needs to be done here is a way to link a
+            ;; MIME handle back to it's parent MIME handle (in a multilevel
+            ;; MIME article).  That would probably require changing
+            ;; the mm-handle API so we simply store the multipart buffert
+            ;; name as a text property of the "multipart/whatever" string.
+             (add-text-properties 0 (length (car ctl))
+                                 (list 'buffer (mm-copy-to-buffer))
+                                  (car ctl))
             (cons (car ctl) (mm-dissect-multipart ctl))))
          (t
           (mm-dissect-singlepart
@@ -378,6 +418,16 @@ to:
       (insert-buffer-substring obuf beg)
       (current-buffer))))
 
+(defun mm-display-parts (handle &optional no-default)
+  (if (stringp (car handle))
+      (mapcar 'mm-display-parts (cdr handle))
+    (if (bufferp (car handle))
+       (save-restriction
+         (narrow-to-region (point) (point))
+         (mm-display-part handle)
+         (goto-char (point-max)))
+      (mapcar 'mm-display-parts handle))))
+
 (defun mm-display-part (handle &optional no-default)
   "Display the MIME part represented by HANDLE.
 Returns nil if the part is removed; inline if displayed inline;
@@ -540,8 +590,8 @@ external if displayed external."
       (while (setq handle (pop handles))
        (cond
         ((stringp handle)
-         ;; Do nothing.
-         )
+         (when (buffer-live-p (get-text-property 0 'buffer handle))
+           (kill-buffer (get-text-property 0 'buffer handle))))
         ((and (listp handle)
               (stringp (car handle)))
          (mm-remove-parts (cdr handle)))
@@ -557,8 +607,8 @@ external if displayed external."
       (while (setq handle (pop handles))
        (cond
         ((stringp handle)
-         ;; Do nothing.
-         )
+         (when (buffer-live-p (get-text-property 0 'buffer handle))
+           (kill-buffer (get-text-property 0 'buffer handle))))
         ((and (listp handle)
               (stringp (car handle)))
          (mm-destroy-parts (cdr handle)))
@@ -885,14 +935,21 @@ external if displayed external."
   (and (mm-valid-image-format-p format)
        (mm-image-fit-p handle)))
 
-(defun mm-find-part-by-type (handles type &optional notp) 
+(defun mm-find-part-by-type (handles type &optional notp recursive) 
+  "Search in HANDLES for part with TYPE.
+If NOTP, returns first non-matching part.
+If RECURSIVE, search recursively."
   (let (handle)
     (while handles
-      (if (if notp
-             (not (equal (mm-handle-media-type (car handles)) type))
-           (equal (mm-handle-media-type (car handles)) type))
-         (setq handle (car handles)
-               handles nil))
+      (if (and recursive (stringp (caar handles)))
+         (if (setq handle (mm-find-part-by-type (cdar handles) type
+                                                notp recursive))
+             (setq handles nil))
+       (if (if notp
+               (not (equal (mm-handle-media-type (car handles)) type))
+             (equal (mm-handle-media-type (car handles)) type))
+           (setq handle (car handles)
+                 handles nil)))
       (setq handles (cdr handles)))
     handle))
 
@@ -939,41 +996,73 @@ external if displayed external."
 
 (defun mm-possibly-verify-or-decrypt (parts ctl)
   (let ((subtype (cadr (split-string (car ctl) "/")))
-       protocol func)
+       protocol func functest)
     (cond 
      ((equal subtype "signed")
-      (setq protocol (mail-content-type-get ctl 'protocol))
-      (setq func (cdr (assoc protocol mm-verify-function-alist)))
+      (unless (and (setq protocol (mail-content-type-get ctl 'protocol))
+                  (not (equal protocol "multipart/mixed")))
+       ;; The message is broken or draft-ietf-openpgp-multsig-01.
+       (let ((protocols mm-verify-function-alist))
+         (while protocols
+           (if (and (or (not (setq functest (nth 3 (car protocols))))
+                        (funcall functest parts ctl))
+                    (mm-find-part-by-type parts (caar protocols) nil t))
+               (setq protocol (caar protocols)
+                     protocols nil)
+             (setq protocols (cdr protocols))))))
+      (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
       (if (cond
           ((eq mm-verify-option 'never) nil)
           ((eq mm-verify-option 'always) t)
-          ((eq mm-verify-option 'known) func)
-          (t (y-or-n-p 
-              (format "Verify signed part(protocol=%s)?" protocol))))
+          ((eq mm-verify-option 'known) 
+           (and func 
+                (or (not (setq functest 
+                               (nth 3 (assoc protocol 
+                                             mm-verify-function-alist))))
+                    (funcall functest parts ctl))))
+          (t (y-or-n-p
+              (format "Verify signed (%s) part? "
+                      (or (nth 2 (assoc protocol mm-verify-function-alist))
+                          (format "protocol=%s" protocol))))))
          (condition-case err
              (save-excursion
                (if func
                    (funcall func parts ctl)
-                 (error (format "Unknown sign protocol(%s)" protocol))))
+                 (error (format "Unknown sign protocol (%s)" protocol))))
            (error
-            (unless (y-or-n-p (format "%s, continue?" err))
+            (unless (y-or-n-p (format "%s, continue? " err))
               (error "Verify failure."))))))
      ((equal subtype "encrypted")
-      (setq protocol (mail-content-type-get ctl 'protocol))
-      (setq func (cdr (assoc protocol mm-decrypt-function-alist)))
+      (unless (setq protocol (mail-content-type-get ctl 'protocol))
+       ;; The message is broken.
+       (let ((parts parts))
+         (while parts
+           (if (assoc (mm-handle-media-type (car parts)) 
+                      mm-decrypt-function-alist)
+               (setq protocol (mm-handle-media-type (car parts))
+                     parts nil)
+             (setq parts (cdr parts))))))
+      (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
       (if (cond
           ((eq mm-decrypt-option 'never) nil)
           ((eq mm-decrypt-option 'always) t)
-          ((eq mm-decrypt-option 'known) func)
+          ((eq mm-decrypt-option 'known)
+           (and func 
+                (or (not (setq functest 
+                               (nth 3 (assoc protocol 
+                                             mm-decrypt-function-alist))))
+                    (funcall functest parts ctl))))
           (t (y-or-n-p 
-              (format "Decrypt part (protocol=%s)?" protocol))))
+              (format "Decrypt (%s) part? "
+                      (or (nth 2 (assoc protocol mm-decrypt-function-alist))
+                          (format "protocol=%s" protocol))))))
          (condition-case err
              (save-excursion
                (if func
                    (setq parts (funcall func parts ctl))
-                 (error (format "Unknown encrypt protocol(%s)" protocol))))
+                 (error (format "Unknown encrypt protocol (%s)" protocol))))
            (error
-            (unless (y-or-n-p (format "%s, continue?" err))
+            (unless (y-or-n-p (format "%s, continue? " err))
               (error "Decrypt failure."))))))
      (t nil))
     parts))