tm 7.38.
[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.5 1995/12/21 18:45:26 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       (let ((mc-flag nil)                   ; for Mule
145             (file-coding-system
146              (if (featurep 'mule) *noconv*))
147             kanji-flag                      ; for NEmacs
148             (emx-binary-mode t)             ; for OS/2
149             jka-compr-compression-info-list ; for jka-compr
150             jam-zcat-filename-list          ; for jam-zcat
151             require-final-newline)
152         (write-file orig-file)
153         )
154       (kill-buffer (current-buffer))
155       )
156     (save-excursion
157       (setq str (buffer-substring
158                  (save-excursion
159                    (goto-char beg)
160                    (and (search-forward "\n\n")
161                         (match-end 0)))
162                  end))
163       (set-buffer (setq kbuf (get-buffer-create mime/temp-buffer-name)))
164       (insert str)
165       (mime/decode-region encoding (point-min)(point-max))
166       (let ((mc-flag nil)                   ; for Mule
167             (file-coding-system
168              (if (featurep 'mule) *noconv*))
169             kanji-flag                      ; for NEmacs
170             (emx-binary-mode t)             ; for OS/2
171             jka-compr-compression-info-list ; for jka-compr
172             jam-zcat-filename-list          ; for jam-zcat
173             require-final-newline)
174         (write-file sig-file)
175         )
176       ;;(define-program-coding-system (current-buffer) "pgp" *noconv*)
177       (mime-article/show-output-buffer)
178       (setq status
179             (call-process-region (point-min)(point-max)
180                                  "pgp"
181                                  nil mime/output-buffer-name nil
182                                  orig-file))
183       (kill-buffer kbuf)
184       (let ((other-window-scroll-buffer mime/output-buffer-name))
185         (scroll-other-window 8)
186         )
187       ;; (if (= status 0)
188       ;;     (message "Good signature!")
189       ;;   (message "Bad signature!")
190       ;;   )
191       (delete-file orig-file)
192       (delete-file sig-file)
193       )))
194
195 (set-atype 'mime/content-decoding-condition
196            '((type . "application/pgp-signature")
197              (method . mime-article/check-pgp-signature)
198              ))
199
200
201 ;;; @ Internal method for application/pgp-encrypted
202 ;;;
203 ;;; It is based on draft-elkins-pem-pgp-01.txt
204
205 (defun mime-article/decrypt-pgp (beg end cal)
206   (let* ((cnum (mime-article/point-content-number beg))
207          (rcnum (reverse cnum))
208          (rmcnum (cdr rcnum))
209          (knum (car rcnum))
210          (onum (if (> knum 0)
211                    (1- knum)
212                  (1+ knum)))
213          (oinfo (mime-article/rcnum-to-cinfo (cons onum rmcnum)
214                                              mime::article/content-info))
215          (obeg (mime::content-info/point-min oinfo))
216          (oend (mime::content-info/point-max oinfo))
217          )
218     (mime-article/view-application/pgp obeg oend cal)
219     ))
220
221 (set-atype 'mime/content-decoding-condition
222            '((type . "application/pgp-encrypted")
223              (method . mime-article/decrypt-pgp)
224              ))
225
226
227 ;;; @ Internal method for application/pgp-keys
228 ;;;
229 ;;; It is based on draft-elkins-pem-pgp-01.txt
230
231 (autoload 'mc-snarf-keys "mc-toplev")
232
233 (defun mime-article/add-pgp-keys (beg end cal)
234   (let* ((cnum (mime-article/point-content-number beg))
235          (cur-buf (current-buffer))
236          (new-name (format "%s-%s" (buffer-name) cnum))
237          (mother mime::article/preview-buffer)
238          (charset (cdr (assoc "charset" cal)))
239          (encoding (cdr (assq 'encoding cal)))
240          (mode major-mode)
241          str)
242     (setq str (buffer-substring beg end))
243     (switch-to-buffer new-name)
244     (setq buffer-read-only nil)
245     (erase-buffer)
246     (insert str)
247     (goto-char (point-min))
248     (if (re-search-forward "^\n" nil t)
249         (delete-region (point-min) (match-end 0))
250       )
251     (mime/decode-region encoding (point-min)(point-max))
252     (mc-snarf-keys)
253     (kill-buffer (current-buffer))
254     ))
255
256 (set-atype 'mime/content-decoding-condition
257            '((type . "application/pgp-keys")
258              (method . mime-article/add-pgp-keys)
259              ))
260
261          
262 ;;; @ end
263 ;;;
264
265 (provide 'tm-pgp)