- (insert-file-contents output-file-name))
- (when (and (fboundp 'set-buffer-multibyte)
- (subrp (symbol-function 'set-buffer-multibyte))
- (not enable-multibyte-characters))
- (if (zerop (buffer-size))
- (set-buffer-multibyte t)
- (insert (pgg-string-to-multibyte
- (prog1
- (buffer-string)
- (erase-buffer)
- (set-buffer-multibyte t))))))
- (delete-file output-file-name))))
- (kill-buffer (process-buffer process)))))
-
-(defun pgg-gpg-wait-for-status (process status-list)
- (with-current-buffer (process-buffer process)
- (setq pgg-gpg-pending-status-list status-list)
- (while (and (eq (process-status process) 'run)
- pgg-gpg-pending-status-list)
- (accept-process-output process 1))))
-
-(defun pgg-gpg-wait-for-completion (process)
- (process-send-eof process)
- (while (eq (process-status process) 'run)
- ;; We can't use accept-process-output instead of sit-for here
- ;; because it may cause an interrupt during the sentinel execution.
- (sit-for 0.1)))
-
-(defun pgg-gpg-status-USERID_HINT (process line)
- (if (string-match "\\`USERID_HINT \\([^ ]+\\) \\(.*\\)" line)
- (let* ((key-id (match-string 1 line))
- (user-id (match-string 2 line))
- (entry (assoc key-id pgg-gpg-user-id-alist)))
- (if entry
- (setcdr entry user-id)
- (setq pgg-gpg-user-id-alist (cons (cons key-id user-id)
- pgg-gpg-user-id-alist))))))
-
-(defun pgg-gpg-status-NEED_PASSPHRASE (process line)
- (if (string-match "\\`NEED_PASSPHRASE \\([^ ]+\\)" line)
- (setq pgg-gpg-key-id (match-string 1 line))))
-
-(defun pgg-gpg-status-NEED_PASSPHRASE_SYM (process line)
- (setq pgg-gpg-key-id 'SYM))
-
-(defun pgg-gpg-status-NEED_PASSPHRASE_PIN (process line)
- (setq pgg-gpg-key-id 'PIN))
-
-(defun pgg-gpg-status-GET_HIDDEN (process line)
- (let ((entry (assoc pgg-gpg-key-id pgg-gpg-user-id-alist)))
- (if (setq pgg-gpg-passphrase
- (if (eq pgg-gpg-key-id 'SYM)
- (pgg-read-passphrase
- "GnuPG passphrase for symmetric encryption: ")
- (pgg-read-passphrase
- (format "GnuPG passphrase for %s: "
- (if entry
- (cdr entry)
- pgg-gpg-key-id))
- (if (eq pgg-gpg-key-id 'PIN)
- "PIN"
- pgg-gpg-key-id))))
- (process-send-string process (concat pgg-gpg-passphrase "\n")))))
-
-(defun pgg-gpg-status-GOOD_PASSPHRASE (process line)
- (when (and pgg-gpg-passphrase
- (stringp pgg-gpg-key-id))
- (pgg-add-passphrase-to-cache pgg-gpg-key-id pgg-gpg-passphrase)
- (setq pgg-gpg-passphrase nil)))
-
-(defun pgg-gpg-status-BAD_PASSPHRASE (process line)
- (when pgg-gpg-passphrase
- (fillarray pgg-gpg-passphrase 0)
- (setq pgg-gpg-passphrase nil)))
+ (insert-file-contents output-file-name)))
+ (set-buffer errors-buffer)
+ (if (memq status '(stop signal))
+ (error "%s exited abnormally: '%s'" program exit-status))
+ (if (= 127 exit-status)
+ (error "%s could not be found" program))))
+ (if passphrase-with-newline
+ (pgg-clear-string passphrase-with-newline))
+ (if encoded-passphrase-with-new-line
+ (pgg-clear-string encoded-passphrase-with-new-line))
+ (if (and process (eq 'run (process-status process)))
+ (interrupt-process process))
+ (if (file-exists-p output-file-name)
+ (delete-file output-file-name))
+ (set-default-file-modes orig-mode))))
+
+(defun pgg-gpg-possibly-cache-passphrase (passphrase &optional key notruncate)
+ (if (and passphrase
+ pgg-cache-passphrase
+ (progn
+ (goto-char (point-min))
+ (re-search-forward "^\\[GNUPG:] \\(GOOD_PASSPHRASE\\>\\)\\|\\(SIG_CREATED\\)" nil t)))
+ (pgg-add-passphrase-to-cache
+ (or key
+ (progn
+ (goto-char (point-min))
+ (if (re-search-forward
+ "^\\[GNUPG:] NEED_PASSPHRASE\\(_PIN\\)? \\w+ ?\\w*" nil t)
+ (substring (match-string 0) -8))))
+ passphrase
+ notruncate)))
+
+(defvar pgg-gpg-all-secret-keys 'unknown)
+
+(defun pgg-gpg-lookup-all-secret-keys ()
+ "Return all secret keys present in secret key ring."
+ (when (eq pgg-gpg-all-secret-keys 'unknown)
+ (setq pgg-gpg-all-secret-keys '())
+ (let ((args (list "--with-colons" "--no-greeting" "--batch"
+ "--list-secret-keys")))
+ (with-temp-buffer
+ (apply #'call-process pgg-gpg-program nil t nil args)
+ (goto-char (point-min))
+ (while (re-search-forward
+ "^\\(sec\\|pub\\):[^:]*:[^:]*:[^:]*:\\([^:]*\\)" nil t)
+ (push (substring (match-string 2) 8)
+ pgg-gpg-all-secret-keys)))))
+ pgg-gpg-all-secret-keys)