1 ;;; tm-play.el --- decoder for tm-view.el
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
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.39 1997/02/01 17:54:51 morioka Exp $
8 ;; Keywords: mail, news, MIME, multimedia
10 ;; This file is part of tm (Tools for MIME).
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.
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.
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.
35 (defvar mime-preview/after-decoded-position nil)
37 (defun mime-preview/decode-content ()
39 (let ((pc (mime-preview/point-pcinfo (point))))
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))
50 (goto-char mime-preview/after-decoded-position)
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))
62 (if (< beg (point-min))
63 (setq beg (point-min))
65 (if (< (point-max) end)
66 (setq end (point-max))
69 (setq cal (list* (cons 'type ctype)
70 (cons 'encoding encoding)
71 (cons 'major-mode major-mode)
73 (if mime-viewer/decoding-mode
75 (cons 'mode mime-viewer/decoding-mode)
78 (setq ret (mime/get-content-decoding-alist cal))
79 (setq method (cdr (assq 'method ret)))
80 (cond ((and (symbolp method)
82 (funcall method beg end ret)
84 ((and (listp method)(stringp (car method)))
85 (mime-article/start-external-method-region beg end ret)
88 (mime-article/show-output-buffer
89 "No method are specified for %s\n" ctype)
94 (defun field-unifier-for-mode (a b)
103 (defun mime/get-content-decoding-alist (al)
104 (get-unified-alist mime/content-decoding-condition al)
108 ;;; @ external decoder
111 (defun mime-article/start-external-method-region (beg end cal)
114 (narrow-to-region beg end)
116 (let ((method (cdr (assoc 'method cal)))
117 (name (mime-article/get-filename cal))
120 (let ((file (make-temp-name
121 (expand-file-name "TM" mime/tmp-dir)))
126 (if (re-search-forward "^$" nil t)
132 (write-region b end file)
133 (message "External method is starting...")
135 'name (replace-as-filename name) cal))
136 (setq cal (put-alist 'file file cal))
139 mime/output-buffer-name (car method)
141 (mime-article/make-method-args cal
144 (apply (function start-process) args)
145 (mime-article/show-output-buffer)
149 (defun mime-article/make-method-args (cal format)
154 (let* ((item (eval arg))
155 (ret (cdr (assoc item cal)))
159 (if (eq item 'encoding)
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))
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))
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)
182 (goto-char (point-max))
184 (insert (apply (function format) forms))
186 (select-window the-win)
193 (defvar mime-viewer/file-name-char-regexp "[A-Za-z0-9+_-]")
195 (defvar mime-viewer/file-name-regexp-1
196 (concat mime-viewer/file-name-char-regexp "+\\."
197 mime-viewer/file-name-char-regexp "+"))
199 (defvar mime-viewer/file-name-regexp-2
200 (concat (regexp-* mime-viewer/file-name-char-regexp)
201 "\\(\\." mime-viewer/file-name-char-regexp "+\\)*"))
203 (defun mime-article/get-original-filename (param &optional encoding)
204 (or (mime-article/get-uu-filename param encoding)
206 (or (if (or (and (setq ret (mime/Content-Disposition))
207 (setq ret (assoc "filename" (cdr ret)))
209 (setq ret (assoc "name" param))
210 (setq ret (assoc "x-name" param))
212 (std11-strip-quoted-string (cdr ret))
215 (std11-find-field-body '("Content-Description"
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))
224 (defun mime-article/get-filename (param)
225 (replace-as-filename (mime-article/get-original-filename param))
229 ;;; @ mail/news message
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)
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)
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)
251 (or (cdr (assq major-mode mime-viewer/code-converter-alist))
252 'mime-viewer/default-code-convert-region))
254 (setq str (buffer-substring beg end))
255 (switch-to-buffer new-name)
258 (goto-char (point-min))
259 (if (re-search-forward "^\n" nil t)
260 (delete-region (point-min) (match-end 0))
262 (setq major-mode 'mime/show-message-mode)
263 (setq mime::article/code-converter code-converter)
264 (mime/viewer-mode mother)
268 ;;; @ message/partial
271 (defvar mime-article/coding-system-alist
272 (list (cons 'mh-show-mode *noconv*)
273 (cons t (mime-charset-to-coding-system default-mime-charset))
276 (cond (running-mule-merged-emacs
277 (defun mime-article::write-region (start end file)
278 (let ((coding-system-for-write
280 (or (assq major-mode mime-article/coding-system-alist)
281 (assq t mime-article/coding-system-alist)
283 (write-region start end file)
287 running-xemacs-with-mule)
288 (defun mime-article::write-region (start end file)
289 (let ((file-coding-system
291 (or (assq major-mode mime-article/coding-system-alist)
292 (assq t mime-article/coding-system-alist)
294 (write-region start end file)
298 (defun mime-article::write-region (start end file)
299 (let ((kanji-fileio-code
301 (or (assq major-mode mime-article/kanji-code-alist)
302 (assq t mime-article/kanji-code-alist)
304 (write-region start end file)
308 (defalias 'mime-article::write-region 'write-region)
311 (defun mime-article/decode-message/partial (beg end cal)
313 (let* ((root-dir (expand-file-name
314 (concat "m-prts-" (user-login-name)) mime/tmp-dir))
315 (id (cdr (assoc "id" cal)))
316 (number (cdr (assoc "number" cal)))
317 (total (cdr (assoc "total" cal)))
319 (mother mime::article/preview-buffer)
321 (or (file-exists-p root-dir)
322 (make-directory root-dir)
324 (setq id (replace-as-filename id))
325 (setq root-dir (concat root-dir "/" id))
326 (or (file-exists-p root-dir)
327 (make-directory root-dir)
329 (setq file (concat root-dir "/FULL"))
330 (if (file-exists-p file)
331 (let ((full-buf (get-buffer-create "FULL"))
332 (pwin (or (get-buffer-window mother)
333 (get-largest-window)))
335 (save-window-excursion
336 (set-buffer full-buf)
338 (as-binary-input-file (insert-file-contents file))
339 (setq major-mode 'mime/show-message-mode)
340 (mime/viewer-mode mother)
342 (set-window-buffer pwin
344 (set-buffer full-buf)
345 mime::article/preview-buffer))
348 (re-search-forward "^$")
349 (goto-char (1+ (match-end 0)))
350 (setq file (concat root-dir "/" number))
351 (mime-article::write-region (point) (point-max) file)
352 (let ((total-file (concat root-dir "/CT")))
356 (or (file-exists-p total-file)
358 (set-buffer (find-file-noselect total-file))
362 (kill-buffer (current-buffer))
364 (string-to-number total)
366 (and (file-exists-p total-file)
368 (set-buffer (find-file-noselect total-file))
369 (and (re-search-forward "[0-9]+" nil t)
371 (buffer-substring (match-beginning 0)
374 (kill-buffer (current-buffer))
377 (if (and total (> total 0))
380 (set-buffer (get-buffer-create mime/temp-buffer-name))
381 (let ((full-buf (current-buffer)))
385 (setq file (concat root-dir "/" (int-to-string i)))
386 (if (not (file-exists-p file))
389 (as-binary-input-file (insert-file-contents file))
390 (goto-char (point-max))
393 (as-binary-output-file (write-file (concat root-dir "/FULL")))
396 (let ((file (format "%s/%d" root-dir i)))
397 (and (file-exists-p file)
402 (let ((file (expand-file-name "CT" root-dir)))
403 (and (file-exists-p file)
406 (save-window-excursion
407 (setq major-mode 'mime/show-message-mode)
408 (mime/viewer-mode mother)
410 (let ((pwin (or (get-buffer-window mother)
413 (pbuf (save-excursion
414 (set-buffer full-buf)
415 mime::article/preview-buffer)))
416 (set-window-buffer pwin pbuf)
425 (defun mime-article/decode-caesar (beg end cal)
426 (let* ((cnum (mime-article/point-content-number beg))
427 (cur-buf (current-buffer))
428 (new-name (format "%s-%s" (buffer-name) cnum))
429 (mother mime::article/preview-buffer)
430 (charset (cdr (assoc "charset" cal)))
431 (encoding (cdr (assq 'encoding cal)))
434 (setq str (buffer-substring beg end))
435 (switch-to-buffer new-name)
436 (setq buffer-read-only nil)
439 (goto-char (point-min))
440 (if (re-search-forward "^\n" nil t)
441 (delete-region (point-min) (match-end 0))
443 (let ((m (cdr (or (assq mode mime-viewer/code-converter-alist)
444 (assq t mime-viewer/code-converter-alist)))))
446 (funcall m charset encoding)
449 (set-mark (point-min))
450 (goto-char (point-max))
462 ;;; tm-play.el ends here