Fixed.
[elisp/epg.git] / epg.el
diff --git a/epg.el b/epg.el
index aa06274..62c02fc 100644 (file)
--- a/epg.el
+++ b/epg.el
@@ -26,7 +26,8 @@
 ;;; Code:
 
 (defgroup epg ()
-  "The EasyPG Library")
+  "The EasyPG Library"
+  :group 'emacs)
 
 (defcustom epg-gpg-program "gpg"
   "The `gpg' executable."
     (3 . "Certificate Chain too long")
     (4 . "Error storing certificate")))
 
+(defconst epg-no-data-reason-alist
+  '((1 . "No armored data")
+    (2 . "Expected a packet but did not found one")
+    (3 . "Invalid packet found, this may indicate a non OpenPGP message")
+    (4 . "Signature expected but not found")))
+
+(defconst epg-unexpected-reason-alist nil)
+
 (defvar epg-key-validity-alist
   '((?o . unknown)
     (?i . invalid)
                cipher-algorithm digest-algorithm compress-algorithm
                #'epg-passphrase-callback-function
                #'epg-progress-callback-function
-               nil nil nil nil)))
+               nil nil nil nil nil)))
 
 (defun epg-context-protocol (context)
   "Return the protocol used within CONTEXT."
@@ -275,6 +284,12 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aref (cdr context) 12))
 
+(defun epg-context-operation (context)
+  "Return the name of the current cryptographic operation."
+  (unless (eq (car context) 'epg-context)
+    (signal 'wrong-type-argument (list 'epg-context-p context)))
+  (aref (cdr context) 13))
+
 (defun epg-context-set-protocol (context protocol)
   "Set the protocol used within CONTEXT."
   (unless (eq (car context) 'epg-context)
@@ -356,6 +371,12 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aset (cdr context) 12 result))
 
+(defun epg-context-set-operation (context operation)
+  "Set the name of the current cryptographic operation."
+  (unless (eq (car context) 'epg-context)
+    (signal 'wrong-type-argument (list 'epg-context-p context)))
+  (aset (cdr context) 13 operation))
+
 (defun epg-make-signature (status &optional key-id)
   "Return a signature object."
   (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil)))
