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.
36 (error (defvar bbdb-buffer-name nil)))
39 (defvar mime-acting-situation-example-list nil)
41 (defvar mime-acting-situation-example-list-max-size 16)
43 (defun mime-save-acting-situation-examples ()
44 (let* ((file mime-acting-situation-examples-file)
45 (buffer (get-buffer-create " *mime-example*")))
49 (setq buffer-file-name file)
51 (insert ";;; " (file-name-nondirectory file) "\n")
52 (insert "\n;; This file is generated automatically by "
53 mime-view-version "\n\n")
54 (insert ";;; Code:\n\n")
55 (pp `(setq mime-acting-situation-example-list
56 ',mime-acting-situation-example-list)
59 (file-name-nondirectory file)
62 (kill-buffer buffer))))
64 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
66 (defun mime-reduce-acting-situation-examples ()
67 (let* ((rest mime-acting-situation-example-list)
68 (min-example (car rest))
69 (min-score (cdr min-example)))
71 (let* ((example (car rest))
72 (score (cdr example)))
73 (cond ((< score min-score)
78 (if (<= (length (car example))(length (car min-example)))
79 (setq min-example example)
82 (setq rest (cdr rest)))
83 (setq mime-acting-situation-example-list
84 (delq min-example mime-acting-situation-example-list))
85 (setq min-example (car min-example))
86 (let ((examples mime-acting-situation-example-list)
90 (let* ((ret (mime-compare-situation-with-example min-example
92 (ret-score (car ret)))
93 (cond ((> ret-score max-score)
94 (setq max-score ret-score
95 max-examples (list (cdr ret)))
97 ((= ret-score max-score)
98 (setq max-examples (cons (cdr ret) max-examples))
100 (setq examples (cdr examples)))
102 (let* ((example (car max-examples))
103 (cell (assoc example mime-acting-situation-example-list)))
105 (setcdr cell (1+ (cdr cell)))
106 (setq mime-acting-situation-example-list
107 (cons (cons example 0)
108 mime-acting-situation-example-list))
110 (setq max-examples (cdr max-examples))
114 ;;; @ content decoder
117 (defvar mime-preview-after-decoded-position nil)
119 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
120 "Play current entity.
121 It decodes current entity to call internal or external method. The
122 method is selected from variable `mime-acting-condition'.
123 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
124 `mime-acting-situation-example-list'.
125 If MODE is specified, play as it. Default MODE is \"play\"."
127 (let ((entity (get-text-property (point) 'mime-view-entity)))
129 (let ((the-buf (current-buffer))
130 (raw-buffer (mime-entity-buffer entity)))
131 (setq mime-preview-after-decoded-position (point))
132 (set-buffer raw-buffer)
133 (mime-raw-play-entity entity (or mode "play") nil ignore-examples)
134 (when (eq (current-buffer) raw-buffer)
136 (goto-char mime-preview-after-decoded-position)
139 (defun mime-sort-situation (situation)
149 (disposition-type . 6)
153 (let ((ret (assq a-t order)))
155 (setq a-order (cdr ret))
161 (let ((ret (assq b-t order)))
163 (setq b-order (cdr ret))
168 (if (= a-order b-order)
169 (string< (format "%s" a-t)(format "%s" b-t))
174 (defsubst mime-delq-null-situation (situations field
175 &optional ignored-value)
178 (let* ((situation (car situations))
179 (cell (assq field situation)))
181 (or (eq (cdr cell) ignored-value)
182 (setq dest (cons situation dest))
184 (setq situations (cdr situations)))
187 (defun mime-compare-situation-with-example (situation example)
188 (let ((example (copy-alist example))
191 (let* ((cell (car situation))
193 (ecell (assoc key example)))
195 (if (equal cell ecell)
196 (setq match (1+ match))
197 (setq example (delq ecell example))
200 (setq situation (cdr situation))
205 (defun mime-raw-play-entity (entity &optional mode situation ignore-examples
207 "Play entity specified by ENTITY.
208 It decodes the entity to call internal or external method. The method
209 is selected from variable `mime-acting-condition'. If MODE is
210 specified, play as it. Default MODE is \"play\"."
213 (setq situation (mime-entity-situation entity)))
215 (setq situation (cons (cons 'mode mode) situation))
218 (or (assq 'ignore-examples situation)
220 (cons (cons 'ignore-examples ignore-examples) situation)))
223 (mime-delq-null-situation
224 (ctree-find-calist mime-acting-condition situation
225 mime-view-find-every-acting-situation)
226 'method ignored-method))
235 (let ((situation (car rest))
236 (examples mime-acting-situation-example-list))
239 (mime-compare-situation-with-example
240 situation (caar examples)))
241 (ret-score (car ret)))
242 (cond ((> ret-score max-score)
243 (setq max-score ret-score
244 max-escore (cdar examples)
245 max-examples (list (cdr ret))
246 max-situations (list situation))
248 ((= ret-score max-score)
249 (cond ((> (cdar examples) max-escore)
250 (setq max-escore (cdar examples)
251 max-examples (list (cdr ret))
252 max-situations (list situation))
254 ((= (cdar examples) max-escore)
256 (cons (cdr ret) max-examples))
257 (or (member situation max-situations)
259 (cons situation max-situations)))
261 (setq examples (cdr examples))))
262 (setq rest (cdr rest)))
264 (setq ret max-situations)
266 (let* ((example (car max-examples))
268 (assoc example mime-acting-situation-example-list)))
270 (setcdr cell (1+ (cdr cell)))
271 (setq mime-acting-situation-example-list
272 (cons (cons example 0)
273 mime-acting-situation-example-list))
275 (setq max-examples (cdr max-examples))
278 (setq ret (select-menu-alist
284 (cdr (assq 'method situation)))
287 (setq ret (mime-sort-situation ret))
288 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
293 (setq method (cdr (assq 'method ret)))
294 (cond ((and (symbolp method)
296 (funcall method entity ret)
299 (mime-activate-mailcap-method entity ret)
301 ;; ((and (listp method)(stringp (car method)))
302 ;; (mime-activate-external-method entity ret)
305 (mime-show-echo-buffer "No method are specified for %s\n"
306 (mime-entity-type/subtype entity))
311 ;;; @ external decoder
314 (defvar mime-mailcap-method-filename-alist nil)
316 (defun mime-activate-mailcap-method (entity situation)
319 (let ((start (mime-entity-point-min entity))
320 (end (mime-entity-point-max entity)))
321 (narrow-to-region start end)
323 (let ((method (cdr (assoc 'method situation)))
324 (name (mime-entity-safe-filename entity)))
326 (if (and name (not (string= name "")))
327 (expand-file-name name temporary-file-directory)
329 (expand-file-name "EMI" temporary-file-directory))
331 (mime-write-entity-content entity name)
332 (message "External method is starting...")
335 (mailcap-format-command
337 (cons (cons 'filename name) situation))))
338 (start-process command mime-echo-buffer-name
339 shell-file-name shell-command-switch command)
341 (set-alist 'mime-mailcap-method-filename-alist process name)
342 (set-process-sentinel process 'mime-mailcap-method-sentinel)
346 (defun mime-mailcap-method-sentinel (process event)
347 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
348 (if (file-exists-p file)
351 (remove-alist 'mime-mailcap-method-filename-alist process)
352 (message (format "%s %s" process event)))
354 (defvar mime-echo-window-is-shared-with-bbdb
355 (module-installed-p 'bbdb)
356 "*If non-nil, mime-echo window is shared with BBDB window.")
358 (defvar mime-echo-window-height
361 (/ (window-height) 5)
363 "*Size of mime-echo window.
364 It allows function or integer. If it is function,
365 `mime-show-echo-buffer' calls it to get height of mime-echo window.
366 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
369 (defun mime-show-echo-buffer (&rest forms)
370 "Show mime-echo buffer to display MIME-playing information."
371 (get-buffer-create mime-echo-buffer-name)
372 (let ((the-win (selected-window))
373 (win (get-buffer-window mime-echo-buffer-name)))
375 (unless (and mime-echo-window-is-shared-with-bbdb
377 (setq win (get-buffer-window bbdb-buffer-name))
379 (select-window (get-buffer-window mime-preview-buffer))
380 (setq win (split-window-vertically
382 (if (functionp mime-echo-window-height)
383 (funcall mime-echo-window-height)
384 mime-echo-window-height)
387 (set-window-buffer win mime-echo-buffer-name)
390 (goto-char (point-max))
392 (insert (apply (function format) forms))
394 (select-window the-win)
401 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
403 (defvar mime-view-file-name-regexp-1
404 (concat mime-view-file-name-char-regexp "+\\."
405 mime-view-file-name-char-regexp "+"))
407 (defvar mime-view-file-name-regexp-2
408 (concat (regexp-* mime-view-file-name-char-regexp)
409 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
411 (defun mime-entity-safe-filename (entity)
413 (or (mime-entity-filename entity)
415 (or (mime-read-field 'Content-Description entity)
416 (mime-read-field 'Subject entity))))
418 (or (string-match mime-view-file-name-regexp-1 subj)
419 (string-match mime-view-file-name-regexp-2 subj)))
420 (substring subj (match-beginning 0)(match-end 0))
423 (replace-as-filename filename)
427 ;;; @ file extraction
430 (defun mime-save-content (entity situation)
431 (let* ((name (mime-entity-safe-filename entity))
432 (filename (if (and name (not (string-equal name "")))
433 (expand-file-name name
434 (save-window-excursion
438 (interactive "DDirectory: ")
440 (save-window-excursion
444 (interactive "FFilename: ")
445 (expand-file-name file)))))))
447 (if (file-exists-p filename)
448 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
450 (mime-write-entity-content entity filename)
457 (defvar mime-magic-type-alist
458 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
459 ("^\211PNG" image png)
460 ("^GIF8[79]" image gif)
461 ("^II\\*\000" image tiff)
462 ("^MM\000\\*" image tiff)
464 ("^\000\000\001\263" video mpeg)
466 "*Alist of regexp about magic-number vs. corresponding media-types.
467 Each element looks like (REGEXP TYPE SUBTYPE).
468 REGEXP is a regular expression to match against the beginning of the
470 TYPE is symbol to indicate primary type of media-type.
471 SUBTYPE is symbol to indicate subtype of media-type.")
473 (defun mime-detect-content (entity situation)
475 (let ((mdata (save-excursion
476 ;;(set-buffer (mime-entity-buffer entity))
477 (let* ((start (mime-entity-body-start entity))
482 (mime-decode-string (buffer-substring start end)
483 (mime-entity-encoding entity))
485 (rest mime-magic-type-alist))
486 (while (not (let ((cell (car rest)))
488 (if (string-match (car cell) mdata)
489 (setq type (nth 1 cell)
490 subtype (nth 2 cell))
493 (setq rest (cdr rest))))
495 (mime-raw-play-entity
497 (put-alist 'type type
498 (put-alist 'subtype subtype
500 (copy-alist situation))))
501 (cdr (assq 'ignore-examples situation))
502 'mime-detect-content)
507 ;;; @ mail/news message
510 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
511 "Quitting method for mime-view.
512 It is registered to variable `mime-preview-quitting-method-alist'."
513 (let ((mother mime-mother-buffer)
514 (win-conf mime-preview-original-window-configuration)
516 (kill-buffer mime-raw-buffer)
517 (mime-preview-kill-buffer)
518 (set-window-configuration win-conf)
519 (pop-to-buffer mother)
522 (defun mime-view-message/rfc822 (entity situation)
524 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
525 (mother mime-preview-buffer)
526 (children (car (mime-entity-children entity))))
527 (set-buffer (get-buffer-create new-name))
529 (insert-buffer-substring (mime-entity-buffer children)
530 (mime-entity-point-min children)
531 (mime-entity-point-max children))
532 (setq mime-message-structure children)
533 (setq major-mode 'mime-show-message-mode)
534 (mime-view-buffer (current-buffer) nil mother
535 nil (if (mime-entity-cooked-p entity) 'cooked))
539 ;;; @ message/partial
542 (defun mime-store-message/partial-piece (entity cal)
543 (goto-char (mime-entity-point-min entity))
546 (concat "m-prts-" (user-login-name)) temporary-file-directory))
547 (id (cdr (assoc "id" cal)))
548 (number (cdr (assoc "number" cal)))
549 (total (cdr (assoc "total" cal)))
551 (mother mime-preview-buffer)
553 (or (file-exists-p root-dir)
554 (make-directory root-dir)
556 (setq id (replace-as-filename id))
557 (setq root-dir (concat root-dir "/" id))
558 (or (file-exists-p root-dir)
559 (make-directory root-dir)
561 (setq file (concat root-dir "/FULL"))
562 (if (file-exists-p file)
563 (let ((full-buf (get-buffer-create "FULL"))
564 (pwin (or (get-buffer-window mother)
565 (get-largest-window)))
567 (save-window-excursion
568 (set-buffer full-buf)
570 (as-binary-input-file (insert-file-contents file))
571 (setq major-mode 'mime-show-message-mode)
572 (mime-view-buffer (current-buffer) nil mother)
574 (set-window-buffer pwin
576 (set-buffer full-buf)
577 mime-preview-buffer))
580 (setq file (concat root-dir "/" number))
581 (mime-write-entity-body entity file)
582 (let ((total-file (concat root-dir "/CT")))
586 (or (file-exists-p total-file)
589 (get-buffer-create mime-temp-buffer-name))
592 (write-region (point-min)(point-max) total-file)
593 (kill-buffer (current-buffer))
595 (string-to-number total)
597 (and (file-exists-p total-file)
599 (set-buffer (find-file-noselect total-file))
601 (and (re-search-forward "[0-9]+" nil t)
603 (buffer-substring (match-beginning 0)
606 (kill-buffer (current-buffer))
609 (if (and total (> total 0))
612 (set-buffer (get-buffer-create mime-temp-buffer-name))
613 (let ((full-buf (current-buffer)))
617 (setq file (concat root-dir "/" (int-to-string i)))
618 (or (file-exists-p file)
621 (as-binary-input-file (insert-file-contents file))
622 (goto-char (point-max))
625 (as-binary-output-file
626 (write-region (point-min)(point-max)
627 (expand-file-name "FULL" root-dir)))
630 (let ((file (format "%s/%d" root-dir i)))
631 (and (file-exists-p file)
636 (let ((file (expand-file-name "CT" root-dir)))
637 (and (file-exists-p file)
640 (save-window-excursion
641 (setq major-mode 'mime-show-message-mode)
642 (mime-view-buffer (current-buffer) nil mother)
644 (let ((pwin (or (get-buffer-window mother)
645 (get-largest-window)))
646 (pbuf (save-excursion
647 (set-buffer full-buf)
648 mime-preview-buffer)))
649 (set-window-buffer pwin pbuf)
655 ;;; @ message/external-body
658 (defvar mime-raw-dired-function
659 (if (and (>= emacs-major-version 19) window-system)
660 (function dired-other-frame)
661 (function mime-raw-dired-function-for-one-frame)
664 (defun mime-raw-dired-function-for-one-frame (dir)
665 (let ((win (or (get-buffer-window mime-preview-buffer)
666 (get-largest-window))))
671 (defun mime-view-message/external-anon-ftp (entity cal)
672 (let* ((site (cdr (assoc "site" cal)))
673 (directory (cdr (assoc "directory" cal)))
674 (name (cdr (assoc "name" cal)))
675 (pathname (concat "/anonymous@" site ":" directory)))
676 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
677 (funcall mime-raw-dired-function pathname)
678 (goto-char (point-min))
679 (search-forward name)
682 (defvar mime-raw-browse-url-function (function mime-browse-url))
684 (defun mime-view-message/external-url (entity cal)
685 (let ((url (cdr (assoc "url" cal))))
686 (message (concat "Accessing " url " ..."))
687 (funcall mime-raw-browse-url-function url)))
693 (defun mime-view-caesar (entity situation)
694 "Internal method for mime-view to display ROT13-47-48 message."
695 (let* ((new-name (format "%s-%s" (buffer-name)
696 (mime-entity-number entity)))
697 (mother mime-preview-buffer))
698 (let ((pwin (or (get-buffer-window mother)
699 (get-largest-window)))
700 (buf (get-buffer-create new-name)))
701 (set-window-buffer pwin buf)
705 (setq buffer-read-only nil)
707 (mime-insert-text-content entity)
708 (mule-caesar-region (point-min) (point-max))
709 (set-buffer-modified-p nil)
711 (view-buffer new-name)
720 (let* ((file mime-acting-situation-examples-file)
721 (buffer (get-buffer-create " *mime-example*")))
722 (if (file-readable-p file)
727 (insert-file-contents file)
732 (while (and (> (length mime-acting-situation-example-list)
733 mime-acting-situation-example-list-max-size)
735 (mime-reduce-acting-situation-examples)
738 (error (setq mime-acting-situation-example-list nil)))
740 (kill-buffer buffer))))
742 ;;; mime-play.el ends here