* pgg.el: Autoload url-insert-file-contents instead of loading w3/url.
[gnus] / lisp / pgg.el
1 ;;; pgg.el --- glue for the various PGP implementations.
2
3 ;; Copyright (C) 1999, 2000, 2003, 2004, 2005 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 GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (require 'pgg-def)
31 (require 'pgg-parse)
32 (require 'password)
33 (autoload 'url-insert-file-contents "url")
34
35 ;; Don't merge these two `eval-when-compile's.
36 (eval-when-compile
37   (require 'cl))
38
39 ;;; @ utility functions
40 ;;;
41
42 (defvar pgg-fetch-key-function (if (fboundp 'url-insert-file-contents)
43                                    (function pgg-fetch-key-with-w3)))
44
45 (defun pgg-invoke (func scheme &rest args)
46   (progn
47     (require (intern (format "pgg-%s" scheme)))
48     (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
49
50 (put 'pgg-save-coding-system 'lisp-indent-function 2)
51
52 (defmacro pgg-save-coding-system (start end &rest body)
53   `(if (interactive-p)
54        (let ((buffer (current-buffer)))
55          (with-temp-buffer
56            (let (buffer-undo-list)
57              (insert-buffer-substring buffer ,start ,end)
58              (encode-coding-region (point-min)(point-max)
59                                    buffer-file-coding-system)
60              (prog1 (save-excursion ,@body)
61                (push nil buffer-undo-list)
62                (ignore-errors (undo))))))
63      (save-restriction
64        (narrow-to-region ,start ,end)
65        ,@body)))
66
67 (defun pgg-temp-buffer-show-function (buffer)
68   (let ((window (or (get-buffer-window buffer 'visible)
69                     (split-window-vertically))))
70     (set-window-buffer window buffer)
71     (shrink-window-if-larger-than-buffer window)))
72
73 (defun pgg-display-output-buffer (start end status)
74   (if status
75       (progn
76         (delete-region start end)
77         (insert-buffer-substring pgg-output-buffer)
78         (decode-coding-region start (point) buffer-file-coding-system))
79     (let ((temp-buffer-show-function
80            (function pgg-temp-buffer-show-function)))
81       (with-output-to-temp-buffer pgg-echo-buffer
82         (set-buffer standard-output)
83         (insert-buffer-substring pgg-errors-buffer)))))
84
85 (defun pgg-read-passphrase (prompt &optional key)
86   (when pgg-cache-passphrase
87     (password-read prompt (setq key (pgg-truncate-key-identifier key)))))
88
89 (defun pgg-add-passphrase-cache (key passphrase)
90   (let ((password-cache-expiry pgg-passphrase-cache-expiry))
91     (password-cache-add (setq key (pgg-truncate-key-identifier key))
92                         passphrase)))
93
94 (defun pgg-remove-passphrase-cache (key)
95   (password-cache-remove key))
96
97 (defmacro pgg-convert-lbt-region (start end lbt)
98   `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
99      (goto-char ,start)
100      (case ,lbt
101        (CRLF
102         (while (progn
103                  (end-of-line)
104                  (> (marker-position pgg-conversion-end) (point)))
105           (insert "\r")
106           (forward-line 1)))
107        (LF
108         (while (re-search-forward "\r$" pgg-conversion-end t)
109           (replace-match ""))))))
110
111 (put 'pgg-as-lbt 'lisp-indent-function 3)
112
113 (defmacro pgg-as-lbt (start end lbt &rest body)
114   `(let ((inhibit-read-only t)
115          buffer-read-only
116          buffer-undo-list)
117      (pgg-convert-lbt-region ,start ,end ,lbt)
118      (let ((,end (point)))
119        ,@body)
120      (push nil buffer-undo-list)
121      (ignore-errors (undo))))
122
123 (put 'pgg-process-when-success 'lisp-indent-function 0)
124
125 (defmacro pgg-process-when-success (&rest body)
126   `(with-current-buffer pgg-output-buffer
127      (if (zerop (buffer-size)) nil ,@body t)))
128
129 (defalias 'pgg-make-temp-file
130   (if (fboundp 'make-temp-file)
131       'make-temp-file
132     (lambda (prefix &optional dir-flag)
133       (let ((file (expand-file-name
134                    (make-temp-name prefix)
135                    (if (fboundp 'temp-directory)
136                        (temp-directory)
137                      temporary-file-directory))))
138         (if dir-flag
139             (make-directory file))
140         file))))
141
142 ;;; @ interface functions
143 ;;;
144
145 ;;;###autoload
146 (defun pgg-encrypt-region (start end rcpts &optional sign)
147   "Encrypt the current region between START and END for RCPTS.
148 If optional argument SIGN is non-nil, do a combined sign and encrypt."
149   (interactive
150    (list (region-beginning)(region-end)
151          (split-string (read-string "Recipients: ") "[ \t,]+")))
152   (let ((status
153          (pgg-save-coding-system start end
154            (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
155                        (point-min) (point-max) rcpts sign))))
156     (when (interactive-p)
157       (pgg-display-output-buffer start end status))
158     status))
159
160 ;;;###autoload
161 (defun pgg-encrypt (rcpts &optional sign start end)
162   "Encrypt the current buffer for RCPTS.
163 If optional argument SIGN is non-nil, do a combined sign and encrypt.
164 If optional arguments START and END are specified, only encrypt within
165 the region."
166   (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
167   (let* ((start (or start (point-min)))
168          (end (or end (point-max)))
169          (status (pgg-encrypt-region start end rcpts sign)))
170     (when (interactive-p)
171       (pgg-display-output-buffer start end status))
172     status))
173
174 ;;;###autoload
175 (defun pgg-decrypt-region (start end)
176   "Decrypt the current region between START and END."
177   (interactive "r")
178   (let* ((buf (current-buffer))
179          (status
180           (pgg-save-coding-system start end
181             (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
182                         (point-min) (point-max)))))
183     (when (interactive-p)
184       (pgg-display-output-buffer start end status))
185     status))
186
187 ;;;###autoload
188 (defun pgg-decrypt (&optional start end)
189   "Decrypt the current buffer.
190 If optional arguments START and END are specified, only decrypt within
191 the region."
192   (interactive "")
193   (let* ((start (or start (point-min)))
194          (end (or end (point-max)))
195          (status (pgg-decrypt-region start end)))
196     (when (interactive-p)
197       (pgg-display-output-buffer start end status))
198     status))
199
200 ;;;###autoload
201 (defun pgg-sign-region (start end &optional cleartext)
202   "Make the signature from text between START and END.
203 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
204 a detached signature.
205 If this function is called interactively, CLEARTEXT is enabled
206 and the the output is displayed."
207   (interactive "r")
208   (let ((status (pgg-save-coding-system start end
209                   (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
210                               (point-min) (point-max)
211                               (or (interactive-p) cleartext)))))
212     (when (interactive-p)
213       (pgg-display-output-buffer start end status))
214     status))
215
216 ;;;###autoload
217 (defun pgg-sign (&optional cleartext start end)
218   "Sign the current buffer.
219 If the optional argument CLEARTEXT is non-nil, it does not create a
220 detached signature.
221 If optional arguments START and END are specified, only sign data
222 within the region.
223 If this function is called interactively, CLEARTEXT is enabled
224 and the the output is displayed."
225   (interactive "")
226   (let* ((start (or start (point-min)))
227          (end (or end (point-max)))
228          (status (pgg-sign-region start end (or (interactive-p) cleartext))))
229     (when (interactive-p)
230       (pgg-display-output-buffer start end status))
231     status))
232   
233 ;;;###autoload
234 (defun pgg-verify-region (start end &optional signature fetch)
235   "Verify the current region between START and END.
236 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
237 the detached signature of the current region.
238
239 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
240 signer's public key from `pgg-default-keyserver-address'."
241   (interactive "r")
242   (let* ((packet
243           (if (null signature) nil
244             (with-temp-buffer
245               (buffer-disable-undo)
246               (if (fboundp 'set-buffer-multibyte)
247                   (set-buffer-multibyte nil))
248               (insert-file-contents signature)
249               (cdr (assq 2 (pgg-decode-armor-region
250                             (point-min)(point-max)))))))
251          (key (cdr (assq 'key-identifier packet)))
252          status keyserver)
253     (and (stringp key)
254          pgg-query-keyserver
255          (setq key (concat "0x" (pgg-truncate-key-identifier key)))
256          (null (pgg-lookup-key key))
257          (or fetch (interactive-p))
258          (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
259          (setq keyserver
260                (or (cdr (assq 'preferred-key-server packet))
261                    pgg-default-keyserver-address))
262          (pgg-fetch-key keyserver key))
263     (setq status 
264           (pgg-save-coding-system start end
265             (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
266                         (point-min) (point-max) signature)))
267     (when (interactive-p)
268       (let ((temp-buffer-show-function
269              (function pgg-temp-buffer-show-function)))
270         (with-output-to-temp-buffer pgg-echo-buffer
271           (set-buffer standard-output)
272           (insert-buffer-substring (if status pgg-output-buffer
273                                      pgg-errors-buffer)))))
274     status))
275
276 ;;;###autoload
277 (defun pgg-verify (&optional signature fetch start end)
278   "Verify the current buffer.
279 If the optional argument SIGNATURE is non-nil, it is treated as
280 the detached signature of the current region.
281 If the optional argument FETCH is non-nil, we attempt to fetch the
282 signer's public key from `pgg-default-keyserver-address'.
283 If optional arguments START and END are specified, only verify data
284 within the region."
285   (interactive "")
286   (let* ((start (or start (point-min)))
287          (end (or end (point-max)))
288          (status (pgg-verify-region start end signature fetch)))
289     (when (interactive-p)
290       (let ((temp-buffer-show-function
291              (function pgg-temp-buffer-show-function)))
292         (with-output-to-temp-buffer pgg-echo-buffer
293           (set-buffer standard-output)
294           (insert-buffer-substring (if status pgg-output-buffer
295                                      pgg-errors-buffer)))))
296     status))
297
298 ;;;###autoload
299 (defun pgg-insert-key ()
300   "Insert the ASCII armored public key."
301   (interactive)
302   (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
303
304 ;;;###autoload
305 (defun pgg-snarf-keys-region (start end)
306   "Import public keys in the current region between START and END."
307   (interactive "r")
308   (pgg-save-coding-system start end
309     (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
310                 start end)))
311
312 ;;;###autoload
313 (defun pgg-snarf-keys ()
314   "Import public keys in the current buffer."
315   (interactive "")
316   (pgg-snarf-keys-region (point-min) (point-max)))
317
318 (defun pgg-lookup-key (string &optional type)
319   (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
320
321 (defvar pgg-insert-url-function  (function pgg-insert-url-with-w3))
322
323 (defun pgg-insert-url-with-w3 (url)
324   (ignore-errors
325     (let (buffer-file-name)
326       (url-insert-file-contents url))))
327
328 (defvar pgg-insert-url-extra-arguments nil)
329 (defvar pgg-insert-url-program nil)
330
331 (defun pgg-insert-url-with-program (url)
332   (let ((args (copy-sequence pgg-insert-url-extra-arguments))
333         process)
334     (insert
335      (with-temp-buffer
336        (setq process
337              (apply #'start-process " *PGG url*" (current-buffer)
338                     pgg-insert-url-program (nconc args (list url))))
339        (set-process-sentinel process #'ignore)
340        (while (eq 'run (process-status process))
341          (accept-process-output process 5))
342        (delete-process process)
343        (if (and process (eq 'run (process-status process)))
344            (interrupt-process process))
345        (buffer-string)))))
346
347 (defun pgg-fetch-key (keyserver key)
348   "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
349   (with-current-buffer (get-buffer-create pgg-output-buffer)
350     (buffer-disable-undo)
351     (erase-buffer)
352     (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
353                      (substring keyserver 0 (1- (match-end 0))))))
354       (save-excursion
355         (funcall pgg-insert-url-function
356                  (if proto keyserver
357                    (format "http://%s:11371/pks/lookup?op=get&search=%s"
358                            keyserver key))))
359       (when (re-search-forward "^-+BEGIN" nil 'last)
360         (delete-region (point-min) (match-beginning 0))
361         (when (re-search-forward "^-+END" nil t)
362           (delete-region (progn (end-of-line) (point))
363                          (point-max)))
364         (insert "\n")
365         (with-temp-buffer
366           (insert-buffer-substring pgg-output-buffer)
367           (pgg-snarf-keys-region (point-min)(point-max)))))))
368
369
370 (provide 'pgg)
371
372 ;;; arch-tag: 9cc705dd-1e6a-4c90-8dce-c3561f9a2cf4
373 ;;; pgg.el ends here