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