1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
3 ;; Copyright (C) 1999 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Keywords: PGP, OpenPGP
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'pgg))
31 "PGP 2.* and 6.* interface"
34 (defcustom pgg-pgp-program "pgp"
35 "PGP 2.* and 6.* executable."
39 (defcustom pgg-pgp-shell-file-name "/bin/sh"
40 "The GnuPG executable."
44 (defcustom pgg-pgp-extra-args nil
45 "Extra arguments for every PGP invocation."
50 (luna-define-class pgg-scheme-pgp (pgg-scheme))
53 (defvar pgg-pgp-user-id nil
54 "GnuPG ID of your default identity.")
56 (defvar pgg-scheme-pgp-instance nil)
59 (defun pgg-make-scheme-pgp ()
60 (or pgg-scheme-pgp-instance
61 (setq pgg-scheme-pgp-instance
62 (luna-make-entity 'pgg-scheme-pgp))))
64 (defun pgg-pgp-process-region (start end passphrase program args)
65 (let* ((errors-file-name
66 (concat temporary-file-directory
67 (make-temp-name "pgg-errors")))
71 (list (concat "2>" errors-file-name))))
72 (shell-file-name pgg-pgp-shell-file-name)
73 (output-buffer pgg-output-buffer)
74 (errors-buffer pgg-errors-buffer)
75 (process-connection-type nil)
76 process status exit-status)
77 (with-current-buffer (get-buffer-create output-buffer)
81 (apply #'start-process-shell-command "*PGP*" output-buffer
83 (set-process-sentinel process 'ignore)
85 (setenv "PGPPASSFD" "0")
86 (process-send-string process (concat passphrase "\n")))
87 (process-send-region process start end)
88 (process-send-eof process)
89 (while (eq 'run (process-status process))
90 (accept-process-output process 5))
91 (setq status (process-status process)
92 exit-status (process-exit-status process))
93 (delete-process process)
94 (with-current-buffer output-buffer
95 (goto-char (point-min))
96 (while (search-forward "\r$" nil t)
98 (if (memq status '(stop signal))
99 (error "%s exited abnormally: '%s'" program exit-status))
100 (if (= 127 exit-status)
101 (error "%s could not be found" program))
103 (set-buffer (get-buffer-create errors-buffer))
104 (buffer-disable-undo)
106 (insert-file-contents errors-file-name)
107 (delete-file errors-file-name)
109 (if (and process (eq 'run (process-status process)))
110 (interrupt-process process))
114 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp)
115 start end recipients)
116 (let* ((pgg-pgp-user-id pgg-default-user-id)
119 (format "PGP passphrase for %s: " pgg-pgp-user-id)))
121 `("+encrypttoself=off +verbose=1" "+batchmode"
122 "+language=us" "-fate"
124 (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
126 (pgg-pgp-process-region start end passphrase
127 pgg-pgp-program args)
128 (with-current-buffer pgg-output-buffer
129 (when (zerop (buffer-size))
130 (insert-buffer-substring pgg-errors-buffer)))
133 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp)
135 (let* ((pgg-pgp-user-id pgg-default-user-id)
138 (format "PGP passphrase for %s: " pgg-pgp-user-id)))
140 '("+verbose=1" "+batchmode" "+language=us" "-f")))
141 (pgg-pgp-process-region start end passphrase
142 pgg-pgp-program args)
143 (with-current-buffer pgg-output-buffer
144 (when (zerop (buffer-size))
145 (insert-buffer-substring pgg-errors-buffer)))
148 (luna-define-method sign-region ((scheme pgg-scheme-pgp)
150 (let* ((pgg-pgp-user-id pgg-default-user-id)
153 (format "PGP passphrase for %s: " pgg-pgp-user-id)))
155 (list "-fbast" "+verbose=1" "+language=us" "+batchmode"
156 "-u" pgg-pgp-user-id)))
157 (pgg-pgp-process-region start end passphrase
158 pgg-pgp-program args)
159 (with-current-buffer pgg-output-buffer
160 (when (zerop (buffer-size))
161 (insert-buffer-substring pgg-errors-buffer)))
164 (luna-define-method verify-region ((scheme pgg-scheme-pgp)
165 start end &optional signature)
166 (let* ((basename (expand-file-name "pgg" temporary-file-directory))
167 (orig-file (make-temp-name basename))
168 (args '("+verbose=1" "+batchmode" "+language=us")))
169 (write-region-as-binary start end orig-file)
170 (when (stringp signature)
171 (copy-file signature (setq signature (concat orig-file ".asc")))
172 (setq args (append args (list signature orig-file)))
174 (pgg-pgp-process-region (point-min)(point-max) nil
175 pgg-pgp-program args)
176 (delete-file orig-file)
177 (delete-file signature)
178 (set-buffer pgg-output-buffer)
179 (with-current-buffer pgg-output-buffer
180 (when (zerop (buffer-size))
181 (insert-buffer-substring pgg-errors-buffer)))
184 (luna-define-method insert-key ((scheme pgg-scheme-pgp))
185 (let* ((pgg-pgp-user-id pgg-default-user-id)
187 (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
188 (concat "\"" pgg-pgp-user-id "\""))))
189 (pgg-pgp-process-region (point)(point) nil
190 pgg-pgp-program args)
191 (insert-buffer-substring pgg-output-buffer)
194 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp)
196 (let* ((pgg-pgp-user-id pgg-default-user-id)
197 (basename (expand-file-name "pgg" temporary-file-directory))
198 (key-file (make-temp-name basename))
200 (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
202 (write-region-as-raw-text-CRLF start end key-file)
203 (pgg-pgp-process-region start end nil
204 pgg-pgp-program args)
205 (delete-file key-file)
210 ;;; pgg-pgp.el ends here