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