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