Fix typo in last commit.
[gnus] / lisp / pgg.el
1 ;;; pgg.el --- glue for the various PGP implementations.
2
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Created: 1999/10/28
7 ;; Keywords: PGP
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
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31
32 (require 'pgg-def)
33 (require 'pgg-parse)
34
35 (eval-when-compile
36   (ignore-errors
37     (require 'w3)
38     (require 'url)))
39
40 (defvar pgg-temporary-file-directory
41   (cond ((fboundp 'temp-directory) (temp-directory))
42         ((boundp 'temporary-file-directory) temporary-file-directory)
43         ("/tmp/")))
44
45 ;;; @ utility functions
46 ;;;
47
48 (defvar pgg-fetch-key-function (if (fboundp 'url-insert-file-contents)
49                                    (function pgg-fetch-key-with-w3)))
50
51 (defun pgg-invoke (func scheme &rest args)
52   (progn
53     (require (intern (format "pgg-%s" scheme)))
54     (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
55
56 (put 'pgg-save-coding-system 'lisp-indent-function 2)
57
58 (defmacro pgg-save-coding-system (start end &rest body)
59   `(if (interactive-p)
60        (let ((buffer (current-buffer)))
61          (with-temp-buffer
62            (let (buffer-undo-list)
63              (insert-buffer-substring buffer ,start ,end)
64              (encode-coding-region (point-min)(point-max)
65                                    buffer-file-coding-system)
66              (prog1 (save-excursion ,@body)
67                (push nil buffer-undo-list)
68                (ignore-errors (undo))))))
69      (save-restriction
70        (narrow-to-region ,start ,end)
71        ,@body)))
72
73 (defun pgg-temp-buffer-show-function (buffer)
74   (let ((window (split-window-vertically)))
75     (set-window-buffer window buffer)
76     (shrink-window-if-larger-than-buffer window)))
77
78 (defun pgg-display-output-buffer (start end status)
79   (if status
80       (progn
81         (delete-region start end)
82         (insert-buffer-substring pgg-output-buffer)
83         (decode-coding-region start (point) buffer-file-coding-system))
84     (let ((temp-buffer-show-function
85            (function pgg-temp-buffer-show-function)))
86       (with-output-to-temp-buffer pgg-echo-buffer
87         (set-buffer standard-output)
88         (insert-buffer-substring pgg-errors-buffer)))))
89
90 (defvar pgg-passphrase-cache-expiry 16)
91 (defvar pgg-passphrase-cache (make-vector 7 0))
92
93 (defvar pgg-read-passphrase nil)
94 (defun pgg-read-passphrase (prompt &optional key)
95   (if (not pgg-read-passphrase)
96       (if (functionp 'read-passwd)
97           (setq pgg-read-passphrase 'read-passwd)
98         (if (load "passwd" t)
99             (setq pgg-read-passphrase 'read-passwd)
100           (autoload 'ange-ftp-read-passwd "ange-ftp")
101           (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
102   (or (and pgg-cache-passphrase
103            key (setq key (pgg-truncate-key-identifier key))
104            (symbol-value (intern-soft key pgg-passphrase-cache)))
105       (funcall pgg-read-passphrase prompt)))
106
107 (defun pgg-add-passphrase-cache (key passphrase)
108   (setq key (pgg-truncate-key-identifier key))
109   (set (intern key pgg-passphrase-cache)
110        passphrase)
111   (run-at-time pgg-passphrase-cache-expiry nil
112                #'pgg-remove-passphrase-cache
113                key))
114
115 (defun pgg-remove-passphrase-cache (key)
116   (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
117     (when passphrase
118       (fillarray passphrase ?_)
119       (unintern key pgg-passphrase-cache))))
120
121 (defmacro pgg-convert-lbt-region (start end lbt)
122   `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
123      (goto-char ,start)
124      (case ,lbt
125        (CRLF
126         (while (progn
127                  (end-of-line)
128                  (> (marker-position pgg-conversion-end) (point)))
129           (insert "\r")
130           (forward-line 1)))
131        (LF
132         (while (re-search-forward "\r$" pgg-conversion-end t)
133           (replace-match ""))))))
134
135 (put 'pgg-as-lbt 'lisp-indent-function 3)
136
137 (defmacro pgg-as-lbt (start end lbt &rest body)
138   `(let ((inhibit-read-only t)
139          buffer-read-only
140          buffer-undo-list)
141      (pgg-convert-lbt-region ,start ,end ,lbt)
142      (let ((,end (point)))
143        ,@body)
144      (push nil buffer-undo-list)
145      (ignore-errors (undo))))
146
147 (put 'pgg-process-when-success 'lisp-indent-function 0)
148
149 (defmacro pgg-process-when-success (&rest body)
150   `(with-current-buffer pgg-output-buffer
151      (if (zerop (buffer-size)) nil ,@body t)))
152
153 ;;; @ interface functions
154 ;;;
155
156 ;;;###autoload
157 (defun pgg-encrypt-region (start end rcpts &optional sign)
158   "Encrypt the current region between START and END for RCPTS.
159 If optional argument SIGN is non-nil, do a combined sign and encrypt."
160   (interactive
161    (list (region-beginning)(region-end)
162          (split-string (read-string "Recipients: ") "[ \t,]+")))
163   (let ((status
164          (pgg-save-coding-system start end
165            (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
166                        (point-min) (point-max) rcpts sign))))
167     (when (interactive-p)
168       (pgg-display-output-buffer start end status))
169     status))
170
171 ;;;###autoload
172 (defun pgg-decrypt-region (start end)
173   "Decrypt the current region between START and END."
174   (interactive "r")
175   (let ((status
176          (pgg-save-coding-system start end
177            (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
178                        (point-min) (point-max)))))
179     (when (interactive-p)
180       (pgg-display-output-buffer start end status))
181     status))
182
183 ;;;###autoload
184 (defun pgg-sign-region (start end &optional cleartext)
185   "Make the signature from text between START and END.
186 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
187 a detached signature."
188   (interactive "r")
189   (let ((status (pgg-save-coding-system start end
190                   (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
191                               (point-min) (point-max)
192                               (or (interactive-p) cleartext)))))
193     (when (interactive-p)
194       (pgg-display-output-buffer start end status))
195     status))
196
197 ;;;###autoload
198 (defun pgg-verify-region (start end &optional signature fetch)
199   "Verify the current region between START and END.
200 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
201 the detached signature of the current region.
202
203 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
204 signer's public key from `pgg-default-keyserver-address'."
205   (interactive "r")
206   (let* ((packet
207           (if (null signature) nil
208             (with-temp-buffer
209               (buffer-disable-undo)
210               (if (fboundp 'set-buffer-multibyte)
211                   (set-buffer-multibyte nil))
212               (insert-file-contents signature)
213               (cdr (assq 2 (pgg-decode-armor-region
214                             (point-min)(point-max)))))))
215          (key (cdr (assq 'key-identifier packet)))
216          status keyserver)
217     (and (stringp key)
218          (setq key (concat "0x" (pgg-truncate-key-identifier key)))
219          (null (pgg-lookup-key key))
220          (or fetch (interactive-p))
221          (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
222          (setq keyserver
223                (or (cdr (assq 'preferred-key-server packet))
224                    pgg-default-keyserver-address))
225          (pgg-fetch-key keyserver key))
226     (setq status 
227           (pgg-save-coding-system start end
228             (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
229                         (point-min) (point-max) signature)))
230     (when (interactive-p)
231       (let ((temp-buffer-show-function
232              (function pgg-temp-buffer-show-function)))
233         (with-output-to-temp-buffer pgg-echo-buffer
234           (set-buffer standard-output)
235           (insert-buffer-substring (if status pgg-output-buffer
236                                      pgg-errors-buffer)))))
237     status))
238
239 ;;;###autoload
240 (defun pgg-insert-key ()
241   "Insert the ASCII armored public key."
242   (interactive)
243   (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
244
245 ;;;###autoload
246 (defun pgg-snarf-keys-region (start end)
247   "Import public keys in the current region between START and END."
248   (interactive "r")
249   (pgg-save-coding-system start end
250     (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
251                 start end)))
252
253 (defun pgg-lookup-key (string &optional type)
254   (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
255
256 (defvar pgg-insert-url-function  (function pgg-insert-url-with-w3))
257
258 (defun pgg-insert-url-with-w3 (url)
259   (ignore-errors
260     (require 'w3)
261     (require 'url)
262     (let (buffer-file-name)
263       (url-insert-file-contents url))))
264
265 (defvar pgg-insert-url-extra-arguments nil)
266 (defvar pgg-insert-url-program nil)
267
268 (defun pgg-insert-url-with-program (url)
269   (let ((args (copy-sequence pgg-insert-url-extra-arguments))
270         process)
271     (insert
272      (with-temp-buffer
273        (setq process
274              (apply #'start-process " *PGG url*" (current-buffer)
275                     pgg-insert-url-program (nconc args (list url))))
276        (set-process-sentinel process #'ignore)
277        (while (eq 'run (process-status process))
278          (accept-process-output process 5))
279        (delete-process process)
280        (if (and process (eq 'run (process-status process)))
281            (interrupt-process process))
282        (buffer-string)))))
283
284 (defun pgg-fetch-key (keyserver key)
285   "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
286   (with-current-buffer (get-buffer-create pgg-output-buffer)
287     (buffer-disable-undo)
288     (erase-buffer)
289     (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
290                      (substring keyserver 0 (1- (match-end 0))))))
291       (save-excursion
292         (funcall pgg-insert-url-function
293                  (if proto keyserver
294                    (format "http://%s:11371/pks/lookup?op=get&search=%s"
295                            keyserver key))))
296       (when (re-search-forward "^-+BEGIN" nil 'last)
297         (delete-region (point-min) (match-beginning 0))
298         (when (re-search-forward "^-+END" nil t)
299           (delete-region (progn (end-of-line) (point))
300                          (point-max)))
301         (insert "\n")
302         (with-temp-buffer
303           (insert-buffer-substring pgg-output-buffer)
304           (pgg-snarf-keys-region (point-min)(point-max)))))))
305
306
307 (provide 'pgg)
308
309 ;;; pgg.el ends here