This commit was generated by cvs2svn to compensate for changes in r434,
[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.20 1996/07/15 14:04:46 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     (or win
201         (if (and mime/output-buffer-window-is-shared-with-bbdb
202                  (boundp 'bbdb-buffer-name)
203                  (setq win (get-buffer-window bbdb-buffer-name))
204                  )
205             (set-window-buffer win mime/output-buffer-name)
206           (select-window (get-buffer-window mime::article/preview-buffer))
207           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
208           (set-window-buffer win mime/output-buffer-name)
209           ))
210     (select-window win)
211     (goto-char (point-max))
212     (if forms
213         (insert (apply (function format) forms))
214       )
215     (select-window the-win)
216     ))
217
218
219 ;;; @ file name
220 ;;;
221
222 (defvar mime-viewer/file-name-char-regexp "[A-Za-z0-9+_-]")
223
224 (defvar mime-viewer/file-name-regexp-1
225   (concat mime-viewer/file-name-char-regexp "+\\."
226           mime-viewer/file-name-char-regexp "+"))
227
228 (defvar mime-viewer/file-name-regexp-2
229   (concat (regexp-* mime-viewer/file-name-char-regexp)
230           "\\(\\." mime-viewer/file-name-char-regexp "+\\)*"))
231
232 (defun mime-article/get-original-filename (param &optional encoding)
233   (or (mime-article/get-uu-filename param encoding)
234       (let (ret)
235         (or (if (or (and (setq ret (mime/Content-Disposition))
236                          (setq ret (assoc "filename" (cdr ret)))
237                          )
238                     (setq ret (assoc "name" param))
239                     (setq ret (assoc "x-name" param))
240                     )
241                 (rfc822/strip-quoted-string (cdr ret))
242               )
243             (if (setq ret
244                       (or (rfc822/get-field-body "Content-Description")
245                           (rfc822/get-field-body "Subject")
246                           ))
247                 (if (or (string-match mime-viewer/file-name-regexp-1 ret)
248                         (string-match mime-viewer/file-name-regexp-2 ret))
249                     (substring ret (match-beginning 0)(match-end 0))
250                   ))
251             ))
252       ))
253
254 (defun mime-article/get-filename (param)
255   (replace-as-filename (mime-article/get-original-filename param))
256   )
257
258
259 ;;; @ mail/news message
260 ;;;
261
262 (defun mime-viewer/quitting-method-for-mime/show-message-mode ()
263   (let ((mother mime::preview/mother-buffer)
264         (win-conf mime::preview/original-window-configuration)
265         )
266     (kill-buffer
267      (mime::preview-content-info/buffer (car mime::preview/content-list)))
268     (mime-viewer/kill-buffer)
269     (set-window-configuration win-conf)
270     (pop-to-buffer mother)
271     ;;(goto-char (point-min))
272     ;;(mime-viewer/up-content)
273     ))
274
275 (defun mime-article/view-message/rfc822 (beg end cal)
276   (let* ((cnum (mime-article/point-content-number beg))
277          (cur-buf (current-buffer))
278          (new-name (format "%s-%s" (buffer-name) cnum))
279          (mother mime::article/preview-buffer)
280          (code-converter
281           (or (cdr (assq major-mode mime-viewer/code-converter-alist))
282               'mime-viewer/default-code-convert-region))
283          str)
284     (setq str (buffer-substring beg end))
285     (switch-to-buffer new-name)
286     (erase-buffer)
287     (insert str)
288     (goto-char (point-min))
289     (if (re-search-forward "^\n" nil t)
290         (delete-region (point-min) (match-end 0))
291       )
292     (setq major-mode 'mime/show-message-mode)
293     (setq mime::article/code-converter code-converter)
294     (mime/viewer-mode mother)
295     ))
296
297
298 ;;; @ message/partial
299 ;;;
300
301 (defvar mime-article/coding-system-alist
302   (and (boundp 'MULE)
303        '((mh-show-mode . *noconv*)
304          (t            . *ctext*)
305          )))             
306
307 (defvar mime-article/kanji-code-alist
308   (and (boundp 'NEMACS)
309        '((mh-show-mode . nil)
310          (t            . 2)
311          ))) 
312
313 (defun mime-article/decode-message/partial (beg end cal)
314   (goto-char beg)
315   (let* ((root-dir (expand-file-name
316                     (concat "m-prts-" (user-login-name)) mime/tmp-dir))
317          (id (cdr (assoc "id" cal)))
318          (number (cdr (assoc "number" cal)))
319          (total (cdr (assoc "total" cal)))
320          (the-buf (current-buffer))
321          file
322          (mother mime::article/preview-buffer)
323          (win-conf (save-excursion
324                      (set-buffer mother)
325                      mime::preview/original-window-configuration))
326          )
327     (if (not (file-exists-p root-dir))
328         (make-directory root-dir)
329       )
330     (setq id (replace-as-filename id))
331     (setq root-dir (concat root-dir "/" id))
332     (if (not (file-exists-p root-dir))
333         (make-directory root-dir)
334       )
335     (setq file (concat root-dir "/FULL"))
336     (if (not (file-exists-p file))
337         (progn
338           (re-search-forward "^$")
339           (goto-char (1+ (match-end 0)))
340           (setq file (concat root-dir "/" number))
341           (let ((file-coding-system
342                  (cdr
343                   (or (assq major-mode mime-article/coding-system-alist)
344                       (assq t mime-article/coding-system-alist)
345                       )))
346                 (kanji-fileio-code
347                  (cdr
348                   (or (assq major-mode mime-article/kanji-code-alist)
349                       (assq t mime-article/kanji-code-alist)
350                       )))
351                 )
352             (write-region (point) (point-max) file)
353             )
354           (if (get-buffer mime/temp-buffer-name)
355               (kill-buffer mime/temp-buffer-name)
356             )
357           (switch-to-buffer mime/temp-buffer-name)
358           (let ((i 1)
359                 (max (string-to-int total))
360                 (file-coding-system-for-read (if (boundp 'MULE)
361                                                  *noconv*))
362                 kanji-fileio-code)
363             (catch 'tag
364               (while (<= i max)
365                 (setq file (concat root-dir "/" (int-to-string i)))
366                 (if (not (file-exists-p file))
367                     (progn
368                       (switch-to-buffer the-buf)
369                       (throw 'tag nil)
370                       ))
371                 (insert-file-contents file)
372                 (goto-char (point-max))
373                 (setq i (1+ i))
374                 )
375               ;;(delete-other-windows)
376               (let ((buf (current-buffer)))
377                 (write-file (concat root-dir "/FULL"))
378                 (set-window-configuration win-conf)
379                 (let ((win (get-buffer-window mother)))
380                   (if win
381                       (select-window win)
382                     ))
383                 (set-window-buffer (selected-window) buf)
384                 ;;(set-window-buffer buf)
385                 (setq major-mode 'mime/show-message-mode)
386                 )
387               (mime/viewer-mode mother)
388               (pop-to-buffer (current-buffer))
389               ))
390           )
391       (progn
392         ;;(delete-other-windows)
393         (set-window-configuration win-conf)
394         (select-window (get-buffer-window mother))
395         (let ((file-coding-system-for-read
396                (if (boundp 'MULE) *noconv*))
397               kanji-fileio-code)
398           (set-buffer (get-buffer-create "FULL"))
399           (insert-file-contents file)
400           )
401         (setq major-mode 'mime/show-message-mode)
402         (mime/viewer-mode mother)
403         ;;(pop-to-buffer (current-buffer))
404         ))
405     ))
406
407
408 ;;; @ rot13-47
409 ;;;
410
411 (defun mime-article/decode-caesar (beg end cal)
412   (let* ((cnum (mime-article/point-content-number beg))
413          (cur-buf (current-buffer))
414          (new-name (format "%s-%s" (buffer-name) cnum))
415          (mother mime::article/preview-buffer)
416          (charset (cdr (assoc "charset" cal)))
417          (encoding (cdr (assq 'encoding cal)))
418          (mode major-mode)
419          str)
420     (setq str (buffer-substring beg end))
421     (switch-to-buffer new-name)
422     (setq buffer-read-only nil)
423     (erase-buffer)
424     (insert str)
425     (goto-char (point-min))
426     (if (re-search-forward "^\n" nil t)
427         (delete-region (point-min) (match-end 0))
428       )
429     (let ((m (assq mode mime-viewer/code-converter-alist)))
430       (if (and m (fboundp (setq m (cdr m))))
431           (funcall m (point-min) (point-max) charset encoding)
432         (mime-viewer/default-code-convert-region (point-min) (point-max)
433                                                  charset encoding)
434         ))
435     (save-excursion
436       (set-mark (point-min))
437       (goto-char (point-max))
438       (tm:caesar-region)
439       )
440     (view-mode)
441     ))
442
443
444 ;;; @ end
445 ;;;
446
447 (provide 'tm-play)
448
449 ;;; tm-play.el ends here