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 (defun mime-compare-situation-with-example (situation example)
221 (let ((example (copy-alist example))
224 (let* ((cell (car situation))
226 (ecell (assoc key example)))
228 (if (equal cell ecell)
229 (setq match (1+ match))
230 (setq example (delq ecell example))
233 (setq situation (cdr situation))
239 (defun mime-play-entity (entity &optional situation ignored-method)
240 "Play entity specified by ENTITY.
241 It decodes the entity to call internal or external method. The method
242 is selected from variable `mime-acting-condition'. If MODE is
243 specified, play as it. Default MODE is \"play\"."
245 (in-calist-package 'mime-view)
247 (mime-delq-null-situation
248 (ctree-find-calist mime-acting-condition
249 (mime-entity-situation entity situation)
250 mime-view-find-every-acting-situation)
251 'method ignored-method))
252 (or (assq 'ignore-examples situation)
260 (let ((situation (car rest))
261 (examples mime-acting-situation-example-list))
264 (mime-compare-situation-with-example
265 situation (caar examples)))
266 (ret-score (car ret)))
267 (cond ((> ret-score max-score)
268 (setq max-score ret-score
269 max-escore (cdar examples)
270 max-examples (list (cdr ret))
271 max-situations (list situation))
273 ((= ret-score max-score)
274 (cond ((> (cdar examples) max-escore)
275 (setq max-escore (cdar examples)
276 max-examples (list (cdr ret))
277 max-situations (list situation))
279 ((= (cdar examples) max-escore)
281 (cons (cdr ret) max-examples))
282 (or (member situation max-situations)
284 (cons situation max-situations)))
286 (setq examples (cdr examples))))
287 (setq rest (cdr rest)))
289 (setq ret max-situations)
291 (let* ((example (car max-examples))
293 (assoc example mime-acting-situation-example-list)))
295 (setcdr cell (1+ (cdr cell)))
296 (setq mime-acting-situation-example-list
297 (cons (cons example 0)
298 mime-acting-situation-example-list))
300 (setq max-examples (cdr max-examples))
303 (setq ret (select-menu-alist
309 (cdr (assq 'method situation)))
312 (setq ret (mime-sort-situation ret))
313 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
318 (setq method (cdr (assq 'method ret)))
319 (cond ((and (symbolp method)
321 (funcall method entity ret)
324 (mime-activate-mailcap-method entity ret)
326 ;; ((and (listp method)(stringp (car method)))
327 ;; (mime-activate-external-method entity ret)
330 (mime-show-echo-buffer "No method are specified for %s\n"
331 (mime-type/subtype-string
332 (cdr (assq 'type situation))
333 (cdr (assq 'subtype situation))))
334 (if (y-or-n-p "Do you want to save current entity to disk?")
335 (mime-save-content entity situation))
340 ;;; @ external decoder
343 (defvar mime-mailcap-method-filename-alist nil)
345 (defun mime-activate-mailcap-method (entity situation)
346 (let ((method (cdr (assoc 'method situation)))
347 (name (mime-entity-safe-filename entity)))
349 (if (and name (not (string= name "")))
350 (expand-file-name name temporary-file-directory)
352 (expand-file-name "EMI" temporary-file-directory))
354 (mime-write-entity-content entity name)
355 (message "External method is starting...")
358 (mailcap-format-command
360 (cons (cons 'filename name) situation))))
361 (start-process command mime-echo-buffer-name
362 shell-file-name shell-command-switch command)
364 (set-alist 'mime-mailcap-method-filename-alist process name)
365 (set-process-sentinel process 'mime-mailcap-method-sentinel)
369 (defun mime-mailcap-method-sentinel (process event)
370 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
371 (if (file-exists-p file)
374 (remove-alist 'mime-mailcap-method-filename-alist process)
375 (message (format "%s %s" process event)))
377 (defvar mime-echo-window-is-shared-with-bbdb
378 (module-installed-p 'bbdb)
379 "*If non-nil, mime-echo window is shared with BBDB window.")
381 (defvar mime-echo-window-height
384 (/ (window-height) 5)
386 "*Size of mime-echo window.
387 It allows function or integer. If it is function,
388 `mime-show-echo-buffer' calls it to get height of mime-echo window.
389 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
392 (defun mime-show-echo-buffer (&rest forms)
393 "Show mime-echo buffer to display MIME-playing information."
394 (get-buffer-create mime-echo-buffer-name)
395 (let ((the-win (selected-window))
396 (win (get-buffer-window mime-echo-buffer-name)))
398 (unless (and mime-echo-window-is-shared-with-bbdb
400 (setq win (get-buffer-window bbdb-buffer-name))
402 (select-window (get-buffer-window (or mime-preview-buffer
404 (setq win (split-window-vertically
406 (if (functionp mime-echo-window-height)
407 (funcall mime-echo-window-height)
408 mime-echo-window-height)
411 (set-window-buffer win mime-echo-buffer-name)
414 (goto-char (point-max))
416 (let ((buffer-read-only nil))
417 (insert (apply (function format) forms))
419 (select-window the-win)
426 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
428 (defvar mime-view-file-name-regexp-1
429 (concat mime-view-file-name-char-regexp "+\\."
430 mime-view-file-name-char-regexp "+"))
432 (defvar mime-view-file-name-regexp-2
433 (concat (regexp-* mime-view-file-name-char-regexp)
434 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
436 (defun mime-entity-safe-filename (entity)
438 (or (mime-entity-filename entity)
440 (or (mime-entity-read-field entity 'Content-Description)
441 (mime-entity-read-field entity 'Subject))))
443 (or (string-match mime-view-file-name-regexp-1 subj)
444 (string-match mime-view-file-name-regexp-2 subj)))
445 (substring subj (match-beginning 0)(match-end 0))
448 (replace-as-filename filename)
452 ;;; @ file extraction
455 (defun mime-save-content (entity situation)
456 (let ((name (or (mime-entity-safe-filename entity)
457 (format "%s" (mime-entity-media-type entity))))
458 (dir (if (eq t mime-save-directory)
460 mime-save-directory))
462 (setq filename (read-file-name
463 (concat "File name: (default "
464 (file-name-nondirectory name) ") ")
466 (concat (file-name-as-directory dir)
467 (file-name-nondirectory name))))
468 (if (file-directory-p filename)
469 (setq filename (concat (file-name-as-directory filename)
470 (file-name-nondirectory name))))
471 (if (file-exists-p filename)
472 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
474 (mime-write-entity-content entity (expand-file-name filename))
481 (defvar mime-magic-type-alist
482 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
483 ("^\211PNG" image png)
484 ("^GIF8[79]" image gif)
485 ("^II\\*\000" image tiff)
486 ("^MM\000\\*" image tiff)
488 ("^\000\000\001\263" video mpeg)
490 "*Alist of regexp about magic-number vs. corresponding media-types.
491 Each element looks like (REGEXP TYPE SUBTYPE).
492 REGEXP is a regular expression to match against the beginning of the
494 TYPE is symbol to indicate primary type of media-type.
495 SUBTYPE is symbol to indicate subtype of media-type.")
497 (defun mime-detect-content (entity situation)
499 (let ((mdata (mime-entity-content entity))
500 (rest mime-magic-type-alist))
501 (while (not (let ((cell (car rest)))
503 (if (string-match (car cell) mdata)
504 (setq type (nth 1 cell)
505 subtype (nth 2 cell))
508 (setq rest (cdr rest))))
509 (setq situation (del-alist 'method (copy-alist situation)))
510 (mime-play-entity entity
512 (put-alist 'type type
513 (put-alist 'subtype subtype
516 'mime-detect-content)))
519 ;;; @ mail/news message
522 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
523 "Quitting method for mime-view.
524 It is registered to variable `mime-preview-quitting-method-alist'."
525 (let ((raw-buffer (mime-entity-buffer
526 (get-text-property (point-min) 'mime-view-entity)))
527 (mother mime-mother-buffer)
528 (win-conf mime-preview-original-window-configuration))
529 (kill-buffer raw-buffer)
530 (mime-preview-kill-buffer)
531 (set-window-configuration win-conf)
532 (pop-to-buffer mother)
535 (defun mime-view-message/rfc822 (entity situation)
537 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
538 (mother (current-buffer))
539 (children (car (mime-entity-children entity)))
541 (mime-display-message
542 children new-name mother nil
543 (cdr (assq 'major-mode
544 (get-text-property (point) 'mime-view-situation))))))
545 (or (get-buffer-window preview-buffer)
546 (let ((m-win (get-buffer-window mother)))
548 (set-window-buffer m-win preview-buffer)
549 (switch-to-buffer preview-buffer)
553 ;;; @ message/partial
556 (defun mime-store-message/partial-piece (entity cal)
559 (concat "m-prts-" (user-login-name)) temporary-file-directory))
560 (id (cdr (assoc "id" cal)))
561 (number (cdr (assoc "number" cal)))
562 (total (cdr (assoc "total" cal)))
564 (mother (current-buffer)))
565 (or (file-exists-p root-dir)
566 (make-directory root-dir))
567 (setq id (replace-as-filename id))
568 (setq root-dir (concat root-dir "/" id))
569 (or (file-exists-p root-dir)
570 (make-directory root-dir))
571 (setq file (concat root-dir "/FULL"))
572 (if (file-exists-p file)
573 (let ((full-buf (get-buffer-create "FULL"))
574 (pwin (or (get-buffer-window mother)
575 (get-largest-window)))
577 (save-window-excursion
578 (set-buffer full-buf)
580 (as-binary-input-file (insert-file-contents file))
581 (setq major-mode 'mime-show-message-mode)
582 (mime-view-buffer (current-buffer) nil mother)
583 (setq pbuf (current-buffer))
585 (set-window-buffer pwin pbuf)
588 (setq file (concat root-dir "/" number))
589 (mime-write-entity-body entity file)
590 (let ((total-file (concat root-dir "/CT")))
594 (or (file-exists-p total-file)
597 (get-buffer-create mime-temp-buffer-name))
600 (write-region (point-min)(point-max) total-file)
601 (kill-buffer (current-buffer))
603 (string-to-number total)
605 (and (file-exists-p total-file)
607 (set-buffer (find-file-noselect total-file))
609 (and (re-search-forward "[0-9]+" nil t)
611 (buffer-substring (match-beginning 0)
614 (kill-buffer (current-buffer))
617 (if (and total (> total 0)
618 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
622 (set-buffer (get-buffer-create mime-temp-buffer-name))
623 (let ((full-buf (current-buffer)))
627 (setq file (concat root-dir "/" (int-to-string i)))
628 (or (file-exists-p file)
631 (as-binary-input-file (insert-file-contents file))
632 (goto-char (point-max))
635 (as-binary-output-file
636 (write-region (point-min)(point-max)
637 (expand-file-name "FULL" root-dir)))
640 (let ((file (format "%s/%d" root-dir i)))
641 (and (file-exists-p file)
646 (let ((file (expand-file-name "CT" root-dir)))
647 (and (file-exists-p file)
650 (let ((pwin (or (get-buffer-window mother)
651 (get-largest-window)))
652 (pbuf (mime-display-message
653 (mime-open-entity 'buffer (current-buffer))
654 nil mother nil 'mime-show-message-mode)))
655 (set-window-buffer pwin pbuf)
661 ;;; @ message/external-body
664 (defvar mime-raw-dired-function
665 (if (and (>= emacs-major-version 19) window-system)
666 (function dired-other-frame)
667 (function mime-raw-dired-function-for-one-frame)
670 (defun mime-raw-dired-function-for-one-frame (dir)
671 (let ((win (or (get-buffer-window mime-preview-buffer)
672 (get-largest-window))))
677 (defun mime-view-message/external-anon-ftp (entity cal)
678 (let* ((site (cdr (assoc "site" cal)))
679 (directory (cdr (assoc "directory" cal)))
680 (name (cdr (assoc "name" cal)))
681 (pathname (concat "/anonymous@" site ":" directory)))
682 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
683 (funcall mime-raw-dired-function pathname)
684 (goto-char (point-min))
685 (search-forward name)
688 (defvar mime-raw-browse-url-function mime-browse-url-function)
690 (defun mime-view-message/external-url (entity cal)
691 (let ((url (cdr (assoc "url" cal))))
692 (message (concat "Accessing " url " ..."))
693 (funcall mime-raw-browse-url-function url)))
699 (defun mime-view-caesar (entity situation)
700 "Internal method for mime-view to display ROT13-47-48 message."
701 (let ((buf (get-buffer-create
702 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
703 (with-current-buffer buf
704 (setq buffer-read-only nil)
706 (mime-insert-text-content entity)
707 (mule-caesar-region (point-min) (point-max))
708 (set-buffer-modified-p nil)
710 (let ((win (get-buffer-window (current-buffer))))
711 (or (eq (selected-window) win)
712 (select-window (or win (get-largest-window)))
715 (goto-char (point-min))
724 (let* ((file mime-acting-situation-examples-file)
725 (buffer (get-buffer-create " *mime-example*")))
726 (if (file-readable-p file)
731 (insert-file-contents file)
736 (while (and (> (length mime-acting-situation-example-list)
737 mime-acting-situation-example-list-max-size)
739 (mime-reduce-acting-situation-examples)
742 (error (setq mime-acting-situation-example-list nil)))
744 (kill-buffer buffer))))
746 ;;; mime-play.el ends here