Fix last change on rcirc-print and rcirc-decode-coding-system
[emacs.git] / lisp / gnus / mml1991.el
bloba5d778845c101832fef5fc81df01d17373095a08
1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998-2011 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 <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-and-compile
29 ;; For Emacs <22.2 and XEmacs.
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
32 (if (locate-library "password-cache")
33 (require 'password-cache)
34 (require 'password)))
36 (eval-when-compile
37 (require 'cl)
38 (require 'mm-util))
40 (require 'mm-encode)
41 (require 'mml-sec)
43 (defvar mc-pgp-always-sign)
45 (autoload 'quoted-printable-decode-region "qp")
46 (autoload 'quoted-printable-encode-region "qp")
48 (autoload 'mm-decode-content-transfer-encoding "mm-bodies")
49 (autoload 'mm-encode-content-transfer-encoding "mm-bodies")
50 (autoload 'message-options-get "message")
51 (autoload 'message-options-set "message")
53 (defvar mml1991-use mml2015-use
54 "The package used for PGP.")
56 (defvar mml1991-function-alist
57 '((mailcrypt mml1991-mailcrypt-sign
58 mml1991-mailcrypt-encrypt)
59 (pgg mml1991-pgg-sign
60 mml1991-pgg-encrypt)
61 (epg mml1991-epg-sign
62 mml1991-epg-encrypt))
63 "Alist of PGP functions.")
65 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
66 "If t, cache passphrase.")
68 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
69 "How many seconds the passphrase is cached.
70 Whether the passphrase is cached at all is controlled by
71 `mml1991-cache-passphrase'.")
73 (defvar mml1991-signers nil
74 "A list of your own key ID which will be used to sign a message.")
76 (defvar mml1991-encrypt-to-self nil
77 "If t, add your own key ID to recipient list when encryption.")
79 ;;; mailcrypt wrapper
81 (autoload 'mc-sign-generic "mc-toplev")
83 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
84 (defvar mml1991-verify-function 'mailcrypt-verify)
86 (defun mml1991-mailcrypt-sign (cont)
87 (let ((text (current-buffer))
88 headers signature
89 (result-buffer (get-buffer-create "*GPG Result*")))
90 ;; Save MIME Content[^ ]+: headers from signing
91 (goto-char (point-min))
92 (while (looking-at "^Content[^ ]+:") (forward-line))
93 (unless (bobp)
94 (setq headers (buffer-string))
95 (delete-region (point-min) (point)))
96 (goto-char (point-max))
97 (unless (bolp)
98 (insert "\n"))
99 (quoted-printable-decode-region (point-min) (point-max))
100 (with-temp-buffer
101 (setq signature (current-buffer))
102 (insert-buffer-substring text)
103 (unless (mc-sign-generic (message-options-get 'message-sender)
104 nil nil nil nil)
105 (unless (> (point-max) (point-min))
106 (pop-to-buffer result-buffer)
107 (error "Sign error")))
108 (goto-char (point-min))
109 (while (re-search-forward "\r+$" nil t)
110 (replace-match "" t t))
111 (quoted-printable-encode-region (point-min) (point-max))
112 (set-buffer text)
113 (delete-region (point-min) (point-max))
114 (if headers (insert headers))
115 (insert "\n")
116 (insert-buffer-substring signature)
117 (goto-char (point-max)))))
119 (declare-function mc-encrypt-generic "ext:mc-toplev"
120 (&optional recipients scheme start end from sign))
122 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
123 (let ((text (current-buffer))
124 (mc-pgp-always-sign
125 (or mc-pgp-always-sign
126 sign
127 (eq t (or (message-options-get 'message-sign-encrypt)
128 (message-options-set
129 'message-sign-encrypt
130 (or (y-or-n-p "Sign the message? ")
131 'not))))
132 'never))
133 cipher
134 (result-buffer (get-buffer-create "*GPG Result*")))
135 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
136 (goto-char (point-min))
137 (while (looking-at "^Content[^ ]+:") (forward-line))
138 (unless (bobp)
139 (delete-region (point-min) (point)))
140 (with-temp-buffer
141 (inline (mm-disable-multibyte))
142 (setq cipher (current-buffer))
143 (insert-buffer-substring text)
144 (unless (mc-encrypt-generic
146 (message-options-get 'message-recipients)
147 (message-options-set 'message-recipients
148 (read-string "Recipients: ")))
150 (point-min) (point-max)
151 (message-options-get 'message-sender)
152 'sign)
153 (unless (> (point-max) (point-min))
154 (pop-to-buffer result-buffer)
155 (error "Encrypt error")))
156 (goto-char (point-min))
157 (while (re-search-forward "\r+$" nil t)
158 (replace-match "" t t))
159 (set-buffer text)
160 (delete-region (point-min) (point-max))
161 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
162 ;;(insert "Version: 1\n\n")
163 (insert "\n")
164 (insert-buffer-substring cipher)
165 (goto-char (point-max)))))
167 ;; pgg wrapper
169 (autoload 'pgg-sign-region "pgg")
170 (autoload 'pgg-encrypt-region "pgg")
172 (defvar pgg-default-user-id)
173 (defvar pgg-errors-buffer)
174 (defvar pgg-output-buffer)
176 (defun mml1991-pgg-sign (cont)
177 (let ((pgg-text-mode t)
178 (pgg-default-user-id (or (message-options-get 'mml-sender)
179 pgg-default-user-id))
180 headers cte)
181 ;; Don't sign headers.
182 (goto-char (point-min))
183 (when (re-search-forward "^$" nil t)
184 (setq headers (buffer-substring (point-min) (point)))
185 (save-restriction
186 (narrow-to-region (point-min) (point))
187 (setq cte (mail-fetch-field "content-transfer-encoding")))
188 (forward-line 1)
189 (delete-region (point-min) (point))
190 (when cte
191 (setq cte (intern (downcase cte)))
192 (mm-decode-content-transfer-encoding cte)))
193 (unless (pgg-sign-region (point-min) (point-max) t)
194 (pop-to-buffer pgg-errors-buffer)
195 (error "Encrypt error"))
196 (delete-region (point-min) (point-max))
197 (mm-with-unibyte-current-buffer
198 (insert-buffer-substring pgg-output-buffer)
199 (goto-char (point-min))
200 (while (re-search-forward "\r+$" nil t)
201 (replace-match "" t t))
202 (when cte
203 (mm-encode-content-transfer-encoding cte))
204 (goto-char (point-min))
205 (when headers
206 (insert headers))
207 (insert "\n"))
210 (defun mml1991-pgg-encrypt (cont &optional sign)
211 (goto-char (point-min))
212 (when (re-search-forward "^$" nil t)
213 (let ((cte (save-restriction
214 (narrow-to-region (point-min) (point))
215 (mail-fetch-field "content-transfer-encoding"))))
216 ;; Strip MIME headers since it will be ASCII armored.
217 (forward-line 1)
218 (delete-region (point-min) (point))
219 (when cte
220 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
221 (unless (let ((pgg-text-mode t))
222 (pgg-encrypt-region
223 (point-min) (point-max)
224 (split-string
226 (message-options-get 'message-recipients)
227 (message-options-set 'message-recipients
228 (read-string "Recipients: ")))
229 "[ \f\t\n\r\v,]+")
230 sign))
231 (pop-to-buffer pgg-errors-buffer)
232 (error "Encrypt error"))
233 (delete-region (point-min) (point-max))
234 (insert "\n")
235 (insert-buffer-substring pgg-output-buffer)
238 ;; epg wrapper
240 (defvar epg-user-id-alist)
242 (autoload 'epg-make-context "epg")
243 (autoload 'epg-passphrase-callback-function "epg")
244 (autoload 'epa-select-keys "epa")
245 (autoload 'epg-list-keys "epg")
246 (autoload 'epg-context-set-armor "epg")
247 (autoload 'epg-context-set-textmode "epg")
248 (autoload 'epg-context-set-signers "epg")
249 (autoload 'epg-context-set-passphrase-callback "epg")
250 (autoload 'epg-sign-string "epg")
251 (autoload 'epg-encrypt-string "epg")
252 (autoload 'epg-configuration "epg-config")
253 (autoload 'epg-expand-group "epg-config")
255 (defvar mml1991-epg-secret-key-id-list nil)
257 (defun mml1991-epg-passphrase-callback (context key-id ignore)
258 (if (eq key-id 'SYM)
259 (epg-passphrase-callback-function context key-id nil)
260 (let* ((entry (assoc key-id epg-user-id-alist))
261 (passphrase
262 (password-read
263 (format "GnuPG passphrase for %s: "
264 (if entry
265 (cdr entry)
266 key-id))
267 (if (eq key-id 'PIN)
268 "PIN"
269 key-id))))
270 (when passphrase
271 (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
272 (password-cache-add key-id passphrase))
273 (setq mml1991-epg-secret-key-id-list
274 (cons key-id mml1991-epg-secret-key-id-list))
275 (copy-sequence passphrase)))))
277 (defun mml1991-epg-sign (cont)
278 (let ((context (epg-make-context))
279 headers cte signers signature)
280 (if (eq mm-sign-option 'guided)
281 (setq signers (epa-select-keys context "Select keys for signing.
282 If no one is selected, default secret key is used. "
283 mml1991-signers t))
284 (if mml1991-signers
285 (setq signers (mapcar (lambda (name)
286 (car (epg-list-keys context name t)))
287 mml1991-signers))))
288 (epg-context-set-armor context t)
289 (epg-context-set-textmode context t)
290 (epg-context-set-signers context signers)
291 (if mml1991-cache-passphrase
292 (epg-context-set-passphrase-callback
293 context
294 #'mml1991-epg-passphrase-callback))
295 ;; Don't sign headers.
296 (goto-char (point-min))
297 (when (re-search-forward "^$" nil t)
298 (setq headers (buffer-substring (point-min) (point)))
299 (save-restriction
300 (narrow-to-region (point-min) (point))
301 (setq cte (mail-fetch-field "content-transfer-encoding")))
302 (forward-line 1)
303 (delete-region (point-min) (point))
304 (when cte
305 (setq cte (intern (downcase cte)))
306 (mm-decode-content-transfer-encoding cte)))
307 (condition-case error
308 (setq signature (epg-sign-string context (buffer-string) 'clear)
309 mml1991-epg-secret-key-id-list nil)
310 (error
311 (while mml1991-epg-secret-key-id-list
312 (password-cache-remove (car mml1991-epg-secret-key-id-list))
313 (setq mml1991-epg-secret-key-id-list
314 (cdr mml1991-epg-secret-key-id-list)))
315 (signal (car error) (cdr error))))
316 (delete-region (point-min) (point-max))
317 (mm-with-unibyte-current-buffer
318 (insert signature)
319 (goto-char (point-min))
320 (while (re-search-forward "\r+$" nil t)
321 (replace-match "" t t))
322 (when cte
323 (mm-encode-content-transfer-encoding cte))
324 (goto-char (point-min))
325 (when headers
326 (insert headers))
327 (insert "\n"))
330 (defun mml1991-epg-encrypt (cont &optional sign)
331 (goto-char (point-min))
332 (when (re-search-forward "^$" nil t)
333 (let ((cte (save-restriction
334 (narrow-to-region (point-min) (point))
335 (mail-fetch-field "content-transfer-encoding"))))
336 ;; Strip MIME headers since it will be ASCII armored.
337 (forward-line 1)
338 (delete-region (point-min) (point))
339 (when cte
340 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
341 (let ((context (epg-make-context))
342 (recipients
343 (if (message-options-get 'message-recipients)
344 (split-string
345 (message-options-get 'message-recipients)
346 "[ \f\t\n\r\v,]+")))
347 cipher signers config)
348 ;; We should remove this check if epg-0.0.6 is released.
349 (if (and (condition-case nil
350 (require 'epg-config)
351 (error))
352 (functionp #'epg-expand-group))
353 (setq config (epg-configuration)
354 recipients
355 (apply #'nconc
356 (mapcar (lambda (recipient)
357 (or (epg-expand-group config recipient)
358 (list recipient)))
359 recipients))))
360 (if (eq mm-encrypt-option 'guided)
361 (setq recipients
362 (epa-select-keys context "Select recipients for encryption.
363 If no one is selected, symmetric encryption will be performed. "
364 recipients))
365 (setq recipients
366 (delq nil (mapcar (lambda (name)
367 (car (epg-list-keys context name)))
368 recipients))))
369 (if mml1991-encrypt-to-self
370 (if mml1991-signers
371 (setq recipients
372 (nconc recipients
373 (mapcar (lambda (name)
374 (car (epg-list-keys context name)))
375 mml1991-signers)))
376 (error "mml1991-signers not set")))
377 (when sign
378 (if (eq mm-sign-option 'guided)
379 (setq signers (epa-select-keys context "Select keys for signing.
380 If no one is selected, default secret key is used. "
381 mml1991-signers t))
382 (if mml1991-signers
383 (setq signers (mapcar (lambda (name)
384 (car (epg-list-keys context name t)))
385 mml1991-signers))))
386 (epg-context-set-signers context signers))
387 (epg-context-set-armor context t)
388 (epg-context-set-textmode context t)
389 (if mml1991-cache-passphrase
390 (epg-context-set-passphrase-callback
391 context
392 #'mml1991-epg-passphrase-callback))
393 (condition-case error
394 (setq cipher
395 (epg-encrypt-string context (buffer-string) recipients sign)
396 mml1991-epg-secret-key-id-list nil)
397 (error
398 (while mml1991-epg-secret-key-id-list
399 (password-cache-remove (car mml1991-epg-secret-key-id-list))
400 (setq mml1991-epg-secret-key-id-list
401 (cdr mml1991-epg-secret-key-id-list)))
402 (signal (car error) (cdr error))))
403 (delete-region (point-min) (point-max))
404 (insert "\n" cipher))
407 ;;;###autoload
408 (defun mml1991-encrypt (cont &optional sign)
409 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
410 (if func
411 (funcall func cont sign)
412 (error "Cannot find encrypt function"))))
414 ;;;###autoload
415 (defun mml1991-sign (cont)
416 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
417 (if func
418 (funcall func cont)
419 (error "Cannot find sign function"))))
421 (provide 'mml1991)
423 ;; Local Variables:
424 ;; coding: iso-8859-1
425 ;; End:
427 ;;; mml1991.el ends here