tm 7.52.1.
[elisp/tm.git] / tm-play.el
1 ;;;
2 ;;; tm-play.el --- decoder for tm-view.el
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1994 .. 1996 MORIOKA Tomohiko
6 ;;;
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; Created: 1995/9/26 (separated from tm-view.el)
10 ;;; Version:
11 ;;;     $Id: tm-play.el,v 7.16 1996/04/21 19:57:22 morioka Exp $
12 ;;; Keywords: mail, news, MIME, multimedia
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 ;;; Code:
31
32 (require 'tm-view)
33
34 (defvar mime-viewer/external-progs "/usr/local/share/tm/"
35   "*Directory containing tm external methods.")
36
37
38 ;;; @ content decoder
39 ;;;
40
41 (defvar mime-preview/after-decoded-position nil)
42
43 (defun mime-preview/decode-content ()
44   (interactive)
45   (let ((pc (mime-preview/point-pcinfo (point))))
46     (if pc
47         (let ((the-buf (current-buffer)))
48           (setq mime-preview/after-decoded-position (point))
49           (set-buffer (mime::preview-content-info/buffer pc))
50           (mime-article/decode-content
51            (mime::preview-content-info/content-info pc))
52           (if (eq (current-buffer)
53                   (mime::preview-content-info/buffer pc))
54               (progn
55                 (set-buffer the-buf)
56                 (goto-char mime-preview/after-decoded-position)
57                 ))
58           ))))
59
60
61 (defun mime-article/decode-content (cinfo)
62   (let ((beg (mime::content-info/point-min cinfo))
63         (end (mime::content-info/point-max cinfo))
64         (ctype (mime::content-info/type cinfo))
65         (params (mime::content-info/parameters cinfo))
66         (encoding (mime::content-info/encoding cinfo))
67         )
68     ;; Check for VM
69     (if (< beg (point-min))
70         (setq beg (point-min))
71       )
72     (if (< (point-max) end)
73         (setq end (point-max))
74       )
75     (if ctype
76         (let (method cal ret)
77           (setq cal (append (list (cons 'type ctype)
78                                   (cons 'encoding encoding)
79                                   (cons 'major-mode major-mode)
80                                   )
81                             params))
82           (if mime-viewer/decoding-mode
83               (setq cal (cons
84                          (cons 'mode mime-viewer/decoding-mode)
85                          cal))
86             )
87           (setq ret (mime/get-content-decoding-alist cal))
88           (setq method (cdr (assoc 'method ret)))
89           (cond ((and (symbolp method)
90                       (fboundp method))
91                  (funcall method beg end ret)
92                  )
93                 ((and (listp method)(stringp (car method)))
94                  (mime-article/start-external-method-region beg end ret)
95                  )
96                 (t
97                  (mime-article/show-output-buffer
98                   "No method are specified for %s\n" ctype)
99                  ))
100           ))
101     ))
102
103 (defun mime/get-content-decoding-alist (al)
104   (get-unified-alist mime/content-decoding-condition al)
105   )
106
107
108 ;;; @ external decoder
109 ;;;
110
111 (defun mime-article/start-external-method-region (beg end cal)
112   (save-excursion
113     (save-restriction
114       (narrow-to-region beg end)
115       (goto-char beg)
116       (let ((method (cdr (assoc 'method cal)))
117             (name (mime-article/get-filename cal))
118             )
119         (if method
120             (let ((file (make-temp-name
121                          (expand-file-name "TM" mime/tmp-dir)))
122                   b args)
123               (if (nth 1 method)
124                   (setq b beg)
125                 (setq b
126                       (if (re-search-forward "^$" nil t)
127                           (1+ (match-end 0))
128                         (point-min)
129                         ))
130                 )
131               (goto-char b)
132               (write-region b end file)
133               (message "External method is starting...")
134               (setq cal (put-alist
135                          'name (replace-as-filename name) cal))
136               (setq cal (put-alist 'file file cal))
137               (setq args (nconc
138                           (list (car method)
139                                 mime/output-buffer-name (car method)
140                                 )
141                           (mime-article/make-method-args cal
142                                                          (cdr (cdr method)))
143                           ))
144               (let ((process-environment
145                      (let ((rest process-environment)
146                            dest cell)
147                        (while (setq cell (car rest))
148                          (setq dest
149                                (cons
150                                 (if (string-match "^PATH=" cell)
151                                     (format "PATH=%s:%s"
152                                             mime-viewer/external-progs
153                                             (substring cell (match-end 0)))
154                                   cell)
155                                 dest))
156                          (setq rest (cdr rest))
157                          )
158                        (nconc (nreverse dest) rest)
159                        )))
160                 (apply (function start-process) args)
161                 )
162               (mime-article/show-output-buffer)
163               ))
164         ))))
165
166 (defun mime-article/make-method-args (cal format)
167   (mapcar (function
168            (lambda (arg)
169              (if (stringp arg)
170                  arg
171                (let* ((item (eval arg))
172                       (ret (cdr (assoc item cal)))
173                       )
174                  (if ret
175                      ret
176                    (if (eq item 'encoding)
177                        "7bit"
178                      ""))
179                  ))
180              ))
181           format))
182
183 (defun mime-article/show-output-buffer (&rest forms)
184   (get-buffer-create mime/output-buffer-name)
185   (let ((the-win (selected-window))
186         (win (get-buffer-window mime/output-buffer-name))
187         )
188     (if (null win)
189         (progn
190           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
191           (set-window-buffer win mime/output-buffer-name)
192           ))
193     (select-window win)
194     (goto-char (point-max))
195     (if forms
196         (insert (apply (function format) forms))
197       )
198     (select-window the-win)
199     ))
200
201
202 ;;; @ file name
203 ;;;
204
205 (defvar mime-viewer/file-name-char-regexp "[A-Za-z0-9+_-]")
206
207 (defvar mime-viewer/file-name-regexp-1
208   (concat mime-viewer/file-name-char-regexp "+\\."
209           mime-viewer/file-name-char-regexp "+"))
210
211 (defvar mime-viewer/file-name-regexp-2
212   (concat (regexp-* mime-viewer/file-name-char-regexp)
213           "\\(\\." mime-viewer/file-name-char-regexp "+\\)*"))
214
215 (defun mime-article/get-original-filename (param &optional encoding)
216   (or (mime-article/get-uu-filename param encoding)
217       (let (ret)
218         (or (if (or (and (setq ret (mime/Content-Disposition))
219                          (setq ret (assoc "filename" (cdr ret)))
220                          )
221                     (setq ret (assoc "name" param))
222                     (setq ret (assoc "x-name" param))
223                     )
224                 (rfc822/strip-quoted-string (cdr ret))
225               )
226             (if (setq ret
227                       (or (rfc822/get-field-body "Content-Description")
228                           (rfc822/get-field-body "Subject")
229                           ))
230                 (if (or (string-match mime-viewer/file-name-regexp-1 ret)
231                         (string-match mime-viewer/file-name-regexp-2 ret))
232                     (substring ret (match-beginning 0)(match-end 0))
233                   ))
234             ))
235       ))
236
237 (defun mime-article/get-filename (param)
238   (replace-as-filename (mime-article/get-original-filename param))
239   )
240
241
242 ;;; @ mail/news message
243 ;;;
244
245 (defun mime-viewer/quitting-method-for-mime/show-message-mode ()
246   (let ((mother mime::preview/mother-buffer)
247         (win-conf mime::preview/original-window-configuration)
248         )
249     (kill-buffer
250      (mime::preview-content-info/buffer (car mime::preview/content-list)))
251     (mime-viewer/kill-buffer)
252     (set-window-configuration win-conf)
253     (pop-to-buffer mother)
254     ;;(goto-char (point-min))
255     ;;(mime-viewer/up-content)
256     ))
257
258 (defun mime-article/view-message/rfc822 (beg end cal)
259   (let* ((cnum (mime-article/point-content-number beg))
260          (cur-buf (current-buffer))
261          (new-name (format "%s-%s" (buffer-name) cnum))
262          (mother mime::article/preview-buffer)
263          (code-converter
264           (or (cdr (assq major-mode mime-viewer/code-converter-alist))
265               'mime-viewer/default-code-convert-region))
266          str)
267     (setq str (buffer-substring beg end))
268     (switch-to-buffer new-name)
269     (erase-buffer)
270     (insert str)
271     (goto-char (point-min))
272     (if (re-search-forward "^\n" nil t)
273         (delete-region (point-min) (match-end 0))
274       )
275     (setq major-mode 'mime/show-message-mode)
276     (setq mime::article/code-converter code-converter)
277     (mime/viewer-mode mother)
278     ))
279
280
281 ;;; @ message/partial
282 ;;;
283
284 (defvar mime-article/coding-system-alist
285   (and (boundp 'MULE)
286        '((mh-show-mode . *noconv*)
287          (t            . *ctext*)
288          )))             
289
290 (defvar mime-article/kanji-code-alist
291   (and (boundp 'NEMACS)
292        '((mh-show-mode . nil)
293          (t            . 2)
294          ))) 
295
296 (defun mime-article/decode-message/partial (beg end cal)
297   (goto-char beg)
298   (let* ((root-dir (expand-file-name
299                     (concat "m-prts-" (user-login-name)) mime/tmp-dir))
300          (id (cdr (assoc "id" cal)))
301          (number (cdr (assoc "number" cal)))
302          (total (cdr (assoc "total" cal)))
303          (the-buf (current-buffer))
304          file
305          (mother mime::article/preview-buffer)
306          (win-conf (save-excursion
307                      (set-buffer mother)
308                      mime::preview/original-window-configuration))
309          )
310     (if (not (file-exists-p root-dir))
311         (make-directory root-dir)
312       )
313     (setq id (replace-as-filename id))
314     (setq root-dir (concat root-dir "/" id))
315     (if (not (file-exists-p root-dir))
316         (make-directory root-dir)
317       )
318     (setq file (concat root-dir "/FULL"))
319     (if (not (file-exists-p file))
320         (progn
321           (re-search-forward "^$")
322           (goto-char (1+ (match-end 0)))
323           (setq file (concat root-dir "/" number))
324           (let ((file-coding-system
325                  (cdr
326                   (or (assq major-mode mime-article/coding-system-alist)
327                       (assq t mime-article/coding-system-alist)
328                       )))
329                 (kanji-fileio-code
330                  (cdr
331                   (or (assq major-mode mime-article/kanji-code-alist)
332                       (assq t mime-article/kanji-code-alist)
333                       )))
334                 )
335             (write-region (point) (point-max) file)
336             )
337           (if (get-buffer mime/temp-buffer-name)
338               (kill-buffer mime/temp-buffer-name)
339             )
340           (switch-to-buffer mime/temp-buffer-name)
341           (let ((i 1)
342                 (max (string-to-int total))
343                 (file-coding-system-for-read (if (boundp 'MULE)
344                                                  *noconv*))
345                 kanji-fileio-code)
346             (catch 'tag
347               (while (<= i max)
348                 (setq file (concat root-dir "/" (int-to-string i)))
349                 (if (not (file-exists-p file))
350                     (progn
351                       (switch-to-buffer the-buf)
352                       (throw 'tag nil)
353                       ))
354                 (insert-file-contents file)
355                 (goto-char (point-max))
356                 (setq i (1+ i))
357                 )
358               ;;(delete-other-windows)
359               (let ((buf (current-buffer)))
360                 (write-file (concat root-dir "/FULL"))
361                 (set-window-configuration win-conf)
362                 (let ((win (get-buffer-window mother)))
363                   (if win
364                       (select-window win)
365                     ))
366                 (set-window-buffer (selected-window) buf)
367                 ;;(set-window-buffer buf)
368                 (setq major-mode 'mime/show-message-mode)
369                 )
370               (mime/viewer-mode mother)
371               (pop-to-buffer (current-buffer))
372               ))
373           )
374       (progn
375         ;;(delete-other-windows)
376         (set-window-configuration win-conf)
377         (select-window (get-buffer-window mother))
378         (let ((file-coding-system-for-read
379                (if (boundp 'MULE) *noconv*))
380               kanji-fileio-code)
381           (set-buffer (get-buffer-create "FULL"))
382           (insert-file-contents file)
383           )
384         (setq major-mode 'mime/show-message-mode)
385         (mime/viewer-mode mother)
386         ;;(pop-to-buffer (current-buffer))
387         ))
388     ))
389
390
391 ;;; @ rot13-47
392 ;;;
393
394 (defun mime-article/decode-caesar (beg end cal)
395   (let* ((cnum (mime-article/point-content-number beg))
396          (cur-buf (current-buffer))
397          (new-name (format "%s-%s" (buffer-name) cnum))
398          (mother mime::article/preview-buffer)
399          (charset (cdr (assoc "charset" cal)))
400          (encoding (cdr (assq 'encoding cal)))
401          (mode major-mode)
402          str)
403     (setq str (buffer-substring beg end))
404     (switch-to-buffer new-name)
405     (setq buffer-read-only nil)
406     (erase-buffer)
407     (insert str)
408     (goto-char (point-min))
409     (if (re-search-forward "^\n" nil t)
410         (delete-region (point-min) (match-end 0))
411       )
412     (let ((m (assq mode mime-viewer/code-converter-alist)))
413       (if (and m (fboundp (setq m (cdr m))))
414           (funcall m (point-min) (point-max) charset encoding)
415         (mime-viewer/default-code-convert-region (point-min) (point-max)
416                                                  charset encoding)
417         ))
418     (save-excursion
419       (set-mark (point-min))
420       (goto-char (point-max))
421       (tm:caesar-region)
422       )
423     (view-mode)
424     ))
425
426
427 ;;; @ end
428 ;;;
429
430 (provide 'tm-play)
431
432 ;;; tm-play.el ends here