Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / gnus / mml1991.el
blobb2056b2fd0d04f0482e688d308a12852d0dbb2d0
1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998-2018 Free Software Foundation, Inc.
5 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
6 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
7 ;; Keywords: PGP
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile (require 'mm-util))
30 (require 'mm-encode)
31 (require 'mml-sec)
33 (defvar mc-pgp-always-sign)
35 (autoload 'quoted-printable-decode-region "qp")
36 (autoload 'quoted-printable-encode-region "qp")
38 (autoload 'mm-decode-content-transfer-encoding "mm-bodies")
39 (autoload 'mm-encode-content-transfer-encoding "mm-bodies")
40 (autoload 'message-options-get "message")
41 (autoload 'message-options-set "message")
43 (require 'mml2015)
45 (defvar mml1991-use mml2015-use
46 "The package used for PGP.")
48 (defvar mml1991-function-alist
49 '((mailcrypt mml1991-mailcrypt-sign
50 mml1991-mailcrypt-encrypt)
51 (pgg mml1991-pgg-sign
52 mml1991-pgg-encrypt)
53 (epg mml1991-epg-sign
54 mml1991-epg-encrypt))
55 "Alist of PGP functions.")
57 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
58 "If t, cache passphrase.")
59 (make-obsolete-variable 'mml1991-cache-passphrase
60 'mml-secure-cache-passphrase
61 "25.1")
63 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
64 "How many seconds the passphrase is cached.
65 Whether the passphrase is cached at all is controlled by
66 `mml1991-cache-passphrase'.")
67 (make-obsolete-variable 'mml1991-passphrase-cache-expiry
68 'mml-secure-passphrase-cache-expiry
69 "25.1")
71 (defvar mml1991-signers nil
72 "A list of your own key ID which will be used to sign a message.")
74 (defvar mml1991-encrypt-to-self nil
75 "If t, add your own key ID to recipient list when encryption.")
78 ;;; mailcrypt wrapper
80 (autoload 'mc-sign-generic "mc-toplev")
82 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
83 (defvar mml1991-verify-function 'mailcrypt-verify)
85 (defun mml1991-mailcrypt-sign (cont)
86 (let ((text (current-buffer))
87 headers signature
88 (result-buffer (get-buffer-create "*GPG Result*")))
89 ;; Save MIME Content[^ ]+: headers from signing
90 (goto-char (point-min))
91 (while (looking-at "^Content[^ ]+:") (forward-line))
92 (unless (bobp)
93 (setq headers (buffer-string))
94 (delete-region (point-min) (point)))
95 (goto-char (point-max))
96 (unless (bolp)
97 (insert "\n"))
98 (quoted-printable-decode-region (point-min) (point-max))
99 (with-temp-buffer
100 (setq signature (current-buffer))
101 (insert-buffer-substring text)
102 (unless (mc-sign-generic (message-options-get 'message-sender)
103 nil nil nil nil)
104 (unless (> (point-max) (point-min))
105 (pop-to-buffer result-buffer)
106 (error "Sign error")))
107 (goto-char (point-min))
108 (while (re-search-forward "\r+$" nil t)
109 (replace-match "" t t))
110 (quoted-printable-encode-region (point-min) (point-max))
111 (set-buffer text)
112 (delete-region (point-min) (point-max))
113 (if headers (insert headers))
114 (insert "\n")
115 (insert-buffer-substring signature)
116 (goto-char (point-max)))))
118 (declare-function mc-encrypt-generic "ext:mc-toplev"
119 (&optional recipients scheme start end from sign))
121 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
122 (let ((text (current-buffer))
123 (mc-pgp-always-sign
124 (or mc-pgp-always-sign
125 sign
126 (eq t (or (message-options-get 'message-sign-encrypt)
127 (message-options-set
128 'message-sign-encrypt
129 (or (y-or-n-p "Sign the message? ")
130 'not))))
131 'never))
132 cipher
133 (result-buffer (get-buffer-create "*GPG Result*")))
134 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
135 (goto-char (point-min))
136 (while (looking-at "^Content[^ ]+:") (forward-line))
137 (unless (bobp)
138 (delete-region (point-min) (point)))
139 (with-temp-buffer
140 (inline (mm-disable-multibyte))
141 (setq cipher (current-buffer))
142 (insert-buffer-substring text)
143 (unless (mc-encrypt-generic
145 (message-options-get 'message-recipients)
146 (message-options-set 'message-recipients
147 (read-string "Recipients: ")))
149 (point-min) (point-max)
150 (message-options-get 'message-sender)
151 'sign)
152 (unless (> (point-max) (point-min))
153 (pop-to-buffer result-buffer)
154 (error "Encrypt error")))
155 (goto-char (point-min))
156 (while (re-search-forward "\r+$" nil t)
157 (replace-match "" t t))
158 (set-buffer text)
159 (delete-region (point-min) (point-max))
160 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
161 ;;(insert "Version: 1\n\n")
162 (insert "\n")
163 (insert-buffer-substring cipher)
164 (goto-char (point-max)))))
166 ;; pgg wrapper
168 (autoload 'pgg-sign-region "pgg")
169 (autoload 'pgg-encrypt-region "pgg")
171 (defvar pgg-default-user-id)
172 (defvar pgg-errors-buffer)
173 (defvar pgg-output-buffer)
175 (defun mml1991-pgg-sign (cont)
176 (let ((pgg-text-mode t)
177 (pgg-default-user-id (or (message-options-get 'mml-sender)
178 pgg-default-user-id))
179 headers cte)
180 ;; Don't sign headers.
181 (goto-char (point-min))
182 (when (re-search-forward "^$" nil t)
183 (setq headers (buffer-substring (point-min) (point)))
184 (save-restriction
185 (narrow-to-region (point-min) (point))
186 (setq cte (mail-fetch-field "content-transfer-encoding")))
187 (forward-line 1)
188 (delete-region (point-min) (point))
189 (when cte
190 (setq cte (intern (downcase cte)))
191 (mm-decode-content-transfer-encoding cte)))
192 (unless (pgg-sign-region (point-min) (point-max) t)
193 (pop-to-buffer pgg-errors-buffer)
194 (error "Encrypt error"))
195 (delete-region (point-min) (point-max))
196 (insert
197 (with-temp-buffer
198 (set-buffer-multibyte nil)
199 (insert-buffer-substring pgg-output-buffer)
200 (goto-char (point-min))
201 (while (re-search-forward "\r+$" nil t)
202 (replace-match "" t t))
203 (when cte
204 (mm-encode-content-transfer-encoding cte))
205 (goto-char (point-min))
206 (when headers
207 (insert headers))
208 (insert "\n")
209 (buffer-string)))
212 (defun mml1991-pgg-encrypt (cont &optional sign)
213 (goto-char (point-min))
214 (when (re-search-forward "^$" nil t)
215 (let ((cte (save-restriction
216 (narrow-to-region (point-min) (point))
217 (mail-fetch-field "content-transfer-encoding"))))
218 ;; Strip MIME headers since it will be ASCII armored.
219 (forward-line 1)
220 (delete-region (point-min) (point))
221 (when cte
222 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
223 (unless (let ((pgg-text-mode t))
224 (pgg-encrypt-region
225 (point-min) (point-max)
226 (split-string
228 (message-options-get 'message-recipients)
229 (message-options-set 'message-recipients
230 (read-string "Recipients: ")))
231 "[ \f\t\n\r\v,]+")
232 sign))
233 (pop-to-buffer pgg-errors-buffer)
234 (error "Encrypt error"))
235 (delete-region (point-min) (point-max))
236 (insert "\n")
237 (insert-buffer-substring pgg-output-buffer)
240 ;; epg wrapper
242 (defvar epg-user-id-alist)
244 (autoload 'epg-make-context "epg")
245 (autoload 'epg-passphrase-callback-function "epg")
246 (autoload 'epa-select-keys "epa")
247 (autoload 'epg-list-keys "epg")
248 (autoload 'epg-context-set-armor "epg")
249 (autoload 'epg-context-set-textmode "epg")
250 (autoload 'epg-context-set-signers "epg")
251 (autoload 'epg-context-set-passphrase-callback "epg")
252 (autoload 'epg-key-sub-key-list "epg")
253 (autoload 'epg-sub-key-capability "epg")
254 (autoload 'epg-sub-key-validity "epg")
255 (autoload 'epg-sub-key-fingerprint "epg")
256 (autoload 'epg-sign-string "epg")
257 (autoload 'epg-encrypt-string "epg")
258 (autoload 'epg-configuration "epg-config")
259 (autoload 'epg-expand-group "epg-config")
261 (defun mml1991-epg-sign (cont)
262 (let ((inhibit-redisplay t)
263 headers cte)
264 ;; Don't sign headers.
265 (goto-char (point-min))
266 (when (re-search-forward "^$" nil t)
267 (setq headers (buffer-substring (point-min) (point)))
268 (save-restriction
269 (narrow-to-region (point-min) (point))
270 (setq cte (mail-fetch-field "content-transfer-encoding")))
271 (forward-line 1)
272 (delete-region (point-min) (point))
273 (when cte
274 (setq cte (intern (downcase cte)))
275 (mm-decode-content-transfer-encoding cte)))
276 (let* ((pair (mml-secure-epg-sign 'OpenPGP 'clear))
277 (signature (car pair)))
278 (unless (stringp signature)
279 (error "Signature failed"))
280 (delete-region (point-min) (point-max))
281 (insert
282 (with-temp-buffer
283 (set-buffer-multibyte nil)
284 (insert signature)
285 (goto-char (point-min))
286 (while (re-search-forward "\r+$" nil t)
287 (replace-match "" t t))
288 (when cte
289 (mm-encode-content-transfer-encoding cte))
290 (goto-char (point-min))
291 (when headers
292 (insert headers))
293 (insert "\n")
294 (buffer-string)))
295 t)))
297 (defun mml1991-epg-encrypt (cont &optional sign)
298 (goto-char (point-min))
299 (when (re-search-forward "^$" nil t)
300 (let ((cte (save-restriction
301 (narrow-to-region (point-min) (point))
302 (mail-fetch-field "content-transfer-encoding"))))
303 ;; Strip MIME headers since it will be ASCII armored.
304 (forward-line 1)
305 (delete-region (point-min) (point))
306 (when cte
307 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
308 (let ((cipher (mml-secure-epg-encrypt 'OpenPGP cont sign)))
309 (delete-region (point-min) (point-max))
310 (insert "\n" cipher))
313 ;;;###autoload
314 (defun mml1991-encrypt (cont &optional sign)
315 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
316 (if func
317 (funcall func cont sign)
318 (error "Cannot find encrypt function"))))
320 ;;;###autoload
321 (defun mml1991-sign (cont)
322 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
323 (if func
324 (funcall func cont)
325 (error "Cannot find sign function"))))
327 (provide 'mml1991)
329 ;; Local Variables:
330 ;; coding: utf-8
331 ;; End:
333 ;;; mml1991.el ends here