update.
[elisp/semi.git] / pgg-pgp5.el
1 ;;; pgg-pgp5.el --- PGP 5.* support for PGG.
2
3 ;; Copyright (C) 1999 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
6 ;; Created: 1999/11/02
7 ;; Keywords: PGP, OpenPGP
8
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
10
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.
15
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.
20
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.
25
26 ;;; Code:
27
28 (eval-when-compile (require 'pgg))
29
30 (defgroup pgg-pgp5 ()
31   "PGP 5.* interface"
32   :group 'pgg)
33
34 (defcustom pgg-pgp5-pgpe-program "pgpe" 
35   "PGP 5.* 'pgpe' executable."
36   :group 'pgg-pgp5
37   :type 'string)
38
39 (defcustom pgg-pgp5-pgps-program "pgps" 
40   "PGP 5.* 'pgps' executable."
41   :group 'pgg-pgp5
42   :type 'string)
43
44 (defcustom pgg-pgp5-pgpk-program "pgpk" 
45   "PGP 5.* 'pgpk' executable."
46   :group 'pgg-pgp5
47   :type 'string)
48
49 (defcustom pgg-pgp5-pgpv-program "pgpv" 
50   "PGP 5.* 'pgpv' executable."
51   :group 'pgg-pgp5
52   :type 'string)
53
54 (defcustom pgg-pgp5-shell-file-name "/bin/sh"
55   "The GnuPG executable."
56   :group 'pgg-pgp5
57   :type 'string)
58
59 (defcustom pgg-pgp5-extra-args nil
60   "Extra arguments for every PGP invocation."
61   :group 'pgg-pgp5
62   :type 'string)
63
64 (eval-and-compile
65   (luna-define-class pgg-scheme-pgp5 (pgg-scheme))
66   )
67   
68 (defvar pgg-pgp5-user-id nil
69   "GnuPG ID of your default identity.")
70
71 (defvar pgg-scheme-pgp5-instance nil)
72
73 ;;;###autoload
74 (defun pgg-make-scheme-pgp5 ()
75   (or pgg-scheme-pgp5-instance
76       (setq pgg-scheme-pgp5-instance
77             (luna-make-entity 'pgg-scheme-pgp5))))
78
79 (defun pgg-pgp5-process-region (start end passphrase program args)
80   (let* ((errors-file-name
81           (concat temporary-file-directory 
82                   (make-temp-name "pgg-errors")))
83          (args 
84           (append args 
85                   pgg-pgp5-extra-args
86                   (list (concat "2>" errors-file-name))))
87          (shell-file-name pgg-pgp5-shell-file-name)
88          (output-buffer pgg-output-buffer)
89          (errors-buffer pgg-errors-buffer)
90          (process-connection-type nil)
91          process status exit-status)
92     (with-current-buffer (get-buffer-create output-buffer)
93       (buffer-disable-undo)
94       (erase-buffer))
95     (setq process
96           (apply #'start-process-shell-command "*PGP*" output-buffer
97                  program args))
98     (set-process-sentinel process 'ignore)
99     (when passphrase
100       (setenv "PGPPASSFD" "0")
101       (process-send-string process (concat passphrase "\n")))
102     (process-send-region process start end)
103     (process-send-eof process)
104     (while (eq 'run (process-status process))
105       (accept-process-output process 5))
106     (setq status (process-status process)
107           exit-status (process-exit-status process))
108     (delete-process process)
109     (with-current-buffer output-buffer
110       (goto-char (point-min))
111       (while (search-forward "\r$" nil t)
112         (replace-match ""))
113       (if (memq status '(stop signal))
114           (error "%s exited abnormally: '%s'" program exit-status))
115       (if (= 127 exit-status)
116           (error "%s could not be found" program))
117
118       (set-buffer (get-buffer-create errors-buffer))
119       (buffer-disable-undo)
120       (erase-buffer)
121       (insert-file-contents errors-file-name)
122       (delete-file errors-file-name)
123       
124       (if (and process (eq 'run (process-status process)))
125           (interrupt-process process))
126       )
127     ))
128
129 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp5) 
130                                     start end recipients)
131   (let* ((pgg-pgp5-user-id pgg-default-user-id)
132          (passphrase
133           (pgg-read-passphrase 
134            (format "PGP passphrase for %s: " pgg-pgp5-user-id)))
135          (args 
136           `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
137             ,@(if recipients
138                   (apply #'append 
139                          (mapcar (lambda (rcpt) 
140                                    (list "-r" 
141                                          (concat "\"" rcpt "\""))) 
142                                  recipients))))))
143     (pgg-pgp5-process-region start end passphrase 
144                              pgg-pgp5-pgpe-program args)
145     (with-current-buffer pgg-output-buffer
146       (when (zerop (buffer-size))
147         (insert-buffer-substring pgg-errors-buffer)))
148     ))
149
150 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp5) 
151                                     start end)
152   (let* ((pgg-pgp5-user-id pgg-default-user-id)
153          (passphrase
154           (pgg-read-passphrase 
155            (format "PGP passphrase for %s: " pgg-pgp5-user-id)))
156          (args 
157           '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
158     (pgg-pgp5-process-region start end passphrase 
159                              pgg-pgp5-pgpv-program args)
160     (with-current-buffer pgg-output-buffer
161       (when (zerop (buffer-size))
162         (insert-buffer-substring pgg-errors-buffer)))
163     ))
164
165 (luna-define-method sign-region ((scheme pgg-scheme-pgp5) 
166                                  start end)
167   (let* ((pgg-pgp5-user-id pgg-default-user-id)
168          (passphrase
169           (pgg-read-passphrase 
170            (format "PGP passphrase for %s: " pgg-pgp5-user-id)))
171          (args 
172           (list "-fbat" "+verbose=1" "+language=us" "+batchmode=1"
173                 "-u" pgg-pgp5-user-id)))
174     (pgg-pgp5-process-region start end passphrase 
175                              pgg-pgp5-pgps-program args)
176     (with-current-buffer pgg-output-buffer
177       (when (zerop (buffer-size))
178         (insert-buffer-substring pgg-errors-buffer)))
179     ))
180
181 (luna-define-method verify-region ((scheme pgg-scheme-pgp5) 
182                                    start end &optional signature)
183   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
184          (orig-file (make-temp-name basename))
185          (args '("+verbose=1" "+batchmode=1" "+language=us")))
186     (write-region-as-binary start end orig-file)
187     (when (stringp signature)
188       (copy-file signature (setq signature (concat orig-file ".asc")))
189       (setq args (append args (list signature)))
190       )
191     (pgg-pgp5-process-region (point-min)(point-max) nil
192                              pgg-pgp5-pgpv-program args)
193     (delete-file orig-file)
194     (delete-file signature)
195     (set-buffer pgg-output-buffer)
196     (with-current-buffer pgg-output-buffer
197       (when (zerop (buffer-size))
198         (insert-buffer-substring pgg-errors-buffer)))
199     ))
200
201 (luna-define-method insert-key ((scheme pgg-scheme-pgp5))
202   (let* ((pgg-pgp5-user-id pgg-default-user-id)
203          (args
204           (list "+verbose=1" "+batchmode=1" "+language=us" "-x" 
205                 (concat "\"" pgg-pgp5-user-id "\""))))
206     (pgg-pgp5-process-region (point)(point) nil
207                              pgg-pgp5-pgpk-program args)
208     (insert-buffer-substring pgg-output-buffer)
209     ))
210
211 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp5)
212                                        start end)
213   (let* ((pgg-pgp5-user-id pgg-default-user-id)
214          (basename (expand-file-name "pgg" temporary-file-directory))
215          (key-file (make-temp-name basename))
216          (args 
217           (list "+verbose=1" "+batchmode=1" "+language=us" "-a" 
218                 key-file)))
219     (write-region-as-raw-text-CRLF start end key-file)
220     (pgg-pgp5-process-region start end nil
221                              pgg-pgp5-pgpk-program args)
222     (delete-file key-file)
223     ))
224
225 (provide 'pgg-pgp5)
226
227 ;;; pgg-pgp5.el ends here