1 ;;; mime-play.el --- Playback processing module for mime-view.el
3 ;; Copyright (C) 1994,1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Created: 1995/9/26 (separated from tm-view.el)
7 ;; Renamed: 1997/2/21 from tm-play.el
8 ;; Keywords: MIME, multimedia, mail, news
10 ;; This file is part of SEMI (Secretariat of Emacs MIME Interfaces).
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.
37 (error (defvar bbdb-buffer-name nil))
40 (defvar mime-acting-situation-examples nil)
42 (defun mime-save-acting-situation-examples ()
43 (let* ((file mime-acting-situation-examples-file)
44 (buffer (get-buffer-create " *mime-example*")))
48 (setq buffer-file-name file)
50 (insert ";;; " (file-name-nondirectory file) "\n")
51 (insert "\n;; This file is generated automatically by "
52 mime-view-version-string "\n\n")
53 (insert ";;; Code:\n\n")
54 (pp `(setq mime-acting-situation-examples
55 ',mime-acting-situation-examples)
58 (file-name-nondirectory file)
61 (kill-buffer buffer))))
63 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
69 (defvar mime-preview-after-decoded-position nil)
71 (defun mime-preview-play-current-entity (&optional mode)
73 It decodes current entity to call internal or external method. The
74 method is selected from variable `mime-acting-condition'.
75 If MODE is specified, play as it. Default MODE is \"play\"."
79 (let ((entity (get-text-property (point) 'mime-view-entity)))
81 (let ((the-buf (current-buffer))
82 (raw-buffer (mime-entity-buffer entity)))
83 (setq mime-preview-after-decoded-position (point))
84 (set-buffer raw-buffer)
85 (mime-raw-play-entity entity mode)
86 (when (eq (current-buffer) raw-buffer)
88 (goto-char mime-preview-after-decoded-position)
91 (defun mime-sort-situation (situation)
102 (let ((ret (assq a-t order)))
104 (setq a-order (cdr ret))
110 (let ((ret (assq b-t order)))
112 (setq b-order (cdr ret))
117 (if (= a-order b-order)
118 (string< (format "%s" a-t)(format "%s" b-t))
123 (defsubst mime-delq-null-situation (situations field)
126 (let ((situation (car situations)))
127 (if (assq field situation)
128 (setq dest (cons situation dest))
130 (setq situations (cdr situations)))
133 (defun mime-raw-play-entity (entity &optional mode)
134 "Play entity specified by ENTITY.
135 It decodes the entity to call internal or external method. The method
136 is selected from variable `mime-acting-condition'. If MODE is
137 specified, play as it. Default MODE is \"play\"."
138 (let (method cal ret)
139 (setq cal (mime-entity-situation entity))
141 (setq cal (cons (cons 'mode mode) cal))
144 (or (ctree-match-calist mime-acting-situation-examples cal)
145 (ctree-match-calist-partially mime-acting-situation-examples
149 (or (mime-delq-null-situation
150 (ctree-find-calist mime-acting-condition ret
151 mime-view-find-every-acting-situation)
153 (mime-delq-null-situation
154 (ctree-find-calist mime-acting-condition cal
155 mime-view-find-every-acting-situation)
159 (setq ret (select-menu-alist
165 (cdr (assq 'method situation)))
168 (setq ret (mime-sort-situation ret))
169 (ctree-set-calist-strictly 'mime-acting-situation-examples ret)
174 (setq method (cdr (assq 'method ret)))
175 (cond ((and (symbolp method)
177 (funcall method entity ret)
180 (mime-activate-mailcap-method entity ret)
182 ((and (listp method)(stringp (car method)))
183 (mime-activate-external-method entity ret)
186 (mime-show-echo-buffer "No method are specified for %s\n"
187 (mime-entity-type/subtype entity))
192 ;;; @ external decoder
195 (defvar mime-mailcap-method-filename-alist nil)
197 (defun mime-activate-mailcap-method (entity situation)
200 (let ((start (mime-entity-point-min entity))
201 (end (mime-entity-point-max entity)))
202 (narrow-to-region start end)
204 (let ((method (cdr (assoc 'method situation)))
205 (name (expand-file-name (mime-raw-get-filename situation)
206 mime-temp-directory)))
207 (mime-write-decoded-region (mime-entity-body-start entity) end
208 name (cdr (assq 'encoding situation)))
209 (message "External method is starting...")
212 (mailcap-format-command
214 (cons (cons 'filename name) situation))))
215 (start-process command mime-echo-buffer-name
216 shell-file-name shell-command-switch command)
218 (set-alist 'mime-mailcap-method-filename-alist process name)
219 (set-process-sentinel process 'mime-mailcap-method-sentinel)
223 (defun mime-mailcap-method-sentinel (process event)
224 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
225 (if (file-exists-p file)
228 (remove-alist 'mime-mailcap-method-filename-alist process)
229 (message (format "%s %s" process event)))
231 (defun mime-activate-external-method (entity cal)
234 (let ((beg (mime-entity-point-min entity))
235 (end (mime-entity-point-max entity)))
236 (narrow-to-region beg end)
238 (let ((method (cdr (assoc 'method cal)))
239 (name (mime-raw-get-filename cal)))
241 (let ((file (make-temp-name
242 (expand-file-name "TM" mime-temp-directory)))
247 (if (re-search-forward "^$" nil t)
253 (write-region b end file)
254 (message "External method is starting...")
256 'name (replace-as-filename name) cal))
257 (setq cal (put-alist 'file file cal))
260 mime-echo-buffer-name (car method)
262 (mime-make-external-method-args
263 cal (cdr (cdr method)))
265 (apply (function start-process) args)
266 (mime-show-echo-buffer)
270 (defun mime-make-external-method-args (cal format)
275 (let* ((item (eval arg))
276 (ret (cdr (assoc item cal)))
280 (if (eq item 'encoding)
287 (defvar mime-echo-window-is-shared-with-bbdb t
288 "*If non-nil, mime-echo window is shared with BBDB window.")
290 (defvar mime-echo-window-height
293 (/ (window-height) 5)
295 "*Size of mime-echo window.
296 It allows function or integer. If it is function,
297 `mime-show-echo-buffer' calls it to get height of mime-echo window.
298 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
301 (defun mime-show-echo-buffer (&rest forms)
302 "Show mime-echo buffer to display MIME-playing information."
303 (get-buffer-create mime-echo-buffer-name)
304 (let ((the-win (selected-window))
305 (win (get-buffer-window mime-echo-buffer-name))
308 (if (and mime-echo-window-is-shared-with-bbdb
309 (boundp 'bbdb-buffer-name)
310 (setq win (get-buffer-window bbdb-buffer-name))
312 (set-window-buffer win mime-echo-buffer-name)
313 (select-window (get-buffer-window mime-preview-buffer))
314 (setq win (split-window-vertically
316 (if (functionp mime-echo-window-height)
317 (funcall mime-echo-window-height)
318 mime-echo-window-height)
320 (set-window-buffer win mime-echo-buffer-name)
323 (goto-char (point-max))
325 (insert (apply (function format) forms))
327 (select-window the-win)
334 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
336 (defvar mime-view-file-name-regexp-1
337 (concat mime-view-file-name-char-regexp "+\\."
338 mime-view-file-name-char-regexp "+"))
340 (defvar mime-view-file-name-regexp-2
341 (concat (regexp-* mime-view-file-name-char-regexp)
342 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
344 (defun mime-raw-get-original-filename (param)
345 (or (if (member (cdr (assq 'encoding param))
346 mime-view-uuencode-encoding-name-list)
347 (mime-raw-get-uu-filename))
349 (or (if (or (and (setq ret (mime-read-Content-Disposition))
353 (mime-content-disposition-parameters ret)))
355 (setq ret (assoc "name" param))
356 (setq ret (assoc "x-name" param))
358 (std11-strip-quoted-string (cdr ret))
361 (std11-find-field-body '("Content-Description"
363 (if (or (string-match mime-view-file-name-regexp-1 ret)
364 (string-match mime-view-file-name-regexp-2 ret))
365 (substring ret (match-beginning 0)(match-end 0))
370 (defun mime-raw-get-filename (param)
371 (replace-as-filename (mime-raw-get-original-filename param))
375 ;;; @ file extraction
378 (defun mime-method-to-save (entity cal)
379 (let ((beg (mime-entity-point-min entity))
380 (end (mime-entity-point-max entity)))
382 (let* ((name (save-restriction
383 (narrow-to-region beg end)
384 (mime-raw-get-filename cal)
386 (encoding (or (cdr (assq 'encoding cal)) "7bit"))
387 (filename (if (and name (not (string-equal name "")))
388 (expand-file-name name
389 (save-window-excursion
393 (interactive "DDirectory: ")
395 (save-window-excursion
399 (interactive "FFilename: ")
400 (expand-file-name file)))))))
402 (if (file-exists-p filename)
403 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
405 (re-search-forward "\n\n")
406 (mime-write-decoded-region (match-end 0) end filename encoding)
410 ;;; @ mail/news message
413 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
414 "Quitting method for mime-view.
415 It is registered to variable `mime-preview-quitting-method-alist'."
416 (let ((mother mime-mother-buffer)
417 (win-conf mime-preview-original-window-configuration)
419 (kill-buffer mime-raw-buffer)
420 (mime-preview-kill-buffer)
421 (set-window-configuration win-conf)
422 (pop-to-buffer mother)
425 (defun mime-method-to-display-message/rfc822 (entity cal)
426 (let* ((beg (mime-entity-point-min entity))
427 (end (mime-entity-point-max entity))
428 (cnum (mime-raw-point-to-entity-number beg))
429 (new-name (format "%s-%s" (buffer-name) cnum))
430 (mother mime-preview-buffer)
432 (cdr (or (assq major-mode mime-raw-representation-type-alist)
433 (assq t mime-raw-representation-type-alist))))
435 (setq str (buffer-substring beg end))
436 (switch-to-buffer new-name)
439 (goto-char (point-min))
440 (if (re-search-forward "^\n" nil t)
441 (delete-region (point-min) (match-end 0))
443 (setq major-mode 'mime-show-message-mode)
444 (setq mime-raw-representation-type representation-type)
445 (mime-view-mode mother)
449 ;;; @ message/partial
452 (defun mime-raw-write-region (start end filename)
453 "Write current region into specified file.
454 When called from a program, takes three arguments:
455 START, END and FILENAME. START and END are buffer positions.
456 It refer `mime-raw-representation-type' or `major-mode
457 mime-raw-representation-type-alist'. If it is `binary', region is
458 saved as binary. Otherwise the region is saved by `write-region'."
459 (let ((presentation-type
460 (or mime-raw-representation-type
461 (cdr (or (assq major-mode mime-raw-representation-type-alist)
462 (assq t mime-raw-representation-type-alist))))))
463 (if (eq presentation-type 'binary)
464 (write-region-as-binary start end filename)
465 (write-region start end filename)
468 (defun mime-method-to-store-message/partial (entity cal)
469 (goto-char (mime-entity-point-min entity))
472 (concat "m-prts-" (user-login-name)) mime-temp-directory))
473 (id (cdr (assoc "id" cal)))
474 (number (cdr (assoc "number" cal)))
475 (total (cdr (assoc "total" cal)))
477 (mother mime-preview-buffer)
479 (or (file-exists-p root-dir)
480 (make-directory root-dir)
482 (setq id (replace-as-filename id))
483 (setq root-dir (concat root-dir "/" id))
484 (or (file-exists-p root-dir)
485 (make-directory root-dir)
487 (setq file (concat root-dir "/FULL"))
488 (if (file-exists-p file)
489 (let ((full-buf (get-buffer-create "FULL"))
490 (pwin (or (get-buffer-window mother)
491 (get-largest-window)))
493 (save-window-excursion
494 (set-buffer full-buf)
496 (as-binary-input-file (insert-file-contents file))
497 (setq major-mode 'mime-show-message-mode)
498 (mime-view-mode mother)
500 (set-window-buffer pwin
502 (set-buffer full-buf)
503 mime-preview-buffer))
506 (re-search-forward "^$")
507 (goto-char (1+ (match-end 0)))
508 (setq file (concat root-dir "/" number))
509 (mime-raw-write-region (point) (mime-entity-point-max entity) file)
510 (let ((total-file (concat root-dir "/CT")))
514 (or (file-exists-p total-file)
517 (get-buffer-create mime-temp-buffer-name))
520 (write-region (point-min)(point-max) total-file)
521 (kill-buffer (current-buffer))
523 (string-to-number total)
525 (and (file-exists-p total-file)
527 (set-buffer (find-file-noselect total-file))
529 (and (re-search-forward "[0-9]+" nil t)
531 (buffer-substring (match-beginning 0)
534 (kill-buffer (current-buffer))
537 (if (and total (> total 0))
540 (set-buffer (get-buffer-create mime-temp-buffer-name))
541 (let ((full-buf (current-buffer)))
545 (setq file (concat root-dir "/" (int-to-string i)))
546 (or (file-exists-p file)
549 (as-binary-input-file (insert-file-contents file))
550 (goto-char (point-max))
553 (as-binary-output-file
554 (write-region (point-min)(point-max)
555 (expand-file-name "FULL" root-dir)))
558 (let ((file (format "%s/%d" root-dir i)))
559 (and (file-exists-p file)
564 (let ((file (expand-file-name "CT" root-dir)))
565 (and (file-exists-p file)
568 (save-window-excursion
569 (setq major-mode 'mime-show-message-mode)
570 (mime-view-mode mother)
572 (let ((pwin (or (get-buffer-window mother)
575 (pbuf (save-excursion
576 (set-buffer full-buf)
577 mime-preview-buffer)))
578 (set-window-buffer pwin pbuf)
584 ;;; @ message/external-body
587 (defvar mime-raw-dired-function
588 (if (and (>= emacs-major-version 19) window-system)
589 (function dired-other-frame)
590 (function mime-raw-dired-function-for-one-frame)
593 (defun mime-raw-dired-function-for-one-frame (dir)
594 (let ((win (or (get-buffer-window mime-preview-buffer)
595 (get-largest-window))))
600 (defun mime-method-to-display-message/external-ftp (entity cal)
601 (let* ((site (cdr (assoc "site" cal)))
602 (directory (cdr (assoc "directory" cal)))
603 (name (cdr (assoc "name" cal)))
604 (pathname (concat "/anonymous@" site ":" directory)))
605 (message (concat "Accessing " (expand-file-name name pathname) "..."))
606 (funcall mime-raw-dired-function pathname)
607 (goto-char (point-min))
608 (search-forward name)
615 (defun mime-method-to-display-caesar (entity situation)
616 "Internal method for mime-view to display ROT13-47-48 message."
617 (let* ((new-name (format "%s-%s" (buffer-name)
618 (mime-entity-number entity)))
619 (mother mime-preview-buffer))
620 (let ((pwin (or (get-buffer-window mother)
621 (get-largest-window)))
622 (buf (get-buffer-create new-name)))
623 (set-window-buffer pwin buf)
627 (setq buffer-read-only nil)
629 (mime-text-insert-decoded-body entity)
630 (mule-caesar-region (point-min) (point-max))
631 (set-buffer-modified-p nil)
633 (view-buffer new-name)
642 (let* ((file mime-acting-situation-examples-file)
643 (buffer (get-buffer-create " *mime-example*")))
644 (if (file-readable-p file)
649 (insert-file-contents file)
652 (or (eq (car mime-acting-situation-examples) 'type)
653 (setq mime-acting-situation-examples nil))
655 (kill-buffer buffer))))
657 ;;; mime-play.el ends here