(epa-dired-decrypt-file): Query user for a new file.
[elisp/epg.git] / epa-dired.el
1 (require 'epa)
2 (require 'dired)
3
4 (defvar epa-dired-map
5   (let ((keymap (make-sparse-keymap)))
6     (define-key keymap "d" 'epa-dired-do-decrypt)
7     (define-key keymap "v" 'epa-dired-do-verify)
8     (define-key keymap "s" 'epa-dired-do-sign)
9     (define-key keymap "e" 'epa-dired-do-encrypt)
10     keymap))
11
12 (fset 'epa-dired-prefix epa-dired-map)
13
14 (defun epa-dired-setup ()
15   (add-hook 'dired-mode-hook 'epa-dired-mode-hook))
16
17 (defun epa-dired-mode-hook ()
18   (define-key dired-mode-map ":" 'epa-dired-prefix))
19
20 (defun epa-dired-decrypt-file (file)
21   (let* ((default-name (file-name-sans-extension file))
22          (plain (read-file-name
23                  (concat "To file (default "
24                          (file-name-nondirectory default-name)
25                          ") ")
26                  (file-name-directory default-name)
27                  default-name))
28          (context (epg-make-context)))
29     (message "Decrypting %s..." (file-name-nondirectory file))
30     (epg-decrypt-file context file plain)
31     (message "Decrypting %s...done" (file-name-nondirectory file))))
32
33 (defun epa-dired-do-decrypt ()
34   (interactive)
35   (let ((file-list (dired-get-marked-files)))
36     (while file-list
37       (epa-dired-decrypt-file (car file-list))
38       (setq file-list (cdr file-list)))))
39
40 (defun epa-dired-verify-file (file)
41   (let* ((context (epg-make-context))
42          (plain (if (equal (file-name-extension file) "sig")
43                     (file-name-sans-extension file))))
44     (message "Verifying %s..." (file-name-nondirectory file))
45     (epg-verify-file context file plain)
46     (message "Verifying %s...done" (file-name-nondirectory file))))
47
48 (defun epa-dired-do-verify ()
49   (interactive)
50   (let ((file-list (dired-get-marked-files)))
51     (while file-list
52       (epa-dired-verify-file (car file-list))
53       (setq file-list (cdr file-list)))))
54
55 (defun epa-dired-sign-file (file detached)
56   (let ((signature (concat file (if detached ".sig" ".gpg")))
57         (context (epg-make-context)))
58     (message "Signing %s..." (file-name-nondirectory file))
59     (epg-sign-file context file signature)
60     (message "Signing %s...done" (file-name-nondirectory file))))
61
62 (defun epa-dired-do-sign (&optional detached)
63   (interactive "p")
64   (let ((file-list (dired-get-marked-files)))
65     (while file-list
66       (epa-dired-sign-file (expand-file-name (car file-list)) detached)
67       (setq file-list (cdr file-list)))
68     (revert-buffer)))
69
70 (defun epa-dired-encrypt-file (keys file)
71   (let ((cipher (concat file ".gpg"))
72         (context (epg-make-context)))
73     (message "Encrypting %s..." (file-name-nondirectory file))
74     (epg-encrypt-file context
75                       file
76                       (mapcar (lambda (key)
77                                 (epg-sub-key-id
78                                  (car (epg-key-sub-key-list key))))
79                               keys)
80                       cipher)
81     (message "Encrypting %s...done" (file-name-nondirectory file))))
82
83 (defun epa-dired-do-encrypt ()
84   (interactive)
85   (let ((file-list (dired-get-marked-files)))
86     (while file-list
87       (epa-ask-keys "Select recipents for encryption.
88 If no one is selected, symmetric encryption will be performed.  "
89                     #'epa-dired-encrypt-file nil
90                     (expand-file-name (car file-list)))
91       (setq file-list (cdr file-list)))))
92
93 (provide 'epa-dired)
94
95 ;;; epa-dired.el ends here