Add newlines between pictures and text.
[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, 2005, 2006,
4 ;;   2007, 2008, 2009, 2010  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 3 of the License, or
15 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 ;; For Emacs < 22.2.
30 (eval-and-compile
31   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
32
33   (if (locate-library "password-cache")
34       (require 'password-cache)
35     (require 'password)))
36
37 (eval-when-compile
38   (require 'cl)
39   (require 'mm-util))
40
41 (require 'mm-encode)
42 (require 'mml-sec)
43
44 (defvar mc-pgp-always-sign)
45
46 (autoload 'quoted-printable-decode-region "qp")
47 (autoload 'quoted-printable-encode-region "qp")
48
49 (autoload 'mm-decode-content-transfer-encoding "mm-bodies")
50 (autoload 'mm-encode-content-transfer-encoding "mm-bodies")
51 (autoload 'message-options-get "message")
52 (autoload 'message-options-set "message")
53
54 (defvar mml1991-use mml2015-use
55   "The package used for PGP.")
56
57 (defvar mml1991-function-alist
58   '((mailcrypt mml1991-mailcrypt-sign
59                mml1991-mailcrypt-encrypt)
60     (pgg mml1991-pgg-sign
61          mml1991-pgg-encrypt)
62     (epg mml1991-epg-sign
63          mml1991-epg-encrypt))
64   "Alist of PGP functions.")
65
66 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
67   "If t, cache passphrase.")
68
69 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
70   "How many seconds the passphrase is cached.
71 Whether the passphrase is cached at all is controlled by
72 `mml1991-cache-passphrase'.")
73
74 (defvar mml1991-signers nil
75   "A list of your own key ID which will be used to sign a message.")
76
77 (defvar mml1991-encrypt-to-self nil
78   "If t, add your own key ID to recipient list when encryption.")
79
80 ;;; mailcrypt wrapper
81
82 (autoload 'mc-sign-generic "mc-toplev")
83
84 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
85 (defvar mml1991-verify-function 'mailcrypt-verify)
86
87 (defun mml1991-mailcrypt-sign (cont)
88   (let ((text (current-buffer))
89         headers signature
90         (result-buffer (get-buffer-create "*GPG Result*")))
91     ;; Save MIME Content[^ ]+: headers from signing
92     (goto-char (point-min))
93     (while (looking-at "^Content[^ ]+:") (forward-line))
94     (unless (bobp)
95       (setq headers (buffer-string))
96       (delete-region (point-min) (point)))
97     (goto-char (point-max))
98     (unless (bolp)
99       (insert "\n"))
100     (quoted-printable-decode-region (point-min) (point-max))
101     (with-temp-buffer
102       (setq signature (current-buffer))
103       (insert-buffer-substring text)
104       (unless (mc-sign-generic (message-options-get 'message-sender)
105                                nil nil nil nil)
106         (unless (> (point-max) (point-min))
107           (pop-to-buffer result-buffer)
108           (error "Sign error")))
109       (goto-char (point-min))
110       (while (re-search-forward "\r+$" nil t)
111         (replace-match "" t t))
112       (quoted-printable-encode-region (point-min) (point-max))
113       (set-buffer text)
114       (delete-region (point-min) (point-max))
115       (if headers (insert headers))
116       (insert "\n")
117       (insert-buffer-substring signature)
118       (goto-char (point-max)))))
119
120 (declare-function mc-encrypt-generic "ext:mc-toplev"
121                   (&optional recipients scheme start end from sign))
122
123 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
124   (let ((text (current-buffer))
125         (mc-pgp-always-sign
126          (or mc-pgp-always-sign
127              sign
128              (eq t (or (message-options-get 'message-sign-encrypt)
129                        (message-options-set
130                         'message-sign-encrypt
131                         (or (y-or-n-p "Sign the message? ")
132                             'not))))
133              'never))
134         cipher
135         (result-buffer (get-buffer-create "*GPG Result*")))
136     ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
137     (goto-char (point-min))
138     (while (looking-at "^Content[^ ]+:") (forward-line))
139     (unless (bobp)
140       (delete-region (point-min) (point)))
141     (mm-with-unibyte-current-buffer
142       (with-temp-buffer
143         (inline (mm-disable-multibyte))
144         (setq cipher (current-buffer))
145         (insert-buffer-substring text)
146         (unless (mc-encrypt-generic
147                  (or
148                   (message-options-get 'message-recipients)
149                   (message-options-set 'message-recipients
150                                        (read-string "Recipients: ")))
151                  nil
152                  (point-min) (point-max)
153                  (message-options-get 'message-sender)
154                  'sign)
155           (unless (> (point-max) (point-min))
156             (pop-to-buffer result-buffer)
157             (error "Encrypt error")))
158         (goto-char (point-min))
159         (while (re-search-forward "\r+$" nil t)
160           (replace-match "" t t))
161         (set-buffer text)
162         (delete-region (point-min) (point-max))
163         ;;(insert "Content-Type: application/pgp-encrypted\n\n")
164         ;;(insert "Version: 1\n\n")
165         (insert "\n")
166         (insert-buffer-substring cipher)
167         (goto-char (point-max))))))
168
169 ;; pgg wrapper
170
171 (defvar pgg-default-user-id)
172 (defvar pgg-errors-buffer)
173 (defvar pgg-output-buffer)
174
175 (defun mml1991-pgg-sign (cont)
176   (let ((pgg-text-mode t)
177         (pgg-default-user-id (or (message-options-get 'mml-sender)
178                                  pgg-default-user-id))
179         headers cte)
180     ;; Don't sign headers.
181     (goto-char (point-min))
182     (when (re-search-forward "^$" nil t)
183       (setq headers (buffer-substring (point-min) (point)))
184       (save-restriction
185         (narrow-to-region (point-min) (point))
186         (setq cte (mail-fetch-field "content-transfer-encoding")))
187       (forward-line 1)
188       (delete-region (point-min) (point))
189       (when cte
190         (setq cte (intern (downcase cte)))
191         (mm-decode-content-transfer-encoding cte)))
192     (unless (pgg-sign-region (point-min) (point-max) t)
193       (pop-to-buffer pgg-errors-buffer)
194       (error "Encrypt error"))
195     (delete-region (point-min) (point-max))
196     (mm-with-unibyte-current-buffer
197       (insert-buffer-substring pgg-output-buffer)
198       (goto-char (point-min))
199       (while (re-search-forward "\r+$" nil t)
200         (replace-match "" t t))
201       (when cte
202         (mm-encode-content-transfer-encoding cte))
203       (goto-char (point-min))
204       (when headers
205         (insert headers))
206       (insert "\n"))
207     t))
208
209 (defun mml1991-pgg-encrypt (cont &optional sign)
210   (goto-char (point-min))
211   (when (re-search-forward "^$" nil t)
212     (let ((cte (save-restriction
213                  (narrow-to-region (point-min) (point))
214                  (mail-fetch-field "content-transfer-encoding"))))
215       ;; Strip MIME headers since it will be ASCII armored.
216       (forward-line 1)
217       (delete-region (point-min) (point))
218       (when cte
219         (mm-decode-content-transfer-encoding (intern (downcase cte))))))
220   (unless (let ((pgg-text-mode t))
221             (pgg-encrypt-region
222              (point-min) (point-max)
223              (split-string
224               (or
225                (message-options-get 'message-recipients)
226                (message-options-set 'message-recipients
227                                     (read-string "Recipients: ")))
228               "[ \f\t\n\r\v,]+")
229              sign))
230     (pop-to-buffer pgg-errors-buffer)
231     (error "Encrypt error"))
232   (delete-region (point-min) (point-max))
233   (insert "\n")
234   (insert-buffer-substring pgg-output-buffer)
235   t)
236
237 ;; epg wrapper
238
239 (defvar epg-user-id-alist)
240
241 (autoload 'epg-make-context "epg")
242 (autoload 'epg-passphrase-callback-function "epg")
243 (autoload 'epa-select-keys "epa")
244 (autoload 'epg-list-keys "epg")
245 (autoload 'epg-context-set-armor "epg")
246 (autoload 'epg-context-set-textmode "epg")
247 (autoload 'epg-context-set-signers "epg")
248 (autoload 'epg-context-set-passphrase-callback "epg")
249 (autoload 'epg-sign-string "epg")
250 (autoload 'epg-encrypt-string "epg")
251 (autoload 'epg-configuration "epg-config")
252 (autoload 'epg-expand-group "epg-config")
253
254 (defvar mml1991-epg-secret-key-id-list nil)
255
256 (defun mml1991-epg-passphrase-callback (context key-id ignore)
257   (if (eq key-id 'SYM)
258       (epg-passphrase-callback-function context key-id nil)
259     (let* ((entry (assoc key-id epg-user-id-alist))
260            (passphrase
261             (password-read
262              (format "GnuPG passphrase for %s: "
263                      (if entry
264                          (cdr entry)
265                        key-id))
266              (if (eq key-id 'PIN)
267                  "PIN"
268                key-id))))
269       (when passphrase
270         (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
271           (password-cache-add key-id passphrase))
272         (setq mml1991-epg-secret-key-id-list
273               (cons key-id mml1991-epg-secret-key-id-list))
274         (copy-sequence passphrase)))))
275
276 (defun mml1991-epg-sign (cont)
277   (let ((context (epg-make-context))
278         headers cte signers signature)
279     (if (eq mm-sign-option 'guided)
280         (setq signers (epa-select-keys context "Select keys for signing.
281 If no one is selected, default secret key is used.  "
282                                        mml1991-signers t))
283       (if mml1991-signers
284           (setq signers (mapcar (lambda (name)
285                                   (car (epg-list-keys context name t)))
286                                 mml1991-signers))))
287     (epg-context-set-armor context t)
288     (epg-context-set-textmode context t)
289     (epg-context-set-signers context signers)
290     (if mml1991-cache-passphrase
291         (epg-context-set-passphrase-callback
292          context
293          #'mml1991-epg-passphrase-callback))
294     ;; Don't sign headers.
295     (goto-char (point-min))
296     (when (re-search-forward "^$" nil t)
297       (setq headers (buffer-substring (point-min) (point)))
298       (save-restriction
299         (narrow-to-region (point-min) (point))
300         (setq cte (mail-fetch-field "content-transfer-encoding")))
301       (forward-line 1)
302       (delete-region (point-min) (point))
303       (when cte
304         (setq cte (intern (downcase cte)))
305         (mm-decode-content-transfer-encoding cte)))
306     (condition-case error
307         (setq signature (epg-sign-string context (buffer-string) 'clear)
308               mml1991-epg-secret-key-id-list nil)
309       (error
310        (while mml1991-epg-secret-key-id-list
311          (password-cache-remove (car mml1991-epg-secret-key-id-list))
312          (setq mml1991-epg-secret-key-id-list
313                (cdr mml1991-epg-secret-key-id-list)))
314        (signal (car error) (cdr error))))
315     (delete-region (point-min) (point-max))
316     (mm-with-unibyte-current-buffer
317       (insert signature)
318       (goto-char (point-min))
319       (while (re-search-forward "\r+$" nil t)
320         (replace-match "" t t))
321       (when cte
322         (mm-encode-content-transfer-encoding cte))
323       (goto-char (point-min))
324       (when headers
325         (insert headers))
326       (insert "\n"))
327     t))
328
329 (defun mml1991-epg-encrypt (cont &optional sign)
330   (goto-char (point-min))
331   (when (re-search-forward "^$" nil t)
332     (let ((cte (save-restriction
333                  (narrow-to-region (point-min) (point))
334                  (mail-fetch-field "content-transfer-encoding"))))
335       ;; Strip MIME headers since it will be ASCII armored.
336       (forward-line 1)
337       (delete-region (point-min) (point))
338       (when cte
339         (mm-decode-content-transfer-encoding (intern (downcase cte))))))
340   (let ((context (epg-make-context))
341         (recipients
342          (if (message-options-get 'message-recipients)
343              (split-string
344               (message-options-get 'message-recipients)
345               "[ \f\t\n\r\v,]+")))
346         cipher signers config)
347     ;; We should remove this check if epg-0.0.6 is released.
348     (if (and (condition-case nil
349                  (require 'epg-config)
350                (error))
351              (functionp #'epg-expand-group))
352         (setq config (epg-configuration)
353               recipients
354               (apply #'nconc
355                      (mapcar (lambda (recipient)
356                                (or (epg-expand-group config recipient)
357                                    (list recipient)))
358                              recipients))))
359     (if (eq mm-encrypt-option 'guided)
360         (setq recipients
361               (epa-select-keys context "Select recipients for encryption.
362 If no one is selected, symmetric encryption will be performed.  "
363                                recipients))
364       (setq recipients
365             (delq nil (mapcar (lambda (name)
366                                 (car (epg-list-keys context name)))
367                               recipients))))
368     (if mml1991-encrypt-to-self
369         (if mml1991-signers
370             (setq recipients
371                   (nconc recipients
372                          (mapcar (lambda (name)
373                                    (car (epg-list-keys context name)))
374                                  mml1991-signers)))
375           (error "mml1991-signers not set")))
376     (when sign
377       (if (eq mm-sign-option 'guided)
378           (setq signers (epa-select-keys context "Select keys for signing.
379 If no one is selected, default secret key is used.  "
380                                          mml1991-signers t))
381         (if mml1991-signers
382             (setq signers (mapcar (lambda (name)
383                                     (car (epg-list-keys context name t)))
384                                   mml1991-signers))))
385       (epg-context-set-signers context signers))
386     (epg-context-set-armor context t)
387     (epg-context-set-textmode context t)
388     (if mml1991-cache-passphrase
389         (epg-context-set-passphrase-callback
390          context
391          #'mml1991-epg-passphrase-callback))
392     (condition-case error
393         (setq cipher
394               (epg-encrypt-string context (buffer-string) recipients sign)
395               mml1991-epg-secret-key-id-list nil)
396       (error
397        (while mml1991-epg-secret-key-id-list
398          (password-cache-remove (car mml1991-epg-secret-key-id-list))
399          (setq mml1991-epg-secret-key-id-list
400                (cdr mml1991-epg-secret-key-id-list)))
401        (signal (car error) (cdr error))))
402     (delete-region (point-min) (point-max))
403     (insert "\n" cipher))
404   t)
405
406 ;;;###autoload
407 (defun mml1991-encrypt (cont &optional sign)
408   (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
409     (if func
410         (funcall func cont sign)
411       (error "Cannot find encrypt function"))))
412
413 ;;;###autoload
414 (defun mml1991-sign (cont)
415   (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
416     (if func
417         (funcall func cont)
418       (error "Cannot find sign function"))))
419
420 (provide 'mml1991)
421
422 ;; Local Variables:
423 ;; coding: iso-8859-1
424 ;; End:
425
426 ;;; mml1991.el ends here