@@ -872,12 +893,12 @@ This function is for internal use only."
     (setq epg-pending-status-list status-list)
     (while (and (eq (process-status (epg-context-process context)) 'run)
                epg-pending-status-list)
-      (accept-process-output (epg-context-process context) 0 1))))
+      (accept-process-output (epg-context-process context) 1))))
 
 (defun epg-wait-for-completion (context)
   "Wait until the `epg-gpg-program' process completes."
   (while (eq (process-status (epg-context-process context)) 'run)
-    (accept-process-output (epg-context-process context) 0 1)))
+    (accept-process-output (epg-context-process context) 1)))
 
 (defun epg-reset (context)
   "Reset the CONTEXT."
@@ -1026,7 +1047,7 @@ This function is for internal use only."
           (epg-context-result-for epg-context 'verify))))
 
 (defun epg-status-ERRSIG (process string)
-  (let ((signatures (car (epg-context-result-for epg-context 'verify))))
+  (let ((signatures (epg-context-result-for epg-context 'verify)))
     (unless signatures
       (setq signatures (list (epg-make-signature 'error)))
       (epg-context-set-result-for epg-context 'verify signatures))
@@ -1256,11 +1277,12 @@ This function is for internal use only."
             (epg-context-result-for epg-context 'error)))))
 
 (defun epg-passphrase-callback-function (context key-id handback)
-  (read-passwd
-   (if (eq key-id 'SYM)
-       "Passphrase for symmetric encryption: "
+  (if (eq key-id 'SYM)
+      (read-passwd "Passphrase for symmetric encryption: "
+                  (eq (epg-context-operation context) 'encrypt))
+    (read-passwd
      (if (eq key-id 'PIN)
-        "Passphrase for PIN: "
+       "Passphrase for PIN: "
        (let ((entry (assoc key-id epg-user-id-alist)))
         (if entry
             (format "Passphrase for %s %s: " key-id (cdr entry))
@@ -1336,24 +1358,6 @@ This function is for internal use only."
    (aref line 5)
    (aref line 6)))
 
-(defun epg-list-keys-postprocess-one-key (key)
-  (let (key-id user-id-string entry)
-    (epg-key-set-sub-key-list
-     key
-     (nreverse (epg-key-sub-key-list key)))
-    (epg-key-set-user-id-list
-     key
-     (nreverse (epg-key-user-id-list key)))
-    (setq key-id
-         (epg-sub-key-id (car (epg-key-sub-key-list key)))
-         user-id-string
-         (epg-user-id-string (car (epg-key-user-id-list key)))
-         entry (assoc key-id epg-user-id-alist))
-    (if entry
-       (setcdr entry user-id-string)
-      (setq epg-user-id-alist (cons (cons key-id user-id-string)
-                                   epg-user-id-alist)))))
-
 ;;;###autoload
 (defun epg-list-keys (context &optional name mode)
   "Return a list of epg-key objects matched with NAME.
@@ -1362,12 +1366,10 @@ If MODE is t or 'secret, only secret keyring should be searched.
 Otherwise, only public keyring should be searched and the key
 signatures should be included."
   (let ((lines (epg-list-keys-1 context name mode))
-       keys cert)
+       keys cert pointer pointer-1)
     (while lines
       (cond
        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
-       (if (car keys)
-           (epg-list-keys-postprocess-one-key (car keys)))
        (setq cert (member (aref (car lines) 0) '("crt" "crs"))
              keys (cons (epg-make-key
                          (if (aref (car lines) 8)
@@ -1417,9 +1419,22 @@ signatures should be included."
          (epg-user-id-signature-list
           (car (epg-key-user-id-list (car keys))))))))
       (setq lines (cdr lines)))
-    (if (car keys)
-       (epg-list-keys-postprocess-one-key (car keys)))
-    (nreverse keys)))
+    (setq keys (nreverse keys)
+         pointer keys)
+    (while pointer
+      (epg-key-set-sub-key-list
+       (car pointer)
+       (nreverse (epg-key-sub-key-list (car pointer))))
+      (setq pointer-1 (epg-key-set-user-id-list
+                         (car pointer)
+                         (nreverse (epg-key-user-id-list (car pointer)))))
+      (while pointer-1
+       (epg-user-id-set-signature-list
+        (car pointer-1)
+        (nreverse (epg-user-id-signature-list (car pointer-1))))
+       (setq pointer-1 (cdr pointer-1)))
+      (setq pointer (cdr pointer)))
+    keys))
 
 (if (fboundp 'make-temp-file)
     (defalias 'epg-make-temp-file 'make-temp-file)
@@ -1499,6 +1514,7 @@ If you are unsure, use synchronous version of this function
 `epg-decrypt-file' or `epg-decrypt-string' instead."
   (unless (epg-data-file cipher)
     (error "Not a file"))
+  (epg-context-set-operation context 'decrypt)
   (epg-context-set-result context nil)
   (epg-start context (list "--decrypt" (epg-data-file cipher)))
   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
@@ -1560,6 +1576,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-verify-file' or `epg-verify-string' instead."
+  (epg-context-set-operation context 'verify)
   (epg-context-set-result context nil)
   (if signed-text
       ;; Detached signature.
@@ -1657,6 +1674,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-sign-file' or `epg-sign-string' instead."
+  (epg-context-set-operation context 'sign)
   (epg-context-set-result context nil)
   (epg-start context
             (append (list (if (memq mode '(t detached))
@@ -1742,6 +1760,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-encrypt-file' or `epg-encrypt-string' instead."
+  (epg-context-set-operation context 'encrypt)
   (epg-context-set-result context nil)
   (epg-start context
             (append (if always-trust '("--always-trust"))
@@ -1837,6 +1856,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
+  (epg-context-set-operation context 'export-keys)
   (epg-context-set-result context nil)
   (epg-start context (cons "--export"
                           (mapcar
@@ -1880,6 +1900,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
+  (epg-context-set-operation context 'import-keys)
   (epg-context-set-result context nil)
   (epg-start context (list "--import" (epg-data-file keys)))
   (when (epg-data-string keys)
@@ -1919,6 +1940,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
+  (epg-context-set-operation context 'receive-keys)
   (epg-context-set-result context nil)
   (epg-start context (cons "--recv-keys" key-id-list)))
 
@@ -1947,6 +1969,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-delete-keys' instead."
+  (epg-context-set-operation context 'delete-keys)
   (epg-context-set-result context nil)
   (epg-start context (cons (if allow-secret
                               "--delete-secret-key"
@@ -1978,6 +2001,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-sign-keys' instead."
+  (epg-context-set-operation context 'sign-keys)
   (epg-context-set-result context nil)
   (epg-start context (cons (if local
                               "--lsign-key"
@@ -2010,6 +2034,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
+  (epg-context-set-operation context 'generate-key)
   (epg-context-set-result context nil)
   (if (epg-data-file parameters)
       (epg-start context (list "--batch" "--genkey"