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