Mercurial > emacs
annotate lisp/epa.el @ 97554:fe22268df13f mh-e-8_1
Update for MH-E release 8.1.
author | Bill Wohler <wohler@newt.com> |
---|---|
date | Tue, 19 Aug 2008 04:42:07 +0000 |
parents | b056a93eb26f |
children | 73388588c9b4 |
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) | |
272 (epa--show-key (widget-get widget :value))) | |
273 | |
274 (defun epa--key-widget-value-create (widget) | |
275 (let* ((key (widget-get widget :value)) | |
276 (primary-sub-key (car (epg-key-sub-key-list key))) | |
277 (primary-user-id (car (epg-key-user-id-list key)))) | |
278 (insert (format "%c " | |
279 (if (epg-sub-key-validity primary-sub-key) | |
280 (car (rassq (epg-sub-key-validity primary-sub-key) | |
281 epg-key-validity-alist)) | |
282 ? )) | |
283 (epg-sub-key-id primary-sub-key) | |
284 " " | |
285 (if primary-user-id | |
286 (if (stringp (epg-user-id-string primary-user-id)) | |
287 (epg-user-id-string primary-user-id) | |
288 (epg-decode-dn (epg-user-id-string primary-user-id))) | |
289 "")))) | |
290 | |
291 (defun epa--key-widget-button-face-get (widget) | |
292 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list | |
293 (widget-get widget :value)))))) | |
294 (if validity | |
295 (cdr (assq validity epa-validity-face-alist)) | |
296 'default))) | |
297 | |
298 (defun epa--key-widget-help-echo (widget) | |
299 (format "Show %s" | |
300 (epg-sub-key-id (car (epg-key-sub-key-list | |
301 (widget-get widget :value)))))) | |
302 | |
303 (eval-and-compile | |
304 (if (fboundp 'encode-coding-string) | |
305 (defalias 'epa--encode-coding-string 'encode-coding-string) | |
306 (defalias 'epa--encode-coding-string 'identity))) | |
307 | |
308 (eval-and-compile | |
309 (if (fboundp 'decode-coding-string) | |
310 (defalias 'epa--decode-coding-string 'decode-coding-string) | |
311 (defalias 'epa--decode-coding-string 'identity))) | |
312 | |
313 (defun epa-key-list-mode () | |
314 "Major mode for `epa-list-keys'." | |
315 (kill-all-local-variables) | |
316 (buffer-disable-undo) | |
317 (setq major-mode 'epa-key-list-mode | |
318 mode-name "Keys" | |
319 truncate-lines t | |
320 buffer-read-only t) | |
321 (use-local-map epa-key-list-mode-map) | |
322 (make-local-variable 'font-lock-defaults) | |
323 (setq font-lock-defaults '(epa-font-lock-keywords t)) | |
324 ;; In XEmacs, auto-initialization of font-lock is not effective | |
325 ;; if buffer-file-name is not set. | |
326 (font-lock-set-defaults) | |
327 (make-local-variable 'epa-exit-buffer-function) | |
328 (make-local-variable 'revert-buffer-function) | |
329 (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
|
330 (run-mode-hooks 'epa-key-list-mode-hook)) |
91647 | 331 |
332 (defun epa-key-mode () | |
333 "Major mode for a key description." | |
334 (kill-all-local-variables) | |
335 (buffer-disable-undo) | |
336 (setq major-mode 'epa-key-mode | |
337 mode-name "Key" | |
338 truncate-lines t | |
339 buffer-read-only t) | |
340 (use-local-map epa-key-mode-map) | |
341 (make-local-variable 'font-lock-defaults) | |
342 (setq font-lock-defaults '(epa-font-lock-keywords t)) | |
343 ;; In XEmacs, auto-initialization of font-lock is not effective | |
344 ;; if buffer-file-name is not set. | |
345 (font-lock-set-defaults) | |
346 (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
|
347 (run-mode-hooks 'epa-key-mode-hook)) |
91647 | 348 |
349 (defun epa-info-mode () | |
350 "Major mode for `epa-info-buffer'." | |
351 (kill-all-local-variables) | |
352 (buffer-disable-undo) | |
353 (setq major-mode 'epa-info-mode | |
354 mode-name "Info" | |
355 truncate-lines t | |
356 buffer-read-only t) | |
357 (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
|
358 (run-mode-hooks 'epa-info-mode-hook)) |
91647 | 359 |
360 (defun epa-mark-key (&optional arg) | |
361 "Mark a key on the current line. | |
362 If ARG is non-nil, unmark the key." | |
363 (interactive "P") | |
364 (let ((inhibit-read-only t) | |
365 buffer-read-only | |
366 properties) | |
367 (beginning-of-line) | |
368 (unless (get-text-property (point) 'epa-key) | |
369 (error "No key on this line")) | |
370 (setq properties (text-properties-at (point))) | |
371 (delete-char 1) | |
372 (insert (if arg " " "*")) | |
373 (set-text-properties (1- (point)) (point) properties) | |
374 (forward-line))) | |
375 | |
376 (defun epa-unmark-key (&optional arg) | |
377 "Unmark a key on the current line. | |
378 If ARG is non-nil, mark the key." | |
379 (interactive "P") | |
380 (epa-mark-key (not arg))) | |
381 | |
382 (defun epa-exit-buffer () | |
383 "Exit the current buffer. | |
384 `epa-exit-buffer-function' is called if it is set." | |
385 (interactive) | |
386 (funcall epa-exit-buffer-function)) | |
387 | |
388 (defun epa--insert-keys (keys) | |
389 (save-excursion | |
390 (save-restriction | |
391 (narrow-to-region (point) (point)) | |
392 (let (point) | |
393 (while keys | |
394 (setq point (point)) | |
395 (insert " ") | |
396 (add-text-properties point (point) | |
397 (list 'epa-key (car keys) | |
398 'front-sticky nil | |
399 'rear-nonsticky t | |
400 'start-open t | |
401 'end-open t)) | |
402 (widget-create 'epa-key :value (car keys)) | |
403 (insert "\n") | |
91731
7efbdc83b944
EasyPG: Implement some suggestions from emacs-devel.
Michael Olson <mwolson@gnu.org>
parents:
91703
diff
changeset
|
404 (setq keys (cdr keys)))) |
91647 | 405 (add-text-properties (point-min) (point-max) |
406 (list 'epa-list-keys t | |
407 'front-sticky nil | |
408 'rear-nonsticky t | |
409 'start-open t | |
410 'end-open t))))) | |
411 | |
412 (defun epa--list-keys (name secret) | |
413 (unless (and epa-keys-buffer | |
414 (buffer-live-p epa-keys-buffer)) | |
415 (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) | |
416 (set-buffer epa-keys-buffer) | |
417 (epa-key-list-mode) | |
418 (let ((inhibit-read-only t) | |
419 buffer-read-only | |
420 (point (point-min)) | |
421 (context (epg-make-context epa-protocol))) | |
422 (unless (get-text-property point 'epa-list-keys) | |
423 (setq point (next-single-property-change point 'epa-list-keys))) | |
424 (when point | |
425 (delete-region point | |
426 (or (next-single-property-change point 'epa-list-keys) | |
427 (point-max))) | |
428 (goto-char point)) | |
429 (epa--insert-keys (epg-list-keys context name secret)) | |
430 (widget-setup) | |
431 (set-keymap-parent (current-local-map) widget-keymap)) | |
432 (make-local-variable 'epa-list-keys-arguments) | |
433 (setq epa-list-keys-arguments (list name secret)) | |
434 (goto-char (point-min)) | |
435 (pop-to-buffer (current-buffer))) | |
436 | |
437 ;;;###autoload | |
438 (defun epa-list-keys (&optional name) | |
439 "List all keys matched with NAME from the public keyring." | |
440 (interactive | |
441 (if current-prefix-arg | |
442 (let ((name (read-string "Pattern: " | |
443 (if epa-list-keys-arguments | |
444 (car epa-list-keys-arguments))))) | |
445 (list (if (equal name "") nil name))) | |
446 (list nil))) | |
447 (epa--list-keys name nil)) | |
448 | |
449 ;;;###autoload | |
450 (defun epa-list-secret-keys (&optional name) | |
451 "List all keys matched with NAME from the private keyring." | |
452 (interactive | |
453 (if current-prefix-arg | |
454 (let ((name (read-string "Pattern: " | |
455 (if epa-list-keys-arguments | |
456 (car epa-list-keys-arguments))))) | |
457 (list (if (equal name "") nil name))) | |
458 (list nil))) | |
459 (epa--list-keys name t)) | |
460 | |
461 (defun epa--key-list-revert-buffer (&optional ignore-auto noconfirm) | |
462 (apply #'epa--list-keys epa-list-keys-arguments)) | |
463 | |
464 (defun epa--marked-keys () | |
465 (or (save-excursion | |
466 (set-buffer epa-keys-buffer) | |
467 (goto-char (point-min)) | |
468 (let (keys key) | |
469 (while (re-search-forward "^\\*" nil t) | |
470 (if (setq key (get-text-property (match-beginning 0) | |
471 'epa-key)) | |
472 (setq keys (cons key keys)))) | |
473 (nreverse keys))) | |
474 (save-excursion | |
475 (beginning-of-line) | |
476 (let ((key (get-text-property (point) 'epa-key))) | |
477 (if key | |
478 (list key)))))) | |
479 | |
480 (defun epa--select-keys (prompt keys) | |
481 (save-excursion | |
482 (unless (and epa-keys-buffer | |
483 (buffer-live-p epa-keys-buffer)) | |
484 (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) | |
485 (set-buffer epa-keys-buffer) | |
486 (epa-key-list-mode) | |
487 (let ((inhibit-read-only t) | |
488 buffer-read-only) | |
489 (erase-buffer) | |
490 (insert prompt "\n" | |
491 (substitute-command-keys "\ | |
492 - `\\[epa-mark-key]' to mark a key on the line | |
493 - `\\[epa-unmark-key]' to unmark a key on the line\n")) | |
494 (widget-create 'link | |
495 :notify (lambda (&rest ignore) (abort-recursive-edit)) | |
496 :help-echo | |
497 (substitute-command-keys | |
498 "Click here or \\[abort-recursive-edit] to cancel") | |
499 "Cancel") | |
500 (widget-create 'link | |
501 :notify (lambda (&rest ignore) (exit-recursive-edit)) | |
502 :help-echo | |
503 (substitute-command-keys | |
504 "Click here or \\[exit-recursive-edit] to finish") | |
505 "OK") | |
506 (insert "\n\n") | |
507 (epa--insert-keys keys) | |
508 (widget-setup) | |
509 (set-keymap-parent (current-local-map) widget-keymap) | |
510 (setq epa-exit-buffer-function #'abort-recursive-edit) | |
511 (goto-char (point-min)) | |
512 (pop-to-buffer (current-buffer))) | |
513 (unwind-protect | |
514 (progn | |
515 (recursive-edit) | |
516 (epa--marked-keys)) | |
517 (if (get-buffer-window epa-keys-buffer) | |
518 (delete-window (get-buffer-window epa-keys-buffer))) | |
519 (kill-buffer epa-keys-buffer)))) | |
520 | |
521 ;;;###autoload | |
522 (defun epa-select-keys (context prompt &optional names secret) | |
523 "Display a user's keyring and ask him to select keys. | |
524 CONTEXT is an epg-context. | |
525 PROMPT is a string to prompt with. | |
526 NAMES is a list of strings to be matched with keys. If it is nil, all | |
527 the keys are listed. | |
528 If SECRET is non-nil, list secret keys instead of public keys." | |
529 (let ((keys (epg-list-keys context names secret))) | |
93506 | 530 (epa--select-keys prompt keys))) |
91647 | 531 |
532 (defun epa--show-key (key) | |
533 (let* ((primary-sub-key (car (epg-key-sub-key-list key))) | |
534 (entry (assoc (epg-sub-key-id primary-sub-key) | |
535 epa-key-buffer-alist)) | |
536 (inhibit-read-only t) | |
537 buffer-read-only | |
538 pointer) | |
539 (unless entry | |
540 (setq entry (cons (epg-sub-key-id primary-sub-key) nil) | |
541 epa-key-buffer-alist (cons entry epa-key-buffer-alist))) | |
542 (unless (and (cdr entry) | |
543 (buffer-live-p (cdr entry))) | |
544 (setcdr entry (generate-new-buffer | |
545 (format "*Key*%s" (epg-sub-key-id primary-sub-key))))) | |
546 (set-buffer (cdr entry)) | |
547 (epa-key-mode) | |
548 (make-local-variable 'epa-key) | |
549 (setq epa-key key) | |
550 (erase-buffer) | |
551 (setq pointer (epg-key-user-id-list key)) | |
552 (while pointer | |
553 (if (car pointer) | |
554 (insert " " | |
555 (if (epg-user-id-validity (car pointer)) | |
556 (char-to-string | |
557 (car (rassq (epg-user-id-validity (car pointer)) | |
558 epg-key-validity-alist))) | |
559 " ") | |
560 " " | |
561 (if (stringp (epg-user-id-string (car pointer))) | |
562 (epg-user-id-string (car pointer)) | |
563 (epg-decode-dn (epg-user-id-string (car pointer)))) | |
564 "\n")) | |
565 (setq pointer (cdr pointer))) | |
566 (setq pointer (epg-key-sub-key-list key)) | |
567 (while pointer | |
568 (insert " " | |
569 (if (epg-sub-key-validity (car pointer)) | |
570 (char-to-string | |
571 (car (rassq (epg-sub-key-validity (car pointer)) | |
572 epg-key-validity-alist))) | |
573 " ") | |
574 " " | |
575 (epg-sub-key-id (car pointer)) | |
576 " " | |
577 (format "%dbits" | |
578 (epg-sub-key-length (car pointer))) | |
579 " " | |
580 (cdr (assq (epg-sub-key-algorithm (car pointer)) | |
581 epg-pubkey-algorithm-alist)) | |
582 "\n\tCreated: " | |
583 (condition-case nil | |
584 (format-time-string "%Y-%m-%d" | |
585 (epg-sub-key-creation-time (car pointer))) | |
586 (error "????-??-??")) | |
587 (if (epg-sub-key-expiration-time (car pointer)) | |
588 (format "\n\tExpires: %s" | |
589 (condition-case nil | |
590 (format-time-string "%Y-%m-%d" | |
591 (epg-sub-key-expiration-time | |
592 (car pointer))) | |
593 (error "????-??-??"))) | |
594 "") | |
595 "\n\tCapabilities: " | |
596 (mapconcat #'symbol-name | |
597 (epg-sub-key-capability (car pointer)) | |
598 " ") | |
599 "\n\tFingerprint: " | |
600 (epg-sub-key-fingerprint (car pointer)) | |
601 "\n") | |
602 (setq pointer (cdr pointer))) | |
603 (goto-char (point-min)) | |
604 (pop-to-buffer (current-buffer)))) | |
605 | |
606 (defun epa-display-info (info) | |
607 (if epa-popup-info-window | |
608 (save-selected-window | |
609 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer)) | |
610 (setq epa-info-buffer (generate-new-buffer "*Info*"))) | |
611 (if (get-buffer-window epa-info-buffer) | |
612 (delete-window (get-buffer-window epa-info-buffer))) | |
613 (save-excursion | |
614 (set-buffer epa-info-buffer) | |
615 (let ((inhibit-read-only t) | |
616 buffer-read-only) | |
617 (erase-buffer) | |
618 (insert info)) | |
619 (epa-info-mode) | |
620 (goto-char (point-min))) | |
621 (if (> (window-height) | |
622 epa-info-window-height) | |
623 (set-window-buffer (split-window nil (- (window-height) | |
624 epa-info-window-height)) | |
625 epa-info-buffer) | |
626 (pop-to-buffer epa-info-buffer) | |
627 (if (> (window-height) epa-info-window-height) | |
628 (shrink-window (- (window-height) epa-info-window-height))))) | |
629 (message "%s" info))) | |
630 | |
631 (defun epa-display-verify-result (verify-result) | |
632 (epa-display-info (epg-verify-result-to-string verify-result))) | |
633 (make-obsolete 'epa-display-verify-result 'epa-display-info) | |
634 | |
635 (defun epa-passphrase-callback-function (context key-id handback) | |
636 (if (eq key-id 'SYM) | |
637 (read-passwd "Passphrase for symmetric encryption: " | |
638 (eq (epg-context-operation context) 'encrypt)) | |
639 (read-passwd | |
640 (if (eq key-id 'PIN) | |
641 "Passphrase for PIN: " | |
642 (let ((entry (assoc key-id epg-user-id-alist))) | |
643 (if entry | |
644 (format "Passphrase for %s %s: " key-id (cdr entry)) | |
645 (format "Passphrase for %s: " key-id))))))) | |
646 | |
647 (defun epa-progress-callback-function (context what char current total | |
648 handback) | |
649 (message "%s%d%% (%d/%d)" (or handback | |
650 (concat what ": ")) | |
651 (if (> total 0) (floor (* (/ current (float total)) 100)) 0) | |
652 current total)) | |
653 | |
654 ;;;###autoload | |
655 (defun epa-decrypt-file (file) | |
656 "Decrypt FILE." | |
657 (interactive "fFile: ") | |
658 (setq file (expand-file-name file)) | |
659 (let* ((default-name (file-name-sans-extension file)) | |
660 (plain (expand-file-name | |
661 (read-file-name | |
662 (concat "To file (default " | |
663 (file-name-nondirectory default-name) | |
664 ") ") | |
665 (file-name-directory default-name) | |
666 default-name))) | |
667 (context (epg-make-context epa-protocol))) | |
668 (epg-context-set-passphrase-callback context | |
669 #'epa-passphrase-callback-function) | |
670 (epg-context-set-progress-callback context | |
671 (cons | |
672 #'epa-progress-callback-function | |
673 (format "Decrypting %s..." | |
674 (file-name-nondirectory file)))) | |
675 (message "Decrypting %s..." (file-name-nondirectory file)) | |
676 (epg-decrypt-file context file plain) | |
677 (message "Decrypting %s...wrote %s" (file-name-nondirectory file) | |
678 (file-name-nondirectory plain)) | |
679 (if (epg-context-result-for context 'verify) | |
680 (epa-display-info (epg-verify-result-to-string | |
681 (epg-context-result-for context 'verify)))))) | |
682 | |
683 ;;;###autoload | |
684 (defun epa-verify-file (file) | |
685 "Verify FILE." | |
686 (interactive "fFile: ") | |
687 (setq file (expand-file-name file)) | |
688 (let* ((context (epg-make-context epa-protocol)) | |
689 (plain (if (equal (file-name-extension file) "sig") | |
690 (file-name-sans-extension file)))) | |
691 (epg-context-set-progress-callback context | |
692 (cons | |
693 #'epa-progress-callback-function | |
694 (format "Verifying %s..." | |
695 (file-name-nondirectory file)))) | |
696 (message "Verifying %s..." (file-name-nondirectory file)) | |
697 (epg-verify-file context file plain) | |
698 (message "Verifying %s...done" (file-name-nondirectory file)) | |
699 (if (epg-context-result-for context 'verify) | |
700 (epa-display-info (epg-verify-result-to-string | |
701 (epg-context-result-for context 'verify)))))) | |
702 | |
703 (defun epa--read-signature-type () | |
704 (let (type c) | |
705 (while (null type) | |
706 (message "Signature type (n,c,d,?) ") | |
707 (setq c (read-char)) | |
708 (cond ((eq c ?c) | |
709 (setq type 'clear)) | |
710 ((eq c ?d) | |
711 (setq type 'detached)) | |
712 ((eq c ??) | |
713 (with-output-to-temp-buffer "*Help*" | |
714 (save-excursion | |
715 (set-buffer standard-output) | |
716 (insert "\ | |
717 n - Create a normal signature | |
718 c - Create a cleartext signature | |
719 d - Create a detached signature | |
720 ? - Show this help | |
721 ")))) | |
722 (t | |
723 (setq type 'normal)))))) | |
724 | |
725 ;;;###autoload | |
726 (defun epa-sign-file (file signers mode) | |
727 "Sign FILE by SIGNERS keys selected." | |
728 (interactive | |
729 (let ((verbose current-prefix-arg)) | |
730 (list (expand-file-name (read-file-name "File: ")) | |
731 (if verbose | |
732 (epa-select-keys (epg-make-context epa-protocol) | |
733 "Select keys for signing. | |
734 If no one is selected, default secret key is used. " | |
735 nil t)) | |
736 (if verbose | |
737 (epa--read-signature-type) | |
738 'clear)))) | |
739 (let ((signature (concat file | |
740 (if (eq epa-protocol 'OpenPGP) | |
741 (if (or epa-armor | |
742 (not (memq mode | |
743 '(nil t normal detached)))) | |
744 ".asc" | |
745 (if (memq mode '(t detached)) | |
746 ".sig" | |
747 ".gpg")) | |
748 (if (memq mode '(t detached)) | |
749 ".p7s" | |
750 ".p7m")))) | |
751 (context (epg-make-context epa-protocol))) | |
752 (epg-context-set-armor context epa-armor) | |
753 (epg-context-set-textmode context epa-textmode) | |
754 (epg-context-set-signers context signers) | |
755 (epg-context-set-passphrase-callback context | |
756 #'epa-passphrase-callback-function) | |
757 (epg-context-set-progress-callback context | |
758 (cons | |
759 #'epa-progress-callback-function | |
760 (format "Signing %s..." | |
761 (file-name-nondirectory file)))) | |
762 (message "Signing %s..." (file-name-nondirectory file)) | |
763 (epg-sign-file context file signature mode) | |
764 (message "Signing %s...wrote %s" (file-name-nondirectory file) | |
765 (file-name-nondirectory signature)))) | |
766 | |
767 ;;;###autoload | |
768 (defun epa-encrypt-file (file recipients) | |
769 "Encrypt FILE for RECIPIENTS." | |
770 (interactive | |
771 (list (expand-file-name (read-file-name "File: ")) | |
772 (epa-select-keys (epg-make-context epa-protocol) | |
773 "Select recipients for encryption. | |
774 If no one is selected, symmetric encryption will be performed. "))) | |
775 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP) | |
776 (if epa-armor ".asc" ".gpg") | |
777 ".p7m"))) | |
778 (context (epg-make-context epa-protocol))) | |
779 (epg-context-set-armor context epa-armor) | |
780 (epg-context-set-textmode context epa-textmode) | |
781 (epg-context-set-passphrase-callback context | |
782 #'epa-passphrase-callback-function) | |
783 (epg-context-set-progress-callback context | |
784 (cons | |
785 #'epa-progress-callback-function | |
786 (format "Encrypting %s..." | |
787 (file-name-nondirectory file)))) | |
788 (message "Encrypting %s..." (file-name-nondirectory file)) | |
789 (epg-encrypt-file context file recipients cipher) | |
790 (message "Encrypting %s...wrote %s" (file-name-nondirectory file) | |
791 (file-name-nondirectory cipher)))) | |
792 | |
793 ;;;###autoload | |
794 (defun epa-decrypt-region (start end) | |
795 "Decrypt the current region between START and END. | |
796 | |
93506 | 797 Don't use this command in Lisp programs! |
798 Since this function operates on regions, it does some tricks such | |
799 as coding-system detection and unibyte/multibyte conversion. If | |
800 you are sure how the data in the region should be treated, you | |
801 should consider using the string based counterpart | |
802 `epg-decrypt-string', or the file based counterpart | |
803 `epg-decrypt-file' instead. | |
804 | |
805 For example: | |
806 | |
807 \(let ((context (epg-make-context 'OpenPGP))) | |
808 (decode-coding-string | |
809 (epg-decrypt-string context (buffer-substring start end)) | |
810 'utf-8))" | |
91647 | 811 (interactive "r") |
812 (save-excursion | |
813 (let ((context (epg-make-context epa-protocol)) | |
814 plain) | |
815 (epg-context-set-passphrase-callback context | |
816 #'epa-passphrase-callback-function) | |
817 (epg-context-set-progress-callback context | |
818 (cons | |
819 #'epa-progress-callback-function | |
820 "Decrypting...")) | |
821 (message "Decrypting...") | |
822 (setq plain (epg-decrypt-string context (buffer-substring start end))) | |
823 (message "Decrypting...done") | |
824 (setq plain (epa--decode-coding-string | |
825 plain | |
826 (or coding-system-for-read | |
827 (get-text-property start 'epa-coding-system-used)))) | |
828 (if (y-or-n-p "Replace the original text? ") | |
829 (let ((inhibit-read-only t) | |
830 buffer-read-only) | |
831 (delete-region start end) | |
832 (goto-char start) | |
833 (insert plain)) | |
834 (with-output-to-temp-buffer "*Temp*" | |
835 (set-buffer standard-output) | |
836 (insert plain) | |
837 (epa-info-mode))) | |
838 (if (epg-context-result-for context 'verify) | |
839 (epa-display-info (epg-verify-result-to-string | |
840 (epg-context-result-for context 'verify))))))) | |
841 | |
842 (defun epa--find-coding-system-for-mime-charset (mime-charset) | |
843 (if (featurep 'xemacs) | |
844 (if (fboundp 'find-coding-system) | |
845 (find-coding-system mime-charset)) | |
846 (let ((pointer (coding-system-list))) | |
847 (while (and pointer | |
848 (eq (coding-system-get (car pointer) 'mime-charset) | |
849 mime-charset)) | |
850 (setq pointer (cdr pointer))) | |
851 pointer))) | |
852 | |
853 ;;;###autoload | |
854 (defun epa-decrypt-armor-in-region (start end) | |
855 "Decrypt OpenPGP armors in the current region between START and END. | |
856 | |
93506 | 857 Don't use this command in Lisp programs! |
858 See the reason described in the `epa-decrypt-region' documentation." | |
91647 | 859 (interactive "r") |
860 (save-excursion | |
861 (save-restriction | |
862 (narrow-to-region start end) | |
863 (goto-char start) | |
864 (let (armor-start armor-end) | |
865 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t) | |
866 (setq armor-start (match-beginning 0) | |
867 armor-end (re-search-forward "^-----END PGP MESSAGE-----$" | |
868 nil t)) | |
869 (unless armor-end | |
870 (error "No armor tail")) | |
871 (goto-char armor-start) | |
872 (let ((coding-system-for-read | |
873 (or coding-system-for-read | |
874 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t) | |
875 (epa--find-coding-system-for-mime-charset | |
876 (intern (downcase (match-string 1)))))))) | |
877 (goto-char armor-end) | |
878 (epa-decrypt-region armor-start armor-end))))))) | |
879 | |
880 ;;;###autoload | |
881 (defun epa-verify-region (start end) | |
882 "Verify the current region between START and END. | |
883 | |
93506 | 884 Don't use this command in Lisp programs! |
885 Since this function operates on regions, it does some tricks such | |
886 as coding-system detection and unibyte/multibyte conversion. If | |
887 you are sure how the data in the region should be treated, you | |
888 should consider using the string based counterpart | |
889 `epg-verify-string', or the file based counterpart | |
890 `epg-verify-file' instead. | |
891 | |
892 For example: | |
893 | |
894 \(let ((context (epg-make-context 'OpenPGP))) | |
895 (decode-coding-string | |
896 (epg-verify-string context (buffer-substring start end)) | |
897 'utf-8))" | |
91647 | 898 (interactive "r") |
899 (let ((context (epg-make-context epa-protocol)) | |
900 plain) | |
901 (epg-context-set-progress-callback context | |
902 (cons | |
903 #'epa-progress-callback-function | |
904 "Verifying...")) | |
905 (message "Verifying...") | |
906 (setq plain (epg-verify-string | |
907 context | |
908 (epa--encode-coding-string | |
909 (buffer-substring start end) | |
910 (or coding-system-for-write | |
911 (get-text-property start 'epa-coding-system-used))))) | |
912 (message "Verifying...done") | |
913 (setq plain (epa--decode-coding-string | |
914 plain | |
915 (or coding-system-for-read | |
916 (get-text-property start 'epa-coding-system-used)))) | |
917 (if (y-or-n-p "Replace the original text? ") | |
918 (let ((inhibit-read-only t) | |
919 buffer-read-only) | |
920 (delete-region start end) | |
921 (goto-char start) | |
922 (insert plain)) | |
923 (with-output-to-temp-buffer "*Temp*" | |
924 (set-buffer standard-output) | |
925 (insert plain) | |
926 (epa-info-mode))) | |
927 (if (epg-context-result-for context 'verify) | |
928 (epa-display-info (epg-verify-result-to-string | |
929 (epg-context-result-for context 'verify)))))) | |
930 | |
931 ;;;###autoload | |
932 (defun epa-verify-cleartext-in-region (start end) | |
933 "Verify OpenPGP cleartext signed messages in the current region | |
934 between START and END. | |
935 | |
93506 | 936 Don't use this command in Lisp programs! |
937 See the reason described in the `epa-verify-region' documentation." | |
91647 | 938 (interactive "r") |
939 (save-excursion | |
940 (save-restriction | |
941 (narrow-to-region start end) | |
942 (goto-char start) | |
943 (let (cleartext-start cleartext-end) | |
944 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$" | |
945 nil t) | |
946 (setq cleartext-start (match-beginning 0)) | |
947 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$" | |
948 nil t) | |
949 (error "Invalid cleartext signed message")) | |
950 (setq cleartext-end (re-search-forward | |
951 "^-----END PGP SIGNATURE-----$" | |
952 nil t)) | |
953 (unless cleartext-end | |
954 (error "No cleartext tail")) | |
955 (epa-verify-region cleartext-start cleartext-end)))))) | |
956 | |
957 (eval-and-compile | |
958 (if (fboundp 'select-safe-coding-system) | |
959 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system) | |
960 (defun epa--select-safe-coding-system (from to) | |
961 buffer-file-coding-system))) | |
962 | |
963 ;;;###autoload | |
964 (defun epa-sign-region (start end signers mode) | |
965 "Sign the current region between START and END by SIGNERS keys selected. | |
966 | |
93506 | 967 Don't use this command in Lisp programs! |
968 Since this function operates on regions, it does some tricks such | |
969 as coding-system detection and unibyte/multibyte conversion. If | |
970 you are sure how the data should be treated, you should consider | |
971 using the string based counterpart `epg-sign-string', or the file | |
972 based counterpart `epg-sign-file' instead. | |
973 | |
974 For example: | |
975 | |
976 \(let ((context (epg-make-context 'OpenPGP))) | |
977 (epg-sign-string | |
978 context | |
979 (encode-coding-string (buffer-substring start end) 'utf-8)))" | |
91647 | 980 (interactive |
981 (let ((verbose current-prefix-arg)) | |
982 (setq epa-last-coding-system-specified | |
983 (or coding-system-for-write | |
984 (epa--select-safe-coding-system | |
985 (region-beginning) (region-end)))) | |
986 (list (region-beginning) (region-end) | |
987 (if verbose | |
988 (epa-select-keys (epg-make-context epa-protocol) | |
989 "Select keys for signing. | |
990 If no one is selected, default secret key is used. " | |
991 nil t)) | |
992 (if verbose | |
993 (epa--read-signature-type) | |
994 'clear)))) | |
995 (save-excursion | |
996 (let ((context (epg-make-context epa-protocol)) | |
997 signature) | |
998 ;;(epg-context-set-armor context epa-armor) | |
999 (epg-context-set-armor context t) | |
1000 ;;(epg-context-set-textmode context epa-textmode) | |
1001 (epg-context-set-textmode context t) | |
1002 (epg-context-set-signers context signers) | |
1003 (epg-context-set-passphrase-callback context | |
1004 #'epa-passphrase-callback-function) | |
1005 (epg-context-set-progress-callback context | |
1006 (cons | |
1007 #'epa-progress-callback-function | |
1008 "Signing...")) | |
1009 (message "Signing...") | |
1010 (setq signature (epg-sign-string context | |
1011 (epa--encode-coding-string | |
1012 (buffer-substring start end) | |
1013 epa-last-coding-system-specified) | |
1014 mode)) | |
1015 (message "Signing...done") | |
1016 (delete-region start end) | |
1017 (goto-char start) | |
1018 (add-text-properties (point) | |
1019 (progn | |
1020 (insert (epa--decode-coding-string | |
1021 signature | |
1022 (or coding-system-for-read | |
1023 epa-last-coding-system-specified))) | |
1024 (point)) | |
1025 (list 'epa-coding-system-used | |
1026 epa-last-coding-system-specified | |
1027 'front-sticky nil | |
1028 'rear-nonsticky t | |
1029 'start-open t | |
1030 'end-open t))))) | |
1031 | |
1032 (eval-and-compile | |
1033 (if (fboundp 'derived-mode-p) | |
1034 (defalias 'epa--derived-mode-p 'derived-mode-p) | |
1035 (defun epa--derived-mode-p (&rest modes) | |
1036 "Non-nil if the current major mode is derived from one of MODES. | |
1037 Uses the `derived-mode-parent' property of the symbol to trace backwards." | |
1038 (let ((parent major-mode)) | |
1039 (while (and (not (memq parent modes)) | |
1040 (setq parent (get parent 'derived-mode-parent)))) | |
1041 parent)))) | |
1042 | |
1043 ;;;###autoload | |
1044 (defun epa-encrypt-region (start end recipients sign signers) | |
1045 "Encrypt the current region between START and END for RECIPIENTS. | |
1046 | |
93506 | 1047 Don't use this command in Lisp programs! |
1048 Since this function operates on regions, it does some tricks such | |
1049 as coding-system detection and unibyte/multibyte conversion. If | |
1050 you are sure how the data should be treated, you should consider | |
1051 using the string based counterpart `epg-encrypt-string', or the | |
1052 file based counterpart `epg-encrypt-file' instead. | |
1053 | |
1054 For example: | |
1055 | |
1056 \(let ((context (epg-make-context 'OpenPGP))) | |
1057 (epg-encrypt-string | |
1058 context | |
1059 (encode-coding-string (buffer-substring start end) 'utf-8) | |
1060 nil))" | |
91647 | 1061 (interactive |
1062 (let ((verbose current-prefix-arg) | |
1063 (context (epg-make-context epa-protocol)) | |
1064 sign) | |
1065 (setq epa-last-coding-system-specified | |
1066 (or coding-system-for-write | |
1067 (epa--select-safe-coding-system | |
1068 (region-beginning) (region-end)))) | |
1069 (list (region-beginning) (region-end) | |
1070 (epa-select-keys context | |
1071 "Select recipients for encryption. | |
1072 If no one is selected, symmetric encryption will be performed. ") | |
1073 (setq sign (if verbose (y-or-n-p "Sign? "))) | |
1074 (if sign | |
1075 (epa-select-keys context | |
1076 "Select keys for signing. "))))) | |
1077 (save-excursion | |
1078 (let ((context (epg-make-context epa-protocol)) | |
1079 cipher) | |
1080 ;;(epg-context-set-armor context epa-armor) | |
1081 (epg-context-set-armor context t) | |
1082 ;;(epg-context-set-textmode context epa-textmode) | |
1083 (epg-context-set-textmode context t) | |
1084 (if sign | |
1085 (epg-context-set-signers context signers)) | |
1086 (epg-context-set-passphrase-callback context | |
1087 #'epa-passphrase-callback-function) | |
1088 (epg-context-set-progress-callback context | |
1089 (cons | |
1090 #'epa-progress-callback-function | |
1091 "Encrypting...")) | |
1092 (message "Encrypting...") | |
1093 (setq cipher (epg-encrypt-string context | |
1094 (epa--encode-coding-string | |
1095 (buffer-substring start end) | |
1096 epa-last-coding-system-specified) | |
1097 recipients | |
1098 sign)) | |
1099 (message "Encrypting...done") | |
1100 (delete-region start end) | |
1101 (goto-char start) | |
1102 (add-text-properties (point) | |
1103 (progn | |
1104 (insert cipher) | |
1105 (point)) | |
1106 (list 'epa-coding-system-used | |
1107 epa-last-coding-system-specified | |
1108 'front-sticky nil | |
1109 'rear-nonsticky t | |
1110 'start-open t | |
1111 'end-open t))))) | |
1112 | |
1113 ;;;###autoload | |
1114 (defun epa-delete-keys (keys &optional allow-secret) | |
93506 | 1115 "Delete selected KEYS." |
91647 | 1116 (interactive |
1117 (let ((keys (epa--marked-keys))) | |
1118 (unless keys | |
1119 (error "No keys selected")) | |
1120 (list keys | |
1121 (eq (nth 1 epa-list-keys-arguments) t)))) | |
1122 (let ((context (epg-make-context epa-protocol))) | |
1123 (message "Deleting...") | |
1124 (epg-delete-keys context keys allow-secret) | |
1125 (message "Deleting...done") | |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
1126 (apply #'epa--list-keys epa-list-keys-arguments))) |
91647 | 1127 |
1128 ;;;###autoload | |
1129 (defun epa-import-keys (file) | |
93506 | 1130 "Import keys from FILE." |
91647 | 1131 (interactive "fFile: ") |
1132 (setq file (expand-file-name file)) | |
1133 (let ((context (epg-make-context epa-protocol))) | |
1134 (message "Importing %s..." (file-name-nondirectory file)) | |
1135 (condition-case nil | |
1136 (progn | |
1137 (epg-import-keys-from-file context file) | |
1138 (message "Importing %s...done" (file-name-nondirectory file))) | |
1139 (error | |
1140 (message "Importing %s...failed" (file-name-nondirectory file)))) | |
1141 (if (epg-context-result-for context 'import) | |
1142 (epa-display-info (epg-import-result-to-string | |
1143 (epg-context-result-for context 'import)))) | |
1144 (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
|
1145 (apply #'epa--list-keys epa-list-keys-arguments)))) |
91647 | 1146 |
1147 ;;;###autoload | |
1148 (defun epa-import-keys-region (start end) | |
93506 | 1149 "Import keys from the region." |
91647 | 1150 (interactive "r") |
1151 (let ((context (epg-make-context epa-protocol))) | |
1152 (message "Importing...") | |
1153 (condition-case nil | |
1154 (progn | |
1155 (epg-import-keys-from-string context (buffer-substring start end)) | |
1156 (message "Importing...done")) | |
1157 (error | |
1158 (message "Importing...failed"))) | |
1159 (if (epg-context-result-for context 'import) | |
1160 (epa-display-info (epg-import-result-to-string | |
1161 (epg-context-result-for context 'import)))))) | |
1162 | |
1163 ;;;###autoload | |
1164 (defun epa-import-armor-in-region (start end) | |
1165 "Import keys in the OpenPGP armor format in the current region | |
93506 | 1166 between START and END." |
91647 | 1167 (interactive "r") |
1168 (save-excursion | |
1169 (save-restriction | |
1170 (narrow-to-region start end) | |
1171 (goto-char start) | |
1172 (let (armor-start armor-end) | |
1173 (while (re-search-forward | |
1174 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$" | |
1175 nil t) | |
1176 (setq armor-start (match-beginning 0) | |
1177 armor-end (re-search-forward | |
1178 (concat "^-----END " (match-string 1) "-----$") | |
1179 nil t)) | |
1180 (unless armor-end | |
1181 (error "No armor tail")) | |
1182 (epa-import-keys-region armor-start armor-end)))))) | |
1183 | |
1184 ;;;###autoload | |
1185 (defun epa-export-keys (keys file) | |
93506 | 1186 "Export selected KEYS to FILE." |
91647 | 1187 (interactive |
1188 (let ((keys (epa--marked-keys)) | |
1189 default-name) | |
1190 (unless keys | |
1191 (error "No keys selected")) | |
1192 (setq default-name | |
1193 (expand-file-name | |
1194 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys)))) | |
1195 (if epa-armor ".asc" ".gpg")) | |
1196 default-directory)) | |
1197 (list keys | |
1198 (expand-file-name | |
1199 (read-file-name | |
1200 (concat "To file (default " | |
1201 (file-name-nondirectory default-name) | |
1202 ") ") | |
1203 (file-name-directory default-name) | |
1204 default-name))))) | |
1205 (let ((context (epg-make-context epa-protocol))) | |
1206 (epg-context-set-armor context epa-armor) | |
1207 (message "Exporting to %s..." (file-name-nondirectory file)) | |
1208 (epg-export-keys-to-file context keys file) | |
1209 (message "Exporting to %s...done" (file-name-nondirectory file)))) | |
1210 | |
1211 ;;;###autoload | |
1212 (defun epa-insert-keys (keys) | |
93506 | 1213 "Insert selected KEYS after the point." |
91647 | 1214 (interactive |
1215 (list (epa-select-keys (epg-make-context epa-protocol) | |
1216 "Select keys to export. "))) | |
1217 (let ((context (epg-make-context epa-protocol))) | |
1218 ;;(epg-context-set-armor context epa-armor) | |
1219 (epg-context-set-armor context t) | |
1220 (insert (epg-export-keys-to-string context keys)))) | |
1221 | |
1222 ;; (defun epa-sign-keys (keys &optional local) | |
1223 ;; "Sign selected KEYS. | |
1224 ;; If a prefix-arg is specified, the signature is marked as non exportable. | |
1225 | |
1226 ;; Don't use this command in Lisp programs!" | |
1227 ;; (interactive | |
1228 ;; (let ((keys (epa--marked-keys))) | |
1229 ;; (unless keys | |
1230 ;; (error "No keys selected")) | |
1231 ;; (list keys current-prefix-arg))) | |
1232 ;; (let ((context (epg-make-context epa-protocol))) | |
1233 ;; (epg-context-set-passphrase-callback context | |
1234 ;; #'epa-passphrase-callback-function) | |
1235 ;; (epg-context-set-progress-callback context | |
1236 ;; (cons | |
1237 ;; #'epa-progress-callback-function | |
1238 ;; "Signing keys...")) | |
1239 ;; (message "Signing keys...") | |
1240 ;; (epg-sign-keys context keys local) | |
1241 ;; (message "Signing keys...done"))) | |
1242 ;; (make-obsolete 'epa-sign-keys "Do not use.") | |
1243 | |
1244 (provide 'epa) | |
1245 | |
91687 | 1246 ;; arch-tag: 38d20ced-20d5-4137-b17a-f206335423d7 |
91647 | 1247 ;;; epa.el ends here |