2006-03-22 Simon Josefsson <jas@extundo.com>
authorSimon Josefsson <jas@extundo.com>
Wed, 22 Mar 2006 16:12:16 +0000 (16:12 +0000)
committerSimon Josefsson <jas@extundo.com>
Wed, 22 Mar 2006 16:12:16 +0000 (16:12 +0000)
* pgg-gpg.el (pgg-gpg-use-agent): Disable by default.
(pgg-gpg-update-agent): New function.
(pgg-gpg-use-agent-p): New function.
(pgg-gpg-process-region, pgg-gpg-encrypt-region)
(pgg-gpg-encrypt-symmetric-region, pgg-gpg-decrypt-region)
(pgg-gpg-sign-region): Use it.

lisp/ChangeLog
lisp/pgg-gpg.el

index 18bf852..8108ffd 100644 (file)
@@ -1,3 +1,12 @@
+2006-03-22  Sascha Wilde  <wilde@sha-bang.de>
+
+       * pgg-gpg.el (pgg-gpg-use-agent): Disable by default.
+       (pgg-gpg-update-agent): New function.
+       (pgg-gpg-use-agent-p): New function.
+       (pgg-gpg-process-region, pgg-gpg-encrypt-region)
+       (pgg-gpg-encrypt-symmetric-region, pgg-gpg-decrypt-region)
+       (pgg-gpg-sign-region): Use it.
+
 2006-03-22  Katsumi Yamaoka  <yamaoka@jpl.org>
 
        * gnus-sum.el (gnus-map-articles): Don't funcall symbol macro.
index a9608b7..eefc569 100644 (file)
@@ -4,7 +4,8 @@
 ;;   2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: Daiki Ueno <ueno@unixuser.org>
-;; Symmetric encryption added by: Sascha Wilde <wilde@sha-bang.de>
+;; Symmetric encryption and gpg-agent support added by: 
+;;   Sascha Wilde <wilde@sha-bang.de>
 ;; Created: 1999/10/28
 ;; Keywords: PGP, OpenPGP, GnuPG
 
   :type '(choice (const :tag "New `--recipient' option" "--recipient")
                 (const :tag "Old `--remote-user' option" "--remote-user")))
 
-(defcustom pgg-gpg-use-agent (if (getenv "GPG_AGENT_INFO") t nil)
-  "Whether to use gnupg agent for key caching.
-By default, it will be enabled iff the environment variable
-\"GPG_AGENT_INFO\" is set."
+(defcustom pgg-gpg-use-agent nil
+  "Whether to use gnupg agent for key caching."
   :group 'pgg-gpg
   :type 'boolean)
 
@@ -62,10 +61,11 @@ By default, it will be enabled iff the environment variable
   "GnuPG ID of your default identity.")
 
 (defun pgg-gpg-process-region (start end passphrase program args)
-  (let* ((output-file-name (pgg-make-temp-file "pgg-output"))
+  (let* ((use-agent (pgg-gpg-use-agent-p)) 
+        (output-file-name (pgg-make-temp-file "pgg-output"))
         (args
          `("--status-fd" "2"
-           ,@(if pgg-gpg-use-agent '("--use-agent")
+           ,@(if use-agent '("--use-agent")
                (if passphrase '("--passphrase-fd" "0")))
            "--yes" ; overwrite
            "--output" ,output-file-name
@@ -189,7 +189,7 @@ If optional PASSPHRASE is not specified, it will be obtained from the
 passphrase cache or user."
   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
         (passphrase (or passphrase
-                        (when (and sign (not pgg-gpg-use-agent))
+                        (when (and sign (not (pgg-gpg-use-agent-p)))
                           (pgg-read-passphrase
                            (format "GnuPG passphrase for %s: "
                                    pgg-gpg-user-id)
@@ -221,7 +221,7 @@ passphrase cache or user."
 If optional PASSPHRASE is not specified, it will be obtained from the
 passphrase cache or user."
   (let* ((passphrase (or passphrase
-                        (when (not pgg-gpg-use-agent)
+                        (when (not (pgg-gpg-use-agent-p))
                           (pgg-read-passphrase
                            "GnuPG passphrase for symmetric encryption: "))))
         (args
@@ -250,7 +250,7 @@ passphrase cache or user."
         (pgg-gpg-user-id (or key-id key
                              pgg-gpg-user-id pgg-default-user-id))
         (passphrase (or passphrase
-                        (when (not pgg-gpg-use-agent)
+                        (when (not (pgg-gpg-use-agent-p))
                           (pgg-read-passphrase
                            (format (if (pgg-gpg-symmetric-key-p message-keys)
                                        "Passphrase for symmetric decryption: "
@@ -286,7 +286,7 @@ passphrase cache or user."
   "Make detached signature from text between START and END."
   (let* ((pgg-gpg-user-id (or pgg-gpg-user-id pgg-default-user-id))
         (passphrase (or passphrase
-                        (when (not pgg-gpg-use-agent)
+                        (when (not (pgg-gpg-use-agent-p))
                           (pgg-read-passphrase
                            (format "GnuPG passphrase for %s: "
                                    pgg-gpg-user-id)
@@ -356,6 +356,25 @@ passphrase cache or user."
     (append-to-buffer pgg-output-buffer (point-min)(point-max))
     (pgg-process-when-success)))
 
+(defun pgg-gpg-update-agent ()
+  "Try to connet to gpg-agent and send UPDATESTARTUPTTY."
+  (let* ((agent-info (getenv "GPG_AGENT_INFO")) 
+        (socket (and agent-info
+                     (string-match "^\\([^:]*\\)" agent-info)
+                     (match-string 1 agent-info)))
+        (conn (and socket
+                   (make-network-process :name "gpg-agent-process"
+                                         :host 'local :family 'local
+                                         :service socket))))
+    (when (and conn (eq (process-status conn) 'open))
+      (process-send-string conn "UPDATESTARTUPTTY\n")
+      (delete-process conn)
+      t)))
+
+(defun pgg-gpg-use-agent-p ()
+  "Return t if `pgg-gpg-use-agent' is t and gpg-agent is available."
+  (and pgg-gpg-use-agent (pgg-gpg-update-agent)))
+
 (provide 'pgg-gpg)
 
 ;;; arch-tag: 2aa5d5d8-93a0-4865-9312-33e29830e000