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)))
162 (let ((situation (list (cons 'mode (or mode "play")))))
165 (cons (cons 'ignore-examples ignore-examples)
167 (mime-play-entity entity situation)
170 (defun mime-sort-situation (situation)
180 (disposition-type . 6)
184 (let ((ret (assq a-t order)))
186 (setq a-order (cdr ret))
192 (let ((ret (assq b-t order)))
194 (setq b-order (cdr ret))
199 (if (= a-order b-order)
200 (string< (format "%s" a-t)(format "%s" b-t))
205 (defsubst mime-delq-null-situation (situations field
206 &optional ignored-value)
209 (let* ((situation (car situations))
210 (cell (assq field situation)))
212 (or (eq (cdr cell) ignored-value)
213 (setq dest (cons situation dest))
215 (setq situations (cdr situations)))
218 (defun mime-compare-situation-with-example (situation example)
219 (let ((example (copy-alist example))
222 (let* ((cell (car situation))
224 (ecell (assoc key example)))
226 (if (equal cell ecell)
227 (setq match (1+ match))
228 (setq example (delq ecell example))
231 (setq situation (cdr situation))
237 (defun mime-play-entity (entity &optional situation ignored-method)
238 "Play entity specified by ENTITY.
239 It decodes the entity to call internal or external method. The method
240 is selected from variable `mime-acting-condition'. If MODE is
241 specified, play as it. Default MODE is \"play\"."
243 (in-calist-package 'mime-view)
245 (mime-delq-null-situation
246 (ctree-find-calist mime-acting-condition
247 (mime-entity-situation entity situation)
248 mime-view-find-every-acting-situation)
249 'method ignored-method))
250 (or (assq 'ignore-examples situation)
258 (let ((situation (car rest))
259 (examples mime-acting-situation-example-list))
262 (mime-compare-situation-with-example
263 situation (caar examples)))
264 (ret-score (car ret)))
265 (cond ((> ret-score max-score)
266 (setq max-score ret-score
267 max-escore (cdar examples)
268 max-examples (list (cdr ret))
269 max-situations (list situation))
271 ((= ret-score max-score)
272 (cond ((> (cdar examples) max-escore)
273 (setq max-escore (cdar examples)
274 max-examples (list (cdr ret))
275 max-situations (list situation))
277 ((= (cdar examples) max-escore)
279 (cons (cdr ret) max-examples))
280 (or (member situation max-situations)
282 (cons situation max-situations)))
284 (setq examples (cdr examples))))
285 (setq rest (cdr rest)))
287 (setq ret max-situations)
289 (let* ((example (car max-examples))
291 (assoc example mime-acting-situation-example-list)))
293 (setcdr cell (1+ (cdr cell)))
294 (setq mime-acting-situation-example-list
295 (cons (cons example 0)
296 mime-acting-situation-example-list))
298 (setq max-examples (cdr max-examples))
301 (setq ret (select-menu-alist
307 (cdr (assq 'method situation)))
310 (setq ret (mime-sort-situation ret))
311 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
316 (setq method (cdr (assq 'method ret)))
317 (cond ((and (symbolp method)
319 (funcall method entity ret)
322 (mime-activate-mailcap-method entity ret)
324 ;; ((and (listp method)(stringp (car method)))
325 ;; (mime-activate-external-method entity ret)
328 (mime-show-echo-buffer "No method are specified for %s\n"
329 (mime-type/subtype-string
330 (cdr (assq 'type situation))
331 (cdr (assq 'subtype situation))))
332 (if (y-or-n-p "Do you want to save current entity to disk?")
333 (mime-save-content entity situation))
338 ;;; @ external decoder
341 (defvar mime-mailcap-method-filename-alist nil)
343 (defun mime-activate-mailcap-method (entity situation)
344 (let ((method (cdr (assoc 'method situation)))
345 (name (mime-entity-safe-filename entity)))
347 (if (and name (not (string= name "")))
348 (expand-file-name name temporary-file-directory)
350 (expand-file-name "EMI" temporary-file-directory))
352 (mime-write-entity-content entity name)
353 (message "External method is starting...")
356 (mailcap-format-command
358 (cons (cons 'filename name) situation))))
359 (start-process command mime-echo-buffer-name
360 shell-file-name shell-command-switch command)
362 (set-alist 'mime-mailcap-method-filename-alist process name)
363 (set-process-sentinel process 'mime-mailcap-method-sentinel)
367 (defun mime-mailcap-method-sentinel (process event)
368 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
369 (if (file-exists-p file)
372 (remove-alist 'mime-mailcap-method-filename-alist process)
373 (message (format "%s %s" process event)))
375 (defvar mime-echo-window-is-shared-with-bbdb
376 (module-installed-p 'bbdb)
377 "*If non-nil, mime-echo window is shared with BBDB window.")
379 (defvar mime-echo-window-height
382 (/ (window-height) 5)
384 "*Size of mime-echo window.
385 It allows function or integer. If it is function,
386 `mime-show-echo-buffer' calls it to get height of mime-echo window.
387 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
390 (defun mime-show-echo-buffer (&rest forms)
391 "Show mime-echo buffer to display MIME-playing information.
392 It returns the list of window, start and end positions of inserted text.
393 A window height of the buffer `mime-echo-buffer-name' will be determined
394 by `mime-echo-window-height' (its value or its return value) whenever
395 this function is called."
396 (get-buffer-create mime-echo-buffer-name)
397 (let ((the-win (selected-window))
398 (win (get-buffer-window mime-echo-buffer-name))
399 (height (if (functionp mime-echo-window-height)
400 (funcall mime-echo-window-height)
401 mime-echo-window-height))
406 (enlarge-window (- height (window-height)))
408 (unless (and mime-echo-window-is-shared-with-bbdb
411 (setq win (get-buffer-window bbdb-buffer-name))
414 (select-window (get-buffer-window (or mime-preview-buffer
416 (let ((window-min-height 1))
417 (setq win (split-window-vertically (- (window-height) height)))
419 (set-window-buffer win mime-echo-buffer-name)
422 (goto-char (setq start (point-max)))
424 (let ((buffer-read-only nil))
425 (insert (apply (function format) forms))
428 (list win start (point))
429 (select-window the-win)
436 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
438 (defvar mime-view-file-name-regexp-1
439 (concat mime-view-file-name-char-regexp "+\\."
440 mime-view-file-name-char-regexp "+"))
442 (defvar mime-view-file-name-regexp-2
443 (concat (regexp-* mime-view-file-name-char-regexp)
444 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
446 (defun mime-entity-safe-filename (entity)
448 (or (mime-entity-filename entity)
450 (or (mime-entity-read-field entity 'Content-Description)
451 (mime-entity-read-field entity 'Subject))))
453 (or (string-match mime-view-file-name-regexp-1 subj)
454 (string-match mime-view-file-name-regexp-2 subj)))
455 (substring subj (match-beginning 0)(match-end 0))
458 (replace-as-filename filename)
462 ;;; @ file extraction
465 (defun mime-save-content (entity situation)
466 (let* ((name (mime-entity-safe-filename entity))
467 (filename (if (and name (not (string-equal name "")))
468 (expand-file-name name
469 (save-window-excursion
473 (interactive "DDirectory: ")
475 (save-window-excursion
479 (interactive "FFilename: ")
480 (expand-file-name file)))))))
482 (if (file-exists-p filename)
483 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
485 (mime-write-entity-content entity filename)
492 (defvar mime-magic-type-alist
493 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
494 ("^\211PNG" image png)
495 ("^GIF8[79]" image gif)
496 ("^II\\*\000" image tiff)
497 ("^MM\000\\*" image tiff)
499 ("^\000\000\001\263" video mpeg)
501 "*Alist of regexp about magic-number vs. corresponding media-types.
502 Each element looks like (REGEXP TYPE SUBTYPE).
503 REGEXP is a regular expression to match against the beginning of the
505 TYPE is symbol to indicate primary type of media-type.
506 SUBTYPE is symbol to indicate subtype of media-type.")
508 (defun mime-detect-content (entity situation)
510 (let ((mdata (mime-entity-content entity))
511 (rest mime-magic-type-alist))
512 (while (not (let ((cell (car rest)))
514 (if (string-match (car cell) mdata)
515 (setq type (nth 1 cell)
516 subtype (nth 2 cell))
519 (setq rest (cdr rest))))
520 (setq situation (del-alist 'method (copy-alist situation)))
521 (mime-play-entity entity
523 (put-alist 'type type
524 (put-alist 'subtype subtype
527 'mime-detect-content)))
530 ;;; @ mail/news message
533 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
534 "Quitting method for mime-view.
535 It is registered to variable `mime-preview-quitting-method-alist'."
536 (let ((raw-buffer (mime-entity-buffer
537 (get-text-property (point-min) 'mime-view-entity)))
538 (mother mime-mother-buffer)
539 (win-conf mime-preview-original-window-configuration))
540 (kill-buffer raw-buffer)
541 (mime-preview-kill-buffer)
542 (set-window-configuration win-conf)
543 (pop-to-buffer mother)
546 (defun mime-view-message/rfc822 (entity situation)
548 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
549 (mother (current-buffer))
550 (children (car (mime-entity-children entity)))
552 (mime-display-message
553 children new-name mother nil
554 (cdr (assq 'major-mode
555 (get-text-property (point) 'mime-view-situation))))))
556 (or (get-buffer-window preview-buffer)
557 (let ((m-win (get-buffer-window mother)))
559 (set-window-buffer m-win preview-buffer)
560 (switch-to-buffer preview-buffer)
564 ;;; @ message/partial
567 (defun mime-store-message/partial-piece (entity cal)
570 (concat "m-prts-" (user-login-name)) temporary-file-directory))
571 (id (cdr (assoc "id" cal)))
572 (number (cdr (assoc "number" cal)))
573 (total (cdr (assoc "total" cal)))
575 (mother (current-buffer)))
576 (or (file-exists-p root-dir)
577 (make-directory root-dir))
578 (setq id (replace-as-filename id))
579 (setq root-dir (concat root-dir "/" id))
580 (or (file-exists-p root-dir)
581 (make-directory root-dir))
582 (setq file (concat root-dir "/FULL"))
583 (if (file-exists-p file)
584 (let ((full-buf (get-buffer-create "FULL"))
585 (pwin (or (get-buffer-window mother)
586 (get-largest-window)))
588 (save-window-excursion
589 (set-buffer full-buf)
591 (as-binary-input-file (insert-file-contents file))
592 (setq major-mode 'mime-show-message-mode)
593 (mime-view-buffer (current-buffer) nil mother)
594 (setq pbuf (current-buffer))
596 (set-window-buffer pwin pbuf)
599 (setq file (concat root-dir "/" number))
600 (mime-write-entity-body entity file)
601 (let ((total-file (concat root-dir "/CT")))
605 (or (file-exists-p total-file)
608 (get-buffer-create mime-temp-buffer-name))
611 (write-region (point-min)(point-max) total-file)
612 (kill-buffer (current-buffer))
614 (string-to-number total)
616 (and (file-exists-p total-file)
618 (set-buffer (find-file-noselect total-file))
620 (and (re-search-forward "[0-9]+" nil t)
622 (buffer-substring (match-beginning 0)
625 (kill-buffer (current-buffer))
628 (if (and total (> total 0))
631 (set-buffer (get-buffer-create mime-temp-buffer-name))
632 (let ((full-buf (current-buffer)))
636 (setq file (concat root-dir "/" (int-to-string i)))
637 (or (file-exists-p file)
640 (as-binary-input-file (insert-file-contents file))
641 (goto-char (point-max))
644 (as-binary-output-file
645 (write-region (point-min)(point-max)
646 (expand-file-name "FULL" root-dir)))
649 (let ((file (format "%s/%d" root-dir i)))
650 (and (file-exists-p file)
655 (let ((file (expand-file-name "CT" root-dir)))
656 (and (file-exists-p file)
659 (save-window-excursion
660 (setq major-mode 'mime-show-message-mode)
661 (mime-view-buffer (current-buffer) nil mother)
663 (let ((pwin (or (get-buffer-window mother)
664 (get-largest-window)))
665 (pbuf (save-excursion
666 (set-buffer full-buf)
667 mime-preview-buffer)))
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