Mercurial > emacs
annotate lisp/epa.el @ 99292:673c91ec655c
(Version Control): Moved to maintaining.texi. Subnodes moved as well.
(Document Files): Moved to misc.texi.
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Fri, 31 Oct 2008 22:00:48 +0000 |
parents | 73388588c9b4 |
children | be17c10d7381 |
rev | line source |
---|---|
91647 | 1 ;;; epa.el --- the EasyPG Assistant |
2 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Daiki Ueno <ueno@unixuser.org> | |
5 ;; Keywords: PGP, GnuPG | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94490
diff
changeset
|
9 ;; GNU Emacs is free software: you can redistribute it and/or modify |
91647 | 10 ;; it under the terms of the GNU General Public License as published by |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94490
diff
changeset
|
11 ;; the Free Software Foundation, either version 3 of the License, or |
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94490
diff
changeset
|
12 ;; (at your option) any later version. |
91647 | 13 |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
94678
ee5932bf781d
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94490
diff
changeset
|
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
91647 | 21 |
22 ;;; Code: | |
23 | |
24 (require 'epg) | |
25 (require 'font-lock) | |
26 (require 'widget) | |
27 (eval-when-compile (require 'wid-edit)) | |
28 (require 'derived) | |
29 | |
30 (defgroup epa nil | |
31 "The EasyPG Assistant" | |
91703
bda1e76bc03b
* epa.el (epa-faces, epa):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
91687
diff
changeset
|
32 :version "23.1" |
91647 | 33 :group 'epg) |
34 | |
35 (defcustom epa-popup-info-window t | |
36 "If non-nil, status information from epa commands is displayed on | |
37 the separate window." | |
38 :type 'boolean | |
39 :group 'epa) | |
40 | |
41 (defcustom epa-info-window-height 5 | |
42 "Number of lines used to display status information." | |
43 :type 'integer | |
44 :group 'epa) | |
45 | |
46 (defgroup epa-faces nil | |
47 "Faces for epa-mode." | |
91703
bda1e76bc03b
* epa.el (epa-faces, epa):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
91687
diff
changeset
|
48 :version "23.1" |
91647 | 49 :group 'epa) |
50 | |
51 (defface epa-validity-high | |
52 `((((class color) (background dark)) | |
53 (:foreground "PaleTurquoise" | |
54 ,@(if (assq ':weight custom-face-attributes) | |
55 '(:weight bold) | |
56 '(:bold t)))) | |
57 (t | |
58 (,@(if (assq ':weight custom-face-attributes) | |
59 '(:weight bold) | |
60 '(:bold t))))) | |
61 "Face used for displaying the high validity." | |
62 :group 'epa-faces) | |
63 | |
64 (defface epa-validity-medium | |
65 `((((class color) (background dark)) | |
66 (:foreground "PaleTurquoise" | |
67 ,@(if (assq ':slant custom-face-attributes) | |
68 '(:slant italic) | |
69 '(:italic t)))) | |
70 (t | |
71 (,@(if (assq ':slant custom-face-attributes) | |
72 '(:slant italic) | |
73 '(:italic t))))) | |
74 "Face used for displaying the medium validity." | |
75 :group 'epa-faces) | |
76 | |
77 (defface epa-validity-low | |
78 `((t | |
79 (,@(if (assq ':slant custom-face-attributes) | |
80 '(:slant italic) | |
81 '(:italic t))))) | |
82 "Face used for displaying the low validity." | |
83 :group 'epa-faces) | |
84 | |
85 (defface epa-validity-disabled | |
86 `((t | |
87 (,@(if (assq ':slant custom-face-attributes) | |
88 '(:slant italic) | |
89 '(:italic t)) | |
90 :inverse-video t))) | |
91 "Face used for displaying the disabled validity." | |
92 :group 'epa-faces) | |
93 | |
94 (defface epa-string | |
95 '((((class color) (background dark)) | |
96 (:foreground "lightyellow")) | |
97 (((class color) (background light)) | |
98 (:foreground "blue4"))) | |
99 "Face used for displaying the string." | |
100 :group 'epa-faces) | |
101 | |
102 (defface epa-mark | |
103 `((((class color) (background dark)) | |
104 (:foreground "orange" | |
105 ,@(if (assq ':weight custom-face-attributes) | |
106 '(:weight bold) | |
107 '(:bold t)))) | |
108 (((class color) (background light)) | |
109 (:foreground "red" | |
110 ,@(if (assq ':weight custom-face-attributes) | |
111 '(:weight bold) | |
112 '(:bold t)))) | |
113 (t | |
114 (,@(if (assq ':weight custom-face-attributes) | |
115 '(:weight bold) | |
116 '(:bold t))))) | |
117 "Face used for displaying the high validity." | |
118 :group 'epa-faces) | |
119 | |
120 (defface epa-field-name | |
121 `((((class color) (background dark)) | |
122 (:foreground "PaleTurquoise" | |
123 ,@(if (assq ':weight custom-face-attributes) | |
124 '(:weight bold) | |
125 '(:bold t)))) | |
126 (t | |
127 (,@(if (assq ':weight custom-face-attributes) | |
128 '(:weight bold) | |
129 '(:bold t))))) | |
130 "Face for the name of the attribute field." | |
131 :group 'epa) | |
132 | |
133 (defface epa-field-body | |
134 `((((class color) (background dark)) | |
135 (:foreground "turquoise" | |
136 ,@(if (assq ':slant custom-face-attributes) | |
137 '(:slant italic) | |
138 '(:italic t)))) | |
139 (t | |
140 (,@(if (assq ':slant custom-face-attributes) | |
141 '(:slant italic) | |
142 '(:italic t))))) | |
143 "Face for the body of the attribute field." | |
144 :group 'epa) | |
145 | |
146 (defcustom epa-validity-face-alist | |
147 '((unknown . epa-validity-disabled) | |
148 (invalid . epa-validity-disabled) | |
149 (disabled . epa-validity-disabled) | |
150 (revoked . epa-validity-disabled) | |
151 (expired . epa-validity-disabled) | |
152 (none . epa-validity-low) | |
153 (undefined . epa-validity-low) | |
154 (never . epa-validity-low) | |
155 (marginal . epa-validity-medium) | |
156 (full . epa-validity-high) | |
157 (ultimate . epa-validity-high)) | |
158 "An alist mapping validity values to faces." | |
159 :type '(repeat (cons symbol face)) | |
160 :group 'epa) | |
161 | |
162 (defvar epa-font-lock-keywords | |
163 '(("^\\*" | |
164 (0 'epa-mark)) | |
165 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$" | |
166 (1 'epa-field-name) | |
167 (2 'epa-field-body))) | |
168 "Default expressions to addon in epa-mode.") | |
169 | |
170 (defconst epa-pubkey-algorithm-letter-alist | |
171 '((1 . ?R) | |
172 (2 . ?r) | |
173 (3 . ?s) | |
174 (16 . ?g) | |
175 (17 . ?D) | |
176 (20 . ?G))) | |
177 | |
178 (defvar epa-protocol 'OpenPGP | |
179 "*The default protocol. | |
180 The value can be either OpenPGP or CMS. | |
181 | |
182 You should bind this variable with `let', but do not set it globally.") | |
183 | |
184 (defvar epa-armor nil | |
185 "*If non-nil, epa commands create ASCII armored output. | |
186 | |
187 You should bind this variable with `let', but do not set it globally.") | |
188 | |
189 (defvar epa-textmode nil | |
190 "*If non-nil, epa commands treat input files as text. | |
191 | |
192 You should bind this variable with `let', but do not set it globally.") | |
193 | |
194 (defvar epa-keys-buffer nil) | |
195 (defvar epa-key-buffer-alist nil) | |
196 (defvar epa-key nil) | |
197 (defvar epa-list-keys-arguments nil) | |
198 (defvar epa-info-buffer nil) | |
199 (defvar epa-last-coding-system-specified nil) | |
200 | |
201 (defvar epa-key-list-mode-map | |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
202 (let ((keymap (make-sparse-keymap)) |
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
203 (menu-map (make-sparse-keymap))) |
91647 | 204 (define-key keymap "m" 'epa-mark-key) |
205 (define-key keymap "u" 'epa-unmark-key) | |
206 (define-key keymap "d" 'epa-decrypt-file) | |
207 (define-key keymap "v" 'epa-verify-file) | |
208 (define-key keymap "s" 'epa-sign-file) | |
209 (define-key keymap "e" 'epa-encrypt-file) | |
210 (define-key keymap "r" 'epa-delete-keys) | |
211 (define-key keymap "i" 'epa-import-keys) | |
212 (define-key keymap "o" 'epa-export-keys) | |
213 (define-key keymap "g" 'revert-buffer) | |
214 (define-key keymap "n" 'next-line) | |
215 (define-key keymap "p" 'previous-line) | |
216 (define-key keymap " " 'scroll-up) | |
217 (define-key keymap [delete] 'scroll-down) | |
218 (define-key keymap "q" 'epa-exit-buffer) | |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
219 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map)) |
94490
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
220 (define-key menu-map [epa-key-list-unmark-key] |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
221 '(menu-item "Unmark Key" epa-unmark-key |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
222 :help "Unmark a key")) |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
223 (define-key menu-map [epa-key-list-mark-key] |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
224 '(menu-item "Mark Key" epa-mark-key |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
225 :help "Mark a key")) |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
226 (define-key menu-map [separator-epa-file] '(menu-item "--")) |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
227 (define-key menu-map [epa-verify-file] |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
228 '(menu-item "Verify File..." epa-verify-file |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
229 :help "Verify FILE")) |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
230 (define-key menu-map [epa-sign-file] |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
231 '(menu-item "Sign File..." epa-sign-file |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
232 :help "Sign FILE by SIGNERS keys selected")) |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
233 (define-key menu-map [epa-decrypt-file] |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
234 '(menu-item "Decrypt File..." epa-decrypt-file |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
235 :help "Decrypt FILE")) |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
236 (define-key menu-map [epa-encrypt-file] |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
237 '(menu-item "Encrypt File.." epa-encrypt-file |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
238 :help "Encrypt FILE for RECIPIENTS")) |
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
239 (define-key menu-map [separator-epa-key-list] '(menu-item "--")) |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
240 (define-key menu-map [epa-key-list-delete-keys] |
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
241 '(menu-item "Delete keys" epa-delete-keys |
94490
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
242 :help "Delete Marked Keys")) |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
243 (define-key menu-map [epa-key-list-import-keys] |
94490
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
244 '(menu-item "Import Keys" epa-import-keys |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
245 :help "Import keys from a file")) |
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
246 (define-key menu-map [epa-key-list-export-keys] |
94490
dcbaed9de6ff
(epa-key-list-mode-map): Add more menu entries.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
94471
diff
changeset
|
247 '(menu-item "Export Keys" epa-export-keys |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
248 :help "Export marked keys to a file")) |
91647 | 249 keymap)) |
250 | |
251 (defvar epa-key-mode-map | |
252 (let ((keymap (make-sparse-keymap))) | |
253 (define-key keymap "q" 'epa-exit-buffer) | |
254 keymap)) | |
255 | |
256 (defvar epa-info-mode-map | |
257 (let ((keymap (make-sparse-keymap))) | |
258 (define-key keymap "q" 'delete-window) | |
259 keymap)) | |
260 | |
261 (defvar epa-exit-buffer-function #'bury-buffer) | |
262 | |
263 (define-widget 'epa-key 'push-button | |
264 "Button for representing a epg-key object." | |
265 :format "%[%v%]" | |
266 :button-face-get 'epa--key-widget-button-face-get | |
267 :value-create 'epa--key-widget-value-create | |
268 :action 'epa--key-widget-action | |
269 :help-echo 'epa--key-widget-help-echo) | |
270 | |
271 (defun epa--key-widget-action (widget &optional event) | |
97719
73388588c9b4
(epa--key-widget-action): Save the selected window to
Daiki Ueno <ueno@unixuser.org>
parents:
96760
diff
changeset
|
272 (save-selected-window |
73388588c9b4
(epa--key-widget-action): Save the selected window to
Daiki Ueno <ueno@unixuser.org>
parents:
96760
diff
changeset
|
273 (epa--show-key (widget-get widget :value)))) |
91647 | 274 |
275 (defun epa--key-widget-value-create (widget) | |
276 (let* ((key (widget-get widget :value)) | |
277 (primary-sub-key (car (epg-key-sub-key-list key))) | |
278 (primary-user-id (car (epg-key-user-id-list key)))) | |
279 (insert (format "%c " | |
280 (if (epg-sub-key-validity primary-sub-key) | |
281 (car (rassq (epg-sub-key-validity primary-sub-key) | |
282 epg-key-validity-alist)) | |
283 ? )) | |
284 (epg-sub-key-id primary-sub-key) | |
285 " " | |
286 (if primary-user-id | |
287 (if (stringp (epg-user-id-string primary-user-id)) | |
288 (epg-user-id-string primary-user-id) | |
289 (epg-decode-dn (epg-user-id-string primary-user-id))) | |
290 "")))) | |
291 | |
292 (defun epa--key-widget-button-face-get (widget) | |
293 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list | |
294 (widget-get widget :value)))))) | |
295 (if validity | |
296 (cdr (assq validity epa-validity-face-alist)) | |
297 'default))) | |
298 | |
299 (defun epa--key-widget-help-echo (widget) | |
300 (format "Show %s" | |
301 (epg-sub-key-id (car (epg-key-sub-key-list | |
302 (widget-get widget :value)))))) | |
303 | |
304 (eval-and-compile | |
305 (if (fboundp 'encode-coding-string) | |
306 (defalias 'epa--encode-coding-string 'encode-coding-string) | |
307 (defalias 'epa--encode-coding-string 'identity))) | |
308 | |
309 (eval-and-compile | |
310 (if (fboundp 'decode-coding-string) | |
311 (defalias 'epa--decode-coding-string 'decode-coding-string) | |
312 (defalias 'epa--decode-coding-string 'identity))) | |
313 | |
314 (defun epa-key-list-mode () | |
315 "Major mode for `epa-list-keys'." | |
316 (kill-all-local-variables) | |
317 (buffer-disable-undo) | |
318 (setq major-mode 'epa-key-list-mode | |
319 mode-name "Keys" | |
320 truncate-lines t | |
321 buffer-read-only t) | |
322 (use-local-map epa-key-list-mode-map) | |
323 (make-local-variable 'font-lock-defaults) | |
324 (setq font-lock-defaults '(epa-font-lock-keywords t)) | |
325 ;; In XEmacs, auto-initialization of font-lock is not effective | |
326 ;; if buffer-file-name is not set. | |
327 (font-lock-set-defaults) | |
328 (make-local-variable 'epa-exit-buffer-function) | |
329 (make-local-variable 'revert-buffer-function) | |
330 (setq revert-buffer-function 'epa--key-list-revert-buffer) | |
96760
b056a93eb26f
(epa-key-list-mode): Use run-mode-hooks.
Daiki Ueno <ueno@unixuser.org>
parents:
94678
diff
changeset
|
331 (run-mode-hooks 'epa-key-list-mode-hook)) |
91647 | 332 |
333 (defun epa-key-mode () | |
334 "Major mode for a key description." | |
335 (kill-all-local-variables) | |
336 (buffer-disable-undo) | |
337 (setq major-mode 'epa-key-mode | |
338 mode-name "Key" | |
339 truncate-lines t | |
340 buffer-read-only t) | |
341 (use-local-map epa-key-mode-map) | |
342 (make-local-variable 'font-lock-defaults) | |
343 (setq font-lock-defaults '(epa-font-lock-keywords t)) | |
344 ;; In XEmacs, auto-initialization of font-lock is not effective | |
345 ;; if buffer-file-name is not set. | |
346 (font-lock-set-defaults) | |
347 (make-local-variable 'epa-exit-buffer-function) | |
96760
b056a93eb26f
(epa-key-list-mode): Use run-mode-hooks.
Daiki Ueno <ueno@unixuser.org>
parents:
94678
diff
changeset
|
348 (run-mode-hooks 'epa-key-mode-hook)) |
91647 | 349 |
350 (defun epa-info-mode () | |
351 "Major mode for `epa-info-buffer'." | |
352 (kill-all-local-variables) | |
353 (buffer-disable-undo) | |
354 (setq major-mode 'epa-info-mode | |
355 mode-name "Info" | |
356 truncate-lines t | |
357 buffer-read-only t) | |
358 (use-local-map epa-info-mode-map) | |
96760
b056a93eb26f
(epa-key-list-mode): Use run-mode-hooks.
Daiki Ueno <ueno@unixuser.org>
parents:
94678
diff
changeset
|
359 (run-mode-hooks 'epa-info-mode-hook)) |
91647 | 360 |
361 (defun epa-mark-key (&optional arg) | |
362 "Mark a key on the current line. | |
363 If ARG is non-nil, unmark the key." | |
364 (interactive "P") | |
365 (let ((inhibit-read-only t) | |
366 buffer-read-only | |
367 properties) | |
368 (beginning-of-line) | |
369 (unless (get-text-property (point) 'epa-key) | |
370 (error "No key on this line")) | |
371 (setq properties (text-properties-at (point))) | |
372 (delete-char 1) | |
373 (insert (if arg " " "*")) | |
374 (set-text-properties (1- (point)) (point) properties) | |
375 (forward-line))) | |
376 | |
377 (defun epa-unmark-key (&optional arg) | |
378 "Unmark a key on the current line. | |
379 If ARG is non-nil, mark the key." | |
380 (interactive "P") | |
381 (epa-mark-key (not arg))) | |
382 | |
383 (defun epa-exit-buffer () | |
384 "Exit the current buffer. | |
385 `epa-exit-buffer-function' is called if it is set." | |
386 (interactive) | |
387 (funcall epa-exit-buffer-function)) | |
388 | |
389 (defun epa--insert-keys (keys) | |
390 (save-excursion | |
391 (save-restriction | |
392 (narrow-to-region (point) (point)) | |
393 (let (point) | |
394 (while keys | |
395 (setq point (point)) | |
396 (insert " ") | |
397 (add-text-properties point (point) | |
398 (list 'epa-key (car keys) | |
399 'front-sticky nil | |
400 'rear-nonsticky t | |
401 'start-open t | |
402 'end-open t)) | |
403 (widget-create 'epa-key :value (car keys)) | |
404 (insert "\n") | |
91731
7efbdc83b944
EasyPG: Implement some suggestions from emacs-devel.
Michael Olson <mwolson@gnu.org>
parents:
91703
diff
changeset
|
405 (setq keys (cdr keys)))) |
91647 | 406 (add-text-properties (point-min) (point-max) |
407 (list 'epa-list-keys t | |
408 'front-sticky nil | |
409 'rear-nonsticky t | |
410 'start-open t | |
411 'end-open t))))) | |
412 | |
413 (defun epa--list-keys (name secret) | |
414 (unless (and epa-keys-buffer | |
415 (buffer-live-p epa-keys-buffer)) | |
416 (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) | |
417 (set-buffer epa-keys-buffer) | |
418 (epa-key-list-mode) | |
419 (let ((inhibit-read-only t) | |
420 buffer-read-only | |
421 (point (point-min)) | |
422 (context (epg-make-context epa-protocol))) | |
423 (unless (get-text-property point 'epa-list-keys) | |
424 (setq point (next-single-property-change point 'epa-list-keys))) | |
425 (when point | |
426 (delete-region point | |
427 (or (next-single-property-change point 'epa-list-keys) | |
428 (point-max))) | |
429 (goto-char point)) | |
430 (epa--insert-keys (epg-list-keys context name secret)) | |
431 (widget-setup) | |
432 (set-keymap-parent (current-local-map) widget-keymap)) | |
433 (make-local-variable 'epa-list-keys-arguments) | |
434 (setq epa-list-keys-arguments (list name secret)) | |
435 (goto-char (point-min)) | |
436 (pop-to-buffer (current-buffer))) | |
437 | |
438 ;;;###autoload | |
439 (defun epa-list-keys (&optional name) | |
440 "List all keys matched with NAME from the public keyring." | |
441 (interactive | |
442 (if current-prefix-arg | |
443 (let ((name (read-string "Pattern: " | |
444 (if epa-list-keys-arguments | |
445 (car epa-list-keys-arguments))))) | |
446 (list (if (equal name "") nil name))) | |
447 (list nil))) | |
448 (epa--list-keys name nil)) | |
449 | |
450 ;;;###autoload | |
451 (defun epa-list-secret-keys (&optional name) | |
452 "List all keys matched with NAME from the private keyring." | |
453 (interactive | |
454 (if current-prefix-arg | |
455 (let ((name (read-string "Pattern: " | |
456 (if epa-list-keys-arguments | |
457 (car epa-list-keys-arguments))))) | |
458 (list (if (equal name "") nil name))) | |
459 (list nil))) | |
460 (epa--list-keys name t)) | |
461 | |
462 (defun epa--key-list-revert-buffer (&optional ignore-auto noconfirm) | |
463 (apply #'epa--list-keys epa-list-keys-arguments)) | |
464 | |
465 (defun epa--marked-keys () | |
466 (or (save-excursion | |
467 (set-buffer epa-keys-buffer) | |
468 (goto-char (point-min)) | |
469 (let (keys key) | |
470 (while (re-search-forward "^\\*" nil t) | |
471 (if (setq key (get-text-property (match-beginning 0) | |
472 'epa-key)) | |
473 (setq keys (cons key keys)))) | |
474 (nreverse keys))) | |
475 (save-excursion | |
476 (beginning-of-line) | |
477 (let ((key (get-text-property (point) 'epa-key))) | |
478 (if key | |
479 (list key)))))) | |
480 | |
481 (defun epa--select-keys (prompt keys) | |
482 (save-excursion | |
483 (unless (and epa-keys-buffer | |
484 (buffer-live-p epa-keys-buffer)) | |
485 (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) | |
486 (set-buffer epa-keys-buffer) | |
487 (epa-key-list-mode) | |
488 (let ((inhibit-read-only t) | |
489 buffer-read-only) | |
490 (erase-buffer) | |
491 (insert prompt "\n" | |
492 (substitute-command-keys "\ | |
493 - `\\[epa-mark-key]' to mark a key on the line | |
494 - `\\[epa-unmark-key]' to unmark a key on the line\n")) | |
495 (widget-create 'link | |
496 :notify (lambda (&rest ignore) (abort-recursive-edit)) | |
497 :help-echo | |
498 (substitute-command-keys | |
499 "Click here or \\[abort-recursive-edit] to cancel") | |
500 "Cancel") | |
501 (widget-create 'link | |
502 :notify (lambda (&rest ignore) (exit-recursive-edit)) | |
503 :help-echo | |
504 (substitute-command-keys | |
505 "Click here or \\[exit-recursive-edit] to finish") | |
506 "OK") | |
507 (insert "\n\n") | |
508 (epa--insert-keys keys) | |
509 (widget-setup) | |
510 (set-keymap-parent (current-local-map) widget-keymap) | |
511 (setq epa-exit-buffer-function #'abort-recursive-edit) | |
512 (goto-char (point-min)) | |
513 (pop-to-buffer (current-buffer))) | |
514 (unwind-protect | |
515 (progn | |
516 (recursive-edit) | |
517 (epa--marked-keys)) | |
518 (if (get-buffer-window epa-keys-buffer) | |
519 (delete-window (get-buffer-window epa-keys-buffer))) | |
520 (kill-buffer epa-keys-buffer)))) | |
521 | |
522 ;;;###autoload | |
523 (defun epa-select-keys (context prompt &optional names secret) | |
524 "Display a user's keyring and ask him to select keys. | |
525 CONTEXT is an epg-context. | |
526 PROMPT is a string to prompt with. | |
527 NAMES is a list of strings to be matched with keys. If it is nil, all | |
528 the keys are listed. | |
529 If SECRET is non-nil, list secret keys instead of public keys." | |
530 (let ((keys (epg-list-keys context names secret))) | |
93506 | 531 (epa--select-keys prompt keys))) |
91647 | 532 |
533 (defun epa--show-key (key) | |
534 (let* ((primary-sub-key (car (epg-key-sub-key-list key))) | |
535 (entry (assoc (epg-sub-key-id primary-sub-key) | |
536 epa-key-buffer-alist)) | |
537 (inhibit-read-only t) | |
538 buffer-read-only | |
539 pointer) | |
540 (unless entry | |
541 (setq entry (cons (epg-sub-key-id primary-sub-key) nil) | |
542 epa-key-buffer-alist (cons entry epa-key-buffer-alist))) | |
543 (unless (and (cdr entry) | |
544 (buffer-live-p (cdr entry))) | |
545 (setcdr entry (generate-new-buffer | |
546 (format "*Key*%s" (epg-sub-key-id primary-sub-key))))) | |
547 (set-buffer (cdr entry)) | |
548 (epa-key-mode) | |
549 (make-local-variable 'epa-key) | |
550 (setq epa-key key) | |
551 (erase-buffer) | |
552 (setq pointer (epg-key-user-id-list key)) | |
553 (while pointer | |
554 (if (car pointer) | |
555 (insert " " | |
556 (if (epg-user-id-validity (car pointer)) | |
557 (char-to-string | |
558 (car (rassq (epg-user-id-validity (car pointer)) | |
559 epg-key-validity-alist))) | |
560 " ") | |
561 " " | |
562 (if (stringp (epg-user-id-string (car pointer))) | |
563 (epg-user-id-string (car pointer)) | |
564 (epg-decode-dn (epg-user-id-string (car pointer)))) | |
565 "\n")) | |
566 (setq pointer (cdr pointer))) | |
567 (setq pointer (epg-key-sub-key-list key)) | |
568 (while pointer | |
569 (insert " " | |
570 (if (epg-sub-key-validity (car pointer)) | |
571 (char-to-string | |
572 (car (rassq (epg-sub-key-validity (car pointer)) | |
573 epg-key-validity-alist))) | |
574 " ") | |
575 " " | |
576 (epg-sub-key-id (car pointer)) | |
577 " " | |
578 (format "%dbits" | |
579 (epg-sub-key-length (car pointer))) | |
580 " " | |
581 (cdr (assq (epg-sub-key-algorithm (car pointer)) | |
582 epg-pubkey-algorithm-alist)) | |
583 "\n\tCreated: " | |
584 (condition-case nil | |
585 (format-time-string "%Y-%m-%d" | |
586 (epg-sub-key-creation-time (car pointer))) | |
587 (error "????-??-??")) | |
588 (if (epg-sub-key-expiration-time (car pointer)) | |
589 (format "\n\tExpires: %s" | |
590 (condition-case nil | |
591 (format-time-string "%Y-%m-%d" | |
592 (epg-sub-key-expiration-time | |
593 (car pointer))) | |
594 (error "????-??-??"))) | |
595 "") | |
596 "\n\tCapabilities: " | |
597 (mapconcat #'symbol-name | |
598 (epg-sub-key-capability (car pointer)) | |
599 " ") | |
600 "\n\tFingerprint: " | |
601 (epg-sub-key-fingerprint (car pointer)) | |
602 "\n") | |
603 (setq pointer (cdr pointer))) | |
604 (goto-char (point-min)) | |
605 (pop-to-buffer (current-buffer)))) | |
606 | |
607 (defun epa-display-info (info) | |
608 (if epa-popup-info-window | |
609 (save-selected-window | |
610 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer)) | |
611 (setq epa-info-buffer (generate-new-buffer "*Info*"))) | |
612 (if (get-buffer-window epa-info-buffer) | |
613 (delete-window (get-buffer-window epa-info-buffer))) | |
614 (save-excursion | |
615 (set-buffer epa-info-buffer) | |
616 (let ((inhibit-read-only t) | |
617 buffer-read-only) | |
618 (erase-buffer) | |
619 (insert info)) | |
620 (epa-info-mode) | |
621 (goto-char (point-min))) | |
622 (if (> (window-height) | |
623 epa-info-window-height) | |
624 (set-window-buffer (split-window nil (- (window-height) | |
625 epa-info-window-height)) | |
626 epa-info-buffer) | |
627 (pop-to-buffer epa-info-buffer) | |
628 (if (> (window-height) epa-info-window-height) | |
629 (shrink-window (- (window-height) epa-info-window-height))))) | |
630 (message "%s" info))) | |
631 | |
632 (defun epa-display-verify-result (verify-result) | |
633 (epa-display-info (epg-verify-result-to-string verify-result))) | |
634 (make-obsolete 'epa-display-verify-result 'epa-display-info) | |
635 | |
636 (defun epa-passphrase-callback-function (context key-id handback) | |
637 (if (eq key-id 'SYM) | |
638 (read-passwd "Passphrase for symmetric encryption: " | |
639 (eq (epg-context-operation context) 'encrypt)) | |
640 (read-passwd | |
641 (if (eq key-id 'PIN) | |
642 "Passphrase for PIN: " | |
643 (let ((entry (assoc key-id epg-user-id-alist))) | |
644 (if entry | |
645 (format "Passphrase for %s %s: " key-id (cdr entry)) | |
646 (format "Passphrase for %s: " key-id))))))) | |
647 | |
648 (defun epa-progress-callback-function (context what char current total | |
649 handback) | |
650 (message "%s%d%% (%d/%d)" (or handback | |
651 (concat what ": ")) | |
652 (if (> total 0) (floor (* (/ current (float total)) 100)) 0) | |
653 current total)) | |
654 | |
655 ;;;###autoload | |
656 (defun epa-decrypt-file (file) | |
657 "Decrypt FILE." | |
658 (interactive "fFile: ") | |
659 (setq file (expand-file-name file)) | |
660 (let* ((default-name (file-name-sans-extension file)) | |
661 (plain (expand-file-name | |
662 (read-file-name | |
663 (concat "To file (default " | |
664 (file-name-nondirectory default-name) | |
665 ") ") | |
666 (file-name-directory default-name) | |
667 default-name))) | |
668 (context (epg-make-context epa-protocol))) | |
669 (epg-context-set-passphrase-callback context | |
670 #'epa-passphrase-callback-function) | |
671 (epg-context-set-progress-callback context | |
672 (cons | |
673 #'epa-progress-callback-function | |
674 (format "Decrypting %s..." | |
675 (file-name-nondirectory file)))) | |
676 (message "Decrypting %s..." (file-name-nondirectory file)) | |
677 (epg-decrypt-file context file plain) | |
678 (message "Decrypting %s...wrote %s" (file-name-nondirectory file) | |
679 (file-name-nondirectory plain)) | |
680 (if (epg-context-result-for context 'verify) | |
681 (epa-display-info (epg-verify-result-to-string | |
682 (epg-context-result-for context 'verify)))))) | |
683 | |
684 ;;;###autoload | |
685 (defun epa-verify-file (file) | |
686 "Verify FILE." | |
687 (interactive "fFile: ") | |
688 (setq file (expand-file-name file)) | |
689 (let* ((context (epg-make-context epa-protocol)) | |
690 (plain (if (equal (file-name-extension file) "sig") | |
691 (file-name-sans-extension file)))) | |
692 (epg-context-set-progress-callback context | |
693 (cons | |
694 #'epa-progress-callback-function | |
695 (format "Verifying %s..." | |
696 (file-name-nondirectory file)))) | |
697 (message "Verifying %s..." (file-name-nondirectory file)) | |
698 (epg-verify-file context file plain) | |
699 (message "Verifying %s...done" (file-name-nondirectory file)) | |
700 (if (epg-context-result-for context 'verify) | |
701 (epa-display-info (epg-verify-result-to-string | |
702 (epg-context-result-for context 'verify)))))) | |
703 | |
704 (defun epa--read-signature-type () | |
705 (let (type c) | |
706 (while (null type) | |
707 (message "Signature type (n,c,d,?) ") | |
708 (setq c (read-char)) | |
709 (cond ((eq c ?c) | |
710 (setq type 'clear)) | |
711 ((eq c ?d) | |
712 (setq type 'detached)) | |
713 ((eq c ??) | |
714 (with-output-to-temp-buffer "*Help*" | |
715 (save-excursion | |
716 (set-buffer standard-output) | |
717 (insert "\ | |
718 n - Create a normal signature | |
719 c - Create a cleartext signature | |
720 d - Create a detached signature | |
721 ? - Show this help | |
722 ")))) | |
723 (t | |
724 (setq type 'normal)))))) | |
725 | |
726 ;;;###autoload | |
727 (defun epa-sign-file (file signers mode) | |
728 "Sign FILE by SIGNERS keys selected." | |
729 (interactive | |
730 (let ((verbose current-prefix-arg)) | |
731 (list (expand-file-name (read-file-name "File: ")) | |
732 (if verbose | |
733 (epa-select-keys (epg-make-context epa-protocol) | |
734 "Select keys for signing. | |
735 If no one is selected, default secret key is used. " | |
736 nil t)) | |
737 (if verbose | |
738 (epa--read-signature-type) | |
739 'clear)))) | |
740 (let ((signature (concat file | |
741 (if (eq epa-protocol 'OpenPGP) | |
742 (if (or epa-armor | |
743 (not (memq mode | |
744 '(nil t normal detached)))) | |
745 ".asc" | |
746 (if (memq mode '(t detached)) | |
747 ".sig" | |
748 ".gpg")) | |
749 (if (memq mode '(t detached)) | |
750 ".p7s" | |
751 ".p7m")))) | |
752 (context (epg-make-context epa-protocol))) | |
753 (epg-context-set-armor context epa-armor) | |
754 (epg-context-set-textmode context epa-textmode) | |
755 (epg-context-set-signers context signers) | |
756 (epg-context-set-passphrase-callback context | |
757 #'epa-passphrase-callback-function) | |
758 (epg-context-set-progress-callback context | |
759 (cons | |
760 #'epa-progress-callback-function | |
761 (format "Signing %s..." | |
762 (file-name-nondirectory file)))) | |
763 (message "Signing %s..." (file-name-nondirectory file)) | |
764 (epg-sign-file context file signature mode) | |
765 (message "Signing %s...wrote %s" (file-name-nondirectory file) | |
766 (file-name-nondirectory signature)))) | |
767 | |
768 ;;;###autoload | |
769 (defun epa-encrypt-file (file recipients) | |
770 "Encrypt FILE for RECIPIENTS." | |
771 (interactive | |
772 (list (expand-file-name (read-file-name "File: ")) | |
773 (epa-select-keys (epg-make-context epa-protocol) | |
774 "Select recipients for encryption. | |
775 If no one is selected, symmetric encryption will be performed. "))) | |
776 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP) | |
777 (if epa-armor ".asc" ".gpg") | |
778 ".p7m"))) | |
779 (context (epg-make-context epa-protocol))) | |
780 (epg-context-set-armor context epa-armor) | |
781 (epg-context-set-textmode context epa-textmode) | |
782 (epg-context-set-passphrase-callback context | |
783 #'epa-passphrase-callback-function) | |
784 (epg-context-set-progress-callback context | |
785 (cons | |
786 #'epa-progress-callback-function | |
787 (format "Encrypting %s..." | |
788 (file-name-nondirectory file)))) | |
789 (message "Encrypting %s..." (file-name-nondirectory file)) | |
790 (epg-encrypt-file context file recipients cipher) | |
791 (message "Encrypting %s...wrote %s" (file-name-nondirectory file) | |
792 (file-name-nondirectory cipher)))) | |
793 | |
794 ;;;###autoload | |
795 (defun epa-decrypt-region (start end) | |
796 "Decrypt the current region between START and END. | |
797 | |
93506 | 798 Don't use this command in Lisp programs! |
799 Since this function operates on regions, it does some tricks such | |
800 as coding-system detection and unibyte/multibyte conversion. If | |
801 you are sure how the data in the region should be treated, you | |
802 should consider using the string based counterpart | |
803 `epg-decrypt-string', or the file based counterpart | |
804 `epg-decrypt-file' instead. | |
805 | |
806 For example: | |
807 | |
808 \(let ((context (epg-make-context 'OpenPGP))) | |
809 (decode-coding-string | |
810 (epg-decrypt-string context (buffer-substring start end)) | |
811 'utf-8))" | |
91647 | 812 (interactive "r") |
813 (save-excursion | |
814 (let ((context (epg-make-context epa-protocol)) | |
815 plain) | |
816 (epg-context-set-passphrase-callback context | |
817 #'epa-passphrase-callback-function) | |
818 (epg-context-set-progress-callback context | |
819 (cons | |
820 #'epa-progress-callback-function | |
821 "Decrypting...")) | |
822 (message "Decrypting...") | |
823 (setq plain (epg-decrypt-string context (buffer-substring start end))) | |
824 (message "Decrypting...done") | |
825 (setq plain (epa--decode-coding-string | |
826 plain | |
827 (or coding-system-for-read | |
828 (get-text-property start 'epa-coding-system-used)))) | |
829 (if (y-or-n-p "Replace the original text? ") | |
830 (let ((inhibit-read-only t) | |
831 buffer-read-only) | |
832 (delete-region start end) | |
833 (goto-char start) | |
834 (insert plain)) | |
835 (with-output-to-temp-buffer "*Temp*" | |
836 (set-buffer standard-output) | |
837 (insert plain) | |
838 (epa-info-mode))) | |
839 (if (epg-context-result-for context 'verify) | |
840 (epa-display-info (epg-verify-result-to-string | |
841 (epg-context-result-for context 'verify))))))) | |
842 | |
843 (defun epa--find-coding-system-for-mime-charset (mime-charset) | |
844 (if (featurep 'xemacs) | |
845 (if (fboundp 'find-coding-system) | |
846 (find-coding-system mime-charset)) | |
847 (let ((pointer (coding-system-list))) | |
848 (while (and pointer | |
849 (eq (coding-system-get (car pointer) 'mime-charset) | |
850 mime-charset)) | |
851 (setq pointer (cdr pointer))) | |
852 pointer))) | |
853 | |
854 ;;;###autoload | |
855 (defun epa-decrypt-armor-in-region (start end) | |
856 "Decrypt OpenPGP armors in the current region between START and END. | |
857 | |
93506 | 858 Don't use this command in Lisp programs! |
859 See the reason described in the `epa-decrypt-region' documentation." | |
91647 | 860 (interactive "r") |
861 (save-excursion | |
862 (save-restriction | |
863 (narrow-to-region start end) | |
864 (goto-char start) | |
865 (let (armor-start armor-end) | |
866 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t) | |
867 (setq armor-start (match-beginning 0) | |
868 armor-end (re-search-forward "^-----END PGP MESSAGE-----$" | |
869 nil t)) | |
870 (unless armor-end | |
871 (error "No armor tail")) | |
872 (goto-char armor-start) | |
873 (let ((coding-system-for-read | |
874 (or coding-system-for-read | |
875 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t) | |
876 (epa--find-coding-system-for-mime-charset | |
877 (intern (downcase (match-string 1)))))))) | |
878 (goto-char armor-end) | |
879 (epa-decrypt-region armor-start armor-end))))))) | |
880 | |
881 ;;;###autoload | |
882 (defun epa-verify-region (start end) | |
883 "Verify the current region between START and END. | |
884 | |
93506 | 885 Don't use this command in Lisp programs! |
886 Since this function operates on regions, it does some tricks such | |
887 as coding-system detection and unibyte/multibyte conversion. If | |
888 you are sure how the data in the region should be treated, you | |
889 should consider using the string based counterpart | |
890 `epg-verify-string', or the file based counterpart | |
891 `epg-verify-file' instead. | |
892 | |
893 For example: | |
894 | |
895 \(let ((context (epg-make-context 'OpenPGP))) | |
896 (decode-coding-string | |
897 (epg-verify-string context (buffer-substring start end)) | |
898 'utf-8))" | |
91647 | 899 (interactive "r") |
900 (let ((context (epg-make-context epa-protocol)) | |
901 plain) | |
902 (epg-context-set-progress-callback context | |
903 (cons | |
904 #'epa-progress-callback-function | |
905 "Verifying...")) | |
906 (message "Verifying...") | |
907 (setq plain (epg-verify-string | |
908 context | |
909 (epa--encode-coding-string | |
910 (buffer-substring start end) | |
911 (or coding-system-for-write | |
912 (get-text-property start 'epa-coding-system-used))))) | |
913 (message "Verifying...done") | |
914 (setq plain (epa--decode-coding-string | |
915 plain | |
916 (or coding-system-for-read | |
917 (get-text-property start 'epa-coding-system-used)))) | |
918 (if (y-or-n-p "Replace the original text? ") | |
919 (let ((inhibit-read-only t) | |
920 buffer-read-only) | |
921 (delete-region start end) | |
922 (goto-char start) | |
923 (insert plain)) | |
924 (with-output-to-temp-buffer "*Temp*" | |
925 (set-buffer standard-output) | |
926 (insert plain) | |
927 (epa-info-mode))) | |
928 (if (epg-context-result-for context 'verify) | |
929 (epa-display-info (epg-verify-result-to-string | |
930 (epg-context-result-for context 'verify)))))) | |
931 | |
932 ;;;###autoload | |
933 (defun epa-verify-cleartext-in-region (start end) | |
934 "Verify OpenPGP cleartext signed messages in the current region | |
935 between START and END. | |
936 | |
93506 | 937 Don't use this command in Lisp programs! |
938 See the reason described in the `epa-verify-region' documentation." | |
91647 | 939 (interactive "r") |
940 (save-excursion | |
941 (save-restriction | |
942 (narrow-to-region start end) | |
943 (goto-char start) | |
944 (let (cleartext-start cleartext-end) | |
945 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$" | |
946 nil t) | |
947 (setq cleartext-start (match-beginning 0)) | |
948 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$" | |
949 nil t) | |
950 (error "Invalid cleartext signed message")) | |
951 (setq cleartext-end (re-search-forward | |
952 "^-----END PGP SIGNATURE-----$" | |
953 nil t)) | |
954 (unless cleartext-end | |
955 (error "No cleartext tail")) | |
956 (epa-verify-region cleartext-start cleartext-end)))))) | |
957 | |
958 (eval-and-compile | |
959 (if (fboundp 'select-safe-coding-system) | |
960 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system) | |
961 (defun epa--select-safe-coding-system (from to) | |
962 buffer-file-coding-system))) | |
963 | |
964 ;;;###autoload | |
965 (defun epa-sign-region (start end signers mode) | |
966 "Sign the current region between START and END by SIGNERS keys selected. | |
967 | |
93506 | 968 Don't use this command in Lisp programs! |
969 Since this function operates on regions, it does some tricks such | |
970 as coding-system detection and unibyte/multibyte conversion. If | |
971 you are sure how the data should be treated, you should consider | |
972 using the string based counterpart `epg-sign-string', or the file | |
973 based counterpart `epg-sign-file' instead. | |
974 | |
975 For example: | |
976 | |
977 \(let ((context (epg-make-context 'OpenPGP))) | |
978 (epg-sign-string | |
979 context | |
980 (encode-coding-string (buffer-substring start end) 'utf-8)))" | |
91647 | 981 (interactive |
982 (let ((verbose current-prefix-arg)) | |
983 (setq epa-last-coding-system-specified | |
984 (or coding-system-for-write | |
985 (epa--select-safe-coding-system | |
986 (region-beginning) (region-end)))) | |
987 (list (region-beginning) (region-end) | |
988 (if verbose | |
989 (epa-select-keys (epg-make-context epa-protocol) | |
990 "Select keys for signing. | |
991 If no one is selected, default secret key is used. " | |
992 nil t)) | |
993 (if verbose | |
994 (epa--read-signature-type) | |
995 'clear)))) | |
996 (save-excursion | |
997 (let ((context (epg-make-context epa-protocol)) | |
998 signature) | |
999 ;;(epg-context-set-armor context epa-armor) | |
1000 (epg-context-set-armor context t) | |
1001 ;;(epg-context-set-textmode context epa-textmode) | |
1002 (epg-context-set-textmode context t) | |
1003 (epg-context-set-signers context signers) | |
1004 (epg-context-set-passphrase-callback context | |
1005 #'epa-passphrase-callback-function) | |
1006 (epg-context-set-progress-callback context | |
1007 (cons | |
1008 #'epa-progress-callback-function | |
1009 "Signing...")) | |
1010 (message "Signing...") | |
1011 (setq signature (epg-sign-string context | |
1012 (epa--encode-coding-string | |
1013 (buffer-substring start end) | |
1014 epa-last-coding-system-specified) | |
1015 mode)) | |
1016 (message "Signing...done") | |
1017 (delete-region start end) | |
1018 (goto-char start) | |
1019 (add-text-properties (point) | |
1020 (progn | |
1021 (insert (epa--decode-coding-string | |
1022 signature | |
1023 (or coding-system-for-read | |
1024 epa-last-coding-system-specified))) | |
1025 (point)) | |
1026 (list 'epa-coding-system-used | |
1027 epa-last-coding-system-specified | |
1028 'front-sticky nil | |
1029 'rear-nonsticky t | |
1030 'start-open t | |
1031 'end-open t))))) | |
1032 | |
1033 (eval-and-compile | |
1034 (if (fboundp 'derived-mode-p) | |
1035 (defalias 'epa--derived-mode-p 'derived-mode-p) | |
1036 (defun epa--derived-mode-p (&rest modes) | |
1037 "Non-nil if the current major mode is derived from one of MODES. | |
1038 Uses the `derived-mode-parent' property of the symbol to trace backwards." | |
1039 (let ((parent major-mode)) | |
1040 (while (and (not (memq parent modes)) | |
1041 (setq parent (get parent 'derived-mode-parent)))) | |
1042 parent)))) | |
1043 | |
1044 ;;;###autoload | |
1045 (defun epa-encrypt-region (start end recipients sign signers) | |
1046 "Encrypt the current region between START and END for RECIPIENTS. | |
1047 | |
93506 | 1048 Don't use this command in Lisp programs! |
1049 Since this function operates on regions, it does some tricks such | |
1050 as coding-system detection and unibyte/multibyte conversion. If | |
1051 you are sure how the data should be treated, you should consider | |
1052 using the string based counterpart `epg-encrypt-string', or the | |
1053 file based counterpart `epg-encrypt-file' instead. | |
1054 | |
1055 For example: | |
1056 | |
1057 \(let ((context (epg-make-context 'OpenPGP))) | |
1058 (epg-encrypt-string | |
1059 context | |
1060 (encode-coding-string (buffer-substring start end) 'utf-8) | |
1061 nil))" | |
91647 | 1062 (interactive |
1063 (let ((verbose current-prefix-arg) | |
1064 (context (epg-make-context epa-protocol)) | |
1065 sign) | |
1066 (setq epa-last-coding-system-specified | |
1067 (or coding-system-for-write | |
1068 (epa--select-safe-coding-system | |
1069 (region-beginning) (region-end)))) | |
1070 (list (region-beginning) (region-end) | |
1071 (epa-select-keys context | |
1072 "Select recipients for encryption. | |
1073 If no one is selected, symmetric encryption will be performed. ") | |
1074 (setq sign (if verbose (y-or-n-p "Sign? "))) | |
1075 (if sign | |
1076 (epa-select-keys context | |
1077 "Select keys for signing. "))))) | |
1078 (save-excursion | |
1079 (let ((context (epg-make-context epa-protocol)) | |
1080 cipher) | |
1081 ;;(epg-context-set-armor context epa-armor) | |
1082 (epg-context-set-armor context t) | |
1083 ;;(epg-context-set-textmode context epa-textmode) | |
1084 (epg-context-set-textmode context t) | |
1085 (if sign | |
1086 (epg-context-set-signers context signers)) | |
1087 (epg-context-set-passphrase-callback context | |
1088 #'epa-passphrase-callback-function) | |
1089 (epg-context-set-progress-callback context | |
1090 (cons | |
1091 #'epa-progress-callback-function | |
1092 "Encrypting...")) | |
1093 (message "Encrypting...") | |
1094 (setq cipher (epg-encrypt-string context | |
1095 (epa--encode-coding-string | |
1096 (buffer-substring start end) | |
1097 epa-last-coding-system-specified) | |
1098 recipients | |
1099 sign)) | |
1100 (message "Encrypting...done") | |
1101 (delete-region start end) | |
1102 (goto-char start) | |
1103 (add-text-properties (point) | |
1104 (progn | |
1105 (insert cipher) | |
1106 (point)) | |
1107 (list 'epa-coding-system-used | |
1108 epa-last-coding-system-specified | |
1109 'front-sticky nil | |
1110 'rear-nonsticky t | |
1111 'start-open t | |
1112 'end-open t))))) | |
1113 | |
1114 ;;;###autoload | |
1115 (defun epa-delete-keys (keys &optional allow-secret) | |
93506 | 1116 "Delete selected KEYS." |
91647 | 1117 (interactive |
1118 (let ((keys (epa--marked-keys))) | |
1119 (unless keys | |
1120 (error "No keys selected")) | |
1121 (list keys | |
1122 (eq (nth 1 epa-list-keys-arguments) t)))) | |
1123 (let ((context (epg-make-context epa-protocol))) | |
1124 (message "Deleting...") | |
1125 (epg-delete-keys context keys allow-secret) | |
1126 (message "Deleting...done") | |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
1127 (apply #'epa--list-keys epa-list-keys-arguments))) |
91647 | 1128 |
1129 ;;;###autoload | |
1130 (defun epa-import-keys (file) | |
93506 | 1131 "Import keys from FILE." |
91647 | 1132 (interactive "fFile: ") |
1133 (setq file (expand-file-name file)) | |
1134 (let ((context (epg-make-context epa-protocol))) | |
1135 (message "Importing %s..." (file-name-nondirectory file)) | |
1136 (condition-case nil | |
1137 (progn | |
1138 (epg-import-keys-from-file context file) | |
1139 (message "Importing %s...done" (file-name-nondirectory file))) | |
1140 (error | |
1141 (message "Importing %s...failed" (file-name-nondirectory file)))) | |
1142 (if (epg-context-result-for context 'import) | |
1143 (epa-display-info (epg-import-result-to-string | |
1144 (epg-context-result-for context 'import)))) | |
1145 (if (eq major-mode 'epa-key-list-mode) | |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
1146 (apply #'epa--list-keys epa-list-keys-arguments)))) |
91647 | 1147 |
1148 ;;;###autoload | |
1149 (defun epa-import-keys-region (start end) | |
93506 | 1150 "Import keys from the region." |
91647 | 1151 (interactive "r") |
1152 (let ((context (epg-make-context epa-protocol))) | |
1153 (message "Importing...") | |
1154 (condition-case nil | |
1155 (progn | |
1156 (epg-import-keys-from-string context (buffer-substring start end)) | |
1157 (message "Importing...done")) | |
1158 (error | |
1159 (message "Importing...failed"))) | |
1160 (if (epg-context-result-for context 'import) | |
1161 (epa-display-info (epg-import-result-to-string | |
1162 (epg-context-result-for context 'import)))))) | |
1163 | |
1164 ;;;###autoload | |
1165 (defun epa-import-armor-in-region (start end) | |
1166 "Import keys in the OpenPGP armor format in the current region | |
93506 | 1167 between START and END." |
91647 | 1168 (interactive "r") |
1169 (save-excursion | |
1170 (save-restriction | |
1171 (narrow-to-region start end) | |
1172 (goto-char start) | |
1173 (let (armor-start armor-end) | |
1174 (while (re-search-forward | |
1175 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$" | |
1176 nil t) | |
1177 (setq armor-start (match-beginning 0) | |
1178 armor-end (re-search-forward | |
1179 (concat "^-----END " (match-string 1) "-----$") | |
1180 nil t)) | |
1181 (unless armor-end | |
1182 (error "No armor tail")) | |
1183 (epa-import-keys-region armor-start armor-end)))))) | |
1184 | |
1185 ;;;###autoload | |
1186 (defun epa-export-keys (keys file) | |
93506 | 1187 "Export selected KEYS to FILE." |
91647 | 1188 (interactive |
1189 (let ((keys (epa--marked-keys)) | |
1190 default-name) | |
1191 (unless keys | |
1192 (error "No keys selected")) | |
1193 (setq default-name | |
1194 (expand-file-name | |
1195 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys)))) | |
1196 (if epa-armor ".asc" ".gpg")) | |
1197 default-directory)) | |
1198 (list keys | |
1199 (expand-file-name | |
1200 (read-file-name | |
1201 (concat "To file (default " | |
1202 (file-name-nondirectory default-name) | |
1203 ") ") | |
1204 (file-name-directory default-name) | |
1205 default-name))))) | |
1206 (let ((context (epg-make-context epa-protocol))) | |
1207 (epg-context-set-armor context epa-armor) | |
1208 (message "Exporting to %s..." (file-name-nondirectory file)) | |
1209 (epg-export-keys-to-file context keys file) | |
1210 (message "Exporting to %s...done" (file-name-nondirectory file)))) | |
1211 | |
1212 ;;;###autoload | |
1213 (defun epa-insert-keys (keys) | |
93506 | 1214 "Insert selected KEYS after the point." |
91647 | 1215 (interactive |
1216 (list (epa-select-keys (epg-make-context epa-protocol) | |
1217 "Select keys to export. "))) | |
1218 (let ((context (epg-make-context epa-protocol))) | |
1219 ;;(epg-context-set-armor context epa-armor) | |
1220 (epg-context-set-armor context t) | |
1221 (insert (epg-export-keys-to-string context keys)))) | |
1222 | |
1223 ;; (defun epa-sign-keys (keys &optional local) | |
1224 ;; "Sign selected KEYS. | |
1225 ;; If a prefix-arg is specified, the signature is marked as non exportable. | |
1226 | |
1227 ;; Don't use this command in Lisp programs!" | |
1228 ;; (interactive | |
1229 ;; (let ((keys (epa--marked-keys))) | |
1230 ;; (unless keys | |
1231 ;; (error "No keys selected")) | |
1232 ;; (list keys current-prefix-arg))) | |
1233 ;; (let ((context (epg-make-context epa-protocol))) | |
1234 ;; (epg-context-set-passphrase-callback context | |
1235 ;; #'epa-passphrase-callback-function) | |
1236 ;; (epg-context-set-progress-callback context | |
1237 ;; (cons | |
1238 ;; #'epa-progress-callback-function | |
1239 ;; "Signing keys...")) | |
1240 ;; (message "Signing keys...") | |
1241 ;; (epg-sign-keys context keys local) | |
1242 ;; (message "Signing keys...done"))) | |
1243 ;; (make-obsolete 'epa-sign-keys "Do not use.") | |
1244 | |
1245 (provide 'epa) | |
1246 | |
91687 | 1247 ;; arch-tag: 38d20ced-20d5-4137-b17a-f206335423d7 |
91647 | 1248 ;;; epa.el ends here |