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