Add alternative key bindings to epa-mail.el.
[emacs.git] / lisp / epa-mail.el
blob69fb6d7d7e86c9a34f2d5f2b54d2d51a3266e2fa
1 ;;; epa-mail.el --- the EasyPG Assistant, minor-mode for mail composer
2 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG, mail, message
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'epa)
25 (require 'mail-utils)
27 (defvar epa-mail-mode-map
28 (let ((keymap (make-sparse-keymap)))
29 (define-key keymap "\C-c\C-ed" 'epa-mail-decrypt)
30 (define-key keymap "\C-c\C-ev" 'epa-mail-verify)
31 (define-key keymap "\C-c\C-es" 'epa-mail-sign)
32 (define-key keymap "\C-c\C-ee" 'epa-mail-encrypt)
33 (define-key keymap "\C-c\C-ei" 'epa-mail-import-keys)
34 (define-key keymap "\C-c\C-eo" 'epa-insert-keys)
35 (define-key keymap "\C-c\C-e\C-d" 'epa-mail-decrypt)
36 (define-key keymap "\C-c\C-e\C-v" 'epa-mail-verify)
37 (define-key keymap "\C-c\C-e\C-s" 'epa-mail-sign)
38 (define-key keymap "\C-c\C-e\C-e" 'epa-mail-encrypt)
39 (define-key keymap "\C-c\C-e\C-i" 'epa-mail-import-keys)
40 (define-key keymap "\C-c\C-e\C-o" 'epa-insert-keys)
41 keymap))
43 (defvar epa-mail-mode-hook nil)
44 (defvar epa-mail-mode-on-hook nil)
45 (defvar epa-mail-mode-off-hook nil)
47 ;;;###autoload
48 (define-minor-mode epa-mail-mode
49 "A minor-mode for composing encrypted/clearsigned mails."
50 nil " epa-mail" epa-mail-mode-map)
52 (defun epa-mail--find-usable-key (keys usage)
53 "Find a usable key from KEYS for USAGE."
54 (catch 'found
55 (while keys
56 (let ((pointer (epg-key-sub-key-list (car keys))))
57 (while pointer
58 (if (and (memq usage (epg-sub-key-capability (car pointer)))
59 (not (memq (epg-sub-key-validity (car pointer))
60 '(revoked expired))))
61 (throw 'found (car keys)))
62 (setq pointer (cdr pointer))))
63 (setq keys (cdr keys)))))
65 ;;;###autoload
66 (defun epa-mail-decrypt ()
67 "Decrypt OpenPGP armors in the current buffer.
68 The buffer is expected to contain a mail message.
70 Don't use this command in Lisp programs!"
71 (interactive)
72 (epa-decrypt-armor-in-region (point-min) (point-max)))
74 ;;;###autoload
75 (defun epa-mail-verify ()
76 "Verify OpenPGP cleartext signed messages in the current buffer.
77 The buffer is expected to contain a mail message.
79 Don't use this command in Lisp programs!"
80 (interactive)
81 (epa-verify-cleartext-in-region (point-min) (point-max)))
83 ;;;###autoload
84 (defun epa-mail-sign (start end signers mode)
85 "Sign the current buffer.
86 The buffer is expected to contain a mail message.
88 Don't use this command in Lisp programs!"
89 (interactive
90 (save-excursion
91 (goto-char (point-min))
92 (if (search-forward mail-header-separator nil t)
93 (forward-line))
94 (setq epa-last-coding-system-specified
95 (or coding-system-for-write
96 (epa--select-safe-coding-system (point) (point-max))))
97 (let ((verbose current-prefix-arg))
98 (list (point) (point-max)
99 (if verbose
100 (epa-select-keys (epg-make-context epa-protocol)
101 "Select keys for signing.
102 If no one is selected, default secret key is used. "
103 nil t))
104 (if verbose
105 (epa--read-signature-type)
106 'clear)))))
107 (epa-sign-region start end signers mode))
109 ;;;###autoload
110 (defun epa-mail-encrypt (start end recipients sign signers)
111 "Encrypt the current buffer.
112 The buffer is expected to contain a mail message.
114 Don't use this command in Lisp programs!"
115 (interactive
116 (save-excursion
117 (let ((verbose current-prefix-arg)
118 (context (epg-make-context epa-protocol))
119 recipients recipient-key)
120 (goto-char (point-min))
121 (save-restriction
122 (narrow-to-region (point)
123 (if (search-forward mail-header-separator nil 0)
124 (match-beginning 0)
125 (point)))
126 (setq recipients
127 (mail-strip-quoted-names
128 (mapconcat #'identity
129 (nconc (mail-fetch-field "to" nil nil t)
130 (mail-fetch-field "cc" nil nil t)
131 (mail-fetch-field "bcc" nil nil t))
132 ","))))
133 (if recipients
134 (setq recipients (delete ""
135 (split-string recipients "[ \t\n]+"))))
136 (goto-char (point-min))
137 (if (search-forward mail-header-separator nil t)
138 (forward-line))
139 (setq epa-last-coding-system-specified
140 (or coding-system-for-write
141 (epa--select-safe-coding-system (point) (point-max))))
142 (list (point) (point-max)
143 (if verbose
144 (epa-select-keys
145 context
146 "Select recipients for encryption.
147 If no one is selected, symmetric encryption will be performed. "
148 recipients)
149 (if recipients
150 (mapcar
151 (lambda (recipient)
152 (setq recipient-key
153 (epa-mail--find-usable-key
154 (epg-list-keys
155 (epg-make-context epa-protocol)
156 (concat "<" recipient ">"))
157 'encrypt))
158 (unless (or recipient-key
159 (y-or-n-p
160 (format
161 "No public key for %s; skip it? "
162 recipient)))
163 (error "No public key for %s" recipient))
164 recipient-key)
165 recipients)))
166 (setq sign (if verbose (y-or-n-p "Sign? ")))
167 (if sign
168 (epa-select-keys context
169 "Select keys for signing. "))))))
170 (epa-encrypt-region start end recipients sign signers))
172 ;;;###autoload
173 (defun epa-mail-import-keys ()
174 "Import keys in the OpenPGP armor format in the current buffer.
175 The buffer is expected to contain a mail message.
177 Don't use this command in Lisp programs!"
178 (interactive)
179 (epa-import-armor-in-region (point-min) (point-max)))
181 ;;;###autoload
182 (define-minor-mode epa-global-mail-mode
183 "Minor mode to hook EasyPG into Mail mode."
184 :global t :init-value nil :group 'epa-mail :version "23.1"
185 (remove-hook 'mail-mode-hook 'epa-mail-mode)
186 (if epa-global-mail-mode
187 (add-hook 'mail-mode-hook 'epa-mail-mode)))
189 (provide 'epa-mail)
191 ;; arch-tag: a6f82b3f-d177-4a11-af95-040da55927d2
192 ;;; epa-mail.el ends here