* gnus-vers.el (gnus-revision-number): Increment to 01.
[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 Ldecke <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 ;;; Code:
28
29 (defvar mml1991-use mml2015-use
30   "The package used for PGP.")
31
32 (defvar mml1991-function-alist
33   '((mailcrypt mml1991-mailcrypt-sign
34                mml1991-mailcrypt-encrypt)
35     (gpg mml1991-gpg-sign
36          mml1991-gpg-encrypt))
37   "Alist of PGP functions.")
38
39 ;;; mailcrypt wrapper
40
41 (eval-and-compile
42   (autoload 'mc-sign-generic "mc-toplev"))
43
44 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
45 (defvar mml1991-verify-function 'mailcrypt-verify)
46
47 (defun mml1991-mailcrypt-sign (cont)
48   (let ((text (current-buffer))
49         headers signature
50         (result-buffer (get-buffer-create "*GPG Result*")))
51     ;; Save MIME Content[^ ]+: headers from signing
52     (goto-char (point-min))
53     (while (looking-at "^Content[^ ]+:") (forward-line))
54     (if (> (point) (point-min))
55         (progn
56           (setq headers (buffer-substring (point-min) (point)))
57           (kill-region (point-min) (point))))
58     (goto-char (point-max))
59     (unless (bolp)
60       (insert "\n"))
61     (quoted-printable-decode-region (point-min) (point-max))
62     (with-temp-buffer
63       (setq signature (current-buffer))
64       (insert-buffer text)
65       (unless (mc-sign-generic (message-options-get 'message-sender)
66                                nil nil nil nil)
67         (unless (> (point-max) (point-min))
68           (pop-to-buffer result-buffer)
69           (error "Sign error")))
70       (goto-char (point-min))
71       (while (re-search-forward "\r+$" nil t)
72         (replace-match "" t t))
73       (quoted-printable-encode-region (point-min) (point-max))
74       (set-buffer text)
75       (kill-region (point-min) (point-max))
76       (if headers (insert headers))
77       (insert "\n")
78       (insert-buffer signature)
79       (goto-char (point-max)))))
80
81 (defun mml1991-mailcrypt-encrypt (cont)
82   (let ((text (current-buffer))
83         cipher
84         (result-buffer (get-buffer-create "*GPG Result*")))
85     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
86     (goto-char (point-min))
87     (while (looking-at "^Content[^ ]+:") (forward-line))
88     (if (> (point) (point-min))
89         (progn
90           (kill-region (point-min) (point))))
91     (mm-with-unibyte-current-buffer-mule4
92       (with-temp-buffer
93         (setq cipher (current-buffer))
94         (insert-buffer text)
95         (unless (mc-encrypt-generic
96                  (or
97                   (message-options-get 'message-recipients)
98                   (message-options-set 'message-recipients
99                                        (read-string "Recipients: ")))
100                  nil
101                  (point-min) (point-max)
102                  (message-options-get 'message-sender)
103                  'sign)
104           (unless (> (point-max) (point-min))
105             (pop-to-buffer result-buffer)
106             (error "Encrypt error")))
107         (goto-char (point-min))
108         (while (re-search-forward "\r+$" nil t)
109           (replace-match "" t t))
110         (set-buffer text)
111         (kill-region (point-min) (point-max))
112         ;;(insert "Content-Type: application/pgp-encrypted\n\n")
113         ;;(insert "Version: 1\n\n")
114         (insert "\n")
115         (insert-buffer cipher)
116         (goto-char (point-max))))))
117
118 ;;; gpg wrapper
119
120 (eval-and-compile
121   (autoload 'gpg-sign-cleartext "gpg"))
122
123 (defun mml1991-gpg-sign (cont)
124   (let ((text (current-buffer))
125         headers signature
126         (result-buffer (get-buffer-create "*GPG Result*")))
127     ;; Save MIME Content[^ ]+: headers from signing
128     (goto-char (point-min))
129     (while (looking-at "^Content[^ ]+:") (forward-line))
130     (if (> (point) (point-min))
131         (progn
132           (setq headers (buffer-substring (point-min) (point)))
133           (kill-region (point-min) (point))))
134     (goto-char (point-max))
135     (unless (bolp)
136       (insert "\n"))
137     (quoted-printable-decode-region (point-min) (point-max))
138     (with-temp-buffer
139       (unless (gpg-sign-cleartext text (setq signature (current-buffer))
140                                   result-buffer
141                                   nil
142                                   (message-options-get 'message-sender))
143         (unless (> (point-max) (point-min))
144           (pop-to-buffer result-buffer)
145           (error "Sign error")))
146       (goto-char (point-min))
147       (while (re-search-forward "\r+$" nil t)
148         (replace-match "" t t))
149       (quoted-printable-encode-region (point-min) (point-max))
150       (set-buffer text)
151       (kill-region (point-min) (point-max))
152       (if headers (insert headers))
153       (insert "\n")
154       (insert-buffer signature)
155       (goto-char (point-max)))))
156
157 (defun mml1991-gpg-encrypt (cont)
158   (let ((text (current-buffer))
159         cipher
160         (result-buffer (get-buffer-create "*GPG Result*")))
161     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
162     (goto-char (point-min))
163     (while (looking-at "^Content[^ ]+:") (forward-line))
164     (if (> (point) (point-min))
165         (progn
166           (kill-region (point-min) (point))))
167     (mm-with-unibyte-current-buffer-mule4
168       (with-temp-buffer
169         (unless (gpg-sign-encrypt
170                  text (setq cipher (current-buffer))
171                  result-buffer
172                  (split-string
173                   (or
174                    (message-options-get 'message-recipients)
175                    (message-options-set 'message-recipients
176                                         (read-string "Recipients: ")))
177                   "[ \f\t\n\r\v,]+")
178                  nil
179                  (message-options-get 'message-sender)
180                  t t) ; armor & textmode
181           (unless (> (point-max) (point-min))
182             (pop-to-buffer result-buffer)
183             (error "Encrypt error")))
184         (goto-char (point-min))
185         (while (re-search-forward "\r+$" nil t)
186           (replace-match "" t t))
187         (set-buffer text)
188         (kill-region (point-min) (point-max))
189         ;;(insert "Content-Type: application/pgp-encrypted\n\n")
190         ;;(insert "Version: 1\n\n")
191         (insert "\n")
192         (insert-buffer cipher)
193         (goto-char (point-max))))))
194
195 ;;;###autoload
196 (defun mml1991-encrypt (cont)
197   (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
198     (if func
199         (funcall func cont)
200       (error "Cannot find encrypt function"))))
201
202 ;;;###autoload
203 (defun mml1991-sign (cont)
204   (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
205     (if func
206         (funcall func cont)
207       (error "Cannot find sign function"))))
208
209 (provide 'mml1991)
210
211 ;; Local Variables:
212 ;; coding: iso-8859-1
213 ;; End:
214
215 ;;; mml1991.el ends here