Fix timing problem of Bug#2412.
[bpt/emacs.git] / lisp / epg.el
index 4e6824a..3c80ef9 100644 (file)
@@ -1,16 +1,16 @@
 ;;; epg.el --- the EasyPG Library
 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 
 ;; Author: Daiki Ueno <ueno@unixuser.org>
 ;; Keywords: PGP, GnuPG
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,9 +18,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Code:
 
   (cons 'epg-context
        (vector (or protocol 'OpenPGP) armor textmode include-certs
                cipher-algorithm digest-algorithm compress-algorithm
-               #'epg-passphrase-callback-function
+               (list #'epg-passphrase-callback-function)
                nil
                nil nil nil nil nil nil)))
 
   (aref (cdr context) 0))
 
 (defun epg-context-armor (context)
-  "Return t if the output shouled be ASCII armored in CONTEXT."
+  "Return t if the output should be ASCII armored in CONTEXT."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aref (cdr context) 1))
   (aref (cdr context) 2))
 
 (defun epg-context-include-certs (context)
-  "Return how many certificates should be included in an S/MIME signed
-message."
+  "Return how many certificates should be included in an S/MIME signed message."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aref (cdr context) 3))
@@ -247,13 +244,13 @@ message."
   (aref (cdr context) 8))
 
 (defun epg-context-signers (context)
-  "Return the list of key-id for singning."
+  "Return the list of key-id for signing."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aref (cdr context) 9))
 
 (defun epg-context-sig-notations (context)
-  "Return the list of notations for singning."
+  "Return the list of notations for signing."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aref (cdr context) 10))
@@ -291,7 +288,7 @@ This function is for internal use only."
   (aset (cdr context) 0 protocol))
 
 (defun epg-context-set-armor (context armor)
-  "Specify if the output shouled be ASCII armored in CONTEXT."
+  "Specify if the output should be ASCII armored in CONTEXT."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aset (cdr context) 1 armor))
@@ -331,7 +328,9 @@ This function is for internal use only."
   "Set the function used to query passphrase."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
-  (aset (cdr context) 7 passphrase-callback))
+  (aset (cdr context) 7 (if (consp passphrase-callback)
+                           passphrase-callback
+                         (list passphrase-callback))))
 
 (defun epg-context-set-progress-callback (context
                                          progress-callback)
@@ -339,16 +338,18 @@ This function is for internal use only."
 If optional argument HANDBACK is specified, it is passed to PROGRESS-CALLBACK."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
-  (aset (cdr context) 8 progress-callback))
+  (aset (cdr context) 8 (if (consp progress-callback)
+                           progress-callback
+                         (list progress-callback))))
 
 (defun epg-context-set-signers (context signers)
-  "Set the list of key-id for singning."
+  "Set the list of key-id for signing."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aset (cdr context) 9 signers))
 
 (defun epg-context-set-sig-notations (context notations)
-  "Set the list of notations for singning."
+  "Set the list of notations for signing."
   (unless (eq (car-safe context) 'epg-context)
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aset (cdr context) 10 notations))
@@ -786,7 +787,7 @@ This function is for internal use only."
 
 (defun epg-make-import-status (fingerprint &optional reason new user-id
                                           signature sub-key secret)
-  "Return a import status object."
+  "Return an import status object."
   (cons 'epg-import-status (vector fingerprint reason new user-id signature
                                   sub-key secret)))
 
@@ -838,7 +839,7 @@ This function is for internal use only."
                                          secret-read secret-imported
                                          secret-unchanged not-imported
                                          imports)
-  "Return a import result object."
+  "Return an import result object."
   (cons 'epg-import-result (vector considered no-user-id imported imported-rsa
                                   unchanged new-user-ids new-sub-keys
                                   new-signatures new-revocations secret-read
@@ -1167,12 +1168,15 @@ 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) 1))))
+      (accept-process-output (epg-context-process context) 1))
+    (if epg-pending-status-list
+       (epg-context-set-result-for context 'error 'exit))))
 
 (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) 1)))
