1 ;;; mime-play.el --- Playback processing module for mime-view.el
3 ;; Copyright (C) 1994,1995,1996,1997,1998,1999 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 ((len (length mime-acting-situation-example-list))
75 ir mime-acting-situation-example-list)
79 jr mime-acting-situation-example-list)
83 (setq ret (mime-compare-situation-with-example (car ic)(car jc))
85 det-ret (+ (length (car ic))(length (car jc)))
86 det-org (length (cdr ret))
87 freq (+ (cdr ic)(cdr jc)))
88 (cond ((< max-sim sim)
95 dest (cons (cdr ret) freq))
98 (cond ((> min-det-ret det-ret)
99 (setq min-det-ret det-ret
104 dest (cons (cdr ret) freq))
106 ((= min-det-ret det-ret)
107 (cond ((> min-det-org det-org)
108 (setq min-det-org det-org
112 dest (cons (cdr ret) freq))
114 ((= min-det-org det-org)
115 (cond ((> min-freq freq)
119 dest (cons (cdr ret) freq))
133 (setq jr (nthcdr (1- d-j) mime-acting-situation-example-list))
134 (setcdr jr (cddr jr))
136 (setq mime-acting-situation-example-list
137 (cdr mime-acting-situation-example-list))
138 (setq ir (nthcdr (1- d-i) mime-acting-situation-example-list))
139 (setcdr ir (cddr ir))
141 (if (setq ir (assoc (car dest) mime-acting-situation-example-list))
142 (setcdr ir (+ (cdr ir)(cdr dest)))
143 (setq mime-acting-situation-example-list
144 (cons dest mime-acting-situation-example-list))
148 ;;; @ content decoder
152 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
153 "Play current entity.
154 It decodes current entity to call internal or external method. The
155 method is selected from variable `mime-acting-condition'.
156 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
157 `mime-acting-situation-example-list'.
158 If MODE is specified, play as it. Default MODE is \"play\"."
160 (let ((entity (get-text-property (point) 'mime-view-entity)))
163 (get-text-property (point) 'mime-view-situation)))
167 (if (assq 'mode situation)
168 (put-alist 'mode mode (copy-alist situation))
169 (cons (cons 'mode mode)
173 (cons (cons 'ignore-examples ignore-examples)
175 (mime-play-entity entity situation)
178 (defun mime-sort-situation (situation)
188 (disposition-type . 6)
192 (let ((ret (assq a-t order)))
194 (setq a-order (cdr ret))
200 (let ((ret (assq b-t order)))
202 (setq b-order (cdr ret))
207 (if (= a-order b-order)
208 (string< (format "%s" a-t)(format "%s" b-t))
213 (defsubst mime-delq-null-situation (situations field
214 &optional ignored-value)
217 (let* ((situation (car situations))
218 (cell (assq field situation)))
220 (or (eq (cdr cell) ignored-value)
221 (setq dest (cons situation dest))
223 (setq situations (cdr situations)))
226 (defun mime-compare-situation-with-example (situation example)
227 (let ((example (copy-alist example))
230 (let* ((cell (car situation))
232 (ecell (assoc key example)))
234 (if (equal cell ecell)
235 (setq match (1+ match))
236 (setq example (delq ecell example))
239 (setq situation (cdr situation))
245 (defun mime-play-entity (entity &optional situation ignored-method)
246 "Play entity specified by ENTITY.
247 It decodes the entity to call internal or external method. The method
248 is selected from variable `mime-acting-condition'. If MODE is
249 specified, play as it. Default MODE is \"play\"."
251 (in-calist-package 'mime-view)
253 (mime-delq-null-situation
254 (ctree-find-calist mime-acting-condition
255 (mime-entity-situation entity situation)
256 mime-view-find-every-acting-situation)
257 'method ignored-method))
258 (or (assq 'ignore-examples situation)
266 (let ((situation (car rest))
267 (examples mime-acting-situation-example-list))
270 (mime-compare-situation-with-example
271 situation (caar examples)))
272 (ret-score (car ret)))
273 (cond ((> ret-score max-score)
274 (setq max-score ret-score
275 max-escore (cdar examples)
276 max-examples (list (cdr ret))
277 max-situations (list situation))
279 ((= ret-score max-score)
280 (cond ((> (cdar examples) max-escore)
281 (setq max-escore (cdar examples)
282 max-examples (list (cdr ret))
283 max-situations (list situation))
285 ((= (cdar examples) max-escore)
287 (cons (cdr ret) max-examples))
288 (or (member situation max-situations)
290 (cons situation max-situations)))
292 (setq examples (cdr examples))))
293 (setq rest (cdr rest)))
295 (setq ret max-situations)
297 (let* ((example (car max-examples))
299 (assoc example mime-acting-situation-example-list)))
301 (setcdr cell (1+ (cdr cell)))
302 (setq mime-acting-situation-example-list
303 (cons (cons example 0)
304 mime-acting-situation-example-list))
306 (setq max-examples (cdr max-examples))
309 (setq ret (select-menu-alist
315 (cdr (assq 'method situation)))
318 (setq ret (mime-sort-situation ret))
319 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
324 (setq method (cdr (assq 'method ret)))
325 (cond ((and (symbolp method)
327 (funcall method entity ret)
330 (mime-activate-mailcap-method entity ret)
332 ;; ((and (listp method)(stringp (car method)))
333 ;; (mime-activate-external-method entity ret)
336 (mime-show-echo-buffer "No method are specified for %s\n"
337 (mime-type/subtype-string
338 (cdr (assq 'type situation))
339 (cdr (assq 'subtype situation))))
340 (if (y-or-n-p "Do you want to save current entity to disk?")
341 (mime-save-content entity situation))
346 ;;; @ external decoder
349 (defvar mime-mailcap-method-filename-alist nil)
351 (defun mime-activate-mailcap-method (entity situation)
352 (let ((method (cdr (assoc 'method situation)))
353 (name (mime-entity-safe-filename entity)))
355 (if (and name (not (string= name "")))
356 (expand-file-name name temporary-file-directory)
358 (expand-file-name "EMI" temporary-file-directory))
360 (mime-write-entity-content entity name)
361 (message "External method is starting...")
364 (mailcap-format-command
366 (cons (cons 'filename name) situation))))
367 (start-process command mime-echo-buffer-name
368 shell-file-name shell-command-switch command)
370 (set-alist 'mime-mailcap-method-filename-alist process name)
371 (set-process-sentinel process 'mime-mailcap-method-sentinel)
375 (defun mime-mailcap-method-sentinel (process event)
376 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
377 (if (file-exists-p file)
380 (remove-alist 'mime-mailcap-method-filename-alist process)
381 (message (format "%s %s" process event)))
383 (defvar mime-echo-window-is-shared-with-bbdb
384 (module-installed-p 'bbdb)
385 "*If non-nil, mime-echo window is shared with BBDB window.")
387 (defvar mime-echo-window-height
390 (/ (window-height) 5)
392 "*Size of mime-echo window.
393 It allows function or integer. If it is function,
394 `mime-show-echo-buffer' calls it to get height of mime-echo window.
395 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
398 (defun mime-show-echo-buffer (&rest forms)
399 "Show mime-echo buffer to display MIME-playing information."
400 (get-buffer-create mime-echo-buffer-name)
401 (let ((the-win (selected-window))
402 (win (get-buffer-window mime-echo-buffer-name)))
404 (unless (and mime-echo-window-is-shared-with-bbdb
406 (setq win (get-buffer-window bbdb-buffer-name))
408 (select-window (get-buffer-window (or mime-preview-buffer
410 (setq win (split-window-vertically
412 (if (functionp mime-echo-window-height)
413 (funcall mime-echo-window-height)
414 mime-echo-window-height)
417 (set-window-buffer win mime-echo-buffer-name)
420 (goto-char (point-max))
422 (let ((buffer-read-only nil))
423 (insert (apply (function format) forms))
425 (select-window the-win)
432 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
434 (defvar mime-view-file-name-regexp-1
435 (concat mime-view-file-name-char-regexp "+\\."
436 mime-view-file-name-char-regexp "+"))
438 (defvar mime-view-file-name-regexp-2
439 (concat (regexp-* mime-view-file-name-char-regexp)
440 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
442 (defun mime-entity-safe-filename (entity)
444 (or (mime-entity-filename entity)
446 (or (mime-entity-read-field entity 'Content-Description)
447 (mime-entity-read-field entity 'Subject))))
449 (or (string-match mime-view-file-name-regexp-1 subj)
450 (string-match mime-view-file-name-regexp-2 subj)))
451 (substring subj (match-beginning 0)(match-end 0))
454 (replace-as-filename filename)
458 ;;; @ file extraction
461 (defun mime-save-content (entity situation)
462 (let* ((name (mime-entity-safe-filename entity))
463 (filename (if (and name (not (string-equal name "")))
464 (expand-file-name name
465 (save-window-excursion
469 (interactive "DDirectory: ")
471 (save-window-excursion
475 (interactive "FFilename: ")
476 (expand-file-name file)))))))
478 (if (file-exists-p filename)
479 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
481 (mime-write-entity-content entity filename)
488 (defvar mime-magic-type-alist
489 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
490 ("^\211PNG" image png)
491 ("^GIF8[79]" image gif)
492 ("^II\\*\000" image tiff)
493 ("^MM\000\\*" image tiff)
495 ("^\000\000\001\263" video mpeg)
497 "*Alist of regexp about magic-number vs. corresponding media-types.
498 Each element looks like (REGEXP TYPE SUBTYPE).
499 REGEXP is a regular expression to match against the beginning of the
501 TYPE is symbol to indicate primary type of media-type.
502 SUBTYPE is symbol to indicate subtype of media-type.")
504 (defun mime-detect-content (entity situation)
506 (let ((mdata (mime-entity-content entity))
507 (rest mime-magic-type-alist))
508 (while (not (let ((cell (car rest)))
510 (if (string-match (car cell) mdata)
511 (setq type (nth 1 cell)
512 subtype (nth 2 cell))
515 (setq rest (cdr rest))))
516 (setq situation (del-alist 'method (copy-alist situation)))
517 (mime-play-entity entity
519 (put-alist 'type type
520 (put-alist 'subtype subtype
523 'mime-detect-content)))
526 ;;; @ mail/news message
529 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
530 "Quitting method for mime-view.
531 It is registered to variable `mime-preview-quitting-method-alist'."
532 (let ((raw-buffer (mime-entity-buffer
533 (get-text-property (point-min) 'mime-view-entity)))
534 (mother mime-mother-buffer)
535 (win-conf mime-preview-original-window-configuration))
536 (kill-buffer raw-buffer)
537 (mime-preview-kill-buffer)
538 (set-window-configuration win-conf)
539 (pop-to-buffer mother)
542 (defun mime-view-message/rfc822 (entity situation)
544 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
545 (mother (current-buffer))
546 (children (car (mime-entity-children entity)))
548 (mime-display-message
549 children new-name mother nil
550 (cdr (assq 'major-mode
551 (get-text-property (point) 'mime-view-situation))))))
552 (or (get-buffer-window preview-buffer)
553 (let ((m-win (get-buffer-window mother)))
555 (set-window-buffer m-win preview-buffer)
556 (switch-to-buffer preview-buffer)
560 ;;; @ message/partial
563 (defun mime-store-message/partial-piece (entity cal)
566 (concat "m-prts-" (user-login-name)) temporary-file-directory))
567 (id (cdr (assoc "id" cal)))
568 (number (cdr (assoc "number" cal)))
569 (total (cdr (assoc "total" cal)))
571 (mother (current-buffer)))
572 (or (file-exists-p root-dir)
573 (make-directory root-dir))
574 (setq id (replace-as-filename id))
575 (setq root-dir (concat root-dir "/" id))
576 (or (file-exists-p root-dir)
577 (make-directory root-dir))
578 (setq file (concat root-dir "/FULL"))
579 (if (file-exists-p file)
580 (let ((full-buf (get-buffer-create "FULL"))
581 (pwin (or (get-buffer-window mother)
582 (get-largest-window)))
584 (save-window-excursion
585 (set-buffer full-buf)
587 (as-binary-input-file (insert-file-contents file))
588 (setq major-mode 'mime-show-message-mode)
589 (mime-view-buffer (current-buffer) nil mother)
590 (setq pbuf (current-buffer))
592 (set-window-buffer pwin pbuf)
595 (setq file (concat root-dir "/" number))
596 (mime-write-entity-body entity file)
597 (let ((total-file (concat root-dir "/CT")))
601 (or (file-exists-p total-file)
604 (get-buffer-create mime-temp-buffer-name))
607 (write-region (point-min)(point-max) total-file)
608 (kill-buffer (current-buffer))
610 (string-to-number total)
612 (and (file-exists-p total-file)
614 (set-buffer (find-file-noselect total-file))
616 (and (re-search-forward "[0-9]+" nil t)
618 (buffer-substring (match-beginning 0)
621 (kill-buffer (current-buffer))
624 (if (and total (> total 0)
625 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
629 (set-buffer (get-buffer-create mime-temp-buffer-name))
630 (let ((full-buf (current-buffer)))
634 (setq file (concat root-dir "/" (int-to-string i)))
635 (or (file-exists-p file)
638 (as-binary-input-file (insert-file-contents file))
639 (goto-char (point-max))
642 (as-binary-output-file
643 (write-region (point-min)(point-max)
644 (expand-file-name "FULL" root-dir)))
647 (let ((file (format "%s/%d" root-dir i)))
648 (and (file-exists-p file)
653 (let ((file (expand-file-name "CT" root-dir)))
654 (and (file-exists-p file)
657 (let ((pwin (or (get-buffer-window mother)
658 (get-largest-window)))
659 (pbuf (mime-display-message
660 (mime-open-entity 'buffer (current-buffer))
661 nil mother nil 'mime-show-message-mode)))
662 (set-window-buffer pwin pbuf)
668 ;;; @ message/external-body
671 (defvar mime-raw-dired-function
672 (if (and (>= emacs-major-version 19) window-system)
673 (function dired-other-frame)
674 (function mime-raw-dired-function-for-one-frame)
677 (defun mime-raw-dired-function-for-one-frame (dir)
678 (let ((win (or (get-buffer-window mime-preview-buffer)
679 (get-largest-window))))
684 (defun mime-view-message/external-anon-ftp (entity cal)
685 (let* ((site (cdr (assoc "site" cal)))
686 (directory (cdr (assoc "directory" cal)))
687 (name (cdr (assoc "name" cal)))
688 (pathname (concat "/anonymous@" site ":" directory)))
689 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
690 (funcall mime-raw-dired-function pathname)
691 (goto-char (point-min))
692 (search-forward name)
695 (defvar mime-raw-browse-url-function mime-browse-url-function)
697 (defun mime-view-message/external-url (entity cal)
698 (let ((url (cdr (assoc "url" cal))))
699 (message (concat "Accessing " url " ..."))
700 (funcall mime-raw-browse-url-function url)))
706 (defun mime-view-caesar (entity situation)
707 "Internal method for mime-view to display ROT13-47-48 message."
708 (let ((buf (get-buffer-create
709 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
710 (with-current-buffer buf
711 (setq buffer-read-only nil)
713 (mime-insert-text-content entity)
714 (mule-caesar-region (point-min) (point-max))
715 (set-buffer-modified-p nil)
717 (let ((win (get-buffer-window (current-buffer))))
718 (or (eq (selected-window) win)
719 (select-window (or win (get-largest-window)))
722 (goto-char (point-min))
731 (let* ((file mime-acting-situation-examples-file)
732 (buffer (get-buffer-create " *mime-example*")))
733 (if (file-readable-p file)
738 (insert-file-contents file)
743 (while (and (> (length mime-acting-situation-example-list)
744 mime-acting-situation-example-list-max-size)
746 (mime-reduce-acting-situation-examples)
749 (error (setq mime-acting-situation-example-list nil)))
751 (kill-buffer buffer))))
753 ;;; mime-play.el ends here