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-example-list nil)
42 (defvar mime-acting-situation-example-list-max-size 16)
44 (defun mime-save-acting-situation-examples ()
45 (let* ((file mime-acting-situation-examples-file)
46 (buffer (get-buffer-create " *mime-example*")))
50 (setq buffer-file-name file)
52 (insert ";;; " (file-name-nondirectory file) "\n")
53 (insert "\n;; This file is generated automatically by "
54 mime-view-version-string "\n\n")
55 (insert ";;; Code:\n\n")
56 (pp `(setq mime-acting-situation-example-list
57 ',mime-acting-situation-example-list)
60 (file-name-nondirectory file)
63 (kill-buffer buffer))))
65 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
67 (defun mime-reduce-acting-situation-examples ()
68 (let* ((rest mime-acting-situation-example-list)
69 (min-example (car rest))
70 (min-score (cdr min-example)))
72 (let* ((example (car rest))
73 (score (cdr example)))
74 (cond ((< score min-score)
79 (if (<= (length (car example))(length (car min-example)))
80 (setq min-example example)
83 (setq rest (cdr rest)))
84 (setq mime-acting-situation-example-list
85 (delq min-example mime-acting-situation-example-list))
86 (setq min-example (car min-example))
87 (let ((examples mime-acting-situation-example-list)
91 (let* ((ret (mime-compare-situation-with-example min-example
93 (ret-score (car ret)))
94 (cond ((> ret-score max-score)
95 (setq max-score ret-score
96 max-examples (list (cdr ret)))
98 ((= ret-score max-score)
99 (setq max-examples (cons (cdr ret) max-examples))
101 (setq examples (cdr examples)))
103 (let* ((example (car max-examples))
104 (cell (assoc example mime-acting-situation-example-list)))
106 (setcdr cell (1+ (cdr cell)))
107 (setq mime-acting-situation-example-list
108 (cons (cons example 0)
109 mime-acting-situation-example-list))
111 (setq max-examples (cdr max-examples))
115 ;;; @ content decoder
118 (defvar mime-preview-after-decoded-position nil)
120 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
121 "Play current entity.
122 It decodes current entity to call internal or external method. The
123 method is selected from variable `mime-acting-condition'.
124 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
125 `mime-acting-situation-example-list'.
126 If MODE is specified, play as it. Default MODE is \"play\"."
128 (let ((entity (get-text-property (point) 'mime-view-entity)))
130 (let ((the-buf (current-buffer))
131 (raw-buffer (mime-entity-buffer entity)))
132 (setq mime-preview-after-decoded-position (point))
133 (set-buffer raw-buffer)
134 (mime-raw-play-entity entity (or mode "play") nil ignore-examples)
135 (when (eq (current-buffer) raw-buffer)
137 (goto-char mime-preview-after-decoded-position)
140 (defun mime-sort-situation (situation)
150 (disposition-type . 6)
154 (let ((ret (assq a-t order)))
156 (setq a-order (cdr ret))
162 (let ((ret (assq b-t order)))
164 (setq b-order (cdr ret))
169 (if (= a-order b-order)
170 (string< (format "%s" a-t)(format "%s" b-t))
175 (defsubst mime-delq-null-situation (situations field
176 &optional ignored-value)
179 (let* ((situation (car situations))
180 (cell (assq field situation)))
182 (or (eq (cdr cell) ignored-value)
183 (setq dest (cons situation dest))
185 (setq situations (cdr situations)))
188 (defun mime-compare-situation-with-example (situation example)
189 (let ((example (copy-alist example))
192 (let* ((cell (car situation))
194 (ecell (assoc key example)))
196 (if (equal cell ecell)
197 (setq match (1+ match))
198 (setq example (delq ecell example))
201 (setq situation (cdr situation))
206 (defun mime-raw-play-entity (entity &optional mode situation ignore-examples
208 "Play entity specified by ENTITY.
209 It decodes the entity to call internal or external method. The method
210 is selected from variable `mime-acting-condition'. If MODE is
211 specified, play as it. Default MODE is \"play\"."
214 (setq situation (mime-entity-situation entity)))
216 (setq situation (cons (cons 'mode mode) situation))
219 (or (assq 'ignore-examples situation)
221 (cons (cons 'ignore-examples ignore-examples) situation)))
224 (mime-delq-null-situation
225 (ctree-find-calist mime-acting-condition situation
226 mime-view-find-every-acting-situation)
227 'method ignored-method))
236 (let ((situation (car rest))
237 (examples mime-acting-situation-example-list))
240 (mime-compare-situation-with-example
241 situation (caar examples)))
242 (ret-score (car ret)))
243 (cond ((> ret-score max-score)
244 (setq max-score ret-score
245 max-escore (cdar examples)
246 max-examples (list (cdr ret))
247 max-situations (list situation))
249 ((= ret-score max-score)
250 (cond ((> (cdar examples) max-escore)
251 (setq max-escore (cdar examples)
252 max-examples (list (cdr ret))
253 max-situations (list situation))
255 ((= (cdar examples) max-escore)
257 (cons (cdr ret) max-examples))
258 (or (member situation max-situations)
260 (cons situation max-situations)))
262 (setq examples (cdr examples))))
263 (setq rest (cdr rest)))
265 (setq ret max-situations)
267 (let* ((example (car max-examples))
269 (assoc example mime-acting-situation-example-list)))
271 (setcdr cell (1+ (cdr cell)))
272 (setq mime-acting-situation-example-list
273 (cons (cons example 0)
274 mime-acting-situation-example-list))
276 (setq max-examples (cdr max-examples))
279 (setq ret (select-menu-alist
285 (cdr (assq 'method situation)))
288 (setq ret (mime-sort-situation ret))
289 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
294 (setq method (cdr (assq 'method ret)))
295 (cond ((and (symbolp method)
297 (funcall method entity ret)
300 (mime-activate-mailcap-method entity ret)
302 ;; ((and (listp method)(stringp (car method)))
303 ;; (mime-activate-external-method entity ret)
306 (mime-show-echo-buffer "No method are specified for %s\n"
307 (mime-entity-type/subtype entity))
312 ;;; @ external decoder
315 (defvar mime-mailcap-method-filename-alist nil)
317 (defun mime-activate-mailcap-method (entity situation)
320 (let ((start (mime-entity-point-min entity))
321 (end (mime-entity-point-max entity)))
322 (narrow-to-region start end)
324 (let ((method (cdr (assoc 'method situation)))
325 (name (mime-entity-safe-filename entity)))
327 (if (and name (not (string= name "")))
328 (expand-file-name name mime-temp-directory)
330 (expand-file-name "EMI" mime-temp-directory))
332 (mime-write-entity-content entity name)
333 (message "External method is starting...")
336 (mailcap-format-command
338 (cons (cons 'filename name) situation))))
339 (start-process command mime-echo-buffer-name
340 shell-file-name shell-command-switch command)
342 (set-alist 'mime-mailcap-method-filename-alist process name)
343 (set-process-sentinel process 'mime-mailcap-method-sentinel)
347 (defun mime-mailcap-method-sentinel (process event)
348 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
349 (if (file-exists-p file)
352 (remove-alist 'mime-mailcap-method-filename-alist process)
353 (message (format "%s %s" process event)))
355 (defvar mime-echo-window-is-shared-with-bbdb t
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))
376 (if (and mime-echo-window-is-shared-with-bbdb
377 (boundp 'bbdb-buffer-name)
378 (setq win (get-buffer-window bbdb-buffer-name))
380 (set-window-buffer win mime-echo-buffer-name)
381 (select-window (get-buffer-window mime-preview-buffer))
382 (setq win (split-window-vertically
384 (if (functionp mime-echo-window-height)
385 (funcall mime-echo-window-height)
386 mime-echo-window-height)
388 (set-window-buffer win mime-echo-buffer-name)
391 (goto-char (point-max))
393 (insert (apply (function format) forms))
395 (select-window the-win)
402 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
404 (defvar mime-view-file-name-regexp-1
405 (concat mime-view-file-name-char-regexp "+\\."
406 mime-view-file-name-char-regexp "+"))
408 (defvar mime-view-file-name-regexp-2
409 (concat (regexp-* mime-view-file-name-char-regexp)
410 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
412 (defun mime-entity-safe-filename (entity)
414 (or (mime-entity-filename entity)
416 (or (mime-read-field 'Content-Description entity)
417 (mime-read-field 'Subject entity))))
419 (or (string-match mime-view-file-name-regexp-1 subj)
420 (string-match mime-view-file-name-regexp-2 subj)))
421 (substring subj (match-beginning 0)(match-end 0))
424 (replace-as-filename filename)
428 ;;; @ file extraction
431 (defun mime-save-content (entity situation)
432 (let* ((name (mime-entity-safe-filename entity))
433 (filename (if (and name (not (string-equal name "")))
434 (expand-file-name name
435 (save-window-excursion
439 (interactive "DDirectory: ")
441 (save-window-excursion
445 (interactive "FFilename: ")
446 (expand-file-name file)))))))
448 (if (file-exists-p filename)
449 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
451 (mime-write-entity-content entity filename)
458 (defvar mime-magic-type-alist
459 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
460 ("^\211PNG" image png)
461 ("^GIF8[79]" image gif)
462 ("^II\\*\000" image tiff)
463 ("^MM\000\\*" image tiff)
465 ("^\000\000\001\263" video mpeg)
467 "*Alist of regexp about magic-number vs. corresponding media-types.
468 Each element looks like (REGEXP TYPE SUBTYPE).
469 REGEXP is a regular expression to match against the beginning of the
471 TYPE is symbol to indicate primary type of media-type.
472 SUBTYPE is symbol to indicate subtype of media-type.")
474 (defun mime-detect-content (entity situation)
476 (let ((mdata (save-excursion
477 ;;(set-buffer (mime-entity-buffer entity))
478 (let* ((start (mime-entity-body-start entity))
483 (mime-decode-string (buffer-substring start end)
484 (mime-entity-encoding entity))
486 (rest mime-magic-type-alist))
487 (while (not (let ((cell (car rest)))
489 (if (string-match (car cell) mdata)
490 (setq type (nth 1 cell)
491 subtype (nth 2 cell))
494 (setq rest (cdr rest))))
496 (mime-raw-play-entity
498 (put-alist 'type type
499 (put-alist 'subtype subtype
501 (copy-alist situation))))
502 (cdr (assq 'ignore-examples situation))
503 'mime-detect-content)
508 ;;; @ mail/news message
511 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
512 "Quitting method for mime-view.
513 It is registered to variable `mime-preview-quitting-method-alist'."
514 (let ((mother mime-mother-buffer)
515 (win-conf mime-preview-original-window-configuration)
517 (kill-buffer mime-raw-buffer)
518 (mime-preview-kill-buffer)
519 (set-window-configuration win-conf)
520 (pop-to-buffer mother)
523 (defun mime-view-message/rfc822 (entity situation)
525 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
526 (mother mime-preview-buffer)
527 (children (car (mime-entity-children entity))))
528 (set-buffer (get-buffer-create new-name))
530 (insert-buffer-substring (mime-entity-buffer children)
531 (mime-entity-point-min children)
532 (mime-entity-point-max children))
533 (setq mime-message-structure children)
534 (setq major-mode 'mime-show-message-mode)
535 (mime-view-buffer (current-buffer) nil mother
536 nil (if (mime-entity-cooked-p entity) 'cooked))
540 ;;; @ message/partial
543 (defun mime-store-message/partial-piece (entity cal)
544 (goto-char (mime-entity-point-min entity))
547 (concat "m-prts-" (user-login-name)) mime-temp-directory))
548 (id (cdr (assoc "id" cal)))
549 (number (cdr (assoc "number" cal)))
550 (total (cdr (assoc "total" cal)))
552 (mother mime-preview-buffer)
554 (or (file-exists-p root-dir)
555 (make-directory root-dir)
557 (setq id (replace-as-filename id))
558 (setq root-dir (concat root-dir "/" id))
559 (or (file-exists-p root-dir)
560 (make-directory root-dir)
562 (setq file (concat root-dir "/FULL"))
563 (if (file-exists-p file)
564 (let ((full-buf (get-buffer-create "FULL"))
565 (pwin (or (get-buffer-window mother)
566 (get-largest-window)))
568 (save-window-excursion
569 (set-buffer full-buf)
571 (as-binary-input-file (insert-file-contents file))
572 (setq major-mode 'mime-show-message-mode)
573 (mime-view-buffer (current-buffer) nil mother)
575 (set-window-buffer pwin
577 (set-buffer full-buf)
578 mime-preview-buffer))
581 (setq file (concat root-dir "/" number))
582 (mime-write-entity-body entity file)
583 (let ((total-file (concat root-dir "/CT")))
587 (or (file-exists-p total-file)
590 (get-buffer-create mime-temp-buffer-name))
593 (write-region (point-min)(point-max) total-file)
594 (kill-buffer (current-buffer))
596 (string-to-number total)
598 (and (file-exists-p total-file)
600 (set-buffer (find-file-noselect total-file))
602 (and (re-search-forward "[0-9]+" nil t)
604 (buffer-substring (match-beginning 0)
607 (kill-buffer (current-buffer))
610 (if (and total (> total 0))
613 (set-buffer (get-buffer-create mime-temp-buffer-name))
614 (let ((full-buf (current-buffer)))
618 (setq file (concat root-dir "/" (int-to-string i)))
619 (or (file-exists-p file)
622 (as-binary-input-file (insert-file-contents file))
623 (goto-char (point-max))
626 (as-binary-output-file
627 (write-region (point-min)(point-max)
628 (expand-file-name "FULL" root-dir)))
631 (let ((file (format "%s/%d" root-dir i)))
632 (and (file-exists-p file)
637 (let ((file (expand-file-name "CT" root-dir)))
638 (and (file-exists-p file)
641 (save-window-excursion
642 (setq major-mode 'mime-show-message-mode)
643 (mime-view-buffer (current-buffer) nil mother)
645 (let ((pwin (or (get-buffer-window mother)
646 (get-largest-window)))
647 (pbuf (save-excursion
648 (set-buffer full-buf)
649 mime-preview-buffer)))
650 (set-window-buffer pwin pbuf)
656 ;;; @ message/external-body
659 (defvar mime-raw-dired-function
660 (if (and (>= emacs-major-version 19) window-system)
661 (function dired-other-frame)
662 (function mime-raw-dired-function-for-one-frame)
665 (defun mime-raw-dired-function-for-one-frame (dir)
666 (let ((win (or (get-buffer-window mime-preview-buffer)
667 (get-largest-window))))
672 (defun mime-view-message/external-anon-ftp (entity cal)
673 (let* ((site (cdr (assoc "site" cal)))
674 (directory (cdr (assoc "directory" cal)))
675 (name (cdr (assoc "name" cal)))
676 (pathname (concat "/anonymous@" site ":" directory)))
677 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
678 (funcall mime-raw-dired-function pathname)
679 (goto-char (point-min))
680 (search-forward name)
683 (defvar mime-raw-browse-url-function (function mime-browse-url))
685 (defun mime-view-message/external-url (entity cal)
686 (let ((url (cdr (assoc "url" cal))))
687 (message (concat "Accessing " url " ..."))
688 (funcall mime-raw-browse-url-function url)))
694 (defun mime-view-caesar (entity situation)
695 "Internal method for mime-view to display ROT13-47-48 message."
696 (let* ((new-name (format "%s-%s" (buffer-name)
697 (mime-entity-number entity)))
698 (mother mime-preview-buffer))
699 (let ((pwin (or (get-buffer-window mother)
700 (get-largest-window)))
701 (buf (get-buffer-create new-name)))
702 (set-window-buffer pwin buf)
706 (setq buffer-read-only nil)
708 (mime-text-insert-decoded-body entity)
709 (mule-caesar-region (point-min) (point-max))
710 (set-buffer-modified-p nil)
712 (view-buffer new-name)
721 (let* ((file mime-acting-situation-examples-file)
722 (buffer (get-buffer-create " *mime-example*")))
723 (if (file-readable-p file)
728 (insert-file-contents file)
733 (while (and (> (length mime-acting-situation-example-list)
734 mime-acting-situation-example-list-max-size)
736 (mime-reduce-acting-situation-examples)
739 (error (setq mime-acting-situation-example-list nil)))
741 (kill-buffer buffer))))
743 ;;; mime-play.el ends here