* mml.el (mml-parse-1): Support sender in #secure tags.
[gnus] / lisp / mml1991.el
1 ;;; mml-gpg-old.el --- Old PGP message format (RFC 1991) support for MML
2 ;; Copyright (C) 1998, 1999, 2000, 2001 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 (not yet) 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 ;; RCS: $Id: mml1991.el,v 6.13 2002/10/09 23:27:32 jas Exp $
28
29 ;;; Code:
30
31 (defvar mml1991-use mml2015-use
32   "The package used for PGP.")
33
34 (defvar mml1991-function-alist
35   '((mailcrypt mml1991-mailcrypt-sign
36                mml1991-mailcrypt-encrypt)
37     (gpg mml1991-gpg-sign
38          mml1991-gpg-encrypt)
39     (pgg mml1991-pgg-sign
40          mml1991-pgg-encrypt))
41   "Alist of PGP functions.")
42
43 ;;; mailcrypt wrapper
44
45 (eval-and-compile
46   (autoload 'mc-sign-generic "mc-toplev"))
47
48 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
49 (defvar mml1991-verify-function 'mailcrypt-verify)
50
51 (defun mml1991-mailcrypt-sign (cont)
52   (let ((text (current-buffer))
53         headers signature
54         (result-buffer (get-buffer-create "*GPG Result*")))
55     ;; Save MIME Content[^ ]+: headers from signing
56     (goto-char (point-min))
57     (while (looking-at "^Content[^ ]+:") (forward-line))
58     (if (> (point) (point-min))
59         (progn
60           (setq headers (buffer-substring (point-min) (point)))
61           (kill-region (point-min) (point))))
62     (goto-char (point-max))
63     (unless (bolp)
64       (insert "\n"))
65     (quoted-printable-decode-region (point-min) (point-max))
66     (with-temp-buffer
67       (setq signature (current-buffer))
68       (insert-buffer text)
69       (unless (mc-sign-generic (message-options-get 'message-sender)
70                                nil nil nil nil)
71         (unless (> (point-max) (point-min))
72           (pop-to-buffer result-buffer)
73           (error "Sign error")))
74       (goto-char (point-min))
75       (while (re-search-forward "\r+$" nil t)
76         (replace-match "" t t))
77       (quoted-printable-encode-region (point-min) (point-max))
78       (set-buffer text)
79       (kill-region (point-min) (point-max))
80       (if headers (insert headers))
81       (insert "\n")
82       (insert-buffer signature)
83       (goto-char (point-max)))))
84
85 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
86   (let ((text (current-buffer))
87         (mc-pgp-always-sign
88          (or mc-pgp-always-sign
89              sign
90              (eq t (or (message-options-get 'message-sign-encrypt)
91                        (message-options-set
92                         'message-sign-encrypt
93                         (or (y-or-n-p "Sign the message? ")
94                             'not))))
95              'never))
96         cipher
97         (result-buffer (get-buffer-create "*GPG Result*")))
98     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
99     (goto-char (point-min))
100     (while (looking-at "^Content[^ ]+:") (forward-line))
101     (if (> (point) (point-min))
102         (progn
103           (kill-region (point-min) (point))))
104     (mm-with-unibyte-current-buffer-mule4
105       (with-temp-buffer
106         (setq cipher (current-buffer))
107         (insert-buffer text)
108         (unless (mc-encrypt-generic
109                  (or
110                   (message-options-get 'message-recipients)
111                   (message-options-set 'message-recipients
112                                        (read-string "Recipients: ")))
113                  nil
114                  (point-min) (point-max)
115                  (message-options-get 'message-sender)
116                  'sign)
117           (unless (> (point-max) (point-min))
118             (pop-to-buffer result-buffer)
119             (error "Encrypt error")))
120         (goto-char (point-min))
121         (while (re-search-forward "\r+$" nil t)
122           (replace-match "" t t))
123         (set-buffer text)
124         (kill-region (point-min) (point-max))
125         ;;(insert "Content-Type: application/pgp-encrypted\n\n")
126         ;;(insert "Version: 1\n\n")
127         (insert "\n")
128         (insert-buffer cipher)
129         (goto-char (point-max))))))
130
131 ;;; gpg wrapper
132
133 (eval-and-compile
134   (autoload 'gpg-sign-cleartext "gpg"))
135
136 (defun mml1991-gpg-sign (cont)
137   (let ((text (current-buffer))
138         headers signature
139         (result-buffer (get-buffer-create "*GPG Result*")))
140     ;; Save MIME Content[^ ]+: headers from signing
141     (goto-char (point-min))
142     (while (looking-at "^Content[^ ]+:") (forward-line))
143     (if (> (point) (point-min))
144         (progn
145           (setq headers (buffer-substring (point-min) (point)))
146           (kill-region (point-min) (point))))
147     (goto-char (point-max))
148     (unless (bolp)
149       (insert "\n"))
150     (quoted-printable-decode-region (point-min) (point-max))
151     (with-temp-buffer
152       (unless (gpg-sign-cleartext text (setq signature (current-buffer))
153                                   result-buffer
154                                   nil
155                                   (message-options-get 'message-sender))
156         (unless (> (point-max) (point-min))
157           (pop-to-buffer result-buffer)
158           (error "Sign error")))
159       (goto-char (point-min))
160       (while (re-search-forward "\r+$" nil t)
161         (replace-match "" t t))
162       (quoted-printable-encode-region (point-min) (point-max))
163       (set-buffer text)
164       (kill-region (point-min) (point-max))
165       (if headers (insert headers))
166       (insert "\n")
167       (insert-buffer signature)
168       (goto-char (point-max)))))
169
170 (defun mml1991-gpg-encrypt (cont &optional sign)
171   (let ((text (current-buffer))
172         cipher
173         (result-buffer (get-buffer-create "*GPG Result*")))
174     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
175     (goto-char (point-min))
176     (while (looking-at "^Content[^ ]+:") (forward-line))
177     (if (> (point) (point-min))
178         (progn
179           (kill-region (point-min) (point))))
180     (mm-with-unibyte-current-buffer-mule4
181       (with-temp-buffer
182         (flet ((gpg-encrypt-func 
183                 (sign plaintext ciphertext result recipients &optional
184                       passphrase sign-with-key armor textmode)
185                 (if sign
186                     (gpg-sign-encrypt
187                      plaintext ciphertext result recipients passphrase
188                      sign-with-key armor textmode)
189                   (gpg-encrypt
190                    plaintext ciphertext result recipients passphrase
191                    armor textmode))))
192           (unless (gpg-encrypt-func
193                    sign
194                    text (setq cipher (current-buffer))
195                    result-buffer
196                    (split-string
197                     (or
198                      (message-options-get 'message-recipients)
199                      (message-options-set 'message-recipients
200                                           (read-string "Recipients: ")))
201                     "[ \f\t\n\r\v,]+")
202                    nil
203                    (message-options-get 'message-sender)
204                    t t) ; armor & textmode
205             (unless (> (point-max) (point-min))
206               (pop-to-buffer result-buffer)
207               (error "Encrypt error"))))
208         (goto-char (point-min))
209         (while (re-search-forward "\r+$" nil t)
210           (replace-match "" t t))
211         (set-buffer text)
212         (kill-region (point-min) (point-max))
213         ;;(insert "Content-Type: application/pgp-encrypted\n\n")
214         ;;(insert "Version: 1\n\n")
215         (insert "\n")
216         (insert-buffer cipher)
217         (goto-char (point-max))))))
218
219 ;; pgg wrapper
220
221 (defvar pgg-output-buffer)
222 (defvar pgg-errors-buffer)
223
224 (defun mml1991-pgg-sign (cont)
225   (let (headers)
226     ;; Don't sign headers.
227     (goto-char (point-min))
228     (while (not (looking-at "^$"))
229       (forward-line))
230     (unless (eobp) ;; no headers?
231       (setq headers (buffer-substring (point-min) (point)))
232       (forward-line) ;; skip header/body separator
233       (kill-region (point-min) (point)))
234     (quoted-printable-decode-region (point-min) (point-max))
235     (unless (let ((pgg-default-user-id
236                    (or (message-options-get 'message-sender)
237                        pgg-default-user-id)))
238               (pgg-sign-region (point-min) (point-max) t))
239       (pop-to-buffer pgg-errors-buffer)
240       (error "Encrypt error"))
241     (kill-region (point-min) (point-max))
242     (insert-buffer pgg-output-buffer)
243     (quoted-printable-encode-region (point-min) (point-max))
244     (goto-char (point-min))
245     (if headers (insert headers))
246     (insert "\n")
247     t))
248
249 (defun mml1991-pgg-encrypt (cont &optional sign)
250   (let (headers)
251     ;; Don't sign headers.
252     (goto-char (point-min))
253     (while (not (looking-at "^$"))
254       (forward-line))
255     (unless (eobp) ;; no headers?
256       (setq headers (buffer-substring (point-min) (point)))
257       (forward-line) ;; skip header/body separator
258       (kill-region (point-min) (point)))
259     (unless (pgg-encrypt-region
260              (point-min) (point-max) 
261              (split-string
262               (or
263                (message-options-get 'message-recipients)
264                (message-options-set 'message-recipients
265                                     (read-string "Recipients: ")))
266               "[ \f\t\n\r\v,]+")
267              sign)
268       (pop-to-buffer pgg-errors-buffer)
269       (error "Encrypt error"))
270     (kill-region (point-min) (point-max))
271     (if headers (insert headers))
272     (insert "\n")
273     (insert-buffer pgg-output-buffer)
274     t))
275
276 ;;;###autoload
277 (defun mml1991-encrypt (cont &optional sign)
278   (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
279     (if func
280         (funcall func cont sign)
281       (error "Cannot find encrypt function"))))
282
283 ;;;###autoload
284 (defun mml1991-sign (cont)
285   (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
286     (if func
287         (funcall func cont)
288       (error "Cannot find sign function"))))
289
290 (provide 'mml1991)
291
292 ;; Local Variables:
293 ;; coding: iso-8859-1
294 ;; End:
295
296 ;;; mml1991.el ends here