+    (accept-process-output (epg-context-process context) 1))
+  (sleep-for 0.1))
 
 (defun epg-reset (context)
   "Reset the CONTEXT."
@@ -1242,13 +1246,10 @@ This function is for internal use only."
              (progn
                (setq passphrase
                      (funcall
-                      (if (consp (epg-context-passphrase-callback context))
-                          (car (epg-context-passphrase-callback context))
-                        (epg-context-passphrase-callback context))
+                      (car (epg-context-passphrase-callback context))
                       context
                       epg-key-id
-                      (if (consp (epg-context-passphrase-callback context))
-                          (cdr (epg-context-passphrase-callback context)))))
+                      (cdr (epg-context-passphrase-callback context))))
                (when passphrase
                  (setq passphrase-with-new-line (concat passphrase "\n"))
                  (epg--clear-string passphrase)
@@ -1430,7 +1431,7 @@ This function is for internal use only."
        signature
        (string-to-number (match-string 4 string)))
       (epg-signature-set-pubkey-algorithm
-       signature 
+       signature
        (string-to-number (match-string 5 string)))
       (epg-signature-set-digest-algorithm
        signature
@@ -1496,16 +1497,13 @@ This function is for internal use only."
   (if (and (epg-context-progress-callback context)
           (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
                         string))
-      (funcall (if (consp (epg-context-progress-callback context))
-                  (car (epg-context-progress-callback context))
-                (epg-context-progress-callback context))
+      (funcall (car (epg-context-progress-callback context))
               context
               (match-string 1 string)
               (match-string 2 string)
               (string-to-number (match-string 3 string))
               (string-to-number (match-string 4 string))
-              (if (consp (epg-context-progress-callback context))
-                  (cdr (epg-context-progress-callback context))))))
+              (cdr (epg-context-progress-callback context)))))
 
 (defun epg--status-ENC_TO (context string)
   (if (string-match "\\`\\([0-9A-Za-z]+\\) \\([0-9]+\\) \\([0-9]+\\)" string)
@@ -1744,7 +1742,7 @@ This function is for internal use only."
 (defun epg-list-keys (context &optional name mode)
   "Return a list of epg-key objects matched with NAME.
 If MODE is nil or 'public, only public keyring should be searched.
-If MODE is t or 'secret, only secret keyring should be searched. 
+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.
 NAME is either a string or a list of strings."
@@ -2018,8 +2016,12 @@ If you are unsure, use synchronous version of this function
            (process-send-eof (epg-context-process context))))
     ;; Normal (or cleartext) signature.
     (if (epg-data-file signature)
-       (epg--start context (list "--" (epg-data-file signature)))
-      (epg--start context '("-"))
+       (epg--start context (if (eq (epg-context-protocol context) 'CMS)
+                               (list "--verify" "--" (epg-data-file signature))
+                             (list "--" (epg-data-file signature))))
+      (epg--start context (if (eq (epg-context-protocol context) 'CMS)
+                             '("--verify" "-")
+                           '("-")))
       (if (eq (process-status (epg-context-process context)) 'run)
          (process-send-string (epg-context-process context)
                               (epg-data-string signature)))
@@ -2412,7 +2414,7 @@ If you use this function, you will need to wait for the completion of
 `epg-gpg-program' by using `epg-wait-for-completion' and call
 `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-receive-keys' instead."
   (epg-context-set-operation context 'receive-keys)
   (epg-context-set-result context nil)
   (epg--start context (cons "--recv-keys" key-id-list)))
@@ -2435,7 +2437,7 @@ KEYS is a list of key IDs"
 
 ;;;###autoload
 (defun epg-start-delete-keys (context keys &optional allow-secret)
-  "Initiate an delete keys operation.
+  "Initiate a delete keys operation.
 
 If you use this function, you will need to wait for the completion of
 `epg-gpg-program' by using `epg-wait-for-completion' and call
@@ -2488,7 +2490,7 @@ If you are unsure, use synchronous version of this function
                              (epg-sub-key-id
                               (car (epg-key-sub-key-list key))))
                            keys))))
-(make-obsolete 'epg-start-sign-keys "Do not use.")
+(make-obsolete 'epg-start-sign-keys "do not use.")
 
 ;;;###autoload
 (defun epg-sign-keys (context keys &optional local)
@@ -2501,7 +2503,7 @@ If you are unsure, use synchronous version of this function
            (error "Sign keys failed: %S"
                   (epg-context-result-for context 'error))))
     (epg-reset context)))
-(make-obsolete 'epg-sign-keys "Do not use.")
+(make-obsolete 'epg-sign-keys "do not use.")
 
 ;;;###autoload
 (defun epg-start-generate-key (context parameters)