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 (define-minor-mode epa-mail-mode
40 "A minor-mode for composing encrypted/clearsigned mails."
41 nil " epa-mail" epa-mail-mode-map)
44 (defun epa-mail-decrypt ()
45 "Decrypt OpenPGP armors in the current buffer.
46 The buffer is expected to contain a mail message.
48 Don't use this command in Lisp programs!"
50 (epa-decrypt-armor-in-region (point-min) (point-max)))
53 (defun epa-mail-verify ()
54 "Verify OpenPGP cleartext signed messages in the current buffer.
55 The buffer is expected to contain a mail message.
57 Don't use this command in Lisp programs!"
59 (epa-verify-cleartext-in-region (point-min) (point-max)))
62 (defun epa-mail-sign (start end signers mode)
63 "Sign the current buffer.
64 The buffer is expected to contain a mail message.
66 Don't use this command in Lisp programs!"
69 (goto-char (point-min))
70 (if (search-forward mail-header-separator nil t)
72 (setq epa-last-coding-system-specified
73 (or coding-system-for-write
74 (epa--select-safe-coding-system (point) (point-max))))
75 (let ((verbose current-prefix-arg))
76 (list (point) (point-max)
78 (epa-select-keys (epg-make-context epa-protocol)
79 "Select keys for signing.
80 If no one is selected, default secret key is used. "
83 (epa--read-signature-type)
85 (epa-sign-region start end signers mode))
88 (defun epa-mail-encrypt (start end recipients sign signers)
89 "Encrypt the current buffer.
90 The buffer is expected to contain a mail message.
92 Don't use this command in Lisp programs!"
95 (let ((verbose current-prefix-arg)
96 (context (epg-make-context epa-protocol))
97 recipients recipient-keys)
98 (goto-char (point-min))
100 (narrow-to-region (point)
101 (if (search-forward mail-header-separator nil 0)
105 (mail-strip-quoted-names
106 (mapconcat #'identity
107 (nconc (mail-fetch-field "to" nil nil t)
108 (mail-fetch-field "cc" nil nil t)
109 (mail-fetch-field "bcc" nil nil t))
112 (setq recipients (delete ""
113 (split-string recipients "[ \t\n]+"))))
114 (goto-char (point-min))
115 (if (search-forward mail-header-separator nil t)
117 (setq epa-last-coding-system-specified
118 (or coding-system-for-write
119 (epa--select-safe-coding-system (point) (point-max))))
120 (list (point) (point-max)
124 "Select recipients for encryption.
125 If no one is selected, symmetric encryption will be performed. "
133 (epg-make-context epa-protocol)
134 (concat "<" recipient ">")))
135 (unless (or recipient-keys
138 "No public key for %s; skip it? "
140 (error "No public key for %s" recipient))
143 (setq sign (if verbose (y-or-n-p "Sign? ")))
145 (epa-select-keys context
146 "Select keys for signing. "))))))
147 (epa-encrypt-region start end recipients sign signers))
150 (defun epa-mail-import-keys ()
151 "Import keys in the OpenPGP armor format in the current buffer.
152 The buffer is expected to contain a mail message.
154 Don't use this command in Lisp programs!"
156 (epa-import-armor-in-region (point-min) (point-max)))
160 ;;; epa-mail.el ends here