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 (defcustom mime-save-directory "~/"
40 "*Name of the directory where MIME entity will be saved in.
41 If t, it means current directory."
43 :type '(choice (const :tag "Current directory" t)
46 (defvar mime-acting-situation-example-list nil)
48 (defvar mime-acting-situation-example-list-max-size 16)
50 (defun mime-save-acting-situation-examples ()
51 (let* ((file mime-acting-situation-examples-file)
52 (buffer (get-buffer-create " *mime-example*")))
56 (setq buffer-file-name file)
58 (insert ";;; " (file-name-nondirectory file) "\n")
59 (insert "\n;; This file is generated automatically by "
60 mime-view-version "\n\n")
61 (insert ";;; Code:\n\n")
62 (pp `(setq mime-acting-situation-example-list
63 ',mime-acting-situation-example-list)
66 (file-name-nondirectory file)
69 (kill-buffer buffer))))
71 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
73 (defun mime-reduce-acting-situation-examples ()
74 (let ((len (length mime-acting-situation-example-list))
82 ir mime-acting-situation-example-list)
86 jr mime-acting-situation-example-list)
90 (setq ret (mime-compare-situation-with-example (car ic)(car jc))
92 det-ret (+ (length (car ic))(length (car jc)))
93 det-org (length (cdr ret))
94 freq (+ (cdr ic)(cdr jc)))
95 (cond ((< max-sim sim)
102 dest (cons (cdr ret) freq))
105 (cond ((> min-det-ret det-ret)
106 (setq min-det-ret det-ret
111 dest (cons (cdr ret) freq))
113 ((= min-det-ret det-ret)
114 (cond ((> min-det-org det-org)
115 (setq min-det-org det-org
119 dest (cons (cdr ret) freq))
121 ((= min-det-org det-org)
122 (cond ((> min-freq freq)
126 dest (cons (cdr ret) freq))
140 (setq jr (nthcdr (1- d-j) mime-acting-situation-example-list))
141 (setcdr jr (cddr jr))
143 (setq mime-acting-situation-example-list
144 (cdr mime-acting-situation-example-list))
145 (setq ir (nthcdr (1- d-i) mime-acting-situation-example-list))
146 (setcdr ir (cddr ir))
148 (if (setq ir (assoc (car dest) mime-acting-situation-example-list))
149 (setcdr ir (+ (cdr ir)(cdr dest)))
150 (setq mime-acting-situation-example-list
151 (cons dest mime-acting-situation-example-list))
155 ;;; @ content decoder
159 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
160 "Play current entity.
161 It decodes current entity to call internal or external method. The
162 method is selected from variable `mime-acting-condition'.
163 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
164 `mime-acting-situation-example-list'.
165 If MODE is specified, play as it. Default MODE is \"play\"."
167 (let ((entity (get-text-property (point) 'mime-view-entity)))
170 (get-text-property (point) 'mime-view-situation)))
174 (if (assq 'mode situation)
175 (put-alist 'mode mode (copy-alist situation))
176 (cons (cons 'mode mode)
180 (cons (cons 'ignore-examples ignore-examples)
182 (mime-play-entity entity situation)
185 (defun mime-sort-situation (situation)
195 (disposition-type . 6)
199 (let ((ret (assq a-t order)))
201 (setq a-order (cdr ret))
207 (let ((ret (assq b-t order)))
209 (setq b-order (cdr ret))
214 (if (= a-order b-order)
215 (string< (format "%s" a-t)(format "%s" b-t))
220 (defsubst mime-delq-null-situation (situations field
221 &optional ignored-value)
224 (let* ((situation (car situations))
225 (cell (assq field situation)))
227 (or (eq (cdr cell) ignored-value)
228 (setq dest (cons situation dest))
230 (setq situations (cdr situations)))
233 (defun mime-compare-situation-with-example (situation example)
234 (let ((example (copy-alist example))
237 (let* ((cell (car situation))
239 (ecell (assoc key example)))
241 (if (equal cell ecell)
242 (setq match (1+ match))
243 (setq example (delq ecell example))
246 (setq situation (cdr situation))
252 (defun mime-play-entity (entity &optional situation ignored-method)
253 "Play entity specified by ENTITY.
254 It decodes the entity to call internal or external method. The method
255 is selected from variable `mime-acting-condition'. If MODE is
256 specified, play as it. Default MODE is \"play\"."
258 (in-calist-package 'mime-view)
260 (mime-delq-null-situation
261 (ctree-find-calist mime-acting-condition
262 (mime-entity-situation entity situation)
263 mime-view-find-every-acting-situation)
264 'method ignored-method))
265 (or (assq 'ignore-examples situation)
273 (let ((situation (car rest))
274 (examples mime-acting-situation-example-list))
277 (mime-compare-situation-with-example
278 situation (caar examples)))
279 (ret-score (car ret)))
280 (cond ((> ret-score max-score)
281 (setq max-score ret-score
282 max-escore (cdar examples)
283 max-examples (list (cdr ret))
284 max-situations (list situation))
286 ((= ret-score max-score)
287 (cond ((> (cdar examples) max-escore)
288 (setq max-escore (cdar examples)
289 max-examples (list (cdr ret))
290 max-situations (list situation))
292 ((= (cdar examples) max-escore)
294 (cons (cdr ret) max-examples))
295 (or (member situation max-situations)
297 (cons situation max-situations)))
299 (setq examples (cdr examples))))
300 (setq rest (cdr rest)))
302 (setq ret max-situations)
304 (let* ((example (car max-examples))
306 (assoc example mime-acting-situation-example-list)))
308 (setcdr cell (1+ (cdr cell)))
309 (setq mime-acting-situation-example-list
310 (cons (cons example 0)
311 mime-acting-situation-example-list))
313 (setq max-examples (cdr max-examples))
316 (setq ret (select-menu-alist
322 (cdr (assq 'method situation)))
325 (setq ret (mime-sort-situation ret))
326 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
331 (setq method (cdr (assq 'method ret)))
332 (cond ((and (symbolp method)
334 (funcall method entity ret)
337 (mime-activate-mailcap-method entity ret)
339 ;; ((and (listp method)(stringp (car method)))
340 ;; (mime-activate-external-method entity ret)
343 (mime-show-echo-buffer "No method are specified for %s\n"
344 (mime-type/subtype-string
345 (cdr (assq 'type situation))
346 (cdr (assq 'subtype situation))))
347 (if (y-or-n-p "Do you want to save current entity to disk?")
348 (mime-save-content entity situation))
353 ;;; @ external decoder
356 (defvar mime-mailcap-method-filename-alist nil)
358 (defun mime-activate-mailcap-method (entity situation)
359 (let ((method (cdr (assoc 'method situation)))
360 (name (mime-entity-safe-filename entity)))
362 (if (and name (not (string= name "")))
363 (expand-file-name name temporary-file-directory)
365 (expand-file-name "EMI" temporary-file-directory))
367 (mime-write-entity-content entity name)
368 (message "External method is starting...")
371 (mailcap-format-command
373 (cons (cons 'filename name) situation))))
374 (start-process command mime-echo-buffer-name
375 shell-file-name shell-command-switch command)
377 (set-alist 'mime-mailcap-method-filename-alist process name)
378 (set-process-sentinel process 'mime-mailcap-method-sentinel)
382 (defun mime-mailcap-method-sentinel (process event)
383 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
384 (if (file-exists-p file)
387 (remove-alist 'mime-mailcap-method-filename-alist process)
388 (message (format "%s %s" process event)))
390 (defvar mime-echo-window-is-shared-with-bbdb
391 (module-installed-p 'bbdb)
392 "*If non-nil, mime-echo window is shared with BBDB window.")
394 (defvar mime-echo-window-height
397 (/ (window-height) 5)
399 "*Size of mime-echo window.
400 It allows function or integer. If it is function,
401 `mime-show-echo-buffer' calls it to get height of mime-echo window.
402 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
405 (defun mime-show-echo-buffer (&rest forms)
406 "Show mime-echo buffer to display MIME-playing information."
407 (get-buffer-create mime-echo-buffer-name)
408 (let ((the-win (selected-window))
409 (win (get-buffer-window mime-echo-buffer-name)))
411 (unless (and mime-echo-window-is-shared-with-bbdb
413 (setq win (get-buffer-window bbdb-buffer-name))
415 (select-window (get-buffer-window (or mime-preview-buffer
417 (setq win (split-window-vertically
419 (if (functionp mime-echo-window-height)
420 (funcall mime-echo-window-height)
421 mime-echo-window-height)
424 (set-window-buffer win mime-echo-buffer-name)
427 (goto-char (point-max))
429 (let ((buffer-read-only nil))
430 (insert (apply (function format) forms))
432 (select-window the-win)
439 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
441 (defvar mime-view-file-name-regexp-1
442 (concat mime-view-file-name-char-regexp "+\\."
443 mime-view-file-name-char-regexp "+"))
445 (defvar mime-view-file-name-regexp-2
446 (concat (regexp-* mime-view-file-name-char-regexp)
447 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
449 (defun mime-entity-safe-filename (entity)
451 (or (mime-entity-filename entity)
453 (or (mime-entity-read-field entity 'Content-Description)
454 (mime-entity-read-field entity 'Subject))))
456 (or (string-match mime-view-file-name-regexp-1 subj)
457 (string-match mime-view-file-name-regexp-2 subj)))
458 (substring subj (match-beginning 0)(match-end 0))
461 (replace-as-filename filename)
465 ;;; @ file extraction
468 (defun mime-save-content (entity situation)
469 (let ((name (or (mime-entity-safe-filename entity)
470 (format "%s" (mime-entity-media-type entity))))
471 (dir (if (eq t mime-save-directory)
473 mime-save-directory))
475 (setq filename (read-file-name
476 (concat "File name: (default "
477 (file-name-nondirectory name) ") ")
479 (concat (file-name-as-directory dir)
480 (file-name-nondirectory name))))
481 (if (file-directory-p filename)
482 (setq filename (concat (file-name-as-directory filename)
483 (file-name-nondirectory name))))
484 (if (file-exists-p filename)
485 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
487 (mime-write-entity-content entity (expand-file-name filename))
494 (defvar mime-magic-type-alist
495 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
496 ("^\211PNG" image png)
497 ("^GIF8[79]" image gif)
498 ("^II\\*\000" image tiff)
499 ("^MM\000\\*" image tiff)
501 ("^\000\000\001\263" video mpeg)
503 "*Alist of regexp about magic-number vs. corresponding media-types.
504 Each element looks like (REGEXP TYPE SUBTYPE).
505 REGEXP is a regular expression to match against the beginning of the
507 TYPE is symbol to indicate primary type of media-type.
508 SUBTYPE is symbol to indicate subtype of media-type.")
510 (defun mime-detect-content (entity situation)
512 (let ((mdata (mime-entity-content entity))
513 (rest mime-magic-type-alist))
514 (while (not (let ((cell (car rest)))
516 (if (string-match (car cell) mdata)
517 (setq type (nth 1 cell)
518 subtype (nth 2 cell))
521 (setq rest (cdr rest))))
522 (setq situation (del-alist 'method (copy-alist situation)))
523 (mime-play-entity entity
525 (put-alist 'type type
526 (put-alist 'subtype subtype
529 'mime-detect-content)))
532 ;;; @ mail/news message
535 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
536 "Quitting method for mime-view.
537 It is registered to variable `mime-preview-quitting-method-alist'."
538 (let ((raw-buffer (mime-entity-buffer
539 (get-text-property (point-min) 'mime-view-entity)))
540 (mother mime-mother-buffer)
541 (win-conf mime-preview-original-window-configuration))
542 (kill-buffer raw-buffer)
543 (mime-preview-kill-buffer)
544 (set-window-configuration win-conf)
545 (pop-to-buffer mother)
548 (defun mime-view-message/rfc822 (entity situation)
550 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
551 (mother (current-buffer))
552 (children (car (mime-entity-children entity)))
554 (mime-display-message
555 children new-name mother nil
556 (cdr (assq 'major-mode
557 (get-text-property (point) 'mime-view-situation))))))
558 (or (get-buffer-window preview-buffer)
559 (let ((m-win (get-buffer-window mother)))
561 (set-window-buffer m-win preview-buffer)
562 (switch-to-buffer preview-buffer)
566 ;;; @ message/partial
569 (defun mime-store-message/partial-piece (entity cal)
572 (concat "m-prts-" (user-login-name)) temporary-file-directory))
573 (id (cdr (assoc "id" cal)))
574 (number (cdr (assoc "number" cal)))
575 (total (cdr (assoc "total" cal)))
577 (mother (current-buffer)))
578 (or (file-exists-p root-dir)
579 (make-directory root-dir))
580 (setq id (replace-as-filename id))
581 (setq root-dir (concat root-dir "/" id))
582 (or (file-exists-p root-dir)
583 (make-directory root-dir))
584 (setq file (concat root-dir "/FULL"))
585 (if (file-exists-p file)
586 (let ((full-buf (get-buffer-create "FULL"))
587 (pwin (or (get-buffer-window mother)
588 (get-largest-window)))
590 (save-window-excursion
591 (set-buffer full-buf)
593 (as-binary-input-file (insert-file-contents file))
594 (setq major-mode 'mime-show-message-mode)
595 (mime-view-buffer (current-buffer) nil mother)
596 (setq pbuf (current-buffer))
598 (set-window-buffer pwin pbuf)
601 (setq file (concat root-dir "/" number))
602 (mime-write-entity-body entity file)
603 (let ((total-file (concat root-dir "/CT")))
607 (or (file-exists-p total-file)
610 (get-buffer-create mime-temp-buffer-name))
613 (write-region (point-min)(point-max) total-file)
614 (kill-buffer (current-buffer))
616 (string-to-number total)
618 (and (file-exists-p total-file)
620 (set-buffer (find-file-noselect total-file))
622 (and (re-search-forward "[0-9]+" nil t)
624 (buffer-substring (match-beginning 0)
627 (kill-buffer (current-buffer))
630 (if (and total (> total 0)
631 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
635 (set-buffer (get-buffer-create mime-temp-buffer-name))
636 (let ((full-buf (current-buffer)))
640 (setq file (concat root-dir "/" (int-to-string i)))
641 (or (file-exists-p file)
644 (as-binary-input-file (insert-file-contents file))
645 (goto-char (point-max))
648 (as-binary-output-file
649 (write-region (point-min)(point-max)
650 (expand-file-name "FULL" root-dir)))
653 (let ((file (format "%s/%d" root-dir i)))
654 (and (file-exists-p file)
659 (let ((file (expand-file-name "CT" root-dir)))
660 (and (file-exists-p file)
663 (let ((pwin (or (get-buffer-window mother)
664 (get-largest-window)))
665 (pbuf (mime-display-message
666 (mime-open-entity 'buffer (current-buffer))
667 nil mother nil 'mime-show-message-mode)))
668 (set-window-buffer pwin pbuf)
674 ;;; @ message/external-body
677 (defvar mime-raw-dired-function
678 (if (and (>= emacs-major-version 19) window-system)
679 (function dired-other-frame)
680 (function mime-raw-dired-function-for-one-frame)
683 (defun mime-raw-dired-function-for-one-frame (dir)
684 (let ((win (or (get-buffer-window mime-preview-buffer)
685 (get-largest-window))))
690 (defun mime-view-message/external-anon-ftp (entity cal)
691 (let* ((site (cdr (assoc "site" cal)))
692 (directory (cdr (assoc "directory" cal)))
693 (name (cdr (assoc "name" cal)))
694 (pathname (concat "/anonymous@" site ":" directory)))
695 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
696 (funcall mime-raw-dired-function pathname)
697 (goto-char (point-min))
698 (search-forward name)
701 (defvar mime-raw-browse-url-function mime-browse-url-function)
703 (defun mime-view-message/external-url (entity cal)
704 (let ((url (cdr (assoc "url" cal))))
705 (message (concat "Accessing " url " ..."))
706 (funcall mime-raw-browse-url-function url)))
712 (defun mime-view-caesar (entity situation)
713 "Internal method for mime-view to display ROT13-47-48 message."
714 (let ((buf (get-buffer-create
715 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
716 (with-current-buffer buf
717 (setq buffer-read-only nil)
719 (mime-insert-text-content entity)
720 (mule-caesar-region (point-min) (point-max))
721 (set-buffer-modified-p nil)
723 (let ((win (get-buffer-window (current-buffer))))
724 (or (eq (selected-window) win)
725 (select-window (or win (get-largest-window)))
728 (goto-char (point-min))
737 (let* ((file mime-acting-situation-examples-file)
738 (buffer (get-buffer-create " *mime-example*")))
739 (if (file-readable-p file)
744 (insert-file-contents file)
749 (while (and (> (length mime-acting-situation-example-list)
750 mime-acting-situation-example-list-max-size)
752 (mime-reduce-acting-situation-examples)
755 (error (setq mime-acting-situation-example-list nil)))
757 (kill-buffer buffer))))
759 ;;; mime-play.el ends here