1 ;;; epa-mail.el --- the EasyPG Assistant, minor-mode for mail composer
2 ;; Copyright (C) 2006,2007 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG, mail, message
7 ;; This file is part of EasyPG.
9 ;; This program 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 2, or (at your option)
14 ;; This program 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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
29 (defvar epa-mail-mode-map
30 (let ((keymap (make-sparse-keymap)))
31 (define-key keymap "\C-c\C-ed" 'epa-mail-decrypt)
32 (define-key keymap "\C-c\C-ev" 'epa-mail-verify)
33 (define-key keymap "\C-c\C-es" 'epa-mail-sign)
34 (define-key keymap "\C-c\C-ee" 'epa-mail-encrypt)
35 (define-key keymap "\C-c\C-ei" 'epa-mail-import-keys)
36 (define-key keymap "\C-c\C-eo" 'epa-insert-keys)
39 (defvar epa-mail-mode-hook nil)
40 (defvar epa-mail-mode-on-hook nil)
41 (defvar epa-mail-mode-off-hook nil)
43 (define-minor-mode epa-mail-mode
44 "A minor-mode for composing encrypted/clearsigned mails."
45 nil " epa-mail" epa-mail-mode-map)
48 (defun epa-mail-decrypt ()
49 "Decrypt OpenPGP armors in the current buffer.
50 The buffer is expected to contain a mail message.
52 Don't use this command in Lisp programs!"
54 (epa-decrypt-armor-in-region (point-min) (point-max)))
57 (defun epa-mail-verify ()
58 "Verify OpenPGP cleartext signed messages in the current buffer.
59 The buffer is expected to contain a mail message.
61 Don't use this command in Lisp programs!"
63 (epa-verify-cleartext-in-region (point-min) (point-max)))
66 (defun epa-mail-sign (start end signers mode)
67 "Sign the current buffer.
68 The buffer is expected to contain a mail message.
70 Don't use this command in Lisp programs!"
73 (goto-char (point-min))
74 (if (search-forward mail-header-separator nil t)
76 (setq epa-last-coding-system-specified
77 (or coding-system-for-write
78 (epa--select-safe-coding-system (point) (point-max))))
79 (let ((verbose current-prefix-arg))
80 (list (point) (point-max)
82 (epa-select-keys (epg-make-context epa-protocol)
83 "Select keys for signing.
84 If no one is selected, default secret key is used. "
87 (epa--read-signature-type)
89 (epa-sign-region start end signers mode))
92 (defun epa-mail-encrypt (start end recipients sign signers)
93 "Encrypt the current buffer.
94 The buffer is expected to contain a mail message.
96 Don't use this command in Lisp programs!"
99 (let ((verbose current-prefix-arg)
100 (context (epg-make-context epa-protocol))
101 recipients recipient-keys)
102 (goto-char (point-min))
104 (narrow-to-region (point)
105 (if (search-forward mail-header-separator nil 0)
109 (mail-strip-quoted-names
110 (mapconcat #'identity
111 (nconc (mail-fetch-field "to" nil nil t)
112 (mail-fetch-field "cc" nil nil t)
113 (mail-fetch-field "bcc" nil nil t))
116 (setq recipients (delete ""
117 (split-string recipients "[ \t\n]+"))))
118 (goto-char (point-min))
119 (if (search-forward mail-header-separator nil t)
121 (setq epa-last-coding-system-specified
122 (or coding-system-for-write
123 (epa--select-safe-coding-system (point) (point-max))))
124 (list (point) (point-max)
128 "Select recipients for encryption.
129 If no one is selected, symmetric encryption will be performed. "
137 (epg-make-context epa-protocol)
138 (concat "<" recipient ">")))
139 (unless (or recipient-keys
142 "No public key for %s; skip it? "
144 (error "No public key for %s" recipient))
147 (setq sign (if verbose (y-or-n-p "Sign? ")))
149 (epa-select-keys context
150 "Select keys for signing. "))))))
151 (epa-encrypt-region start end recipients sign signers))
154 (defun epa-mail-import-keys ()
155 "Import keys in the OpenPGP armor format in the current buffer.
156 The buffer is expected to contain a mail message.
158 Don't use this command in Lisp programs!"
160 (epa-import-armor-in-region (point-min) (point-max)))
164 ;;; epa-mail.el ends here