0b0e099b83cb9823f85aee374122af589eb38564
[elisp/epg.git] / epa-mail.el
1 ;;; epa-mail.el --- the EasyPG Assistant, minor-mode for mail composer
2 ;; Copyright (C) 2006,2007 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG, mail, message
6
7 ;; This file is part of EasyPG.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Code:
25
26 (require 'epa)
27 (require 'mail-utils)
28
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)
37     keymap))
38
39 (define-minor-mode epa-mail-mode
40   "A minor-mode for composing encrypted/clearsigned mails."
41   nil " epa-mail" epa-mail-mode-map)
42
43 ;;;###autoload
44 (defun epa-mail-decrypt ()
45   "Decrypt OpenPGP armors in the current buffer.
46 The buffer is expected to contain a mail message.
47
48 Don't use this command in Lisp programs!"
49   (interactive)
50   (epa-decrypt-armor-in-region (point-min) (point-max)))
51
52 ;;;###autoload
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.
56
57 Don't use this command in Lisp programs!"
58   (interactive)
59   (epa-verify-cleartext-in-region (point-min) (point-max)))
60
61 ;;;###autoload
62 (defun epa-mail-sign (start end signers mode)
63   "Sign the current buffer.
64 The buffer is expected to contain a mail message.
65
66 Don't use this command in Lisp programs!"
67   (interactive
68    (save-excursion
69      (goto-char (point-min))
70      (if (search-forward mail-header-separator nil t)
71          (forward-line))
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)
77              (if verbose
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.  "
81                                   nil t))
82              (if verbose
83                  (epa--read-signature-type)
84                'clear)))))
85   (epa-sign-region start end signers mode))
86
87 ;;;###autoload
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.
91
92 Don't use this command in Lisp programs!"
93   (interactive
94    (save-excursion
95      (let ((verbose current-prefix-arg)
96            (context (epg-make-context epa-protocol))
97            recipients recipient-keys)
98        (goto-char (point-min))
99        (save-restriction
100          (narrow-to-region (point)
101                            (if (search-forward mail-header-separator nil 0)
102                                (match-beginning 0)
103                              (point)))
104          (setq recipients
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))
110                            ","))))
111        (if recipients
112            (setq recipients (delete ""
113                                     (split-string recipients "[ \t\n]+"))))
114        (goto-char (point-min))
115        (if (search-forward mail-header-separator nil t)
116            (forward-line))
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)
121              (if verbose
122                  (epa-select-keys
123                   context
124                   "Select recipients for encryption.
125 If no one is selected, symmetric encryption will be performed.  "
126                   recipients)
127                (if recipients
128                    (apply #'nconc
129                           (mapcar
130                            (lambda (recipient)
131                              (setq recipient-keys
132                                    (epg-list-keys
133                                     (epg-make-context epa-protocol)
134                                     (concat "<" recipient ">")))
135                              (unless (or recipient-keys
136                                          (y-or-n-p
137                                           (format
138                                            "No public key for %s; skip it? "
139                                            recipient)))
140                                (error "No public key for %s" recipient))
141                              recipient-keys)
142                            recipients))))
143              (setq sign (if verbose (y-or-n-p "Sign? ")))
144              (if sign
145                  (epa-select-keys context
146                                   "Select keys for signing.  "))))))
147   (epa-encrypt-region start end recipients sign signers))
148
149 ;;;###autoload
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.
153
154 Don't use this command in Lisp programs!"
155   (interactive)
156   (epa-import-armor-in-region (point-min) (point-max)))
157
158 (provide 'epa-mail)
159
160 ;;; epa-mail.el ends here