]> code.delx.au - gnu-emacs/blobdiff - lisp/epg.el
Fix race conditions with MS-Windows lock files by using _sopen.
[gnu-emacs] / lisp / epg.el
index b79c5df416508ead3580b6328e1520b75e3a64b2..3f04aa2e07a919b1c337554b0f9af1eaa4eae08a 100644 (file)
@@ -1,5 +1,5 @@
 ;;; epg.el --- the EasyPG Library -*- lexical-binding: t -*-
-;; Copyright (C) 1999-2000, 2002-2012 Free Software Foundation, Inc.
+;; Copyright (C) 1999-2000, 2002-2013 Free Software Foundation, Inc.
 
 ;; Author: Daiki Ueno <ueno@unixuser.org>
 ;; Keywords: PGP, GnuPG
@@ -37,6 +37,8 @@
 (defvar epg-key-id nil)
 (defvar epg-context nil)
 (defvar epg-debug-buffer nil)
+(defvar epg-agent-file nil)
+(defvar epg-agent-mtime nil)
 
 ;; from gnupg/include/cipher.h
 (defconst epg-cipher-algorithm-alist
@@ -967,34 +969,12 @@ This function is for internal use only."
        (setcdr entry value)
       (epg-context-set-result context (cons (cons name value) result)))))
 
-(defun epg-key-image (key-id)
-  "Return the image of a key, if any"
-  (let ((filename
-        (replace-regexp-in-string
-         "\n" ""
-         (shell-command-to-string
-          (concat "/usr/bin/gpg --photo-viewer 'echo %I >&2' --list-keys "
-                  key-id " > /dev/null")))))
-    (when (and (not (string-equal filename ""))
-              (file-exists-p filename))
-      (create-image filename))))
-
-(defun epg-key-image-to-string (key-id)
-  "Return a string with the image of a key, if any"
-  (let* ((result "")
-         (key-image (epg-key-image key-id)))
-    (when key-image
-      (setq result "  ")
-      (put-text-property 1 2 'display key-image result))
-    result))
-
 (defun epg-signature-to-string (signature)
   "Convert SIGNATURE to a human readable string."
   (let* ((user-id (cdr (assoc (epg-signature-key-id signature)
                              epg-user-id-alist)))
         (pubkey-algorithm (epg-signature-pubkey-algorithm signature))
-        (key-id (epg-signature-key-id signature))
-        (key-image (epg-key-image-to-string key-id)))
+        (key-id (epg-signature-key-id signature)))
     (concat
      (cond ((eq (epg-signature-status signature) 'good)
            "Good signature from ")
@@ -1009,7 +989,6 @@ This function is for internal use only."
           ((eq (epg-signature-status signature) 'no-pubkey)
            "No public key for "))
      key-id
-     key-image
      (if user-id
         (concat " "
                 (if (stringp user-id)
@@ -1177,9 +1156,35 @@ This function is for internal use only."
         (coding-system-for-write 'binary)
         (coding-system-for-read 'binary)
         process-connection-type
+        (process-environment process-environment)
         (orig-mode (default-file-modes))
         (buffer (generate-new-buffer " *epg*"))
-        process)
+        process
+        terminal-name
+        agent-file
+        (agent-mtime '(0 0 0 0)))
+    ;; Set GPG_TTY and TERM for pinentry-curses.  Note that we can't
+    ;; use `terminal-name' here to get the real pty name for the child
+    ;; process, though /dev/fd/0" is not portable.
+    (unless (memq system-type '(ms-dos windows-nt))
+      (with-temp-buffer
+       (condition-case nil
+           (when (= (call-process "tty" "/dev/fd/0" t) 0)
+             (delete-char -1)
+             (setq terminal-name (buffer-string)))
+         (file-error))))
+    (when terminal-name
+      (setq process-environment
+           (cons (concat "GPG_TTY=" terminal-name)
+                 (cons "TERM=xterm" process-environment))))
+    ;; Record modified time of gpg-agent socket to restore the Emacs
+    ;; frame on text terminal in `epg-wait-for-completion'.
+    ;; See
+    ;; <http://lists.gnu.org/archive/html/emacs-devel/2007-02/msg00755.html>
+    ;; for more details.
+    (when (and agent-info (string-match "\\(.*\\):[0-9]+:[0-9]+" agent-info))
+      (setq agent-file (match-string 1 agent-info)
+           agent-mtime (or (nth 5 (file-attributes agent-file)) '(0 0 0 0))))
     (if epg-debug
        (save-excursion
          (unless epg-debug-buffer
@@ -1208,7 +1213,11 @@ This function is for internal use only."
       (make-local-variable 'epg-key-id)
       (setq epg-key-id nil)
       (make-local-variable 'epg-context)
-      (setq epg-context context))
+      (setq epg-context context)
+      (make-local-variable 'epg-agent-file)
+      (setq epg-agent-file agent-file)
+      (make-local-variable 'epg-agent-mtime)
+      (setq epg-agent-mtime agent-mtime))
     (unwind-protect
        (progn
          (set-default-file-modes 448)
@@ -1285,6 +1294,13 @@ This function is for internal use only."
     (accept-process-output (epg-context-process context) 1))
   ;; This line is needed to run the process-filter right now.
   (sleep-for 0.1)
+  ;; Restore Emacs frame on text terminal, when pinentry-curses has terminated.
+  (if (with-current-buffer (process-buffer (epg-context-process context))
+       (and epg-agent-file
+            (> (float-time (or (nth 5 (file-attributes epg-agent-file))
+                               '(0 0 0 0)))
+               (float-time epg-agent-mtime))))
+      (redraw-frame (selected-frame)))
   (epg-context-set-result-for
    context 'error
    (nreverse (epg-context-result-for context 'error))))