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