Re: S/MIME on SEMI (Re: smime.el)

Hiroya Murata <[email protected]> Wed, 05 Jul 2006 00:18:21 +0900
Newsgroups gmane.mail.emacs.mime.japanese
Message-ID <umzbp5rgi.wl%[email protected]>
In the message [emacs-mime-ja : No.02056]
   on Mon, 03 Jul 2006 20:35:15 +0900, Yuuichi Teranishi wrote:

> 必要に迫られて Windows の標準 API で S/MIME on Emacs するしかけを
> (お勉強がてら)作ってみています.

> http://wiki.gohome.org/mopp/

ちょっと使ってみました. あんまり自動で起動されるのは好きではないの
で, ユーザアクションで検証を開始して, 結果を MIME-Echo バッファに
表示出来る様にしてみました. 非同期のままなので一寸使い難いですが,
せっかく作ったのでパッチを付けます. (小くする為に -w を指定してい
ます. そのまま当てるとインデントがおかしくなります)

-- 
Hiroya Murata (村田 浩也) <[email protected]>
PGP fingerprint: 53B6 1B4A 8193 A2D4 1526  BC9E 9AEF 2F6D 249D 5F17
mime-mopp.el.patch (application/octet-stream, 4.2 KB)
--- ../share/mopp/lisp/mime-mopp.el	2006-07-03 12:53:06.000000000 +0900
+++ mime-mopp.el	2006-07-04 23:58:48.045625000 +0900
@@ -31,6 +31,7 @@
 
 (require 'mopp)
 (require 'mime-view)
+(require 'mime-play)
 (require 'mime-edit)
 
 (defcustom mime-mopp-sign-detached t
@@ -44,7 +45,22 @@
 certificate candidates.
 It can be `t' to sign by the `From:' address."
   :group 'mopp
-  :type '(repeat string))
+  :type '(choice (const :tag "By the From: field" t)
+		 (repeat :tag "Specify"
+			 (string :tag "address"))))
+
+(defcustom mime-mopp-auto-verify t
+  "If non-nil, verify a signature automatically in displaying message."
+  :group 'mopp
+  :type boolean)
+
+(defcustom mime-mopp-display-result-function
+  'mime-mopp-display-verify-result-icon
+  "A function to display verification results."
+  :group 'mopp
+  :type '(choice (const :tag "Icon" mime-mopp-display-verify-result-icon)
+		 (const :tag "Popup" mime-mopp-popup-verify-result)
+		 (function :tag "Other function")))
 
 (defvar mime-mopp-signer nil)
 (make-variable-buffer-local 'mime-mopp-signer)
@@ -85,6 +101,9 @@
 			   "^application/\\(x-\\)?pkcs7-signature"
 			   (get-text-property (point-min) 'mime-view-entity)))))
       (dolist (entity entities)
+	(mime-mopp-verify-entity entity)))))
+
+(defun mime-mopp-verify-entity (entity &optional situation)
 	(let* ((entity-node-id (mime-entity-node-id entity))
 	       (mother (mime-entity-parent entity))
 	       (knum (car entity-node-id))
@@ -100,7 +119,7 @@
 	  (setq node-id (mime-entity-node-id mother))
 	  (write-region-as-binary (mime-entity-content entity)
 				  nil sfile nil 'silent)
-	  (unwind-protect
+    (condition-case nil
 	      (progn
 		(with-temp-buffer
 		  (mime-insert-entity orig-entity)
@@ -121,14 +140,25 @@
 				   (mapcar #'std11-address-string
 					   (std11-parse-addresses-string
 					    sender))))
-			     'mime-mopp-verify-result
+		       (lambda (object app-data)
+			 (unwind-protect
+			     (funcall mime-mopp-display-result-function
+				      object app-data)
+			   (dolist (temp-file (nth 1 app-data))
+			     (when (and temp-file
+					(file-writable-p temp-file))
+			       (delete-file temp-file)))))
 			     cfile
 			     (list (cons (current-buffer) node-id)
-				   (list sfile cfile))))))))))
+			     (list sfile cfile))))
+      (error
+       (dolist (temp-file (list sfile cfile))
+	 (when (and temp-file
+		    (file-writable-p temp-file))
+	   (delete-file temp-file)))))))
 
-(defun mime-mopp-verify-result (object app-data)
+(defun mime-mopp-display-verify-result-icon (object app-data)
   ;; ((BUFFER . NODE-ID) (SIGNATURE-FILE CONTENT-FILE))
-  (unwind-protect
       (when (buffer-live-p (car (car app-data)))
 	(with-current-buffer (car (car app-data))
 	  (save-excursion
@@ -157,13 +187,13 @@
 				     mopp-verified-xpm) 'xpm t
 				     :ascent 'center)
 				  'help-echo
-				  (mopp-object-to-string object)))))))
-    (when (and (car (nth 1 app-data))
-	       (file-writable-p (car (nth 1 app-data))))
-      (delete-file (car (nth 1 app-data))))
-    (when (and (nth 1 (nth 1 app-data))
-	       (file-writable-p (nth 1 (nth 1 app-data))))
-      (delete-file (nth 1 (nth 1 app-data))))))
+			      (mopp-object-to-string object))))))))
+
+(defun mime-mopp-popup-verify-result (object app-data)
+  (when (buffer-live-p (car (car app-data)))
+    (with-current-buffer (car (car app-data))
+      (save-excursion
+	(mime-show-echo-buffer "%s" (mopp-object-to-string object))))))
 
 (defvar mime-mopp-buffer-signed nil)
 (make-variable-buffer-local 'mime-mopp-buffer-signed)
@@ -277,7 +307,21 @@
 	  (setq entries (unless result (cdr entries))))))
     (setq mime-mopp-signer result)))
 
-(add-hook 'mime-view-mode-hook 'mime-mopp-verify)
+(when mime-mopp-auto-verify
+  (add-hook 'mime-view-mode-hook 'mime-mopp-verify))
+
+(eval-after-load "mime-view"
+  '(progn
+     (mime-add-condition
+      'action
+      '((type . application)(subtype . pkcs7-signature)
+	(method . mime-mopp-verify-entity)))
+
+     (mime-add-condition
+      'action
+      '((type . application)(subtype . x-pkcs7-signature)
+	(method . mime-mopp-verify-entity)))))
+
 (add-hook 'mime-edit-translate-buffer-hook 'mime-edit-smime-enclose-buffer)
 
 (provide 'mime-mopp)