33cdda5b17c7fa40171effdc0cd638bf7cab6149
[elisp/tm.git] / tm-pgp.el
1 ;;;
2 ;;; tm-pgp.el --- tm-view internal methods for PGP.
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1995 MORIOKA Tomohiko
6 ;;;
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; Created: 1995/12/7
10 ;;; Version:
11 ;;;     $Id: tm-pgp.el,v 7.4 1995/12/19 18:06:49 morioka Exp $
12 ;;; Keywords: mail, news, MIME, multimedia, PGP, security
13 ;;;
14 ;;; This file is part of tm (Tools for MIME).
15 ;;;
16 ;;; This program is free software; you can redistribute it and/or
17 ;;; modify it under the terms of the GNU General Public License as
18 ;;; published by the Free Software Foundation; either version 2, or
19 ;;; (at your option) any later version.
20 ;;;
21 ;;; This program is distributed in the hope that it will be useful,
22 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 ;;; General Public License for more details.
25 ;;;
26 ;;; You should have received a copy of the GNU General Public License
27 ;;; along with This program.  If not, write to the Free Software
28 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;;;
30 ;;; Commentary:
31 ;;;    This module is based on 2 drafts about PGP MIME integration:
32 ;;;     - draft-elkins-pem-pgp-02.txt
33 ;;;             ``MIME Security with Pretty Good Privacy (PGP)''
34 ;;;             by Michael Elkins <elkins@aero.org> (1995/11)
35 ;;;     - draft-kazu-pgp-mime-00.txt
36 ;;;             ``PGP MIME Integration''
37 ;;;             by Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp> (1995/10)
38 ;;;    These drafts may be contrary to each other. You should decide
39 ;;;  which you support.
40 ;;;
41 ;;; Code:
42
43 (require 'mailcrypt)
44 (require 'tm-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          (new-name (format "%s-%s" (buffer-name) cnum))
55          (mother mime::article/preview-buffer)
56          (code-converter
57           (or (cdr (assq major-mode mime-viewer/code-converter-alist))
58               'mime-viewer/default-code-convert-region))
59          str)
60     (setq str (buffer-substring beg end))
61     (switch-to-buffer 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            (mc-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            )
86           ((progn
87              (goto-char (point-min))
88              (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t)
89              )
90            (mc-decrypt)
91            (goto-char (point-min))
92            (delete-region (point-min)
93                           (and
94                            (search-forward "\n\n")
95                            (match-end 0)))
96            ))
97     (setq major-mode 'mime/show-message-mode)
98     (setq mime::article/code-converter code-converter)
99     (mime/viewer-mode mother)
100     ))
101
102 (set-atype 'mime/content-decoding-condition
103            '((type . "application/pgp")
104              (method . mime-article/view-application/pgp)
105              ))
106
107 (set-atype 'mime/content-decoding-condition
108            '((type . "text/x-pgp")
109              (method . mime-article/view-application/pgp)
110              ))
111
112
113 ;;; @ Internal method for application/pgp-signature
114 ;;;
115 ;;; It is based on draft-elkins-pem-pgp-01.txt
116
117 (defun mime-article/check-pgp-signature (beg end cal)
118   (let* ((encoding (cdr (assq 'encoding cal)))
119          (cnum (mime-article/point-content-number beg))
120          (rcnum (reverse cnum))
121          (rmcnum (cdr rcnum))
122          (knum (car rcnum))
123          (onum (if (> knum 0)
124                    (1- knum)
125                  (1+ knum)))
126          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
127                                              mime::article/content-info))
128          status str kbuf
129          (basename (expand-file-name "tm" mime/tmp-dir))
130          (orig-file (make-temp-name basename))
131          (sig-file (concat orig-file ".sig"))
132          )
133     (save-excursion
134       (setq str (buffer-substring
135                  (mime::content-info/point-min oinfo)
136                  (mime::content-info/point-max oinfo)
137                  ))
138       (set-buffer (get-buffer-create mime/temp-buffer-name))
139       (insert str)
140       (goto-char (point-min))
141       (while (re-search-forward "\n" nil t)
142         (replace-match "\r\n")
143         )
144       (write-file orig-file)
145       (kill-buffer (current-buffer))
146       )
147     (save-excursion
148       (setq str (buffer-substring
149                  (save-excursion
150                    (goto-char beg)
151                    (and (search-forward "\n\n")
152                         (match-end 0)))
153                  end))
154       (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
155       (insert str)
156       (mime/decode-region encoding (point-min)(point-max))
157       (let ((mc-flag nil)                   ; for Mule
158             (file-coding-system
159              (if (featurep 'mule) *noconv*))
160             kanji-flag                      ; for NEmacs
161             (emx-binary-mode t)             ; for OS/2
162             jka-compr-compression-info-list ; for jka-compr
163             jam-zcat-filename-list          ; for jam-zcat
164             require-final-newline)
165         (write-file sig-file)
166         )
167       ;;(define-program-coding-system (current-buffer) "pgp" *noconv*)
168       (mime-article/show-output-buffer)
169       (setq status
170             (call-process-region (point-min)(point-max)
171                                  "pgp"
172                                  nil mime/output-buffer-name nil
173                                  orig-file))
174       (kill-buffer kbuf)
175       (let ((other-window-scroll-buffer mime/output-buffer-name))
176         (scroll-other-window 8)
177         )
178       ;; (if (= status 0)
179       ;;     (message "Good signature!")
180       ;;   (message "Bad signature!")
181       ;;   )
182       (delete-file orig-file)
183       (delete-file sig-file)
184       )))
185
186 (set-atype 'mime/content-decoding-condition
187            '((type . "application/pgp-signature")
188              (method . mime-article/check-pgp-signature)
189              ))
190
191
192 ;;; @ Internal method for application/pgp-encrypted
193 ;;;
194 ;;; It is based on draft-elkins-pem-pgp-01.txt
195
196 (defun mime-article/decrypt-pgp (beg end cal)
197   (let* ((cnum (mime-article/point-content-number beg))
198          (rcnum (reverse cnum))
199          (rmcnum (cdr rcnum))
200          (knum (car rcnum))
201          (onum (if (> knum 0)
202                    (1- knum)
203                  (1+ knum)))
204          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
205                                              mime::article/content-info))
206          (obeg (mime::content-info/point-min oinfo))
207          (oend (mime::content-info/point-max oinfo))
208          )
209     (mime-article/view-application/pgp obeg oend cal)
210     ))
211
212 (set-atype 'mime/content-decoding-condition
213            '((type . "application/pgp-encrypted")
214              (method . mime-article/decrypt-pgp)
215              ))
216
217
218 ;;; @ Internal method for application/pgp-keys
219 ;;;
220 ;;; It is based on draft-elkins-pem-pgp-01.txt
221
222 (autoload 'mc-snarf-keys "mc-toplev")
223
224 (defun mime-article/add-pgp-keys (beg end cal)
225   (let* ((cnum (mime-article/point-content-number beg))
226          (cur-buf (current-buffer))
227          (new-name (format "%s-%s" (buffer-name) cnum))
228          (mother mime::article/preview-buffer)
229          (charset (cdr (assoc "charset" cal)))
230          (encoding (cdr (assq 'encoding cal)))
231          (mode major-mode)
232          str)
233     (setq str (buffer-substring beg end))
234     (switch-to-buffer new-name)
235     (setq buffer-read-only nil)
236     (erase-buffer)
237     (insert str)
238     (goto-char (point-min))
239     (if (re-search-forward "^\n" nil t)
240         (delete-region (point-min) (match-end 0))
241       )
242     (mime/decode-region encoding (point-min)(point-max))
243     (mc-snarf-keys)
244     (kill-buffer (current-buffer))
245     ))
246
247 (set-atype 'mime/content-decoding-condition
248            '((type . "application/pgp-keys")
249              (method . mime-article/add-pgp-keys)
250              ))
251
252          
253 ;;; @ end
254 ;;;
255
256 (provide 'tm-pgp)