Mercurial > emacs
annotate lisp/epa.el @ 110889:50d2da482ed7
* doc/misc/Makefile.in: Use $< in rules.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Sat, 09 Oct 2010 13:12:18 -0700 |
parents | f60f4abe5849 |
children | beddb591d182 |
rev | line source |
---|---|
91647 | 1 ;;; epa.el --- the EasyPG Assistant |
106815 | 2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
91647 | 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] |
105275
b78ceb253d15
* speedbar.el (speedbar-item-delete):
Juanma Barranquero <lekktu@gmail.com>
parents:
104395
diff
changeset
|
237 '(menu-item "Encrypt File..." epa-encrypt-file |
94490
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 () | |
105994
009383a57ce8
* x-dnd.el (x-dnd-maybe-call-test-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105275
diff
changeset
|
466 (or (with-current-buffer epa-keys-buffer |
91647 | 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) | |
105994
009383a57ce8
* x-dnd.el (x-dnd-maybe-call-test-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105275
diff
changeset
|
481 (unless (and epa-keys-buffer |
009383a57ce8
* x-dnd.el (x-dnd-maybe-call-test-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105275
diff
changeset
|
482 (buffer-live-p epa-keys-buffer)) |
009383a57ce8
* x-dnd.el (x-dnd-maybe-call-test-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105275
diff
changeset
|
483 (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) |
009383a57ce8
* x-dnd.el (x-dnd-maybe-call-test-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105275
diff
changeset
|
484 (with-current-buffer epa-keys-buffer |
91647 | 485 (epa-key-list-mode) |
486 (let ((inhibit-read-only t) | |
487 buffer-read-only) | |
488 (erase-buffer) | |
489 (insert prompt "\n" | |
490 (substitute-command-keys "\ | |
491 - `\\[epa-mark-key]' to mark a key on the line | |
492 - `\\[epa-unmark-key]' to unmark a key on the line\n")) | |
493 (widget-create 'link | |
494 :notify (lambda (&rest ignore) (abort-recursive-edit)) | |
495 :help-echo | |
496 (substitute-command-keys | |
497 "Click here or \\[abort-recursive-edit] to cancel") | |
498 "Cancel") | |
499 (widget-create 'link | |
500 :notify (lambda (&rest ignore) (exit-recursive-edit)) | |
501 :help-echo | |
502 (substitute-command-keys | |
503 "Click here or \\[exit-recursive-edit] to finish") | |
504 "OK") | |
505 (insert "\n\n") | |
506 (epa--insert-keys keys) | |
507 (widget-setup) | |
508 (set-keymap-parent (current-local-map) widget-keymap) | |
509 (setq epa-exit-buffer-function #'abort-recursive-edit) | |
510 (goto-char (point-min)) | |
108668
5a957bb32b66
* epa.el (epa--select-keys): Don't explicitly delete the window since
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
106815
diff
changeset
|
511 (let ((display-buffer-mark-dedicated 'soft)) |
5a957bb32b66
* epa.el (epa--select-keys): Don't explicitly delete the window since
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
106815
diff
changeset
|
512 (pop-to-buffer (current-buffer)))) |
91647 | 513 (unwind-protect |
514 (progn | |
515 (recursive-edit) | |
516 (epa--marked-keys)) | |
517 (kill-buffer epa-keys-buffer)))) | |
518 | |
519 ;;;###autoload | |
520 (defun epa-select-keys (context prompt &optional names secret) | |
521 "Display a user's keyring and ask him to select keys. | |
522 CONTEXT is an epg-context. | |
523 PROMPT is a string to prompt with. | |
524 NAMES is a list of strings to be matched with keys. If it is nil, all | |
525 the keys are listed. | |
526 If SECRET is non-nil, list secret keys instead of public keys." | |
527 (let ((keys (epg-list-keys context names secret))) | |
93506 | 528 (epa--select-keys prompt keys))) |
91647 | 529 |
530 (defun epa--show-key (key) | |
531 (let* ((primary-sub-key (car (epg-key-sub-key-list key))) | |
532 (entry (assoc (epg-sub-key-id primary-sub-key) | |
533 epa-key-buffer-alist)) | |
534 (inhibit-read-only t) | |
535 buffer-read-only | |
536 pointer) | |
537 (unless entry | |
538 (setq entry (cons (epg-sub-key-id primary-sub-key) nil) | |
539 epa-key-buffer-alist (cons entry epa-key-buffer-alist))) | |
540 (unless (and (cdr entry) | |
541 (buffer-live-p (cdr entry))) | |
542 (setcdr entry (generate-new-buffer | |
543 (format "*Key*%s" (epg-sub-key-id primary-sub-key))))) | |
544 (set-buffer (cdr entry)) | |
545 (epa-key-mode) | |
546 (make-local-variable 'epa-key) | |
547 (setq epa-key key) | |
548 (erase-buffer) | |
549 (setq pointer (epg-key-user-id-list key)) | |
550 (while pointer | |
551 (if (car pointer) | |
552 (insert " " | |
553 (if (epg-user-id-validity (car pointer)) | |
554 (char-to-string | |
555 (car (rassq (epg-user-id-validity (car pointer)) | |
556 epg-key-validity-alist))) | |
557 " ") | |
558 " " | |
559 (if (stringp (epg-user-id-string (car pointer))) | |
560 (epg-user-id-string (car pointer)) | |
561 (epg-decode-dn (epg-user-id-string (car pointer)))) | |
562 "\n")) | |
563 (setq pointer (cdr pointer))) | |
564 (setq pointer (epg-key-sub-key-list key)) | |
565 (while pointer | |
566 (insert " " | |
567 (if (epg-sub-key-validity (car pointer)) | |
568 (char-to-string | |
569 (car (rassq (epg-sub-key-validity (car pointer)) | |
570 epg-key-validity-alist))) | |
571 " ") | |
572 " " | |
573 (epg-sub-key-id (car pointer)) | |
574 " " | |
575 (format "%dbits" | |
576 (epg-sub-key-length (car pointer))) | |
577 " " | |
578 (cdr (assq (epg-sub-key-algorithm (car pointer)) | |
579 epg-pubkey-algorithm-alist)) | |
580 "\n\tCreated: " | |
581 (condition-case nil | |
582 (format-time-string "%Y-%m-%d" | |
583 (epg-sub-key-creation-time (car pointer))) | |
584 (error "????-??-??")) | |
585 (if (epg-sub-key-expiration-time (car pointer)) | |
100175
be17c10d7381
(epa--show-key): Use past tense of "expire" if the key
Daiki Ueno <ueno@unixuser.org>
parents:
97719
diff
changeset
|
586 (format (if (time-less-p (current-time) |
be17c10d7381
(epa--show-key): Use past tense of "expire" if the key
Daiki Ueno <ueno@unixuser.org>
parents:
97719
diff
changeset
|
587 (epg-sub-key-expiration-time |
be17c10d7381
(epa--show-key): Use past tense of "expire" if the key
Daiki Ueno <ueno@unixuser.org>
parents:
97719
diff
changeset
|
588 (car pointer))) |
be17c10d7381
(epa--show-key): Use past tense of "expire" if the key
Daiki Ueno <ueno@unixuser.org>
parents:
97719
diff
changeset
|
589 "\n\tExpires: %s" |
be17c10d7381
(epa--show-key): Use past tense of "expire" if the key
Daiki Ueno <ueno@unixuser.org>
parents:
97719
diff
changeset
|
590 "\n\tExpired: %s") |
91647 | 591 (condition-case nil |
592 (format-time-string "%Y-%m-%d" | |
593 (epg-sub-key-expiration-time | |
594 (car pointer))) | |
595 (error "????-??-??"))) | |
596 "") | |
597 "\n\tCapabilities: " | |
598 (mapconcat #'symbol-name | |
599 (epg-sub-key-capability (car pointer)) | |
600 " ") | |
601 "\n\tFingerprint: " | |
602 (epg-sub-key-fingerprint (car pointer)) | |
603 "\n") | |
604 (setq pointer (cdr pointer))) | |
605 (goto-char (point-min)) | |
606 (pop-to-buffer (current-buffer)))) | |
607 | |
608 (defun epa-display-info (info) | |
609 (if epa-popup-info-window | |
610 (save-selected-window | |
611 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer)) | |
612 (setq epa-info-buffer (generate-new-buffer "*Info*"))) | |
613 (if (get-buffer-window epa-info-buffer) | |
614 (delete-window (get-buffer-window epa-info-buffer))) | |
105994
009383a57ce8
* x-dnd.el (x-dnd-maybe-call-test-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105275
diff
changeset
|
615 (with-current-buffer epa-info-buffer |
91647 | 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))) | |
104395
df3d3d6c4426
* net/newst-treeview.el (newsticker-groups-filename):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
103324
diff
changeset
|
634 (make-obsolete 'epa-display-verify-result 'epa-display-info "23.1") |
91647 | 635 |
636 (defun epa-passphrase-callback-function (context key-id handback) | |
637 (if (eq key-id 'SYM) | |
110877
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
638 (read-passwd |
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
639 (format "Passphrase for symmetric encryption%s: " |
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
640 (let ((elem (epg-context-passphrase-callback context))) |
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
641 ;; Add the file name to the prompt, if any. |
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
642 (if (and (consp elem) |
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
643 (stringp (cdr elem))) |
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
644 (format " for %s" (cdr elem)) |
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
645 ""))) |
f60f4abe5849
(epa-passphrase-callback-function): Say what we're querying the password for.
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
108668
diff
changeset
|
646 (eq (epg-context-operation context) 'encrypt)) |
91647 | 647 (read-passwd |
648 (if (eq key-id 'PIN) | |
649 "Passphrase for PIN: " | |
650 (let ((entry (assoc key-id epg-user-id-alist))) | |
651 (if entry | |
652 (format "Passphrase for %s %s: " key-id (cdr entry)) | |
653 (format "Passphrase for %s: " key-id))))))) | |
654 | |
655 (defun epa-progress-callback-function (context what char current total | |
656 handback) | |
657 (message "%s%d%% (%d/%d)" (or handback | |
658 (concat what ": ")) | |
659 (if (> total 0) (floor (* (/ current (float total)) 100)) 0) | |
660 current total)) | |
661 | |
662 ;;;###autoload | |
663 (defun epa-decrypt-file (file) | |
664 "Decrypt FILE." | |
665 (interactive "fFile: ") | |
666 (setq file (expand-file-name file)) | |
667 (let* ((default-name (file-name-sans-extension file)) | |
668 (plain (expand-file-name | |
669 (read-file-name | |
670 (concat "To file (default " | |
671 (file-name-nondirectory default-name) | |
672 ") ") | |
673 (file-name-directory default-name) | |
674 default-name))) | |
675 (context (epg-make-context epa-protocol))) | |
676 (epg-context-set-passphrase-callback context | |
677 #'epa-passphrase-callback-function) | |
678 (epg-context-set-progress-callback context | |
679 (cons | |
680 #'epa-progress-callback-function | |
681 (format "Decrypting %s..." | |
682 (file-name-nondirectory file)))) | |
683 (message "Decrypting %s..." (file-name-nondirectory file)) | |
684 (epg-decrypt-file context file plain) | |
685 (message "Decrypting %s...wrote %s" (file-name-nondirectory file) | |
686 (file-name-nondirectory plain)) | |
687 (if (epg-context-result-for context 'verify) | |
688 (epa-display-info (epg-verify-result-to-string | |
689 (epg-context-result-for context 'verify)))))) | |
690 | |
691 ;;;###autoload | |
692 (defun epa-verify-file (file) | |
693 "Verify FILE." | |
694 (interactive "fFile: ") | |
695 (setq file (expand-file-name file)) | |
696 (let* ((context (epg-make-context epa-protocol)) | |
697 (plain (if (equal (file-name-extension file) "sig") | |
698 (file-name-sans-extension file)))) | |
699 (epg-context-set-progress-callback context | |
700 (cons | |
701 #'epa-progress-callback-function | |
702 (format "Verifying %s..." | |
703 (file-name-nondirectory file)))) | |
704 (message "Verifying %s..." (file-name-nondirectory file)) | |
705 (epg-verify-file context file plain) | |
706 (message "Verifying %s...done" (file-name-nondirectory file)) | |
707 (if (epg-context-result-for context 'verify) | |
708 (epa-display-info (epg-verify-result-to-string | |
709 (epg-context-result-for context 'verify)))))) | |
710 | |
711 (defun epa--read-signature-type () | |
712 (let (type c) | |
713 (while (null type) | |
714 (message "Signature type (n,c,d,?) ") | |
715 (setq c (read-char)) | |
716 (cond ((eq c ?c) | |
717 (setq type 'clear)) | |
718 ((eq c ?d) | |
719 (setq type 'detached)) | |
720 ((eq c ??) | |
721 (with-output-to-temp-buffer "*Help*" | |
105994
009383a57ce8
* x-dnd.el (x-dnd-maybe-call-test-function):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
105275
diff
changeset
|
722 (with-current-buffer standard-output |
91647 | 723 (insert "\ |
724 n - Create a normal signature | |
725 c - Create a cleartext signature | |
726 d - Create a detached signature | |
727 ? - Show this help | |
728 ")))) | |
729 (t | |
103138
55823cc572a6
(epa--read-signature-type): Fix typo.
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
730 (setq type 'normal)))) |
55823cc572a6
(epa--read-signature-type): Fix typo.
Daiki Ueno <ueno@unixuser.org>
parents:
100908
diff
changeset
|
731 type)) |
91647 | 732 |
733 ;;;###autoload | |
734 (defun epa-sign-file (file signers mode) | |
735 "Sign FILE by SIGNERS keys selected." | |
736 (interactive | |
737 (let ((verbose current-prefix-arg)) | |
738 (list (expand-file-name (read-file-name "File: ")) | |
739 (if verbose | |
740 (epa-select-keys (epg-make-context epa-protocol) | |
741 "Select keys for signing. | |
742 If no one is selected, default secret key is used. " | |
743 nil t)) | |
744 (if verbose | |
745 (epa--read-signature-type) | |
746 'clear)))) | |
747 (let ((signature (concat file | |
748 (if (eq epa-protocol 'OpenPGP) | |
749 (if (or epa-armor | |
750 (not (memq mode | |
751 '(nil t normal detached)))) | |
752 ".asc" | |
753 (if (memq mode '(t detached)) | |
754 ".sig" | |
755 ".gpg")) | |
756 (if (memq mode '(t detached)) | |
757 ".p7s" | |
758 ".p7m")))) | |
759 (context (epg-make-context epa-protocol))) | |
760 (epg-context-set-armor context epa-armor) | |
761 (epg-context-set-textmode context epa-textmode) | |
762 (epg-context-set-signers context signers) | |
763 (epg-context-set-passphrase-callback context | |
764 #'epa-passphrase-callback-function) | |
765 (epg-context-set-progress-callback context | |
766 (cons | |
767 #'epa-progress-callback-function | |
768 (format "Signing %s..." | |
769 (file-name-nondirectory file)))) | |
770 (message "Signing %s..." (file-name-nondirectory file)) | |
771 (epg-sign-file context file signature mode) | |
772 (message "Signing %s...wrote %s" (file-name-nondirectory file) | |
773 (file-name-nondirectory signature)))) | |
774 | |
775 ;;;###autoload | |
776 (defun epa-encrypt-file (file recipients) | |
777 "Encrypt FILE for RECIPIENTS." | |
778 (interactive | |
779 (list (expand-file-name (read-file-name "File: ")) | |
780 (epa-select-keys (epg-make-context epa-protocol) | |
781 "Select recipients for encryption. | |
782 If no one is selected, symmetric encryption will be performed. "))) | |
783 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP) | |
784 (if epa-armor ".asc" ".gpg") | |
785 ".p7m"))) | |
786 (context (epg-make-context epa-protocol))) | |
787 (epg-context-set-armor context epa-armor) | |
788 (epg-context-set-textmode context epa-textmode) | |
789 (epg-context-set-passphrase-callback context | |
790 #'epa-passphrase-callback-function) | |
791 (epg-context-set-progress-callback context | |
792 (cons | |
793 #'epa-progress-callback-function | |
794 (format "Encrypting %s..." | |
795 (file-name-nondirectory file)))) | |
796 (message "Encrypting %s..." (file-name-nondirectory file)) | |
797 (epg-encrypt-file context file recipients cipher) | |
798 (message "Encrypting %s...wrote %s" (file-name-nondirectory file) | |
799 (file-name-nondirectory cipher)))) | |
800 | |
801 ;;;###autoload | |
802 (defun epa-decrypt-region (start end) | |
803 "Decrypt the current region between START and END. | |
804 | |
93506 | 805 Don't use this command in Lisp programs! |
806 Since this function operates on regions, it does some tricks such | |
807 as coding-system detection and unibyte/multibyte conversion. If | |
808 you are sure how the data in the region should be treated, you | |
809 should consider using the string based counterpart | |
810 `epg-decrypt-string', or the file based counterpart | |
811 `epg-decrypt-file' instead. | |
812 | |
813 For example: | |
814 | |
815 \(let ((context (epg-make-context 'OpenPGP))) | |
816 (decode-coding-string | |
817 (epg-decrypt-string context (buffer-substring start end)) | |
818 'utf-8))" | |
91647 | 819 (interactive "r") |
820 (save-excursion | |
821 (let ((context (epg-make-context epa-protocol)) | |
822 plain) | |
823 (epg-context-set-passphrase-callback context | |
824 #'epa-passphrase-callback-function) | |
825 (epg-context-set-progress-callback context | |
826 (cons | |
827 #'epa-progress-callback-function | |
828 "Decrypting...")) | |
829 (message "Decrypting...") | |
830 (setq plain (epg-decrypt-string context (buffer-substring start end))) | |
831 (message "Decrypting...done") | |
832 (setq plain (epa--decode-coding-string | |
833 plain | |
834 (or coding-system-for-read | |
103324
6f4c24703dac
(epa-decrypt-region): Detect encoding if
Daiki Ueno <ueno@unixuser.org>
parents:
103138
diff
changeset
|
835 (get-text-property start 'epa-coding-system-used) |
6f4c24703dac
(epa-decrypt-region): Detect encoding if
Daiki Ueno <ueno@unixuser.org>
parents:
103138
diff
changeset
|
836 'undecided))) |
91647 | 837 (if (y-or-n-p "Replace the original text? ") |
838 (let ((inhibit-read-only t) | |
839 buffer-read-only) | |
840 (delete-region start end) | |
841 (goto-char start) | |
842 (insert plain)) | |
843 (with-output-to-temp-buffer "*Temp*" | |
844 (set-buffer standard-output) | |
845 (insert plain) | |
846 (epa-info-mode))) | |
847 (if (epg-context-result-for context 'verify) | |
848 (epa-display-info (epg-verify-result-to-string | |
849 (epg-context-result-for context 'verify))))))) | |
850 | |
851 (defun epa--find-coding-system-for-mime-charset (mime-charset) | |
852 (if (featurep 'xemacs) | |
853 (if (fboundp 'find-coding-system) | |
854 (find-coding-system mime-charset)) | |
855 (let ((pointer (coding-system-list))) | |
856 (while (and pointer | |
857 (eq (coding-system-get (car pointer) 'mime-charset) | |
858 mime-charset)) | |
859 (setq pointer (cdr pointer))) | |
860 pointer))) | |
861 | |
862 ;;;###autoload | |
863 (defun epa-decrypt-armor-in-region (start end) | |
864 "Decrypt OpenPGP armors in the current region between START and END. | |
865 | |
93506 | 866 Don't use this command in Lisp programs! |
867 See the reason described in the `epa-decrypt-region' documentation." | |
91647 | 868 (interactive "r") |
869 (save-excursion | |
870 (save-restriction | |
871 (narrow-to-region start end) | |
872 (goto-char start) | |
873 (let (armor-start armor-end) | |
874 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t) | |
875 (setq armor-start (match-beginning 0) | |
876 armor-end (re-search-forward "^-----END PGP MESSAGE-----$" | |
877 nil t)) | |
878 (unless armor-end | |
879 (error "No armor tail")) | |
880 (goto-char armor-start) | |
881 (let ((coding-system-for-read | |
882 (or coding-system-for-read | |
883 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t) | |
884 (epa--find-coding-system-for-mime-charset | |
885 (intern (downcase (match-string 1)))))))) | |
886 (goto-char armor-end) | |
887 (epa-decrypt-region armor-start armor-end))))))) | |
888 | |
889 ;;;###autoload | |
890 (defun epa-verify-region (start end) | |
891 "Verify the current region between START and END. | |
892 | |
93506 | 893 Don't use this command in Lisp programs! |
894 Since this function operates on regions, it does some tricks such | |
895 as coding-system detection and unibyte/multibyte conversion. If | |
896 you are sure how the data in the region should be treated, you | |
897 should consider using the string based counterpart | |
898 `epg-verify-string', or the file based counterpart | |
899 `epg-verify-file' instead. | |
900 | |
901 For example: | |
902 | |
903 \(let ((context (epg-make-context 'OpenPGP))) | |
904 (decode-coding-string | |
905 (epg-verify-string context (buffer-substring start end)) | |
906 'utf-8))" | |
91647 | 907 (interactive "r") |
908 (let ((context (epg-make-context epa-protocol)) | |
909 plain) | |
910 (epg-context-set-progress-callback context | |
911 (cons | |
912 #'epa-progress-callback-function | |
913 "Verifying...")) | |
914 (message "Verifying...") | |
915 (setq plain (epg-verify-string | |
916 context | |
917 (epa--encode-coding-string | |
918 (buffer-substring start end) | |
919 (or coding-system-for-write | |
920 (get-text-property start 'epa-coding-system-used))))) | |
921 (message "Verifying...done") | |
922 (setq plain (epa--decode-coding-string | |
923 plain | |
924 (or coding-system-for-read | |
103324
6f4c24703dac
(epa-decrypt-region): Detect encoding if
Daiki Ueno <ueno@unixuser.org>
parents:
103138
diff
changeset
|
925 (get-text-property start 'epa-coding-system-used) |
6f4c24703dac
(epa-decrypt-region): Detect encoding if
Daiki Ueno <ueno@unixuser.org>
parents:
103138
diff
changeset
|
926 'undecided))) |
91647 | 927 (if (y-or-n-p "Replace the original text? ") |
928 (let ((inhibit-read-only t) | |
929 buffer-read-only) | |
930 (delete-region start end) | |
931 (goto-char start) | |
932 (insert plain)) | |
933 (with-output-to-temp-buffer "*Temp*" | |
934 (set-buffer standard-output) | |
935 (insert plain) | |
936 (epa-info-mode))) | |
937 (if (epg-context-result-for context 'verify) | |
938 (epa-display-info (epg-verify-result-to-string | |
939 (epg-context-result-for context 'verify)))))) | |
940 | |
941 ;;;###autoload | |
942 (defun epa-verify-cleartext-in-region (start end) | |
943 "Verify OpenPGP cleartext signed messages in the current region | |
944 between START and END. | |
945 | |
93506 | 946 Don't use this command in Lisp programs! |
947 See the reason described in the `epa-verify-region' documentation." | |
91647 | 948 (interactive "r") |
949 (save-excursion | |
950 (save-restriction | |
951 (narrow-to-region start end) | |
952 (goto-char start) | |
953 (let (cleartext-start cleartext-end) | |
954 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$" | |
955 nil t) | |
956 (setq cleartext-start (match-beginning 0)) | |
957 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$" | |
958 nil t) | |
959 (error "Invalid cleartext signed message")) | |
960 (setq cleartext-end (re-search-forward | |
961 "^-----END PGP SIGNATURE-----$" | |
962 nil t)) | |
963 (unless cleartext-end | |
964 (error "No cleartext tail")) | |
965 (epa-verify-region cleartext-start cleartext-end)))))) | |
966 | |
967 (eval-and-compile | |
968 (if (fboundp 'select-safe-coding-system) | |
969 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system) | |
970 (defun epa--select-safe-coding-system (from to) | |
971 buffer-file-coding-system))) | |
972 | |
973 ;;;###autoload | |
974 (defun epa-sign-region (start end signers mode) | |
975 "Sign the current region between START and END by SIGNERS keys selected. | |
976 | |
93506 | 977 Don't use this command in Lisp programs! |
978 Since this function operates on regions, it does some tricks such | |
979 as coding-system detection and unibyte/multibyte conversion. If | |
980 you are sure how the data should be treated, you should consider | |
981 using the string based counterpart `epg-sign-string', or the file | |
982 based counterpart `epg-sign-file' instead. | |
983 | |
984 For example: | |
985 | |
986 \(let ((context (epg-make-context 'OpenPGP))) | |
987 (epg-sign-string | |
988 context | |
989 (encode-coding-string (buffer-substring start end) 'utf-8)))" | |
91647 | 990 (interactive |
991 (let ((verbose current-prefix-arg)) | |
992 (setq epa-last-coding-system-specified | |
993 (or coding-system-for-write | |
994 (epa--select-safe-coding-system | |
995 (region-beginning) (region-end)))) | |
996 (list (region-beginning) (region-end) | |
997 (if verbose | |
998 (epa-select-keys (epg-make-context epa-protocol) | |
999 "Select keys for signing. | |
1000 If no one is selected, default secret key is used. " | |
1001 nil t)) | |
1002 (if verbose | |
1003 (epa--read-signature-type) | |
1004 'clear)))) | |
1005 (save-excursion | |
1006 (let ((context (epg-make-context epa-protocol)) | |
1007 signature) | |
1008 ;;(epg-context-set-armor context epa-armor) | |
1009 (epg-context-set-armor context t) | |
1010 ;;(epg-context-set-textmode context epa-textmode) | |
1011 (epg-context-set-textmode context t) | |
1012 (epg-context-set-signers context signers) | |
1013 (epg-context-set-passphrase-callback context | |
1014 #'epa-passphrase-callback-function) | |
1015 (epg-context-set-progress-callback context | |
1016 (cons | |
1017 #'epa-progress-callback-function | |
1018 "Signing...")) | |
1019 (message "Signing...") | |
1020 (setq signature (epg-sign-string context | |
1021 (epa--encode-coding-string | |
1022 (buffer-substring start end) | |
1023 epa-last-coding-system-specified) | |
1024 mode)) | |
1025 (message "Signing...done") | |
1026 (delete-region start end) | |
1027 (goto-char start) | |
1028 (add-text-properties (point) | |
1029 (progn | |
1030 (insert (epa--decode-coding-string | |
1031 signature | |
1032 (or coding-system-for-read | |
1033 epa-last-coding-system-specified))) | |
1034 (point)) | |
1035 (list 'epa-coding-system-used | |
1036 epa-last-coding-system-specified | |
1037 'front-sticky nil | |
1038 'rear-nonsticky t | |
1039 'start-open t | |
1040 'end-open t))))) | |
1041 | |
1042 (eval-and-compile | |
1043 (if (fboundp 'derived-mode-p) | |
1044 (defalias 'epa--derived-mode-p 'derived-mode-p) | |
1045 (defun epa--derived-mode-p (&rest modes) | |
1046 "Non-nil if the current major mode is derived from one of MODES. | |
1047 Uses the `derived-mode-parent' property of the symbol to trace backwards." | |
1048 (let ((parent major-mode)) | |
1049 (while (and (not (memq parent modes)) | |
1050 (setq parent (get parent 'derived-mode-parent)))) | |
1051 parent)))) | |
1052 | |
1053 ;;;###autoload | |
1054 (defun epa-encrypt-region (start end recipients sign signers) | |
1055 "Encrypt the current region between START and END for RECIPIENTS. | |
1056 | |
93506 | 1057 Don't use this command in Lisp programs! |
1058 Since this function operates on regions, it does some tricks such | |
1059 as coding-system detection and unibyte/multibyte conversion. If | |
1060 you are sure how the data should be treated, you should consider | |
1061 using the string based counterpart `epg-encrypt-string', or the | |
1062 file based counterpart `epg-encrypt-file' instead. | |
1063 | |
1064 For example: | |
1065 | |
1066 \(let ((context (epg-make-context 'OpenPGP))) | |
1067 (epg-encrypt-string | |
1068 context | |
1069 (encode-coding-string (buffer-substring start end) 'utf-8) | |
1070 nil))" | |
91647 | 1071 (interactive |
1072 (let ((verbose current-prefix-arg) | |
1073 (context (epg-make-context epa-protocol)) | |
1074 sign) | |
1075 (setq epa-last-coding-system-specified | |
1076 (or coding-system-for-write | |
1077 (epa--select-safe-coding-system | |
1078 (region-beginning) (region-end)))) | |
1079 (list (region-beginning) (region-end) | |
1080 (epa-select-keys context | |
1081 "Select recipients for encryption. | |
1082 If no one is selected, symmetric encryption will be performed. ") | |
1083 (setq sign (if verbose (y-or-n-p "Sign? "))) | |
1084 (if sign | |
1085 (epa-select-keys context | |
1086 "Select keys for signing. "))))) | |
1087 (save-excursion | |
1088 (let ((context (epg-make-context epa-protocol)) | |
1089 cipher) | |
1090 ;;(epg-context-set-armor context epa-armor) | |
1091 (epg-context-set-armor context t) | |
1092 ;;(epg-context-set-textmode context epa-textmode) | |
1093 (epg-context-set-textmode context t) | |
1094 (if sign | |
1095 (epg-context-set-signers context signers)) | |
1096 (epg-context-set-passphrase-callback context | |
1097 #'epa-passphrase-callback-function) | |
1098 (epg-context-set-progress-callback context | |
1099 (cons | |
1100 #'epa-progress-callback-function | |
1101 "Encrypting...")) | |
1102 (message "Encrypting...") | |
1103 (setq cipher (epg-encrypt-string context | |
1104 (epa--encode-coding-string | |
1105 (buffer-substring start end) | |
1106 epa-last-coding-system-specified) | |
1107 recipients | |
1108 sign)) | |
1109 (message "Encrypting...done") | |
1110 (delete-region start end) | |
1111 (goto-char start) | |
1112 (add-text-properties (point) | |
1113 (progn | |
1114 (insert cipher) | |
1115 (point)) | |
1116 (list 'epa-coding-system-used | |
1117 epa-last-coding-system-specified | |
1118 'front-sticky nil | |
1119 'rear-nonsticky t | |
1120 'start-open t | |
1121 'end-open t))))) | |
1122 | |
1123 ;;;###autoload | |
1124 (defun epa-delete-keys (keys &optional allow-secret) | |
93506 | 1125 "Delete selected KEYS." |
91647 | 1126 (interactive |
1127 (let ((keys (epa--marked-keys))) | |
1128 (unless keys | |
1129 (error "No keys selected")) | |
1130 (list keys | |
1131 (eq (nth 1 epa-list-keys-arguments) t)))) | |
1132 (let ((context (epg-make-context epa-protocol))) | |
1133 (message "Deleting...") | |
1134 (epg-delete-keys context keys allow-secret) | |
1135 (message "Deleting...done") | |
94471
ecd293096a4b
Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents:
94417
diff
changeset
|
1136 (apply #'epa--list-keys epa-list-keys-arguments))) |
91647 | 1137 |
1138 ;;;###autoload | |
1139 (defun epa-import-keys (file) | |
93506 | 1140 "Import keys from FILE." |
91647 | 1141 (interactive "fFile: ") |
1142 (setq file (expand-file-name file)) | |
1143 (let ((context (epg-make-context epa-protocol))) | |
1144 (message "Importing %s..." (file-name-nondirectory file)) | |
1145 (condition-case nil | |
1146 (progn | |
1147 (epg-import-keys-from-file context file) | |
1148 (message "Importing %s...done" (file-name-nondirectory file))) | |
1149 (error | |
1150 (message "Importing %s...failed" (file-name-nondirectory file)))) | |
1151 (if (epg-context-result-for context 'import) | |
1152 (epa-display-info (epg-import-result-to-string | |
1153 (epg-context-result-for context 'import)))) | |
1154 (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
|
1155 (apply #'epa--list-keys epa-list-keys-arguments)))) |
91647 | 1156 |
1157 ;;;###autoload | |
1158 (defun epa-import-keys-region (start end) | |
93506 | 1159 "Import keys from the region." |
91647 | 1160 (interactive "r") |
1161 (let ((context (epg-make-context epa-protocol))) | |
1162 (message "Importing...") | |
1163 (condition-case nil | |
1164 (progn | |
1165 (epg-import-keys-from-string context (buffer-substring start end)) | |
1166 (message "Importing...done")) | |
1167 (error | |
1168 (message "Importing...failed"))) | |
1169 (if (epg-context-result-for context 'import) | |
1170 (epa-display-info (epg-import-result-to-string | |
1171 (epg-context-result-for context 'import)))))) | |
1172 | |
1173 ;;;###autoload | |
1174 (defun epa-import-armor-in-region (start end) | |
1175 "Import keys in the OpenPGP armor format in the current region | |
93506 | 1176 between START and END." |
91647 | 1177 (interactive "r") |
1178 (save-excursion | |
1179 (save-restriction | |
1180 (narrow-to-region start end) | |
1181 (goto-char start) | |
1182 (let (armor-start armor-end) | |
1183 (while (re-search-forward | |
1184 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$" | |
1185 nil t) | |
1186 (setq armor-start (match-beginning 0) | |
1187 armor-end (re-search-forward | |
1188 (concat "^-----END " (match-string 1) "-----$") | |
1189 nil t)) | |
1190 (unless armor-end | |
1191 (error "No armor tail")) | |
1192 (epa-import-keys-region armor-start armor-end)))))) | |
1193 | |
1194 ;;;###autoload | |
1195 (defun epa-export-keys (keys file) | |
93506 | 1196 "Export selected KEYS to FILE." |
91647 | 1197 (interactive |
1198 (let ((keys (epa--marked-keys)) | |
1199 default-name) | |
1200 (unless keys | |
1201 (error "No keys selected")) | |
1202 (setq default-name | |
1203 (expand-file-name | |
1204 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys)))) | |
1205 (if epa-armor ".asc" ".gpg")) | |
1206 default-directory)) | |
1207 (list keys | |
1208 (expand-file-name | |
1209 (read-file-name | |
1210 (concat "To file (default " | |
1211 (file-name-nondirectory default-name) | |
1212 ") ") | |
1213 (file-name-directory default-name) | |
1214 default-name))))) | |
1215 (let ((context (epg-make-context epa-protocol))) | |
1216 (epg-context-set-armor context epa-armor) | |
1217 (message "Exporting to %s..." (file-name-nondirectory file)) | |
1218 (epg-export-keys-to-file context keys file) | |
1219 (message "Exporting to %s...done" (file-name-nondirectory file)))) | |
1220 | |
1221 ;;;###autoload | |
1222 (defun epa-insert-keys (keys) | |
93506 | 1223 "Insert selected KEYS after the point." |
91647 | 1224 (interactive |
1225 (list (epa-select-keys (epg-make-context epa-protocol) | |
1226 "Select keys to export. "))) | |
1227 (let ((context (epg-make-context epa-protocol))) | |
1228 ;;(epg-context-set-armor context epa-armor) | |
1229 (epg-context-set-armor context t) | |
1230 (insert (epg-export-keys-to-string context keys)))) | |
1231 | |
1232 ;; (defun epa-sign-keys (keys &optional local) | |
1233 ;; "Sign selected KEYS. | |
1234 ;; If a prefix-arg is specified, the signature is marked as non exportable. | |
1235 | |
1236 ;; Don't use this command in Lisp programs!" | |
1237 ;; (interactive | |
1238 ;; (let ((keys (epa--marked-keys))) | |
1239 ;; (unless keys | |
1240 ;; (error "No keys selected")) | |
1241 ;; (list keys current-prefix-arg))) | |
1242 ;; (let ((context (epg-make-context epa-protocol))) | |
1243 ;; (epg-context-set-passphrase-callback context | |
1244 ;; #'epa-passphrase-callback-function) | |
1245 ;; (epg-context-set-progress-callback context | |
1246 ;; (cons | |
1247 ;; #'epa-progress-callback-function | |
1248 ;; "Signing keys...")) | |
1249 ;; (message "Signing keys...") | |
1250 ;; (epg-sign-keys context keys local) | |
1251 ;; (message "Signing keys...done"))) | |
1252 ;; (make-obsolete 'epa-sign-keys "Do not use.") | |
1253 | |
1254 (provide 'epa) | |
1255 | |
91687 | 1256 ;; arch-tag: 38d20ced-20d5-4137-b17a-f206335423d7 |
91647 | 1257 ;;; epa.el ends here |