Import No Gnus v0.4.
[elisp/gnus.git-] / 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, 2006 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 ((pgg-text-mode t)
233         headers cte)
234     ;; Don't sign headers.
235     (goto-char (point-min))
236     (while (not (looking-at "^$"))
237       (forward-line))
238     (unless (eobp) ;; no headers?
239       (setq headers (buffer-substring (point-min) (point)))
240       (forward-line) ;; skip header/body separator
241       (delete-region (point-min) (point)))
242     (when (string-match "^Content-Transfer-Encoding: \\(.+\\)" headers)
243       (setq cte (intern (match-string 1 headers))))
244     (mm-decode-content-transfer-encoding cte)
245     (unless (let ((pgg-default-user-id
246                    (or (message-options-get 'mml-sender)
247                        pgg-default-user-id)))
248               (pgg-sign-region (point-min) (point-max) t))
249       (pop-to-buffer pgg-errors-buffer)
250       (error "Encrypt error"))
251     (delete-region (point-min) (point-max))
252     (mm-with-unibyte-current-buffer
253       (insert-buffer-substring pgg-output-buffer)
254       (goto-char (point-min))
255       (while (re-search-forward "\r+$" nil t)
256         (replace-match "" t t))
257       (mm-encode-content-transfer-encoding cte)
258       (goto-char (point-min))
259       (when headers
260         (insert headers))
261       (insert "\n"))
262     t))
263
264 (defun mml1991-pgg-encrypt (cont &optional sign)
265   (let ((pgg-text-mode t)
266         cte)
267     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
268     (goto-char (point-min))
269     (while (looking-at "^Content[^ ]+:")
270       (when (looking-at "^Content-Transfer-Encoding: \\(.+\\)")
271         (setq cte (intern (match-string 1))))
272       (forward-line))
273     (unless (bobp)
274       (delete-region (point-min) (point)))
275     (mm-decode-content-transfer-encoding cte)
276     (unless (pgg-encrypt-region
277              (point-min) (point-max)
278              (split-string
279               (or
280                (message-options-get 'message-recipients)
281                (message-options-set 'message-recipients
282                                     (read-string "Recipients: ")))
283               "[ \f\t\n\r\v,]+")
284              sign)
285       (pop-to-buffer pgg-errors-buffer)
286       (error "Encrypt error"))
287     (delete-region (point-min) (point-max))
288     ;;(insert "Content-Type: application/pgp-encrypted\n\n")
289     ;;(insert "Version: 1\n\n")
290     (insert "\n")
291     (insert-buffer-substring pgg-output-buffer)
292     t))
293
294 ;;;###autoload
295 (defun mml1991-encrypt (cont &optional sign)
296   (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
297     (if func
298         (funcall func cont sign)
299       (error "Cannot find encrypt function"))))
300
301 ;;;###autoload
302 (defun mml1991-sign (cont)
303   (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
304     (if func
305         (funcall func cont)
306       (error "Cannot find sign function"))))
307
308 (provide 'mml1991)
309
310 ;; Local Variables:
311 ;; coding: iso-8859-1
312 ;; End:
313
314 ;;; arch-tag: e542be18-ab28-4393-9b33-97fe9cf30706
315 ;;; mml1991.el ends here