1ca73d23724234facf34cec8af7e31262c144634
[elisp/gnus.git-] / 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 1.1.1.4 2003-01-14 05:36:30 yamaoka 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     (goto-char (point-min))
244     (while (re-search-forward "\r+$" nil t)
245       (replace-match "" t t))
246     (quoted-printable-encode-region (point-min) (point-max))
247     (goto-char (point-min))
248     (if headers (insert headers))
249     (insert "\n")
250     t))
251
252 (defun mml1991-pgg-encrypt (cont &optional sign)
253   (let (headers)
254     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
255     (goto-char (point-min))
256     (while (looking-at "^Content[^ ]+:") (forward-line))
257     (if (> (point) (point-min))
258         (progn
259           (kill-region (point-min) (point))))
260     (unless (pgg-encrypt-region
261              (point-min) (point-max) 
262              (split-string
263               (or
264                (message-options-get 'message-recipients)
265                (message-options-set 'message-recipients
266                                     (read-string "Recipients: ")))
267               "[ \f\t\n\r\v,]+")
268              sign)
269       (pop-to-buffer pgg-errors-buffer)
270       (error "Encrypt error"))
271     (kill-region (point-min) (point-max))
272     ;;(insert "Content-Type: application/pgp-encrypted\n\n")
273     ;;(insert "Version: 1\n\n")
274     (insert "\n")
275     (insert-buffer pgg-output-buffer)
276     t))
277
278 ;;;###autoload
279 (defun mml1991-encrypt (cont &optional sign)
280   (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
281     (if func
282         (funcall func cont sign)
283       (error "Cannot find encrypt function"))))
284
285 ;;;###autoload
286 (defun mml1991-sign (cont)
287   (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
288     (if func
289         (funcall func cont)
290       (error "Cannot find sign function"))))
291
292 (provide 'mml1991)
293
294 ;; Local Variables:
295 ;; coding: iso-8859-1
296 ;; End:
297
298 ;;; mml1991.el ends here