tm 7.99.
[elisp/tm.git] / tm-play.el
1 ;;; tm-play.el --- decoder for tm-view.el
2
3 ;; Copyright (C) 1994,1995,1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/9/26 (separated from tm-view.el)
7 ;; Version: $Id: tm-play.el,v 7.33 1996/12/07 08:26:26 morioka Exp $
8 ;; Keywords: mail, news, MIME, multimedia
9
10 ;; This file is part of tm (Tools for MIME).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 (require 'tm-view)
30
31   
32 ;;; @ content decoder
33 ;;;
34
35 (defvar mime-preview/after-decoded-position nil)
36
37 (defun mime-preview/decode-content ()
38   (interactive)
39   (let ((pc (mime-preview/point-pcinfo (point))))
40     (if pc
41         (let ((the-buf (current-buffer)))
42           (setq mime-preview/after-decoded-position (point))
43           (set-buffer (mime::preview-content-info/buffer pc))
44           (mime-article/decode-content
45            (mime::preview-content-info/content-info pc))
46           (if (eq (current-buffer)
47                   (mime::preview-content-info/buffer pc))
48               (progn
49                 (set-buffer the-buf)
50                 (goto-char mime-preview/after-decoded-position)
51                 ))
52           ))))
53
54 (defun mime-article/decode-content (cinfo)
55   (let ((beg (mime::content-info/point-min cinfo))
56         (end (mime::content-info/point-max cinfo))
57         (ctype (or (mime::content-info/type cinfo) "text/plain"))
58         (params (mime::content-info/parameters cinfo))
59         (encoding (mime::content-info/encoding cinfo))
60         )
61     ;; Check for VM
62     (if (< beg (point-min))
63         (setq beg (point-min))
64       )
65     (if (< (point-max) end)
66         (setq end (point-max))
67       )
68     (let (method cal ret)
69       (setq cal (list* (cons 'type ctype)
70                        (cons 'encoding encoding)
71                        (cons 'major-mode major-mode)
72                        params))
73       (if mime-viewer/decoding-mode
74           (setq cal (cons
75                      (cons 'mode mime-viewer/decoding-mode)
76                      cal))
77         )
78       (setq ret (mime/get-content-decoding-alist cal))
79       (setq method (cdr (assq 'method ret)))
80       (cond ((and (symbolp method)
81                   (fboundp method))
82              (funcall method beg end ret)
83              )
84             ((and (listp method)(stringp (car method)))
85              (mime-article/start-external-method-region beg end ret)
86              )
87             (t
88              (mime-article/show-output-buffer
89               "No method are specified for %s\n" ctype)
90              ))
91       )
92     ))
93
94 (defun field-unifier-for-mode (a b)
95   (let ((va (cdr a)))
96     (if (if (consp va)
97             (member (cdr b) va)
98           (equal va (cdr b))
99           )
100         (list nil b nil)
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               (apply (function start-process) args)
145               (mime-article/show-output-buffer)
146               ))
147         ))))
148
149 (defun mime-article/make-method-args (cal format)
150   (mapcar (function
151            (lambda (arg)
152              (if (stringp arg)
153                  arg
154                (let* ((item (eval arg))
155                       (ret (cdr (assoc item cal)))
156                       )
157                  (if ret
158                      ret
159                    (if (eq item 'encoding)
160                        "7bit"
161                      ""))
162                  ))
163              ))
164           format))
165
166 (defun mime-article/show-output-buffer (&rest forms)
167   (get-buffer-create mime/output-buffer-name)
168   (let ((the-win (selected-window))
169         (win (get-buffer-window mime/output-buffer-name))
170         )
171     (or win
172         (if (and mime/output-buffer-window-is-shared-with-bbdb
173                  (boundp 'bbdb-buffer-name)
174                  (setq win (get-buffer-window bbdb-buffer-name))
175                  )
176             (set-window-buffer win mime/output-buffer-name)
177           (select-window (get-buffer-window mime::article/preview-buffer))
178           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
179           (set-window-buffer win mime/output-buffer-name)
180           ))
181     (select-window win)
182     (goto-char (point-max))
183     (if forms
184         (insert (apply (function format) forms))
185       )
186     (select-window the-win)
187     ))
188
189
190 ;;; @ file name
191 ;;;
192
193 (defvar mime-viewer/file-name-char-regexp "[A-Za-z0-9+_-]")
194
195 (defvar mime-viewer/file-name-regexp-1
196   (concat mime-viewer/file-name-char-regexp "+\\."
197           mime-viewer/file-name-char-regexp "+"))
198
199 (defvar mime-viewer/file-name-regexp-2
200   (concat (regexp-* mime-viewer/file-name-char-regexp)
201           "\\(\\." mime-viewer/file-name-char-regexp "+\\)*"))
202
203 (defun mime-article/get-original-filename (param &optional encoding)
204   (or (mime-article/get-uu-filename param encoding)
205       (let (ret)
206         (or (if (or (and (setq ret (mime/Content-Disposition))
207                          (setq ret (assoc "filename" (cdr ret)))
208                          )
209                     (setq ret (assoc "name" param))
210                     (setq ret (assoc "x-name" param))
211                     )
212                 (std11-strip-quoted-string (cdr ret))
213               )
214             (if (setq ret
215                       (std11-find-field-body '("Content-Description"
216                                                "Subject")))
217                 (if (or (string-match mime-viewer/file-name-regexp-1 ret)
218                         (string-match mime-viewer/file-name-regexp-2 ret))
219                     (substring ret (match-beginning 0)(match-end 0))
220                   ))
221             ))
222       ))
223
224 (defun mime-article/get-filename (param)
225   (replace-as-filename (mime-article/get-original-filename param))
226   )
227
228
229 ;;; @ mail/news message
230 ;;;
231
232 (defun mime-viewer/quitting-method-for-mime/show-message-mode ()
233   (let ((mother mime::preview/mother-buffer)
234         (win-conf mime::preview/original-window-configuration)
235         )
236     (kill-buffer
237      (mime::preview-content-info/buffer (car mime::preview/content-list)))
238     (mime-viewer/kill-buffer)
239     (set-window-configuration win-conf)
240     (pop-to-buffer mother)
241     ;;(goto-char (point-min))
242     ;;(mime-viewer/up-content)
243     ))
244
245 (defun mime-article/view-message/rfc822 (beg end cal)
246   (let* ((cnum (mime-article/point-content-number beg))
247          (cur-buf (current-buffer))
248          (new-name (format "%s-%s" (buffer-name) cnum))
249          (mother mime::article/preview-buffer)
250          (code-converter
251           (or (cdr (assq major-mode mime-viewer/code-converter-alist))
252               'mime-viewer/default-code-convert-region))
253          str)
254     (setq str (buffer-substring beg end))
255     (switch-to-buffer new-name)
256     (erase-buffer)
257     (insert str)
258     (goto-char (point-min))
259     (if (re-search-forward "^\n" nil t)
260         (delete-region (point-min) (match-end 0))
261       )
262     (setq major-mode 'mime/show-message-mode)
263     (setq mime::article/code-converter code-converter)
264     (mime/viewer-mode mother)
265     ))
266
267
268 ;;; @ message/partial
269 ;;;
270
271 (defvar mime-article/coding-system-alist
272   (and (boundp 'MULE)
273        '((mh-show-mode . *noconv*)
274          (t            . *ctext*)
275          )))             
276
277 (defvar mime-article/kanji-code-alist
278   (and (boundp 'NEMACS)
279        '((mh-show-mode . nil)
280          (t            . 2)
281          ))) 
282
283 (defun mime-article/decode-message/partial (beg end cal)
284   (goto-char beg)
285   (let* ((root-dir (expand-file-name
286                     (concat "m-prts-" (user-login-name)) mime/tmp-dir))
287          (id (cdr (assoc "id" cal)))
288          (number (cdr (assoc "number" cal)))
289          (total (cdr (assoc "total" cal)))
290          (the-buf (current-buffer))
291          file
292          (mother mime::article/preview-buffer)
293          (win-conf (save-excursion
294                      (set-buffer mother)
295                      mime::preview/original-window-configuration))
296          )
297     (if (not (file-exists-p root-dir))
298         (make-directory root-dir)
299       )
300     (setq id (replace-as-filename id))
301     (setq root-dir (concat root-dir "/" id))
302     (if (not (file-exists-p root-dir))
303         (make-directory root-dir)
304       )
305     (setq file (concat root-dir "/FULL"))
306     (if (not (file-exists-p file))
307         (progn
308           (re-search-forward "^$")
309           (goto-char (1+ (match-end 0)))
310           (setq file (concat root-dir "/" number))
311           (let ((file-coding-system
312                  (cdr
313                   (or (assq major-mode mime-article/coding-system-alist)
314                       (assq t mime-article/coding-system-alist)
315                       )))
316                 (kanji-fileio-code
317                  (cdr
318                   (or (assq major-mode mime-article/kanji-code-alist)
319                       (assq t mime-article/kanji-code-alist)
320                       )))
321                 )
322             (write-region (point) (point-max) file)
323             )
324           (if (get-buffer mime/temp-buffer-name)
325               (kill-buffer mime/temp-buffer-name)
326             )
327           (switch-to-buffer mime/temp-buffer-name)
328           (let ((i 1)
329                 (max (string-to-int total))
330                 (file-coding-system-for-read (if (boundp 'MULE)
331                                                  *noconv*))
332                 kanji-fileio-code)
333             (catch 'tag
334               (while (<= i max)
335                 (setq file (concat root-dir "/" (int-to-string i)))
336                 (if (not (file-exists-p file))
337                     (progn
338                       (switch-to-buffer the-buf)
339                       (throw 'tag nil)
340                       ))
341                 (insert-file-contents file)
342                 (goto-char (point-max))
343                 (setq i (1+ i))
344                 )
345               ;;(delete-other-windows)
346               (let ((buf (current-buffer)))
347                 (write-file (concat root-dir "/FULL"))
348                 (set-window-configuration win-conf)
349                 (let ((win (get-buffer-window mother)))
350                   (if win
351                       (select-window win)
352                     ))
353                 (set-window-buffer (selected-window) buf)
354                 ;;(set-window-buffer buf)
355                 (setq major-mode 'mime/show-message-mode)
356                 )
357               (mime/viewer-mode mother)
358               (pop-to-buffer (current-buffer))
359               ))
360           )
361       (progn
362         ;;(delete-other-windows)
363         (set-window-configuration win-conf)
364         (select-window (or (get-buffer-window mother)
365                            (get-buffer-window
366                             (save-excursion
367                               (set-buffer mother)
368                               mime::preview/article-buffer))
369                            (get-largest-window)
370                            ))
371         (as-binary-input-file
372          (set-buffer (get-buffer-create "FULL"))
373          (insert-file-contents file)
374          )
375         (setq major-mode 'mime/show-message-mode)
376         (mime/viewer-mode mother)
377         ;;(pop-to-buffer (current-buffer))
378         ))
379     ))
380
381
382 ;;; @ rot13-47
383 ;;;
384
385 (defun mime-article/decode-caesar (beg end cal)
386   (let* ((cnum (mime-article/point-content-number beg))
387          (cur-buf (current-buffer))
388          (new-name (format "%s-%s" (buffer-name) cnum))
389          (mother mime::article/preview-buffer)
390          (charset (cdr (assoc "charset" cal)))
391          (encoding (cdr (assq 'encoding cal)))
392          (mode major-mode)
393          str)
394     (setq str (buffer-substring beg end))
395     (switch-to-buffer new-name)
396     (setq buffer-read-only nil)
397     (erase-buffer)
398     (insert str)
399     (goto-char (point-min))
400     (if (re-search-forward "^\n" nil t)
401         (delete-region (point-min) (match-end 0))
402       )
403     (let ((m (cdr (or (assq mode mime-viewer/code-converter-alist)
404                       (assq t mime-viewer/code-converter-alist)))))
405       (and (functionp m)
406            (funcall m charset encoding)
407            ))
408     (save-excursion
409       (set-mark (point-min))
410       (goto-char (point-max))
411       (tm:caesar-region)
412       )
413     (view-mode)
414     ))
415
416
417 ;;; @ end
418 ;;;
419
420 (provide 'tm-play)
421
422 ;;; tm-play.el ends here