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