+;; Shouldn't these functions check whether the user even wants to use
+;; font-lock? At least under XEmacs, this fontification is pretty
+;; much unconditional. Also, it would be nice to change for the size
+;; of the fontified region.
+
+(defun mm-display-patch-inline (handle)
+ (mm-display-inline-fontify handle 'diff-mode))
+
+(defun mm-display-elisp-inline (handle)
+ (mm-display-inline-fontify handle 'emacs-lisp-mode))
+
+(defun mm-display-dns-inline (handle)
+ (mm-display-inline-fontify handle 'dns-mode))
+
+;; id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
+;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
+(defvar mm-pkcs7-signed-magic
+ (mm-string-as-unibyte
+ (mapconcat 'char-to-string
+ (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
+ ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
+ ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
+ ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x02) "")))
+
+;; id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
+;; us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
+(defvar mm-pkcs7-enveloped-magic
+ (mm-string-as-unibyte
+ (mapconcat 'char-to-string
+ (list ?\x30 ?\x5c ?\x28 ?\x80 ?\x5c ?\x7c ?\x81 ?\x2e ?\x5c
+ ?\x7c ?\x82 ?\x2e ?\x2e ?\x5c ?\x7c ?\x83 ?\x2e ?\x2e
+ ?\x2e ?\x5c ?\x29 ?\x06 ?\x09 ?\x5c ?\x2a ?\x86 ?\x48
+ ?\x86 ?\xf7 ?\x0d ?\x01 ?\x07 ?\x03) "")))
+
+(defun mm-view-pkcs7-get-type (handle)
+ (mm-with-unibyte-buffer
+ (mm-insert-part handle)
+ (cond ((looking-at mm-pkcs7-enveloped-magic)
+ 'enveloped)
+ ((looking-at mm-pkcs7-signed-magic)
+ 'signed)
+ (t
+ (error "Could not identify PKCS#7 type")))))
+
+(defun mm-view-pkcs7 (handle)
+ (case (mm-view-pkcs7-get-type handle)
+ (enveloped (mm-view-pkcs7-decrypt handle))
+ (signed (mm-view-pkcs7-verify handle))
+ (otherwise (error "Unknown or unimplemented PKCS#7 type"))))
+
+(defun mm-view-pkcs7-verify (handle)
+ ;; A bogus implementation of PKCS#7. FIXME::
+ (mm-insert-part handle)
+ (goto-char (point-min))
+ (if (search-forward "Content-Type: " nil t)
+ (delete-region (point-min) (match-beginning 0)))
+ (goto-char (point-max))
+ (if (re-search-backward "--\r?\n?" nil t)
+ (delete-region (match-end 0) (point-max)))
+ (goto-char (point-min))
+ (while (search-forward "\r\n" nil t)
+ (replace-match "\n"))
+ (message "Verify signed PKCS#7 message is unimplemented.")
+ (sit-for 1)
+ t)
+
+(defun mm-view-pkcs7-decrypt (handle)
+ (insert-buffer-substring (mm-handle-buffer handle))
+ (goto-char (point-min))
+ (insert "MIME-Version: 1.0\n")
+ (mm-insert-headers "application/pkcs7-mime" "base64" "smime.p7m")
+ (smime-decrypt-region
+ (point-min) (point-max)
+ (if (= (length smime-keys) 1)
+ (cadar smime-keys)
+ (smime-get-key-by-email
+ (completing-read
+ (concat "Decipher using which key? "
+ (if smime-keys (concat "(default " (caar smime-keys) ") ")
+ ""))
+ smime-keys nil nil nil nil (car-safe (car-safe smime-keys))))))
+ (goto-char (point-min))
+ (while (search-forward "\r\n" nil t)
+ (replace-match "\n"))
+ (goto-char (point-min)))
+