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