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