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