update.
[elisp/semi.git] / pgg-gpg.el
1 ;;; pgg-gpg.el --- GnuPG support for PGG.
2
3 ;; Copyright (C) 1999 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
6 ;; Created: 1999/10/28
7 ;; Keywords: PGP, OpenPGP, GnuPG
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-gpg ()
31   "GnuPG interface"
32   :group 'pgg)
33
34 (defcustom pgg-gpg-program "gpg" 
35   "The GnuPG executable."
36   :group 'pgg-gpg
37   :type 'string)
38
39 (defcustom pgg-gpg-shell-file-name "/bin/sh"
40   "The GnuPG executable."
41   :group 'pgg-gpg
42   :type 'string)
43
44 (defcustom pgg-gpg-extra-args nil
45   "Extra arguments for every GnuPG invocation."
46   :group 'pgg-gpg
47   :type 'string)
48
49 (eval-and-compile
50   (luna-define-class pgg-scheme-gpg (pgg-scheme))
51   )
52   
53 (defvar pgg-gpg-user-id nil
54   "GnuPG ID of your default identity.")
55
56 (defvar pgg-scheme-gpg-instance nil)
57
58 ;;;###autoload
59 (defun pgg-make-scheme-gpg ()
60   (or pgg-scheme-gpg-instance
61       (setq pgg-scheme-gpg-instance
62             (luna-make-entity 'pgg-scheme-gpg))))
63
64 (defun pgg-gpg-process-region (start end passphrase program args)
65   (let* ((errors-file-name
66           (concat temporary-file-directory 
67                   (make-temp-name "pgg-errors")))
68          (status-file-name
69           (concat temporary-file-directory 
70                   (make-temp-name "pgg-status")))
71          (args 
72           (append
73            `("--status-fd" "3"
74              ,@(if passphrase '("--passphrase-fd" "0"))
75              ,@pgg-gpg-extra-args)
76            args
77            (list (concat "2>" errors-file-name)
78                  (concat "3>" status-file-name))))
79          (shell-file-name pgg-gpg-shell-file-name)
80          (output-buffer pgg-output-buffer)
81          (errors-buffer pgg-errors-buffer)
82          (status-buffer pgg-status-buffer)
83          (process-connection-type nil)
84          process status exit-status)
85     (with-current-buffer (get-buffer-create output-buffer)
86       (buffer-disable-undo)
87       (erase-buffer))
88     (setq process
89           (apply #'start-process-shell-command "*GnuPG*" output-buffer
90                  program args))
91     (set-process-sentinel process 'ignore)
92     (when passphrase
93       (process-send-string process (concat passphrase "\n")))
94     (process-send-region process start end)
95     (process-send-eof process)
96     (while (eq 'run (process-status process))
97       (accept-process-output process 5))
98     (setq status (process-status process)
99           exit-status (process-exit-status process))
100     (delete-process process)
101     (with-current-buffer output-buffer
102       (goto-char (point-min))
103       (while (search-forward "\r$" nil t)
104         (replace-match ""))
105       (if (memq status '(stop signal))
106           (error "%s exited abnormally: '%s'" program exit-status))
107       (if (= 127 exit-status)
108           (error "%s could not be found" program))
109
110       (set-buffer (get-buffer-create errors-buffer))
111       (buffer-disable-undo)
112       (erase-buffer)
113       (insert-file-contents errors-file-name)
114       (delete-file errors-file-name)
115       
116       (set-buffer (get-buffer-create status-buffer))
117       (buffer-disable-undo)
118       (erase-buffer)
119       (insert-file-contents status-file-name)
120       (delete-file status-file-name)
121
122       (if (and process (eq 'run (process-status process)))
123           (interrupt-process process))
124       )
125     ))
126
127 (luna-define-method encrypt-region ((scheme pgg-scheme-gpg) 
128                                     start end recipients)
129   (let* ((pgg-gpg-user-id pgg-default-user-id)
130          (passphrase
131           (pgg-read-passphrase 
132            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)))
133          (args 
134           `("--batch" "--armor" "--textmode" "--always-trust" "--encrypt"
135             ,@(if recipients
136                   (apply #'append 
137                          (mapcar (lambda (rcpt) 
138                                    (list "--remote-user" 
139                                          (concat "\"" rcpt "\""))) 
140                                  recipients))))))
141     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
142     (with-current-buffer pgg-output-buffer
143       (when (zerop (buffer-size))
144         (insert-buffer-substring pgg-errors-buffer)))
145     ))
146
147 (luna-define-method decrypt-region ((scheme pgg-scheme-gpg) 
148                                     start end)
149   (let* ((pgg-gpg-user-id pgg-default-user-id)
150          (passphrase
151           (pgg-read-passphrase 
152            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)))
153          (args '("--batch" "--decrypt")))
154     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
155     (with-current-buffer pgg-output-buffer
156       (when (zerop (buffer-size))
157         (insert-buffer-substring pgg-errors-buffer)))
158     ))
159
160 (luna-define-method sign-region ((scheme pgg-scheme-gpg) 
161                                  start end)
162   (let* ((pgg-gpg-user-id pgg-default-user-id)
163          (passphrase
164           (pgg-read-passphrase 
165            (format "GnuPG passphrase for %s: " pgg-gpg-user-id)))
166          (args 
167           (list "--detach-sign" "--armor" "--batch" "--verbose" 
168                 "--local-user" pgg-gpg-user-id)))
169     (goto-char start)
170     (setq end (set-marker (make-marker) (point-max)))
171     (while (progn (end-of-line) (> (marker-position end) (point)))
172       (insert "\r")
173       (forward-line 1))
174     (pgg-gpg-process-region start end passphrase pgg-gpg-program args)
175     (goto-char start)
176     (while (re-search-forward "\r$" end t)
177       (replace-match ""))
178     (with-current-buffer pgg-output-buffer
179       (when (zerop (buffer-size))
180         (insert-buffer-substring pgg-errors-buffer)))
181     ))
182
183 (luna-define-method verify-region ((scheme pgg-scheme-gpg) 
184                                    start end &optional signature)
185   (let ((args '("--batch" "--verify")))
186     (when (stringp signature)
187       (setq args (append args (list signature))))
188     (pgg-gpg-process-region start end nil pgg-gpg-program args)
189     (set-buffer pgg-errors-buffer)
190     (goto-char (point-min))
191     (while (re-search-forward "^gpg: " nil t)
192       (replace-match ""))
193     (goto-char (point-min))
194     (let ((case-fold-search t))
195       (while (re-search-forward "^warning: " nil t)
196         (delete-region (match-beginning 0)
197                        (progn (beginning-of-line 2) (point)))))
198     (append-to-buffer pgg-output-buffer
199                       (point-min)(point-max))
200     ))
201
202 (luna-define-method insert-key ((scheme pgg-scheme-gpg))
203   (let* ((pgg-gpg-user-id pgg-default-user-id)
204          (args (list "--batch" "--export" "--armor" 
205                      (concat "\"" pgg-gpg-user-id "\""))))
206     (pgg-gpg-process-region (point)(point) nil pgg-gpg-program args)
207     (insert-buffer-substring pgg-output-buffer)
208     ))
209
210 (luna-define-method snarf-keys-region ((scheme pgg-scheme-gpg)
211                                        start end)
212   (let ((args '("--import" "--batch")) status)
213     (pgg-gpg-process-region start end nil pgg-gpg-program args)
214     (set-buffer pgg-status-buffer)
215     (goto-char (point-min))
216     (when (re-search-forward "^\\[GNUPG:] +IMPORT_RES +" nil t)
217       (setq status (buffer-substring (match-end 0) 
218                                      (progn (end-of-line) 
219                                             (point)))
220             status (vconcat (split-string status)))
221       (erase-buffer)
222       (insert (aref status 0) "keys seen\n"
223               (format "\t%d bad, %d new, %d old\n"
224                       (string-to-int (aref status 1))
225                       (+ (string-to-int (aref status 2))
226                          (string-to-int (aref status 10)))
227                       (+ (string-to-int (aref status 4))
228                          (string-to-int (aref status 11))))
229               (if (zerop (aref status 9))
230                   ""
231                 "\tSecret keys are imported\n")))
232     (append-to-buffer pgg-output-buffer
233                       (point-min)(point-max))
234     (with-current-buffer pgg-output-buffer
235       (when (zerop (buffer-size))
236         (insert-buffer-substring pgg-errors-buffer)))
237     ))
238
239 (provide 'pgg-gpg)
240
241 ;;; pgg-gpg.el ends here
242