(mime-pgp-command): New variable.
[elisp/semi.git] / mime-pgp.el
1 ;;; mime-pgp.el --- mime-view internal methods for PGP.
2
3 ;; Copyright (C) 1995,1996,1997 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/12/7
7 ;;      Renamed: 1997/2/27 from tm-pgp.el
8 ;; Version: $Id: mime-pgp.el,v 0.4 1997-03-15 19:43:56 morioka Exp $
9 ;; Keywords: mail, news, MIME, multimedia, PGP, security
10
11 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;    This module is based on 2 drafts about PGP MIME integration:
31
32 ;;      - RFC 2015: "MIME Security with Pretty Good Privacy (PGP)"
33 ;;              by Michael Elkins <elkins@aero.org> (1996/6)
34 ;;
35 ;;      - draft-kazu-pgp-mime-00.txt: "PGP MIME Integration"
36 ;;              by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp>
37 ;;                      (1995/10; expired)
38 ;;
39 ;;    These drafts may be contrary to each other.  You should decide
40 ;;  which you support.  (Maybe you should use PGP/MIME)
41
42 ;;; Code:
43
44 (require 'mime-play)
45
46
47 ;;; @ internal method for application/pgp
48 ;;;
49 ;;; It is based on draft-kazu-pgp-mime-00.txt
50
51 (defun mime-article/view-application/pgp (beg end cal)
52   (let* ((cnum (mime-article/point-content-number beg))
53          (cur-buf (current-buffer))
54          (p-win (or (get-buffer-window mime::article/preview-buffer)
55                     (get-largest-window)))
56          (new-name (format "%s-%s" (buffer-name) cnum))
57          (mother mime::article/preview-buffer)
58          (mode major-mode)
59          code-converter
60          (str (buffer-substring beg end))
61          )
62     (set-buffer (get-buffer-create new-name))
63     (erase-buffer)
64     (insert str)
65     (cond ((progn
66              (goto-char (point-min))
67              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
68              )
69            (funcall (pgp-function 'verify))
70            (goto-char (point-min))
71            (delete-region
72             (point-min)
73             (and
74              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
75              (match-end 0))
76             )
77            (delete-region
78             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
79                  (match-beginning 0))
80             (point-max)
81             )
82            (goto-char (point-min))
83            (while (re-search-forward "^- -" nil t)
84              (replace-match "-")
85              )
86            (setq code-converter
87                  (or
88                   (cdr (assq mode mime-viewer/code-converter-alist))
89                   (function mime-viewer/default-code-convert-region)))
90            )
91           ((progn
92              (goto-char (point-min))
93              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
94              )
95            (as-binary-process (funcall (pgp-function 'decrypt)))
96            (goto-char (point-min))
97            (delete-region (point-min)
98                           (and
99                            (search-forward "\n\n")
100                            (match-end 0)))
101            (setq code-converter (function mime-charset/decode-buffer))
102            ))
103     (setq major-mode 'mime/show-message-mode)
104     (setq mime::article/code-converter code-converter)
105     (save-window-excursion (mime-view-mode mother))
106     (set-window-buffer p-win mime::article/preview-buffer)
107     ))
108
109 (set-atype 'mime/content-decoding-condition
110            '((type . "application/pgp")
111              (method . mime-article/view-application/pgp)
112              ))
113
114 (set-atype 'mime/content-decoding-condition
115            '((type . "text/x-pgp")
116              (method . mime-article/view-application/pgp)
117              ))
118
119
120 ;;; @ Internal method for application/pgp-signature
121 ;;;
122 ;;; It is based on RFC 2015.
123
124 (defvar mime-pgp-command "pgp"
125   "*Name of the PGP command.")
126
127 (defvar mime-pgp-default-language 'en
128   "*Symbol of language for pgp.
129 It should be ISO 639 2 letter language code such as en, ja, ...")
130
131 (defvar mime-pgp-good-signature-regexp-alist
132   '((en . "Good signature from user.*$"))
133   "Alist of language vs regexp to detect ``Good signature''.")
134
135 (defvar mime-pgp-key-expected-regexp-alist
136   '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
137   "Alist of language vs regexp to detect ``Key expected''.")
138
139 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
140   (save-excursion
141     (set-buffer output-buffer)
142     (erase-buffer)
143     )
144   (let* ((lang (or mime-pgp-default-language 'en))
145          (status (call-process-region (point-min)(point-max)
146                                       mime-pgp-command
147                                       nil output-buffer nil
148                                       orig-file (format "+language=%s" lang)))
149          (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist)))
150          )
151     (if (= status 0)
152         (save-excursion
153           (set-buffer output-buffer)
154           (goto-char (point-min))
155           (message
156            (cond ((not (stringp regexp))
157                   "Please specify right regexp for specified language")
158                  ((re-search-forward regexp nil t)
159                   (buffer-substring (match-beginning 0) (match-end 0))
160                   )
161                  (t
162                   "Bad signature")))
163           ))))
164
165 (defun mime-article/check-pgp-signature (beg end cal)
166   (let* ((encoding (cdr (assq 'encoding cal)))
167          (cnum (mime-article/point-content-number beg))
168          (rcnum (reverse cnum))
169          (rmcnum (cdr rcnum))
170          (knum (car rcnum))
171          (onum (if (> knum 0)
172                    (1- knum)
173                  (1+ knum)))
174          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
175                                              mime::article/content-info))
176          status str kbuf
177          (basename (expand-file-name "tm" mime-temp-directory))
178          (orig-file (make-temp-name basename))
179          (sig-file (concat orig-file ".sig"))
180          )
181     (save-excursion
182       (setq str (buffer-substring
183                  (mime::content-info/point-min oinfo)
184                  (mime::content-info/point-max oinfo)
185                  ))
186       (set-buffer (get-buffer-create mime/temp-buffer-name))
187       (insert str)
188       (goto-char (point-min))
189       (while (re-search-forward "\n" nil t)
190         (replace-match "\r\n")
191         )
192       (as-binary-output-file (write-file orig-file))
193       (kill-buffer (current-buffer))
194       )
195     (save-excursion
196       (mime-article/show-output-buffer)
197       )
198     (save-excursion
199       (setq str (buffer-substring
200                  (save-excursion
201                    (goto-char beg)
202                    (and (search-forward "\n\n")
203                         (match-end 0)))
204                  end))
205       (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
206       (insert str)
207       (mime-decode-region (point-min)(point-max) encoding)
208       (as-binary-output-file (write-file sig-file))
209       (or (mime::article/call-pgp-to-check-signature
210            mime/output-buffer-name orig-file)
211           (let (pgp-id)
212             (save-excursion
213               (set-buffer mime/output-buffer-name)
214               (goto-char (point-min))
215               (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
216                                        mime-pgp-key-expected-regexp-alist))))
217                 (cond ((not (stringp regexp))
218                        (message
219                         "Please specify right regexp for specified language")
220                        )
221                       ((re-search-forward regexp nil t)
222                        (setq pgp-id
223                              (concat "0x" (buffer-substring-no-properties
224                                            (match-beginning 1)
225                                            (match-end 1))))
226                        ))))
227             (if (and pgp-id
228                      (y-or-n-p
229                       (format "Key %s not found; attempt to fetch? " pgp-id))
230                      )
231                 (progn
232                   (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
233                   (mime::article/call-pgp-to-check-signature
234                    mime/output-buffer-name orig-file)
235                   ))
236             ))
237       (let ((other-window-scroll-buffer mime/output-buffer-name))
238         (scroll-other-window 8)
239         )
240       (kill-buffer kbuf)
241       (delete-file orig-file)
242       (delete-file sig-file)
243       )))
244
245 (set-atype 'mime/content-decoding-condition
246            '((type . "application/pgp-signature")
247              (method . mime-article/check-pgp-signature)
248              ))
249
250
251 ;;; @ Internal method for application/pgp-encrypted
252 ;;;
253 ;;; It is based on RFC 2015.
254
255 (defun mime-article/decrypt-pgp (beg end cal)
256   (let* ((cnum (mime-article/point-content-number beg))
257          (rcnum (reverse cnum))
258          (rmcnum (cdr rcnum))
259          (knum (car rcnum))
260          (onum (if (> knum 0)
261                    (1- knum)
262                  (1+ knum)))
263          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
264                                              mime::article/content-info))
265          (obeg (mime::content-info/point-min oinfo))
266          (oend (mime::content-info/point-max oinfo))
267          )
268     (mime-article/view-application/pgp obeg oend cal)
269     ))
270
271 (set-atype 'mime/content-decoding-condition
272            '((type . "application/pgp-encrypted")
273              (method . mime-article/decrypt-pgp)
274              ))
275
276
277 ;;; @ Internal method for application/pgp-keys
278 ;;;
279 ;;; It is based on RFC 2015.
280
281 (defun mime-article/add-pgp-keys (beg end cal)
282   (let* ((cnum (mime-article/point-content-number beg))
283          (cur-buf (current-buffer))
284          (new-name (format "%s-%s" (buffer-name) cnum))
285          (mother mime::article/preview-buffer)
286          (charset (cdr (assoc "charset" cal)))
287          (encoding (cdr (assq 'encoding cal)))
288          (mode major-mode)
289          str)
290     (setq str (buffer-substring beg end))
291     (switch-to-buffer new-name)
292     (setq buffer-read-only nil)
293     (erase-buffer)
294     (insert str)
295     (goto-char (point-min))
296     (if (re-search-forward "^\n" nil t)
297         (delete-region (point-min) (match-end 0))
298       )
299     (mime-decode-region (point-min)(point-max) encoding)
300     (funcall (pgp-function 'snarf-keys))
301     (kill-buffer (current-buffer))
302     ))
303
304 (set-atype 'mime/content-decoding-condition
305            '((type . "application/pgp-keys")
306              (method . mime-article/add-pgp-keys)
307              ))
308
309          
310 ;;; @ end
311 ;;;
312
313 (provide 'mime-pgp)
314
315 (run-hooks 'mime-pgp-load-hook)
316
317 ;;; mime-pgp.el ends here