Add comment that this module is based on
[elisp/semi.git] / pgg-pgp.el
1 ;;; pgg-pgp.el --- PGP 2.* and 6.* 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-pgp ()
31   "PGP 2.* and 6.* interface"
32   :group 'pgg)
33
34 (defcustom pgg-pgp-program "pgp" 
35   "PGP 2.* and 6.* executable."
36   :group 'pgg-pgp
37   :type 'string)
38
39 (defcustom pgg-pgp-shell-file-name "/bin/sh"
40   "The GnuPG executable."
41   :group 'pgg-pgp
42   :type 'string)
43
44 (defcustom pgg-pgp-extra-args nil
45   "Extra arguments for every PGP invocation."
46   :group 'pgg-pgp
47   :type 'string)
48
49 (eval-and-compile
50   (luna-define-class pgg-scheme-pgp (pgg-scheme))
51   )
52   
53 (defvar pgg-pgp-user-id nil
54   "GnuPG ID of your default identity.")
55
56 (defvar pgg-scheme-pgp-instance nil)
57
58 ;;;###autoload
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))))
63
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")))
68          (args 
69           (append args 
70                   pgg-pgp-extra-args
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)
78       (buffer-disable-undo)
79       (erase-buffer))
80     (setq process
81           (apply #'start-process-shell-command "*PGP*" output-buffer
82                  program args))
83     (set-process-sentinel process 'ignore)
84     (when passphrase
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)
97         (replace-match ""))
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))
102
103       (set-buffer (get-buffer-create errors-buffer))
104       (buffer-disable-undo)
105       (erase-buffer)
106       (insert-file-contents errors-file-name)
107       (delete-file errors-file-name)
108       
109       (if (and process (eq 'run (process-status process)))
110           (interrupt-process process))
111       )
112     ))
113
114 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp) 
115                                     start end recipients)
116   (let* ((pgg-pgp-user-id pgg-default-user-id)
117          (passphrase
118           (pgg-read-passphrase 
119            (format "PGP passphrase for %s: " pgg-pgp-user-id)))
120          (args 
121           `("+encrypttoself=off +verbose=1" "+batchmode"
122             "+language=us" "-fate"
123             ,@(if recipients
124                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
125                           recipients)))))
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)))
131     ))
132
133 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp) 
134                                     start end)
135   (let* ((pgg-pgp-user-id pgg-default-user-id)
136          (passphrase
137           (pgg-read-passphrase 
138            (format "PGP passphrase for %s: " pgg-pgp-user-id)))
139          (args 
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)))
146     ))
147
148 (luna-define-method sign-region ((scheme pgg-scheme-pgp) 
149                                  start end)
150   (let* ((pgg-pgp-user-id pgg-default-user-id)
151          (passphrase
152           (pgg-read-passphrase 
153            (format "PGP passphrase for %s: " pgg-pgp-user-id)))
154          (args 
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)))
162     ))
163
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)))
173       )
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)))
182     ))
183
184 (luna-define-method insert-key ((scheme pgg-scheme-pgp))
185   (let* ((pgg-pgp-user-id pgg-default-user-id)
186          (args
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)
192     ))
193
194 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp)
195                                        start end)
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))
199          (args 
200           (list "+verbose=1" "+batchmode" "+language=us" "-kaf" 
201                 key-file)))
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)
206     ))
207
208 (provide 'pgg-pgp)
209
210 ;;; pgg-pgp.el ends here