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