* gnus-sum.el (gnus-summary-expire-articles): Save excursion.
[gnus] / lisp / mm-decode.el
index f1fac96..786a3be 100644 (file)
 
 (eval-and-compile
   (autoload 'mm-inline-partial "mm-partial")
-  (autoload 'mm-inline-external-body "mm-extern"))
+  (autoload 'mm-inline-external-body "mm-extern")
+  (autoload 'mm-insert-inline "mm-view"))
 
 (defgroup mime-display ()
   "Display of MIME in mail and news articles."
   :link '(custom-manual "(emacs-mime)Customization")
+  :version "21.1"
+  :group 'mail
+  :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)
   `(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)
@@ -225,13 +239,20 @@ to:
 (defvar mm-dissect-default-type "text/plain")
 
 (autoload 'mml2015-verify "mml2015")
+(autoload 'mml2015-verify-test "mml2015")
+(autoload 'mml-smime-verify "mml-smime")
+(autoload 'mml-smime-verify-test "mml-smime")
 
 (defvar mm-verify-function-alist
-  '(("application/pgp-signature" mml2015-verify "PGP")
-    ("application/pkcs7-signature" mml-smime-verify "S/MIME")
-    ("application/x-pkcs7-signature" mml-smime-verify "S/MIME")))
-
-(defcustom mm-verify-option nil
+  '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
+    ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP" 
+     mm-uu-pgp-signed-test)
+    ("application/pkcs7-signature" mml-smime-verify "S/MIME" 
+     mml-smime-verify-test)
+    ("application/x-pkcs7-signature" mml-smime-verify "S/MIME" 
+     mml-smime-verify-test)))
+
+(defcustom mm-verify-option 'never
   "Option of verifying signed parts.
 `never', not verify; `always', always verify; 
 `known', only verify known protocols. Otherwise, ask user."
@@ -239,32 +260,34 @@ 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 "PGP")))
+  '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
+    ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP" 
+     mm-uu-pgp-encrypted-test)))
 
 (defcustom mm-decrypt-option nil
-  "Option of decrypting signed parts.
+  "Option of decrypting encrypted parts.
 `never', not decrypt; `always', always decrypt; 
 `known', only decrypt 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)
 
-(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)
+(defvar mm-viewer-completion-map
+  (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
+    (set-keymap-parent map minibuffer-local-completion-map)
+    map)
+  "Keymap for input viewer with completion.")
+
+;; Should we bind other key to minibuffer-complete-word?
+(define-key mm-viewer-completion-map " " 'self-insert-command) 
 
 (defvar mm-viewer-completion-map
   (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
@@ -277,6 +300,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
@@ -313,6 +354,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
@@ -356,7 +408,7 @@ to:
                    (match-beginning 0)
                  (point-max)))))
     (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
-    (while (re-search-forward boundary end t)
+    (while (and (< (point) end) (re-search-forward boundary end t))
       (goto-char (match-beginning 0))
       (when start
        (save-excursion
@@ -365,7 +417,7 @@ to:
            (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
       (forward-line 2)
       (setq start (point)))
-    (when start
+    (when (and start (< start end))
       (save-excursion
        (save-restriction
          (narrow-to-region start end)
@@ -384,6 +436,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;
@@ -546,8 +608,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)))
@@ -563,11 +625,11 @@ 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)))
+         (mm-destroy-parts handle))
         (t
          (mm-destroy-part handle)))))))
 
@@ -693,7 +755,12 @@ external if displayed external."
 (defun mm-get-part (handle)
   "Return the contents of HANDLE as a string."
   (mm-with-unibyte-buffer
-    (mm-insert-part handle)
+    (insert (with-current-buffer (mm-handle-buffer handle)
+             (mm-with-unibyte-current-buffer-mule4
+               (buffer-string))))
+    (mm-decode-content-transfer-encoding
+     (mm-handle-encoding handle)
+     (mm-handle-media-type handle))
     (buffer-string)))
 
 (defun mm-insert-part (handle)
@@ -891,23 +958,29 @@ 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 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))
 
 (defun mm-find-raw-part-by-type (ctl type &optional notp) 
   (goto-char (point-min))
-  (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
-        (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
+  (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl 
+                                                                  'boundary)))
+        (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
         start
         (end (save-excursion
                (goto-char (point-max))
@@ -915,14 +988,14 @@ If NOTP, returns first non-matching part."
                    (match-beginning 0)
                  (point-max))))
         result)
-    (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
+    (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
     (while (and (not result)
                (re-search-forward boundary end t))
       (goto-char (match-beginning 0))
       (when start
        (save-excursion
          (save-restriction
-           (narrow-to-region start (point))
+           (narrow-to-region start (1- (point)))
            (when (let ((ctl (ignore-errors 
                               (mail-header-parse-content-type 
                                (mail-fetch-field "content-type")))))
@@ -930,7 +1003,7 @@ If NOTP, returns first non-matching part."
                        (not (equal (car ctl) type))
                      (equal (car ctl) type)))
              (setq result (buffer-substring (point-min) (point-max)))))))
-      (forward-line 2)
+      (forward-line 1)
       (setq start (point)))
     (when (and (not result) start)
       (save-excursion
@@ -945,48 +1018,89 @@ If NOTP, returns first non-matching part."
            (setq result (buffer-substring (point-min) (point-max)))))))
     result))
 
+(defvar mm-security-handle nil)
+(defvar mm-security-from nil)
+
+(defsubst mm-set-handle-multipart-parameter (handle parameter value)
+  ;; HANDLE could be a CTL.
+  (if handle
+      (put-text-property 0 (length (car handle)) parameter value 
+                        (car handle))))
+
 (defun mm-possibly-verify-or-decrypt (parts ctl)
   (let ((subtype (cadr (split-string (car ctl) "/")))
-       protocol func)
+       (mm-security-handle ctl) ;; (car CTL) is the type.
+       (mm-security-from
+        (save-restriction
+          (mail-narrow-to-head)
+          (cadr (mail-extract-address-components 
+                 (or (mail-fetch-field "from") "")))))
+       protocol func functest)
     (cond 
      ((equal subtype "signed")
-      (setq protocol (mail-content-type-get ctl 'protocol))
+      (unless (and (setq protocol 
+                        (mm-handle-multipart-ctl-parameter 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)
+          ((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
-            (unless (y-or-n-p (format "%s, continue? " err))
-              (error "Verify failure."))))))
+         (save-excursion
+           (if func
+               (funcall func parts ctl)
+             (mm-set-handle-multipart-parameter 
+              mm-security-handle 'gnus-details 
+              (format "Unknown sign protocol (%s)" protocol))))))
      ((equal subtype "encrypted")
-      (setq protocol (mail-content-type-get ctl 'protocol))
+      (unless (setq protocol 
+                   (mm-handle-multipart-ctl-parameter 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 (%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
-            (unless (y-or-n-p (format "%s, continue? " err))
-              (error "Decrypt failure."))))))
+         (save-excursion
+           (if func
+               (setq parts (funcall func parts ctl))
+             (mm-set-handle-multipart-parameter 
+              mm-security-handle 'gnus-details 
+              (format "Unknown encrypt protocol (%s)" protocol))))))
      (t nil))
     parts))