Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
[bpt/emacs.git] / lisp / gnus / mml1991.el
1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
3
4 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
5 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
6 ;; Keywords PGP
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30 (require 'cl)
31 (require 'mm-util))
32
33 (autoload 'quoted-printable-decode-region "qp")
34 (autoload 'quoted-printable-encode-region "qp")
35
36 (defvar mml1991-use mml2015-use
37 "The package used for PGP.")
38
39 (defvar mml1991-function-alist
40 '((mailcrypt mml1991-mailcrypt-sign
41 mml1991-mailcrypt-encrypt)
42 (gpg mml1991-gpg-sign
43 mml1991-gpg-encrypt)
44 (pgg mml1991-pgg-sign
45 mml1991-pgg-encrypt))
46 "Alist of PGP functions.")
47
48 ;;; mailcrypt wrapper
49
50 (eval-and-compile
51 (autoload 'mc-sign-generic "mc-toplev"))
52
53 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
54 (defvar mml1991-verify-function 'mailcrypt-verify)
55
56 (defun mml1991-mailcrypt-sign (cont)
57 (let ((text (current-buffer))
58 headers signature
59 (result-buffer (get-buffer-create "*GPG Result*")))
60 ;; Save MIME Content[^ ]+: headers from signing
61 (goto-char (point-min))
62 (while (looking-at "^Content[^ ]+:") (forward-line))
63 (unless (bobp)
64 (setq headers (buffer-string))
65 (delete-region (point-min) (point)))
66 (goto-char (point-max))
67 (unless (bolp)
68 (insert "\n"))
69 (quoted-printable-decode-region (point-min) (point-max))
70 (with-temp-buffer
71 (setq signature (current-buffer))
72 (insert-buffer-substring text)
73 (unless (mc-sign-generic (message-options-get 'message-sender)
74 nil nil nil nil)
75 (unless (> (point-max) (point-min))
76 (pop-to-buffer result-buffer)
77 (error "Sign error")))
78 (goto-char (point-min))
79 (while (re-search-forward "\r+$" nil t)
80 (replace-match "" t t))
81 (quoted-printable-encode-region (point-min) (point-max))
82 (set-buffer text)
83 (delete-region (point-min) (point-max))
84 (if headers (insert headers))
85 (insert "\n")
86 (insert-buffer-substring signature)
87 (goto-char (point-max)))))
88
89 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
90 (let ((text (current-buffer))
91 (mc-pgp-always-sign
92 (or mc-pgp-always-sign
93 sign
94 (eq t (or (message-options-get 'message-sign-encrypt)
95 (message-options-set
96 'message-sign-encrypt
97 (or (y-or-n-p "Sign the message? ")
98 'not))))
99 'never))
100 cipher
101 (result-buffer (get-buffer-create "*GPG Result*")))
102 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
103 (goto-char (point-min))
104 (while (looking-at "^Content[^ ]+:") (forward-line))
105 (unless (bobp)
106 (delete-region (point-min) (point)))
107 (mm-with-unibyte-current-buffer
108 (with-temp-buffer
109 (setq cipher (current-buffer))
110 (insert-buffer-substring text)
111 (unless (mc-encrypt-generic
112 (or
113 (message-options-get 'message-recipients)
114 (message-options-set 'message-recipients
115 (read-string "Recipients: ")))
116 nil
117 (point-min) (point-max)
118 (message-options-get 'message-sender)
119 'sign)
120 (unless (> (point-max) (point-min))
121 (pop-to-buffer result-buffer)
122 (error "Encrypt error")))
123 (goto-char (point-min))
124 (while (re-search-forward "\r+$" nil t)
125 (replace-match "" t t))
126 (set-buffer text)
127 (delete-region (point-min) (point-max))
128 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
129 ;;(insert "Version: 1\n\n")
130 (insert "\n")
131 (insert-buffer-substring cipher)
132 (goto-char (point-max))))))
133
134 ;;; gpg wrapper
135
136 (eval-and-compile
137 (autoload 'gpg-sign-cleartext "gpg"))
138
139 (defun mml1991-gpg-sign (cont)
140 (let ((text (current-buffer))
141 headers signature
142 (result-buffer (get-buffer-create "*GPG Result*")))
143 ;; Save MIME Content[^ ]+: headers from signing
144 (goto-char (point-min))
145 (while (looking-at "^Content[^ ]+:") (forward-line))
146 (unless (bobp)
147 (setq headers (buffer-string))
148 (delete-region (point-min) (point)))
149 (goto-char (point-max))
150 (unless (bolp)
151 (insert "\n"))
152 (quoted-printable-decode-region (point-min) (point-max))
153 (with-temp-buffer
154 (unless (gpg-sign-cleartext text (setq signature (current-buffer))
155 result-buffer
156 nil
157 (message-options-get 'message-sender))
158 (unless (> (point-max) (point-min))
159 (pop-to-buffer result-buffer)
160 (error "Sign error")))
161 (goto-char (point-min))
162 (while (re-search-forward "\r+$" nil t)
163 (replace-match "" t t))
164 (quoted-printable-encode-region (point-min) (point-max))
165 (set-buffer text)
166 (delete-region (point-min) (point-max))
167 (if headers (insert headers))
168 (insert "\n")
169 (insert-buffer-substring signature)
170 (goto-char (point-max)))))
171
172 (defun mml1991-gpg-encrypt (cont &optional sign)
173 (let ((text (current-buffer))
174 cipher
175 (result-buffer (get-buffer-create "*GPG Result*")))
176 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
177 (goto-char (point-min))
178 (while (looking-at "^Content[^ ]+:") (forward-line))
179 (unless (bobp)
180 (delete-region (point-min) (point)))
181 (mm-with-unibyte-current-buffer
182 (with-temp-buffer
183 (flet ((gpg-encrypt-func
184 (sign plaintext ciphertext result recipients &optional
185 passphrase sign-with-key armor textmode)
186 (if sign
187 (gpg-sign-encrypt
188 plaintext ciphertext result recipients passphrase
189 sign-with-key armor textmode)
190 (gpg-encrypt
191 plaintext ciphertext result recipients passphrase
192 armor textmode))))
193 (unless (gpg-encrypt-func
194 sign
195 text (setq cipher (current-buffer))
196 result-buffer
197 (split-string
198 (or
199 (message-options-get 'message-recipients)
200 (message-options-set 'message-recipients
201 (read-string "Recipients: ")))
202 "[ \f\t\n\r\v,]+")
203 nil
204 (message-options-get 'message-sender)
205 t t) ; armor & textmode
206 (unless (> (point-max) (point-min))
207 (pop-to-buffer result-buffer)
208 (error "Encrypt error"))))
209 (goto-char (point-min))
210 (while (re-search-forward "\r+$" nil t)
211 (replace-match "" t t))
212 (set-buffer text)
213 (delete-region (point-min) (point-max))
214 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
215 ;;(insert "Version: 1\n\n")
216 (insert "\n")
217 (insert-buffer-substring cipher)
218 (goto-char (point-max))))))
219
220 ;; pgg wrapper
221
222 (defvar pgg-output-buffer)
223 (defvar pgg-errors-buffer)
224
225 (defun mml1991-pgg-sign (cont)
226 (let (headers cte)
227 ;; Don't sign headers.
228 (goto-char (point-min))
229 (while (not (looking-at "^$"))
230 (forward-line))
231 (unless (eobp) ;; no headers?
232 (setq headers (buffer-substring (point-min) (point)))
233 (forward-line) ;; skip header/body separator
234 (delete-region (point-min) (point)))
235 (when (string-match "^Content-Transfer-Encoding: \\(.+\\)" headers)
236 (setq cte (intern (match-string 1 headers))))
237 (mm-decode-content-transfer-encoding cte)
238 (unless (let ((pgg-default-user-id
239 (or (message-options-get 'mml-sender)
240 pgg-default-user-id)))
241 (pgg-sign-region (point-min) (point-max) t))
242 (pop-to-buffer pgg-errors-buffer)
243 (error "Encrypt error"))
244 (delete-region (point-min) (point-max))
245 (mm-with-unibyte-current-buffer
246 (insert-buffer-substring pgg-output-buffer)
247 (goto-char (point-min))
248 (while (re-search-forward "\r+$" nil t)
249 (replace-match "" t t))
250 (mm-encode-content-transfer-encoding cte)
251 (goto-char (point-min))
252 (when headers
253 (insert headers))
254 (insert "\n"))
255 t))
256
257 (defun mml1991-pgg-encrypt (cont &optional sign)
258 (let (cte)
259 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
260 (goto-char (point-min))
261 (while (looking-at "^Content[^ ]+:")
262 (when (looking-at "^Content-Transfer-Encoding: \\(.+\\)")
263 (setq cte (intern (match-string 1))))
264 (forward-line))
265 (unless (bobp)
266 (delete-region (point-min) (point)))
267 (mm-decode-content-transfer-encoding cte)
268 (unless (pgg-encrypt-region
269 (point-min) (point-max)
270 (split-string
271 (or
272 (message-options-get 'message-recipients)
273 (message-options-set 'message-recipients
274 (read-string "Recipients: ")))
275 "[ \f\t\n\r\v,]+")
276 sign)
277 (pop-to-buffer pgg-errors-buffer)
278 (error "Encrypt error"))
279 (delete-region (point-min) (point-max))
280 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
281 ;;(insert "Version: 1\n\n")
282 (insert "\n")
283 (insert-buffer-substring pgg-output-buffer)
284 t))
285
286 ;;;###autoload
287 (defun mml1991-encrypt (cont &optional sign)
288 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
289 (if func
290 (funcall func cont sign)
291 (error "Cannot find encrypt function"))))
292
293 ;;;###autoload
294 (defun mml1991-sign (cont)
295 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
296 (if func
297 (funcall func cont)
298 (error "Cannot find sign function"))))
299
300 (provide 'mml1991)
301
302 ;; Local Variables:
303 ;; coding: iso-8859-1
304 ;; End:
305
306 ;;; arch-tag: e542be18-ab28-4393-9b33-97fe9cf30706
307 ;;; mml1991.el ends here