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.
32 (require 'eword-decode)
37 (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)))
104 (cond ((> min-det-ret det-ret)
105 (setq min-det-ret det-ret
110 dest (cons (cdr ret) freq)))
111 ((= min-det-ret det-ret)
112 (cond ((> min-det-org det-org)
113 (setq min-det-org det-org
117 dest (cons (cdr ret) freq)))
118 ((= min-det-org det-org)
119 (cond ((> min-freq freq)
123 dest (cons (cdr ret) freq)))))))))))
132 (setq jr (nthcdr (1- d-j) mime-acting-situation-example-list))
133 (setcdr jr (cddr jr))
135 (setq mime-acting-situation-example-list
136 (cdr mime-acting-situation-example-list))
137 (setq ir (nthcdr (1- d-i) mime-acting-situation-example-list))
138 (setcdr ir (cddr ir)))
139 (if (setq ir (assoc (car dest) mime-acting-situation-example-list))
140 (setcdr ir (+ (cdr ir)(cdr dest)))
141 (setq mime-acting-situation-example-list
142 (cons dest mime-acting-situation-example-list)))))
145 ;;; @ content decoder
149 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
150 "Play current entity.
151 It decodes current entity to call internal or external method. The
152 method is selected from variable `mime-acting-condition'.
153 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
154 `mime-acting-situation-example-list'.
155 If MODE is specified, play as it. Default MODE is \"play\"."
157 (let ((entity (get-text-property (point) 'mime-view-entity)))
160 (get-text-property (point) 'mime-view-situation)))
164 (if (assq 'mode situation)
165 (put-alist 'mode mode (copy-alist situation))
166 (cons (cons 'mode mode)
170 (cons (cons 'ignore-examples ignore-examples)
172 (mime-play-entity entity situation)))))
174 (defun mime-sort-situation (situation)
184 (disposition-type . 6)))
187 (let ((ret (assq a-t order)))
189 (setq a-order (cdr ret))
193 (let ((ret (assq b-t order)))
195 (setq b-order (cdr ret))
198 (if (= a-order b-order)
199 (string< (format "%s" a-t)(format "%s" b-t))
200 (< a-order b-order))))))
202 (defsubst mime-delq-null-situation (situations field
203 &optional ignored-value)
206 (let* ((situation (car situations))
207 (cell (assq field situation)))
209 (or (eq (cdr cell) ignored-value)
210 (setq dest (cons situation dest)))))
211 (setq situations (cdr situations)))
214 (defun mime-compare-situation-with-example (situation example)
215 (let ((example (copy-alist example))
218 (let* ((cell (car situation))
220 (ecell (assoc key example)))
222 (if (equal cell ecell)
223 (setq match (1+ match))
224 (setq example (delq ecell example)))))
225 (setq situation (cdr situation)))
226 (cons match example)))
229 (defun mime-play-entity (entity &optional situation ignored-method)
230 "Play entity specified by ENTITY.
231 It decodes the entity to call internal or external method. The method
232 is selected from variable `mime-acting-condition'. If MODE is
233 specified, play as it. Default MODE is \"play\"."
235 (in-calist-package 'mime-view)
237 (mime-delq-null-situation
238 (ctree-find-calist mime-acting-condition
239 (mime-entity-situation entity situation)
240 mime-view-find-every-acting-situation)
241 'method ignored-method))
242 (or (assq 'ignore-examples situation)
250 (let ((situation (car rest))
251 (examples mime-acting-situation-example-list))
254 (mime-compare-situation-with-example
255 situation (caar examples)))
256 (ret-score (car ret)))
257 (cond ((> ret-score max-score)
258 (setq max-score ret-score
259 max-escore (cdar examples)
260 max-examples (list (cdr ret))
261 max-situations (list situation)))
262 ((= ret-score max-score)
263 (cond ((> (cdar examples) max-escore)
264 (setq max-escore (cdar examples)
265 max-examples (list (cdr ret))
266 max-situations (list situation)))
267 ((= (cdar examples) max-escore)
269 (cons (cdr ret) max-examples))
270 (or (member situation max-situations)
272 (cons situation max-situations))))))))
273 (setq examples (cdr examples))))
274 (setq rest (cdr rest)))
276 (setq ret max-situations)
278 (let* ((example (car max-examples))
280 (assoc example mime-acting-situation-example-list)))
282 (setcdr cell (1+ (cdr cell)))
283 (setq mime-acting-situation-example-list
284 (cons (cons example 0)
285 mime-acting-situation-example-list))))
286 (setq max-examples (cdr max-examples)))))))
288 (setq ret (select-menu-alist
294 (cdr (assq 'method situation)))
297 (setq ret (mime-sort-situation ret))
298 (add-to-list 'mime-acting-situation-example-list (cons ret 0)))
300 (setq ret (car ret))))
301 (setq method (cdr (assq 'method ret)))
302 (cond ((and (symbolp method)
304 (funcall method entity ret))
306 (mime-activate-mailcap-method entity ret))
307 ;; ((and (listp method)(stringp (car method)))
308 ;; (mime-activate-external-method entity ret)
311 (mime-show-echo-buffer "No method are specified for %s\n"
312 (mime-type/subtype-string
313 (cdr (assq 'type situation))
314 (cdr (assq 'subtype situation))))
315 (if (y-or-n-p "Do you want to save current entity to disk?")
316 (mime-save-content entity situation))))))
319 ;;; @ external decoder
322 (defvar mime-mailcap-method-filename-alist nil)
324 (defun mime-activate-mailcap-method (entity situation)
325 (let ((method (cdr (assoc 'method situation)))
326 (name (mime-entity-safe-filename entity)))
328 (if (and name (not (string= name "")))
329 (expand-file-name name temporary-file-directory)
331 (expand-file-name "EMI" temporary-file-directory))))
332 (mime-write-entity-content entity name)
333 (message "External method is starting...")
336 (mailcap-format-command
338 (cons (cons 'filename name) situation))))
339 (start-process command mime-echo-buffer-name
340 shell-file-name shell-command-switch command))))
341 (set-alist 'mime-mailcap-method-filename-alist process name)
342 (set-process-sentinel process 'mime-mailcap-method-sentinel))))
344 (defun mime-mailcap-method-sentinel (process event)
345 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
346 (if (file-exists-p file)
348 (remove-alist 'mime-mailcap-method-filename-alist process)
349 (message (format "%s %s" process event)))
351 (defvar mime-echo-window-is-shared-with-bbdb
352 (module-installed-p 'bbdb)
353 "*If non-nil, mime-echo window is shared with BBDB window.")
355 (defvar mime-echo-window-height
358 (/ (window-height) 5)))
359 "*Size of mime-echo window.
360 It allows function or integer. If it is function,
361 `mime-show-echo-buffer' calls it to get height of mime-echo window.
362 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
365 (defun mime-show-echo-buffer (&rest forms)
366 "Show mime-echo buffer to display MIME-playing information."
367 (get-buffer-create mime-echo-buffer-name)
368 (let ((the-win (selected-window))
369 (win (get-buffer-window mime-echo-buffer-name)))
371 (unless (and mime-echo-window-is-shared-with-bbdb
373 (setq win (get-buffer-window bbdb-buffer-name))
375 (select-window (get-buffer-window (or mime-preview-buffer
377 (setq win (split-window-vertically
379 (if (functionp mime-echo-window-height)
380 (funcall mime-echo-window-height)
381 mime-echo-window-height)))))
382 (set-window-buffer win mime-echo-buffer-name))
384 (goto-char (point-max))
386 (let ((buffer-read-only nil))
387 (insert (apply (function format) forms))))
388 (select-window the-win)))
394 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
396 (defvar mime-view-file-name-regexp-1
397 (concat mime-view-file-name-char-regexp "+\\."
398 mime-view-file-name-char-regexp "+"))
400 (defvar mime-view-file-name-regexp-2
401 (concat (regexp-* mime-view-file-name-char-regexp)
402 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
404 (defun mime-entity-safe-filename (entity)
406 (or (mime-entity-filename entity)
408 (or (mime-entity-read-field entity 'Content-Description)
409 (mime-entity-read-field entity 'Subject))))
411 (or (string-match mime-view-file-name-regexp-1 subj)
412 (string-match mime-view-file-name-regexp-2 subj)))
413 (substring subj (match-beginning 0)(match-end 0)))))))
415 (replace-as-filename filename))))
418 ;;; @ file extraction
421 (defun mime-save-content (entity situation)
422 (let ((name (or (mime-entity-safe-filename entity)
423 (format "%s" (mime-entity-media-type entity))))
424 (dir (if (eq t mime-save-directory)
426 mime-save-directory))
428 (setq filename (read-file-name
429 (concat "File name: (default "
430 (file-name-nondirectory name) ") ")
432 (concat (file-name-as-directory dir)
433 (file-name-nondirectory name))))
434 (if (file-directory-p filename)
435 (setq filename (concat (file-name-as-directory filename)
436 (file-name-nondirectory name))))
437 (if (file-exists-p filename)
438 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
440 (mime-write-entity-content entity (expand-file-name filename))))
442 (defun mime-save-content-for-broken-message (entity situation)
443 (let ((name (or (and (mime-entity-filename entity)
445 (mime-entity-filename entity)))
446 (format "%s" (mime-entity-media-type entity))))
447 (dir (if (eq t mime-save-directory)
449 mime-save-directory))
451 (setq filename (read-file-name
452 (concat "File name: (default "
453 (file-name-nondirectory name) ") ")
455 (concat (file-name-as-directory dir)
456 (file-name-nondirectory name))))
457 (if (file-directory-p filename)
458 (setq filename (concat (file-name-as-directory filename)
459 (file-name-nondirectory name))))
460 (if (file-exists-p filename)
461 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
463 (mime-write-entity-content entity (expand-file-name filename))))
468 (defvar mime-magic-type-alist
469 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
470 ("^\211PNG" image png)
471 ("^GIF8[79]" image gif)
472 ("^II\\*\000" image tiff)
473 ("^MM\000\\*" image tiff)
475 ("^\000\000\001\263" video mpeg))
476 "*Alist of regexp about magic-number vs. corresponding media-types.
477 Each element looks like (REGEXP TYPE SUBTYPE).
478 REGEXP is a regular expression to match against the beginning of the
480 TYPE is symbol to indicate primary type of media-type.
481 SUBTYPE is symbol to indicate subtype of media-type.")
483 (defun mime-detect-content (entity situation)
485 (let ((mdata (mime-entity-content entity))
486 (rest mime-magic-type-alist))
487 (while (not (let ((cell (car rest)))
489 (if (string-match (car cell) mdata)
490 (setq type (nth 1 cell)
491 subtype (nth 2 cell)))
493 (setq rest (cdr rest))))
494 (setq situation (del-alist 'method (copy-alist situation)))
495 (mime-play-entity entity
497 (put-alist 'type type
498 (put-alist 'subtype subtype
501 'mime-detect-content)))
504 ;;; @ mail/news message
507 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
508 "Quitting method for mime-view.
509 It is registered to variable `mime-preview-quitting-method-alist'."
510 (let ((raw-buffer (mime-entity-body-buffer
511 (get-text-property (point-min) 'mime-view-entity)))
512 (mother mime-mother-buffer)
513 (win-conf mime-preview-original-window-configuration))
514 ;; Existence of raw-buffer is not guaranteed.
516 (kill-buffer raw-buffer))
517 (mime-preview-kill-buffer)
518 (set-window-configuration win-conf)
519 (pop-to-buffer mother)))
521 (defun mime-view-message/rfc822 (entity situation)
523 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
524 (mother (current-buffer))
525 (children (car (mime-entity-children entity)))
527 (mime-display-message
528 children new-name mother nil
529 (cdr (assq 'major-mode
530 (get-text-property (point) 'mime-view-situation))))))
531 (or (get-buffer-window preview-buffer)
532 (let ((m-win (get-buffer-window mother)))
534 (set-window-buffer m-win preview-buffer)
535 (switch-to-buffer preview-buffer))))))
538 ;;; @ message/partial
541 (defun mime-store-message/partial-piece (entity cal)
544 (concat "m-prts-" (user-login-name)) temporary-file-directory))
545 (id (cdr (assoc "id" cal)))
546 (number (cdr (assoc "number" cal)))
547 (total (cdr (assoc "total" cal)))
549 (mother (current-buffer)))
550 (or (file-exists-p root-dir)
551 (make-directory root-dir))
552 (setq id (replace-as-filename id))
553 (setq root-dir (concat root-dir "/" id))
554 (or (file-exists-p root-dir)
555 (make-directory root-dir))
556 (setq file (concat root-dir "/FULL"))
557 (if (file-exists-p file)
558 (let ((full-buf (get-buffer-create "FULL"))
559 (pwin (or (get-buffer-window mother)
560 (get-largest-window)))
562 (save-window-excursion
563 (set-buffer full-buf)
565 (as-binary-input-file (insert-file-contents file))
566 (setq major-mode 'mime-show-message-mode)
567 (mime-view-buffer (current-buffer) nil mother)
568 (setq pbuf (current-buffer)))
569 (set-window-buffer pwin pbuf)
570 (select-window pwin))
571 (setq file (concat root-dir "/" number))
572 (mime-write-entity-body entity file)
573 (let ((total-file (concat root-dir "/CT")))
577 (or (file-exists-p total-file)
580 (get-buffer-create mime-temp-buffer-name))
583 (write-region (point-min)(point-max) total-file)
584 (kill-buffer (current-buffer))))
585 (string-to-number total))
586 (and (file-exists-p total-file)
588 (set-buffer (find-file-noselect total-file))
590 (and (re-search-forward "[0-9]+" nil t)
592 (buffer-substring (match-beginning 0)
594 (kill-buffer (current-buffer))))))))
595 (if (and total (> total 0)
596 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
600 (set-buffer (get-buffer-create mime-temp-buffer-name))
601 ;; #### ??? full-buf is not referenced.
602 ;; (let ((full-buf (current-buffer)))
606 (setq file (concat root-dir "/" (int-to-string i)))
607 (or (file-exists-p file)
609 (as-binary-input-file (insert-file-contents file))
610 (goto-char (point-max))
612 (as-binary-output-file
613 (write-region (point-min)(point-max)
614 (expand-file-name "FULL" root-dir)))
617 (let ((file (format "%s/%d" root-dir i)))
618 (and (file-exists-p file)
621 (let ((file (expand-file-name "CT" root-dir)))
622 (and (file-exists-p file)
624 (let ((pwin (or (get-buffer-window mother)
625 (get-largest-window)))
626 (pbuf (mime-display-message
627 (mime-open-entity 'buffer (current-buffer))
628 nil mother nil 'mime-show-message-mode)))
629 (set-window-buffer pwin pbuf)
630 (select-window pwin))))))))
633 ;;; @ message/external-body
636 (defvar mime-raw-dired-function
637 (if (and (>= emacs-major-version 19) window-system)
638 (function dired-other-frame)
639 (function mime-raw-dired-function-for-one-frame)))
641 (defun mime-raw-dired-function-for-one-frame (dir)
642 (let ((win (or (get-buffer-window mime-preview-buffer)
643 (get-largest-window))))
647 (defun mime-view-message/external-anon-ftp (entity cal)
648 (let* ((site (cdr (assoc "site" cal)))
649 (directory (cdr (assoc "directory" cal)))
650 (name (cdr (assoc "name" cal)))
651 (pathname (concat "/anonymous@" site ":" directory)))
652 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
653 (funcall mime-raw-dired-function pathname)
654 (goto-char (point-min))
655 (search-forward name)))
657 (defvar mime-raw-browse-url-function mime-browse-url-function)
659 (defun mime-view-message/external-url (entity cal)
660 (let ((url (cdr (assoc "url" cal))))
661 (message (concat "Accessing " url " ..."))
662 (funcall mime-raw-browse-url-function url)))
668 (defun mime-view-caesar (entity situation)
669 "Internal method for mime-view to display ROT13-47-48 message."
670 (let ((buf (get-buffer-create
671 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
672 (with-current-buffer buf
673 (setq buffer-read-only nil)
675 (mime-insert-text-content entity)
676 (mule-caesar-region (point-min) (point-max))
677 (set-buffer-modified-p nil))
678 (let ((win (get-buffer-window (current-buffer))))
679 (or (eq (selected-window) win)
680 (select-window (or win (get-largest-window)))))
682 (goto-char (point-min))))
690 (let* ((file mime-acting-situation-examples-file)
691 (buffer (get-buffer-create " *mime-example*")))
692 (if (file-readable-p file)
697 (insert-file-contents file)
702 (while (and (> (length mime-acting-situation-example-list)
703 mime-acting-situation-example-list-max-size)
705 (mime-reduce-acting-situation-examples)
707 (error (setq mime-acting-situation-example-list nil))))
708 (kill-buffer buffer))))
710 ;;; mime-play.el ends here