(mime-article/check-pgp-signature): `mime-article/show-output-buffer'
[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.17 1997-03-27 20:59:00 morioka Exp $
9 ;; Keywords: PGP, security, MIME, multimedia, mail, news
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          (p-win (or (get-buffer-window mime-view-buffer)
54                     (get-largest-window)))
55          (new-name (format "%s-%s" (buffer-name) cnum))
56          (the-buf (current-buffer))
57          (mother mime-view-buffer)
58          (mode major-mode)
59          text-decoder)
60     (set-buffer (get-buffer-create new-name))
61     (erase-buffer)
62     (insert-buffer-substring the-buf beg end)
63     (cond ((progn
64              (goto-char (point-min))
65              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
66              )
67            (funcall (pgp-function 'verify))
68            (goto-char (point-min))
69            (delete-region
70             (point-min)
71             (and
72              (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE-+\n\n")
73              (match-end 0))
74             )
75            (delete-region
76             (and (re-search-forward "^-+BEGIN PGP SIGNATURE-+")
77                  (match-beginning 0))
78             (point-max)
79             )
80            (goto-char (point-min))
81            (while (re-search-forward "^- -" nil t)
82              (replace-match "-")
83              )
84            (setq text-decoder
85                  (cdr (or (assq mode mime-text-decoder-alist)
86                           (assq t    mime-text-decoder-alist))))
87            )
88           ((progn
89              (goto-char (point-min))
90              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
91              )
92            (as-binary-process (funcall (pgp-function 'decrypt)))
93            (goto-char (point-min))
94            (delete-region (point-min)
95                           (and
96                            (search-forward "\n\n")
97                            (match-end 0)))
98            (setq text-decoder (function mime-text-decode-buffer))
99            ))
100     (setq major-mode 'mime-show-message-mode)
101     (setq mime-text-decoder text-decoder)
102     (save-window-excursion (mime-view-mode mother))
103     (set-window-buffer p-win mime-view-buffer)
104     ))
105
106 (set-atype 'mime/content-decoding-condition
107            '((type . "application/pgp")
108              (method . mime-article/view-application/pgp)
109              ))
110
111 (set-atype 'mime/content-decoding-condition
112            '((type . "text/x-pgp")
113              (method . mime-article/view-application/pgp)
114              ))
115
116
117 ;;; @ Internal method for application/pgp-signature
118 ;;;
119 ;;; It is based on RFC 2015.
120
121 (defvar mime-pgp-command "pgp"
122   "*Name of the PGP command.")
123
124 (defvar mime-pgp-default-language 'en
125   "*Symbol of language for pgp.
126 It should be ISO 639 2 letter language code such as en, ja, ...")
127
128 (defvar mime-pgp-good-signature-regexp-alist
129   '((en . "Good signature from user.*$"))
130   "Alist of language vs regexp to detect ``Good signature''.")
131
132 (defvar mime-pgp-key-expected-regexp-alist
133   '((en . "Key matching expected Key ID \\(\\S +\\) not found"))
134   "Alist of language vs regexp to detect ``Key expected''.")
135
136 (defun mime::article/call-pgp-to-check-signature (output-buffer orig-file)
137   (save-excursion
138     (set-buffer output-buffer)
139     (erase-buffer)
140     )
141   (let* ((lang (or mime-pgp-default-language 'en))
142          (status (call-process-region (point-min)(point-max)
143                                       mime-pgp-command
144                                       nil output-buffer nil
145                                       orig-file (format "+language=%s" lang)))
146          (regexp (cdr (assq lang mime-pgp-good-signature-regexp-alist)))
147          )
148     (if (= status 0)
149         (save-excursion
150           (set-buffer output-buffer)
151           (goto-char (point-min))
152           (message
153            (cond ((not (stringp regexp))
154                   "Please specify right regexp for specified language")
155                  ((re-search-forward regexp nil t)
156                   (buffer-substring (match-beginning 0) (match-end 0))
157                   )
158                  (t
159                   "Bad signature")))
160           ))))
161
162 (defun mime-article/check-pgp-signature (beg end cal)
163   (let* ((encoding (cdr (assq 'encoding cal)))
164          (cnum (mime-article/point-content-number beg))
165          (rcnum (reverse cnum))
166          (rmcnum (cdr rcnum))
167          (knum (car rcnum))
168          (onum (if (> knum 0)
169                    (1- knum)
170                  (1+ knum)))
171          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
172                                              mime::article/content-info))
173          str kbuf
174          (basename (expand-file-name "tm" mime-temp-directory))
175          (orig-file (make-temp-name basename))
176          (sig-file (concat orig-file ".sig"))
177          )
178     (save-excursion
179       (setq str (buffer-substring
180                  (mime::content-info/point-min oinfo)
181                  (mime::content-info/point-max oinfo)
182                  ))
183       (set-buffer (get-buffer-create mime/temp-buffer-name))
184       (insert str)
185       (goto-char (point-min))
186       (while (re-search-forward "\n" nil t)
187         (replace-match "\r\n")
188         )
189       (as-binary-output-file (write-file orig-file))
190       (kill-buffer (current-buffer))
191       )
192     (save-excursion
193       (mime-show-echo-buffer)
194       (setq str (buffer-substring
195                  (save-excursion
196                    (goto-char beg)
197                    (and (search-forward "\n\n")
198                         (match-end 0)))
199                  end))
200       (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
201       (insert str)
202       (mime-decode-region (point-min)(point-max) encoding)
203       (as-binary-output-file (write-file sig-file))
204       (or (mime::article/call-pgp-to-check-signature
205            mime-echo-buffer-name orig-file)
206           (let (pgp-id)
207             (save-excursion
208               (set-buffer mime-echo-buffer-name)
209               (goto-char (point-min))
210               (let ((regexp (cdr (assq (or mime-pgp-default-language 'en)
211                                        mime-pgp-key-expected-regexp-alist))))
212                 (cond ((not (stringp regexp))
213                        (message
214                         "Please specify right regexp for specified language")
215                        )
216                       ((re-search-forward regexp nil t)
217                        (setq pgp-id
218                              (concat "0x" (buffer-substring-no-properties
219                                            (match-beginning 1)
220                                            (match-end 1))))
221                        ))))
222             (if (and pgp-id
223                      (y-or-n-p
224                       (format "Key %s not found; attempt to fetch? " pgp-id))
225                      )
226                 (progn
227                   (funcall (pgp-function 'fetch-key) (cons nil pgp-id))
228                   (mime::article/call-pgp-to-check-signature
229                    mime-echo-buffer-name orig-file)
230                   ))
231             ))
232       (let ((other-window-scroll-buffer mime-echo-buffer-name))
233         (scroll-other-window 8)
234         )
235       (kill-buffer kbuf)
236       (delete-file orig-file)
237       (delete-file sig-file)
238       )))
239
240 (set-atype 'mime/content-decoding-condition
241            '((type . "application/pgp-signature")
242              (method . mime-article/check-pgp-signature)
243              ))
244
245
246 ;;; @ Internal method for application/pgp-encrypted
247 ;;;
248 ;;; It is based on RFC 2015.
249
250 (defun mime-article/decrypt-pgp (beg end cal)
251   (let* ((cnum (mime-article/point-content-number beg))
252          (rcnum (reverse cnum))
253          (rmcnum (cdr rcnum))
254          (knum (car rcnum))
255          (onum (if (> knum 0)
256                    (1- knum)
257                  (1+ knum)))
258          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
259                                              mime::article/content-info))
260          (obeg (mime::content-info/point-min oinfo))
261          (oend (mime::content-info/point-max oinfo))
262          )
263     (mime-article/view-application/pgp obeg oend cal)
264     ))
265
266 (set-atype 'mime/content-decoding-condition
267            '((type . "application/pgp-encrypted")
268              (method . mime-article/decrypt-pgp)
269              ))
270
271
272 ;;; @ Internal method for application/pgp-keys
273 ;;;
274 ;;; It is based on RFC 2015.
275
276 (defun mime-article/add-pgp-keys (beg end cal)
277   (let* ((cnum (mime-article/point-content-number beg))
278          (new-name (format "%s-%s" (buffer-name) cnum))
279          (encoding (cdr (assq 'encoding cal)))
280          str)
281     (setq str (buffer-substring beg end))
282     (switch-to-buffer new-name)
283     (setq buffer-read-only nil)
284     (erase-buffer)
285     (insert str)
286     (goto-char (point-min))
287     (if (re-search-forward "^\n" nil t)
288         (delete-region (point-min) (match-end 0))
289       )
290     (mime-decode-region (point-min)(point-max) encoding)
291     (funcall (pgp-function 'snarf-keys))
292     (kill-buffer (current-buffer))
293     ))
294
295 (set-atype 'mime/content-decoding-condition
296            '((type . "application/pgp-keys")
297              (method . mime-article/add-pgp-keys)
298              ))
299
300          
301 ;;; @ end
302 ;;;
303
304 (provide 'mime-pgp)
305
306 (run-hooks 'mime-pgp-load-hook)
307
308 ;;; mime-pgp.el ends here