1 ;;; mime-play.el --- Playback processing module for mime-view.el
3 ;; Copyright (C) 1994,95,96,97,98,99,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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)
186 (defun mime-play-entity (entity &optional situation ignored-method)
187 "Play entity specified by ENTITY.
188 It decodes the entity to call internal or external method. The method
189 is selected from variable `mime-acting-condition'. If MODE is
190 specified, play as it. Default MODE is \"play\"."
192 (in-calist-package 'mime-view)
194 (mime-delq-null-situation
195 (ctree-find-calist mime-acting-condition
196 (mime-entity-situation entity situation)
197 mime-view-find-every-acting-situation)
198 'method ignored-method))
199 (or (assq 'ignore-examples situation)
207 (let ((situation (car rest))
208 (examples mime-acting-situation-example-list))
211 (mime-compare-situation-with-example
212 situation (caar examples)))
213 (ret-score (car ret)))
214 (cond ((> ret-score max-score)
215 (setq max-score ret-score
216 max-escore (cdar examples)
217 max-examples (list (cdr ret))
218 max-situations (list situation))
220 ((= ret-score max-score)
221 (cond ((> (cdar examples) max-escore)
222 (setq max-escore (cdar examples)
223 max-examples (list (cdr ret))
224 max-situations (list situation))
226 ((= (cdar examples) max-escore)
228 (cons (cdr ret) max-examples))
229 (or (member situation max-situations)
231 (cons situation max-situations)))
233 (setq examples (cdr examples))))
234 (setq rest (cdr rest)))
236 (setq ret max-situations)
238 (let* ((example (car max-examples))
240 (assoc example mime-acting-situation-example-list)))
242 (setcdr cell (1+ (cdr cell)))
243 (setq mime-acting-situation-example-list
244 (cons (cons example 0)
245 mime-acting-situation-example-list))
247 (setq max-examples (cdr max-examples))
250 (setq ret (select-menu-alist
256 (cdr (assq 'method situation)))
259 (setq ret (mime-sort-situation ret))
260 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
265 (setq method (cdr (assq 'method ret)))
266 (cond ((and (symbolp method)
268 (funcall method entity ret)
271 (mime-activate-mailcap-method entity ret)
273 ;; ((and (listp method)(stringp (car method)))
274 ;; (mime-activate-external-method entity ret)
277 (mime-show-echo-buffer "No method are specified for %s\n"
278 (mime-type/subtype-string
279 (cdr (assq 'type situation))
280 (cdr (assq 'subtype situation))))
281 (if (y-or-n-p "Do you want to save current entity to disk?")
282 (mime-save-content entity situation))
287 ;;; @ external decoder
290 (defvar mime-mailcap-method-filename-alist nil)
292 (defun mime-activate-mailcap-method (entity situation)
293 (let ((method (cdr (assoc 'method situation)))
294 (name (mime-entity-safe-filename entity)))
296 (if (and name (not (string= name "")))
297 (expand-file-name name temporary-file-directory)
299 (expand-file-name "EMI" temporary-file-directory))
301 (mime-write-entity-content entity name)
302 (message "External method is starting...")
305 (mailcap-format-command
307 (cons (cons 'filename name) situation))))
308 (start-process command mime-echo-buffer-name
309 shell-file-name shell-command-switch command)
311 (set-alist 'mime-mailcap-method-filename-alist process name)
312 (set-process-sentinel process 'mime-mailcap-method-sentinel)
316 (defun mime-mailcap-method-sentinel (process event)
317 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
318 (if (file-exists-p file)
321 (remove-alist 'mime-mailcap-method-filename-alist process)
322 (message (format "%s %s" process event)))
324 (defvar mime-echo-window-is-shared-with-bbdb
325 (module-installed-p 'bbdb)
326 "*If non-nil, mime-echo window is shared with BBDB window.")
328 (defvar mime-echo-window-height
331 (/ (window-height) 5)
333 "*Size of mime-echo window.
334 It allows function or integer. If it is function,
335 `mime-show-echo-buffer' calls it to get height of mime-echo window.
336 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
339 (defun mime-show-echo-buffer (&rest forms)
340 "Show mime-echo buffer to display MIME-playing information."
341 (get-buffer-create mime-echo-buffer-name)
342 (let ((the-win (selected-window))
343 (win (get-buffer-window mime-echo-buffer-name)))
345 (unless (and mime-echo-window-is-shared-with-bbdb
347 (setq win (get-buffer-window bbdb-buffer-name))
349 (select-window (get-buffer-window (or mime-preview-buffer
351 (setq win (split-window-vertically
353 (if (functionp mime-echo-window-height)
354 (funcall mime-echo-window-height)
355 mime-echo-window-height)
358 (set-window-buffer win mime-echo-buffer-name)
361 (goto-char (point-max))
363 (let ((buffer-read-only nil))
364 (insert (apply (function format) forms))
366 (select-window the-win)
373 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
375 (defvar mime-view-file-name-regexp-1
376 (concat mime-view-file-name-char-regexp "+\\."
377 mime-view-file-name-char-regexp "+"))
379 (defvar mime-view-file-name-regexp-2
380 (concat (regexp-* mime-view-file-name-char-regexp)
381 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
383 (defun mime-entity-safe-filename (entity)
385 (or (mime-entity-filename entity)
387 (or (mime-entity-read-field entity 'Content-Description)
388 (mime-entity-read-field entity 'Subject))))
390 (or (string-match mime-view-file-name-regexp-1 subj)
391 (string-match mime-view-file-name-regexp-2 subj)))
392 (substring subj (match-beginning 0)(match-end 0))
395 (replace-as-filename filename)
399 ;;; @ file extraction
402 (defun mime-save-content (entity situation)
403 (let ((name (or (mime-entity-safe-filename entity)
404 (format "%s" (mime-entity-media-type entity))))
405 (dir (if (eq t mime-save-directory)
407 mime-save-directory))
409 (setq filename (read-file-name
410 (concat "File name: (default "
411 (file-name-nondirectory name) ") ")
413 (concat (file-name-as-directory dir)
414 (file-name-nondirectory name))))
415 (if (file-directory-p filename)
416 (setq filename (concat (file-name-as-directory filename)
417 (file-name-nondirectory name))))
418 (if (file-exists-p filename)
419 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
421 (mime-write-entity-content entity (expand-file-name filename))
428 (defvar mime-magic-type-alist
429 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
430 ("^\211PNG" image png)
431 ("^GIF8[79]" image gif)
432 ("^II\\*\000" image tiff)
433 ("^MM\000\\*" image tiff)
435 ("^\000\000\001\263" video mpeg)
437 "*Alist of regexp about magic-number vs. corresponding media-types.
438 Each element looks like (REGEXP TYPE SUBTYPE).
439 REGEXP is a regular expression to match against the beginning of the
441 TYPE is symbol to indicate primary type of media-type.
442 SUBTYPE is symbol to indicate subtype of media-type.")
444 (defun mime-detect-content (entity situation)
446 (let ((mdata (mime-entity-content entity))
447 (rest mime-magic-type-alist))
448 (while (not (let ((cell (car rest)))
450 (if (string-match (car cell) mdata)
451 (setq type (nth 1 cell)
452 subtype (nth 2 cell))
455 (setq rest (cdr rest))))
456 (setq situation (del-alist 'method (copy-alist situation)))
457 (mime-play-entity entity
459 (put-alist 'type type
460 (put-alist 'subtype subtype
463 'mime-detect-content)))
466 ;;; @ mail/news message
469 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
470 "Quitting method for mime-view.
471 It is registered to variable `mime-preview-quitting-method-alist'."
472 (let ((raw-buffer (mime-entity-buffer
473 (get-text-property (point-min) 'mime-view-entity)))
474 (mother mime-mother-buffer)
475 (win-conf mime-preview-original-window-configuration))
476 (kill-buffer raw-buffer)
477 (mime-preview-kill-buffer)
478 (set-window-configuration win-conf)
479 (pop-to-buffer mother)
482 (defun mime-view-message/rfc822 (entity situation)
484 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
485 (mother (current-buffer))
486 (children (car (mime-entity-children entity)))
488 (mime-display-message
489 children new-name mother nil
490 (cdr (assq 'major-mode
491 (get-text-property (point) 'mime-view-situation))))))
492 (or (get-buffer-window preview-buffer)
493 (let ((m-win (get-buffer-window mother)))
495 (set-window-buffer m-win preview-buffer)
496 (switch-to-buffer preview-buffer)
500 ;;; @ message/partial
503 (defun mime-store-message/partial-piece (entity cal)
506 (concat "m-prts-" (user-login-name)) temporary-file-directory))
507 (id (cdr (assoc "id" cal)))
508 (number (cdr (assoc "number" cal)))
509 (total (cdr (assoc "total" cal)))
511 (mother (current-buffer)))
512 (or (file-exists-p root-dir)
513 (make-directory root-dir))
514 (setq id (replace-as-filename id))
515 (setq root-dir (concat root-dir "/" id))
516 (or (file-exists-p root-dir)
517 (make-directory root-dir))
518 (setq file (concat root-dir "/FULL"))
519 (if (file-exists-p file)
520 (let ((full-buf (get-buffer-create "FULL"))
521 (pwin (or (get-buffer-window mother)
522 (get-largest-window)))
524 (save-window-excursion
525 (set-buffer full-buf)
527 (as-binary-input-file (insert-file-contents file))
528 (setq major-mode 'mime-show-message-mode)
529 (mime-view-buffer (current-buffer) nil mother)
530 (setq pbuf (current-buffer))
532 (set-window-buffer pwin pbuf)
535 (setq file (concat root-dir "/" number))
536 (mime-write-entity-body entity file)
537 (let ((total-file (concat root-dir "/CT")))
541 (or (file-exists-p total-file)
544 (get-buffer-create mime-temp-buffer-name))
547 (write-region (point-min)(point-max) total-file)
548 (kill-buffer (current-buffer))
550 (string-to-number total)
552 (and (file-exists-p total-file)
554 (set-buffer (find-file-noselect total-file))
556 (and (re-search-forward "[0-9]+" nil t)
558 (buffer-substring (match-beginning 0)
561 (kill-buffer (current-buffer))
564 (if (and total (> total 0)
565 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
569 (set-buffer (get-buffer-create mime-temp-buffer-name))
570 (let ((full-buf (current-buffer)))
574 (setq file (concat root-dir "/" (int-to-string i)))
575 (or (file-exists-p file)
578 (as-binary-input-file (insert-file-contents file))
579 (goto-char (point-max))
582 (as-binary-output-file
583 (write-region (point-min)(point-max)
584 (expand-file-name "FULL" root-dir)))
587 (let ((file (format "%s/%d" root-dir i)))
588 (and (file-exists-p file)
593 (let ((file (expand-file-name "CT" root-dir)))
594 (and (file-exists-p file)
597 (let ((pwin (or (get-buffer-window mother)
598 (get-largest-window)))
599 (pbuf (mime-display-message
600 (mime-open-entity 'buffer (current-buffer))
601 nil mother nil 'mime-show-message-mode)))
602 (set-window-buffer pwin pbuf)
608 ;;; @ message/external-body
611 (defvar mime-raw-dired-function
612 (if (and (>= emacs-major-version 19) window-system)
613 (function dired-other-frame)
614 (function mime-raw-dired-function-for-one-frame)
617 (defun mime-raw-dired-function-for-one-frame (dir)
618 (let ((win (or (get-buffer-window mime-preview-buffer)
619 (get-largest-window))))
624 (defun mime-view-message/external-anon-ftp (entity cal)
625 (let* ((site (cdr (assoc "site" cal)))
626 (directory (cdr (assoc "directory" cal)))
627 (name (cdr (assoc "name" cal)))
628 (pathname (concat "/anonymous@" site ":" directory)))
629 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
630 (funcall mime-raw-dired-function pathname)
631 (goto-char (point-min))
632 (search-forward name)
635 (defvar mime-raw-browse-url-function mime-browse-url-function)
637 (defun mime-view-message/external-url (entity cal)
638 (let ((url (cdr (assoc "url" cal))))
639 (message (concat "Accessing " url " ..."))
640 (funcall mime-raw-browse-url-function url)))
646 (defun mime-view-caesar (entity situation)
647 "Internal method for mime-view to display ROT13-47-48 message."
648 (let ((buf (get-buffer-create
649 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
650 (with-current-buffer buf
651 (setq buffer-read-only nil)
653 (mime-insert-text-content entity)
654 (mule-caesar-region (point-min) (point-max))
655 (set-buffer-modified-p nil)
657 (let ((win (get-buffer-window (current-buffer))))
658 (or (eq (selected-window) win)
659 (select-window (or win (get-largest-window)))
662 (goto-char (point-min))
671 (let* ((file mime-acting-situation-examples-file)
672 (buffer (get-buffer-create " *mime-example*")))
673 (if (file-readable-p file)
678 (insert-file-contents file)
683 (while (and (> (length mime-acting-situation-example-list)
684 mime-acting-situation-example-list-max-size)
686 (mime-reduce-acting-situation-examples)
689 (error (setq mime-acting-situation-example-list nil)))
691 (kill-buffer buffer))))
693 ;;; mime-play.el ends here