1 ;;; gpg-ring.el --- Major mode for editing GnuPG key rings.
3 ;; Copyright (C) 2000 RUS-CERT, University Of Stuttgart
5 ;; Author: Florian Weimer <Florian.Weimer@RUS.Uni-Stuttgart.DE>
6 ;; Maintainer: Florian Weimer <Florian.Weimer@RUS.Uni-Stuttgart.DE>
10 ;; $Id: gpg-ring.el,v 1.1.6.1 2001-04-15 23:34:39 yamaoka Exp $
12 ;; This file is NOT (yet?) part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
39 ;;; Customization: Groups:
41 (defgroup gpg-ring nil
42 "GNU Privacy Guard user interface."
43 :tag "GnuPG user interface"
46 ;;; Customization: Variables:
48 (defface gpg-ring-key-invalid-face
50 (:foreground "yellow" :background "red"))
51 (t (:bold t :italic t :underline t)))
52 "Face for strings indicating key invalidity."
55 (defface gpg-ring-uncertain-validity-face
56 '((((class color)) (:foreground "red"))
58 "Face for strings indicating uncertain validity."
61 (defface gpg-ring-full-validity-face
62 '((((class color)) (:foreground "ForestGreen" :bold t))
64 "Face for strings indicating key invalidity."
67 (defvar gpg-ring-mode-hook nil
68 "Normal hook run when entering GnuPG ring mode.")
72 (defconst gpg-ring-algo-alist
74 (rsa-encrypt-only . "RSA-E")
75 (rsa-sign-only . "RSA-S")
76 (elgamal-encrypt-only . "ELG-E")
79 "Alist mapping algorithm IDs to algorithm abbreviations.")
81 (defconst gpg-ring-trust-alist
82 '((not-known "???" gpg-ring-uncertain-validity-face)
83 (disabled "DIS" gpg-ring-key-invalid-face)
84 (revoked "REV" gpg-ring-key-invalid-face)
85 (expired "EXP" gpg-ring-key-invalid-face)
86 (trust-undefined "QES" gpg-ring-uncertain-validity-face)
87 (trust-none "NON" gpg-ring-uncertain-validity-face)
88 (trust-marginal "MAR")
89 (trust-full "FUL" gpg-ring-full-validity-face)
90 (trust-ultimate "ULT" gpg-ring-full-validity-face))
91 "Alist mapping trust IDs to trust abbrevs and faces.")
93 (defvar gpg-ring-mode-map
94 (let ((map (make-keymap)))
95 (suppress-keymap map t)
97 "Keymap for `gpg-ring-mode'.")
99 (define-key gpg-ring-mode-map "0" 'delete-window)
100 (define-key gpg-ring-mode-map "1" 'delete-other-windows)
101 (define-key gpg-ring-mode-map "M" 'gpg-ring-mark-process-all)
102 (define-key gpg-ring-mode-map "U" 'gpg-ring-unmark-all)
103 (define-key gpg-ring-mode-map "a" 'gpg-ring-toggle-show-unusable)
104 (define-key gpg-ring-mode-map "d" 'gpg-ring-mark-delete)
105 (define-key gpg-ring-mode-map "f" 'gpg-ring-update-key)
106 (define-key gpg-ring-mode-map "g" 'gpg-ring-update)
107 (define-key gpg-ring-mode-map "i" 'gpg-ring-show-key)
108 (define-key gpg-ring-mode-map "l" 'gpg-ring-toggle-show-all-ids)
109 (define-key gpg-ring-mode-map "m" 'gpg-ring-mark-process)
110 (define-key gpg-ring-mode-map "n" 'gpg-ring-next-record)
111 (define-key gpg-ring-mode-map "p" 'gpg-ring-previous-record)
112 (define-key gpg-ring-mode-map "q" 'gpg-ring-quit)
113 (define-key gpg-ring-mode-map "u" 'gpg-ring-unmark)
114 (define-key gpg-ring-mode-map "x" 'gpg-ring-extract-keys)
115 (define-key gpg-ring-mode-map "X" 'gpg-ring-extract-keys-to-kill)
117 (define-key gpg-ring-mode-map "\C-c\C-c" 'gpg-ring-action)
119 ;;; Internal functions:
121 (defvar gpg-ring-key-list
123 "List of keys in the key list buffer.")
124 (make-variable-buffer-local 'gpg-ring-key-list)
126 (defvar gpg-ring-update-funcs
128 "List of functions called to obtain the key list.")
129 (make-variable-buffer-local 'gpg-ring-update-funcs)
131 (defvar gpg-ring-show-unusable
133 "If t, show expired, revoked and disabled keys, too.")
134 (make-variable-buffer-local 'gpg-ring-show-unusable)
136 (defvar gpg-ring-show-all-ids
138 "If t, show all user IDs. If nil, show only the primary user ID.")
139 (make-variable-buffer-local 'gpg-ring-show-all-ids)
141 (defvar gpg-ring-marks-alist
143 "Alist of (UNIQUE-ID MARK KEY).
144 UNIQUE-ID is a unique key ID from GnuPG. MARK is either `?D'
145 (marked for deletion), or `?*' (marked for processing).")
146 (make-variable-buffer-local 'gpg-ring-marks-alist)
148 (defvar gpg-ring-action
150 "Function to call when `gpg-ring-action' is invoked.
151 A list of the keys which are marked for processing is passed as argument.")
152 (make-variable-buffer-local 'gpg-ring-action)
154 (defun gpg-ring-mode ()
155 "Mode for editing GnuPG key rings.
156 \\{gpg-ring-mode-map}
157 Turning on gpg-ring-mode runs `gpg-ring-mode-hook'."
159 (kill-all-local-variables)
160 (buffer-disable-undo)
161 (setq truncate-lines t)
162 (setq buffer-read-only t)
163 (use-local-map gpg-ring-mode-map)
164 (setq mode-name "Key Ring")
165 (setq major-mode 'gpg-ring-mode)
166 (run-hooks 'gpg-ring-mode-hook))
169 (defmacro gpg-ring-record-start (&optional pos)
170 "Return buffer position of start of record containing POS."
171 `(get-text-property (or ,pos (point)) 'gpg-record-start))
173 (defun gpg-ring-current-key (&optional pos)
174 "Return GnuPG key at POS, or at point if ommitted."
175 (or (get-text-property (or pos (point)) 'gpg-key)
176 (error "No record on current line")))
178 (defun gpg-ring-goto-record (pos)
179 "Go to record starting at POS.
180 Position point after the marks at the beginning of a record."
184 (defun gpg-ring-next-record ()
185 "Advances point to the start of the next record."
187 (let ((start (next-single-property-change
188 (point) 'gpg-record-start nil (point-max))))
189 ;; Don't advance to the last line of the buffer.
190 (when (/= start (point-max))
191 (gpg-ring-goto-record start))))
193 (defun gpg-ring-previous-record ()
194 "Advances point to the start of the previous record."
196 ;; The last line of the buffer doesn't contain a record.
197 (let ((start (gpg-ring-record-start)))
199 (gpg-ring-goto-record (previous-single-property-change
200 start 'gpg-record-start nil (point-min)))
201 (gpg-ring-goto-record
202 (gpg-ring-record-start (1- (point-max)))))))
204 (defun gpg-ring-set-mark (&optional pos mark)
205 "Set MARK on record at POS, or at point if POS is omitted.
206 If MARK is omitted, clear it."
208 (let* ((start (gpg-ring-record-start pos))
209 (key (gpg-ring-current-key start))
210 (id (gpg-key-unique-id key))
211 (entry (assoc id gpg-ring-marks-alist))
214 ;; Replace the mark character.
215 (subst-char-in-region (point) (1+ (point)) (char-after)
217 ;; Store the mark in alist.
219 (setcdr entry (if mark (list mark key)))
221 (push (list id mark key) gpg-ring-marks-alist))))))
223 (defun gpg-ring-marked-keys (&optional only-marked mark)
224 "Return list of key specs which have MARK.
225 If no marks are present and ONLY-MARKED is not nil, return singleton
226 list with key of the current record. If MARK is omitted, `?*' is
228 (let ((the-marker (or mark ?*))
229 (marks gpg-ring-marks-alist)
232 (let ((mark (pop marks)))
233 ;; If this entry has got the right mark ...
234 (when (equal (nth 1 mark) the-marker)
235 ;; ... rember the key spec.
236 (push (nth 2 mark) key-list))))
237 (or key-list (if (not only-marked) (list (gpg-ring-current-key))))))
239 (defun gpg-ring-mark-process ()
240 "Mark record at point for processing."
242 (gpg-ring-set-mark nil ?*)
243 (gpg-ring-next-record))
245 (defun gpg-ring-mark-delete ()
246 "Mark record at point for processing."
248 (gpg-ring-set-mark nil ?D)
249 (gpg-ring-next-record))
251 (defun gpg-ring-unmark ()
252 "Mark record at point for processing."
255 (gpg-ring-next-record))
257 (defun gpg-ring-mark-process-all ()
258 "Put process mark on all records."
260 (setq gpg-ring-marks-alist
261 (mapcar (lambda (key)
262 (list (gpg-key-unique-id key) ?* key))
264 (gpg-ring-regenerate))
266 (defun gpg-ring-unmark-all ()
267 "Remove all record marks."
269 (setq gpg-ring-marks-alist nil)
270 (gpg-ring-regenerate))
272 (defun gpg-ring-toggle-show-unusable ()
273 "Toggle value if `gpg-ring-show-unusable'."
275 (setq gpg-ring-show-unusable (not gpg-ring-show-unusable))
276 (gpg-ring-regenerate))
278 (defun gpg-ring-toggle-show-all-ids ()
279 "Toggle value of `gpg-ring-show-all-ids'."
281 (setq gpg-ring-show-all-ids (not gpg-ring-show-all-ids))
282 (gpg-ring-regenerate))
284 (defvar gpg-ring-output-buffer-name "*GnuPG Output*"
285 "Name buffer to which output from GnuPG is sent.")
287 (defmacro gpg-ring-with-output-buffer (&rest body)
288 "Erase GnuPG output buffer, evaluate BODY in it, and display it."
289 `(with-current-buffer (get-buffer-create gpg-ring-output-buffer-name)
291 (setq truncate-lines t)
293 (goto-char (point-min))
294 (display-buffer gpg-ring-output-buffer-name)))
296 (defun gpg-ring-quit ()
297 "Bury key list buffer and kill GnuPG output buffer."
299 (let ((output (get-buffer gpg-ring-output-buffer-name)))
301 (kill-buffer output)))
302 (when (eq 'gpg-ring-mode major-mode)
305 (defun gpg-ring-show-key ()
306 "Show information for current key."
308 (let ((keys (gpg-ring-marked-keys)))
309 (gpg-ring-with-output-buffer
310 (gpg-key-insert-information (gpg-key-unique-id-list keys)))))
312 (defun gpg-ring-extract-keys ()
313 "Export currently selected public keys in ASCII armor."
315 (let ((keys (gpg-ring-marked-keys)))
316 (gpg-ring-with-output-buffer
317 (gpg-key-insert-public-key (gpg-key-unique-id-list keys)))))
319 (defun gpg-ring-extract-keys-to-kill ()
320 "Export currently selected public keys in ASCII armor to kill ring."
322 (let ((keys (gpg-ring-marked-keys)))
324 (gpg-key-insert-public-key (gpg-key-unique-id-list keys))
325 (copy-region-as-kill (point-min) (point-max)))))
327 (defun gpg-ring-update-key ()
328 "Fetch key information from key server."
330 (let ((keys (gpg-ring-marked-keys)))
331 (gpg-ring-with-output-buffer
332 (gpg-key-retrieve (gpg-key-unique-id-list keys)))))
334 (defun gpg-ring-insert-key-stat (key)
335 (let* ((validity (gpg-key-validity key))
336 (validity-entry (assq validity gpg-ring-trust-alist))
337 (trust (gpg-key-trust key))
338 (trust-entry (assq trust gpg-ring-trust-alist)))
339 ;; Insert abbrev for key status.
340 (let ((start (point)))
341 (insert (nth 1 validity-entry))
342 ;; Change face if necessary.
343 (when (nth 2 validity-entry)
344 (add-text-properties start (point)
345 (list 'face (nth 2 validity-entry)))))
346 ;; Trust, key ID, length, algorithm, creation date.
347 (insert (format "/%s %-8s/%4d/%-5s created %s"
349 (gpg-short-key-id key)
351 (cdr (assq (gpg-key-algorithm key) gpg-ring-algo-alist))
352 (gpg-key-creation-date key)))
354 (when (gpg-key-expire-date key)
356 (let ((start (point))
357 (expired (eq 'expired validity))
359 (insert (if expired "EXPIRED" "expires")
360 " " (gpg-key-expire-date key))
362 (add-text-properties start (point)
363 '(face gpg-ring-key-invalid-face)))))))
365 (defun gpg-ring-insert-key (key &optional mark)
366 "Inserts description for KEY into current buffer before point."
367 (let ((start (point)))
368 (insert (if mark mark " ")
369 " " (gpg-key-primary-user-id key) "\n"
371 (gpg-ring-insert-key-stat key)
373 (when gpg-ring-show-all-ids
374 (let ((uids (gpg-key-user-ids key)))
376 (insert " ID " (pop uids) "\n"))))
377 (add-text-properties start (point)
378 (list 'gpg-record-start start
381 (defun gpg-ring-regenerate ()
382 "Regenerate the key list buffer from stored data."
384 (let* ((key-list gpg-ring-key-list)
385 ;; Record position of point.
386 (old-record (if (eobp) ; No record on last line.
388 (gpg-key-unique-id (gpg-ring-current-key))))
389 (old-pos (if old-record (- (point) (gpg-ring-record-start))))
390 found new-pos new-pos-offset buffer-read-only new-marks)
391 ;; Replace buffer contents with new data.
394 (let* ((key (pop key-list))
395 (id (gpg-key-unique-id key))
396 (mark (assoc id gpg-ring-marks-alist)))
397 (when (or gpg-ring-show-unusable
398 (not (memq (gpg-key-validity key)
399 '(disabled revoked expired))))
400 ;; Check if point was in this record.
401 (when (and old-record
402 (string-equal old-record id))
403 (setq new-pos (point))
404 (setq new-pos-offset (+ new-pos old-pos)))
405 ;; Check if this record was marked.
408 (push mark new-marks)
409 (gpg-ring-insert-key key (nth 1 mark)))
410 (gpg-ring-insert-key key)))))
411 ;; Replace mark alist with the new one (which does not contain
412 ;; marks for records which vanished during this update).
413 (setq gpg-ring-marks-alist new-marks)
416 ;; We were at the end of the buffer before.
417 (goto-char (point-max))
419 (if (and (< new-pos-offset (point-max))
420 (equal old-record (gpg-key-unique-id
421 (gpg-ring-current-key new-pos-offset))))
422 ;; Record is there, with offset.
423 (goto-char new-pos-offset)
424 ;; Record is there, but not offset.
426 ;; Record is not there.
427 (goto-char (point-min))))))
429 (defun gpg-ring-update ()
430 "Update the key list buffer with new data."
432 (let ((funcs gpg-ring-update-funcs)
434 ;; Merge the sorted lists obtained by calling elements of
435 ;; `gpg-ring-update-funcs'.
437 (let ((additional (funcall (pop funcs)))
439 (while (and additional old)
440 (if (gpg-key-lessp (car additional) (car old))
441 (push (pop additional) new)
442 (if (gpg-key-lessp (car old) (car additional))
444 ;; Keys are perhaps equal. Always Add old key.
446 ;; If new key is equal, drop it, otherwise add it as well.
447 (if (string-equal (gpg-key-unique-id (car old))
448 (gpg-key-unique-id (car additional)))
450 (push (pop additional) new)))))
451 ;; Store new list as old one for next round.
452 (setq old (nconc (nreverse new) old additional))))
453 ;; Store the list in the buffer.
454 (setq gpg-ring-key-list old))
455 (gpg-ring-regenerate))
457 (defun gpg-ring-action ()
458 "Perform the action associated with this buffer."
461 (funcall gpg-ring-action (gpg-ring-marked-keys))
462 (error "No action for this buffer specified")))
465 (defun gpg-ring-keys (&optional key-list-funcs action)
467 (let ((buffer (get-buffer-create "*GnuPG Key List*")))
468 (with-current-buffer buffer
470 (setq gpg-ring-action action)
471 (setq gpg-ring-update-funcs key-list-funcs key-list-funcs)
473 (goto-char (point-min)))
474 (switch-to-buffer buffer)))
477 (defun gpg-ring-public (key-spec)
478 "List public keys matching keys KEY-SPEC."
479 (interactive "sList public keys containing: ")
480 (gpg-ring-keys `((lambda () (gpg-key-list-keys ,key-spec)))))
484 ;;; gpg-ring.el ends here