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