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/>.
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
)
43 (defvar epa-mail-mode-hook nil
)
44 (defvar epa-mail-mode-on-hook nil
)
45 (defvar epa-mail-mode-off-hook nil
)
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."
56 (let ((pointer (epg-key-sub-key-list (car keys
))))
58 (if (and (memq usage
(epg-sub-key-capability (car pointer
)))
59 (not (memq (epg-sub-key-validity (car pointer
))
61 (throw 'found
(car keys
)))
62 (setq pointer
(cdr pointer
))))
63 (setq keys
(cdr keys
)))))
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!"
72 (epa-decrypt-armor-in-region (point-min) (point-max)))
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!"
81 (epa-verify-cleartext-in-region (point-min) (point-max)))
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!"
91 (goto-char (point-min))
92 (if (search-forward mail-header-separator nil t
)
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)
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. "
105 (epa--read-signature-type)
107 (epa-sign-region start end signers mode
))
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!"
117 (let ((verbose current-prefix-arg
)
118 (context (epg-make-context epa-protocol
))
119 recipients recipient-key
)
120 (goto-char (point-min))
122 (narrow-to-region (point)
123 (if (search-forward mail-header-separator nil
0)
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
))
134 (setq recipients
(delete ""
135 (split-string recipients
"[ \t\n]+"))))
136 (goto-char (point-min))
137 (if (search-forward mail-header-separator nil t
)
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)
146 "Select recipients for encryption.
147 If no one is selected, symmetric encryption will be performed. "
153 (epa-mail--find-usable-key
155 (epg-make-context epa-protocol
)
156 (concat "<" recipient
">"))
158 (unless (or recipient-key
161 "No public key for %s; skip it? "
163 (error "No public key for %s" recipient
))
166 (setq sign
(if verbose
(y-or-n-p "Sign? ")))
168 (epa-select-keys context
169 "Select keys for signing. "))))))
170 (epa-encrypt-region start end recipients sign signers
))
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!"
179 (epa-import-armor-in-region (point-min) (point-max)))
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
)))
191 ;; arch-tag: a6f82b3f-d177-4a11-af95-040da55927d2
192 ;;; epa-mail.el ends here