OSDN Git Service

Revert the last change.
[epg/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 (defvar epa-mail-mode-hook nil)
40 (defvar epa-mail-mode-on-hook nil)
41 (defvar epa-mail-mode-off-hook nil)
42
43 (define-minor-mode epa-mail-mode
44   "A minor-mode for composing encrypted/clearsigned mails."
45   nil " epa-mail" epa-mail-mode-map)
46
47 ;;;###autoload
48 (defun epa-mail-decrypt ()
49   "Decrypt OpenPGP armors in the current buffer.
50 The buffer is expected to contain a mail message.
51
52 Don't use this command in Lisp programs!"
53   (interactive)
54   (epa-decrypt-armor-in-region (point-min) (point-max)))
55
56 ;;;###autoload
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.
60
61 Don't use this command in Lisp programs!"
62   (interactive)
63   (epa-verify-cleartext-in-region (point-min) (point-max)))
64
65 ;;;###autoload
66 (defun epa-mail-sign (start end signers mode)
67   "Sign the current buffer.
68 The buffer is expected to contain a mail message.
69
70 Don't use this command in Lisp programs!"
71   (interactive
72    (save-excursion
73      (goto-char (point-min))
74      (if (search-forward mail-header-separator nil t)
75          (forward-line))
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)
81              (if verbose
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.  "
85                                   nil t))
86              (if verbose
87                  (epa--read-signature-type)
88                'clear)))))
89   (epa-sign-region start end signers mode))
90
91 ;;;###autoload
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.
95
96 Don't use this command in Lisp programs!"
97   (interactive
98    (save-excursion
99      (let ((verbose current-prefix-arg)
100            (context (epg-make-context epa-protocol))
101            recipients recipient-keys)
102        (goto-char (point-min))
103        (save-restriction
104          (narrow-to-region (point)
105                            (if (search-forward mail-header-separator nil 0)
106                                (match-beginning 0)
107                              (point)))
108          (setq recipients
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))
114                            ","))))
115        (if recipients
116            (setq recipients (delete ""
117                                     (split-string recipients "[ \t\n]+"))))
118        (goto-char (point-min))
119        (if (search-forward mail-header-separator nil t)
120            (forward-line))
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)
125              (if verbose
126                  (epa-select-keys
127                   context
128                   "Select recipients for encryption.
129 If no one is selected, symmetric encryption will be performed.  "
130                   recipients)
131                (if recipients
132                    (apply #'nconc
133                           (mapcar
134                            (lambda (recipient)
135                              (setq recipient-keys
136                                    (epg-list-keys
137                                     (epg-make-context epa-protocol)
138                                     (concat "<" recipient ">")))
139                              (unless (or recipient-keys
140                                          (y-or-n-p
141                                           (format
142                                            "No public key for %s; skip it? "
143                                            recipient)))
144                                (error "No public key for %s" recipient))
145                              recipient-keys)
146                            recipients))))
147              (setq sign (if verbose (y-or-n-p "Sign? ")))
148              (if sign
149                  (epa-select-keys context
150                                   "Select keys for signing.  "))))))
151   (epa-encrypt-region start end recipients sign signers))
152
153 ;;;###autoload
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.
157
158 Don't use this command in Lisp programs!"
159   (interactive)
160   (epa-import-armor-in-region (point-min) (point-max)))
161
162 (provide 'epa-mail)
163
164 ;;; epa-mail.el ends here