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* ((rest mime-acting-situation-example-list)
68 (min-example (car rest))
69 (min-score (cdr min-example)))
71 (let* ((example (car rest))
72 (score (cdr example)))
73 (cond ((< score min-score)
78 (if (<= (length (car example))(length (car min-example)))
79 (setq min-example example)
82 (setq rest (cdr rest)))
83 (setq mime-acting-situation-example-list
84 (delq min-example mime-acting-situation-example-list))
85 (setq min-example (car min-example))
86 (let ((examples mime-acting-situation-example-list)
90 (let* ((ret (mime-compare-situation-with-example min-example
92 (ret-score (car ret)))
93 (cond ((> ret-score max-score)
94 (setq max-score ret-score
95 max-examples (list (cdr ret)))
97 ((= ret-score max-score)
98 (setq max-examples (cons (cdr ret) max-examples))
100 (setq examples (cdr examples)))
102 (let* ((example (car max-examples))
103 (cell (assoc example mime-acting-situation-example-list)))
105 (setcdr cell (1+ (cdr cell)))
106 (setq mime-acting-situation-example-list
107 (cons (cons example 0)
108 mime-acting-situation-example-list))
110 (setq max-examples (cdr max-examples))
114 ;;; @ content decoder
117 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
118 "Play current entity.
119 It decodes current entity to call internal or external method. The
120 method is selected from variable `mime-acting-condition'.
121 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
122 `mime-acting-situation-example-list'.
123 If MODE is specified, play as it. Default MODE is \"play\"."
125 (let ((entity (get-text-property (point) 'mime-view-entity)))
127 (mime-play-entity entity nil (or mode "play") ignore-examples)
130 (defun mime-sort-situation (situation)
140 (disposition-type . 6)
144 (let ((ret (assq a-t order)))
146 (setq a-order (cdr ret))
152 (let ((ret (assq b-t order)))
154 (setq b-order (cdr ret))
159 (if (= a-order b-order)
160 (string< (format "%s" a-t)(format "%s" b-t))
165 (defsubst mime-delq-null-situation (situations field
166 &optional ignored-value)
169 (let* ((situation (car situations))
170 (cell (assq field situation)))
172 (or (eq (cdr cell) ignored-value)
173 (setq dest (cons situation dest))
175 (setq situations (cdr situations)))
178 (defun mime-compare-situation-with-example (situation example)
179 (let ((example (copy-alist example))
182 (let* ((cell (car situation))
184 (ecell (assoc key example)))
186 (if (equal cell ecell)
187 (setq match (1+ match))
188 (setq example (delq ecell example))
191 (setq situation (cdr situation))
196 (defun mime-play-entity (entity &optional situation mode
197 ignore-examples ignored-method)
198 "Play entity specified by ENTITY.
199 It decodes the entity to call internal or external method. The method
200 is selected from variable `mime-acting-condition'. If MODE is
201 specified, play as it. Default MODE is \"play\"."
204 (setq situation (mime-entity-situation entity)))
206 (setq situation (cons (cons 'mode mode) situation))
209 (or (assq 'ignore-examples situation)
211 (cons (cons 'ignore-examples ignore-examples) situation)))
214 (mime-delq-null-situation
215 (ctree-find-calist mime-acting-condition situation
216 mime-view-find-every-acting-situation)
217 'method ignored-method))
226 (let ((situation (car rest))
227 (examples mime-acting-situation-example-list))
230 (mime-compare-situation-with-example
231 situation (caar examples)))
232 (ret-score (car ret)))
233 (cond ((> ret-score max-score)
234 (setq max-score ret-score
235 max-escore (cdar examples)
236 max-examples (list (cdr ret))
237 max-situations (list situation))
239 ((= ret-score max-score)
240 (cond ((> (cdar examples) max-escore)
241 (setq max-escore (cdar examples)
242 max-examples (list (cdr ret))
243 max-situations (list situation))
245 ((= (cdar examples) max-escore)
247 (cons (cdr ret) max-examples))
248 (or (member situation max-situations)
250 (cons situation max-situations)))
252 (setq examples (cdr examples))))
253 (setq rest (cdr rest)))
255 (setq ret max-situations)
257 (let* ((example (car max-examples))
259 (assoc example mime-acting-situation-example-list)))
261 (setcdr cell (1+ (cdr cell)))
262 (setq mime-acting-situation-example-list
263 (cons (cons example 0)
264 mime-acting-situation-example-list))
266 (setq max-examples (cdr max-examples))
269 (setq ret (select-menu-alist
275 (cdr (assq 'method situation)))
278 (setq ret (mime-sort-situation ret))
279 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
284 (setq method (cdr (assq 'method ret)))
285 (cond ((and (symbolp method)
287 (funcall method entity ret)
290 (mime-activate-mailcap-method entity ret)
292 ;; ((and (listp method)(stringp (car method)))
293 ;; (mime-activate-external-method entity ret)
296 (mime-show-echo-buffer "No method are specified for %s\n"
297 (mime-entity-type/subtype entity))
302 ;;; @ external decoder
305 (defvar mime-mailcap-method-filename-alist nil)
307 (defun mime-activate-mailcap-method (entity situation)
308 (let ((method (cdr (assoc 'method situation)))
309 (name (mime-entity-safe-filename entity)))
311 (if (and name (not (string= name "")))
312 (expand-file-name name temporary-file-directory)
314 (expand-file-name "EMI" temporary-file-directory))
316 (mime-write-entity-content entity name)
317 (message "External method is starting...")
320 (mailcap-format-command
322 (cons (cons 'filename name) situation))))
323 (start-process command mime-echo-buffer-name
324 shell-file-name shell-command-switch command)
326 (set-alist 'mime-mailcap-method-filename-alist process name)
327 (set-process-sentinel process 'mime-mailcap-method-sentinel)
331 (defun mime-mailcap-method-sentinel (process event)
332 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
333 (if (file-exists-p file)
336 (remove-alist 'mime-mailcap-method-filename-alist process)
337 (message (format "%s %s" process event)))
339 (defvar mime-echo-window-is-shared-with-bbdb
340 (module-installed-p 'bbdb)
341 "*If non-nil, mime-echo window is shared with BBDB window.")
343 (defvar mime-echo-window-height
346 (/ (window-height) 5)
348 "*Size of mime-echo window.
349 It allows function or integer. If it is function,
350 `mime-show-echo-buffer' calls it to get height of mime-echo window.
351 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
354 (defun mime-show-echo-buffer (&rest forms)
355 "Show mime-echo buffer to display MIME-playing information."
356 (get-buffer-create mime-echo-buffer-name)
357 (let ((the-win (selected-window))
358 (win (get-buffer-window mime-echo-buffer-name)))
360 (unless (and mime-echo-window-is-shared-with-bbdb
362 (setq win (get-buffer-window bbdb-buffer-name))
364 (select-window (get-buffer-window (or mime-preview-buffer
366 (setq win (split-window-vertically
368 (if (functionp mime-echo-window-height)
369 (funcall mime-echo-window-height)
370 mime-echo-window-height)
373 (set-window-buffer win mime-echo-buffer-name)
376 (goto-char (point-max))
378 (insert (apply (function format) forms))
380 (select-window the-win)
387 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
389 (defvar mime-view-file-name-regexp-1
390 (concat mime-view-file-name-char-regexp "+\\."
391 mime-view-file-name-char-regexp "+"))
393 (defvar mime-view-file-name-regexp-2
394 (concat (regexp-* mime-view-file-name-char-regexp)
395 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
397 (defun mime-entity-safe-filename (entity)
399 (or (mime-entity-filename entity)
401 (or (mime-read-field 'Content-Description entity)
402 (mime-read-field 'Subject entity))))
404 (or (string-match mime-view-file-name-regexp-1 subj)
405 (string-match mime-view-file-name-regexp-2 subj)))
406 (substring subj (match-beginning 0)(match-end 0))
409 (replace-as-filename filename)
413 ;;; @ file extraction
416 (defun mime-save-content (entity situation)
417 (let* ((name (mime-entity-safe-filename entity))
418 (filename (if (and name (not (string-equal name "")))
419 (expand-file-name name
420 (save-window-excursion
424 (interactive "DDirectory: ")
426 (save-window-excursion
430 (interactive "FFilename: ")
431 (expand-file-name file)))))))
433 (if (file-exists-p filename)
434 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
436 (mime-write-entity-content entity filename)
443 (defvar mime-magic-type-alist
444 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
445 ("^\211PNG" image png)
446 ("^GIF8[79]" image gif)
447 ("^II\\*\000" image tiff)
448 ("^MM\000\\*" image tiff)
450 ("^\000\000\001\263" video mpeg)
452 "*Alist of regexp about magic-number vs. corresponding media-types.
453 Each element looks like (REGEXP TYPE SUBTYPE).
454 REGEXP is a regular expression to match against the beginning of the
456 TYPE is symbol to indicate primary type of media-type.
457 SUBTYPE is symbol to indicate subtype of media-type.")
459 (defun mime-detect-content (entity situation)
461 (let ((mdata (mime-entity-content entity))
462 (rest mime-magic-type-alist))
463 (while (not (let ((cell (car rest)))
465 (if (string-match (car cell) mdata)
466 (setq type (nth 1 cell)
467 subtype (nth 2 cell))
470 (setq rest (cdr rest))))
474 (put-alist 'type type
475 (put-alist 'subtype subtype
477 (copy-alist situation))))
479 (cdr (assq 'ignore-examples situation))
480 'mime-detect-content)
485 ;;; @ mail/news message
488 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
489 "Quitting method for mime-view.
490 It is registered to variable `mime-preview-quitting-method-alist'."
491 (let ((mother mime-mother-buffer)
492 (win-conf mime-preview-original-window-configuration)
494 (kill-buffer mime-raw-buffer)
495 (mime-preview-kill-buffer)
496 (set-window-configuration win-conf)
497 (pop-to-buffer mother)
500 (defun mime-view-message/rfc822 (entity situation)
502 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
503 (mother mime-preview-buffer)
504 (children (car (mime-entity-children entity))))
505 (set-buffer (get-buffer-create new-name))
507 (insert-buffer-substring (mime-entity-buffer children)
508 (mime-entity-point-min children)
509 (mime-entity-point-max children))
510 (setq mime-message-structure children)
511 (setq major-mode 'mime-show-message-mode)
512 (mime-view-buffer (current-buffer) nil mother
513 nil (if (mime-entity-cooked-p entity) 'cooked))
517 ;;; @ message/partial
520 (defun mime-store-message/partial-piece (entity cal)
521 (goto-char (mime-entity-point-min entity))
524 (concat "m-prts-" (user-login-name)) temporary-file-directory))
525 (id (cdr (assoc "id" cal)))
526 (number (cdr (assoc "number" cal)))
527 (total (cdr (assoc "total" cal)))
529 (mother mime-preview-buffer)
531 (or (file-exists-p root-dir)
532 (make-directory root-dir)
534 (setq id (replace-as-filename id))
535 (setq root-dir (concat root-dir "/" id))
536 (or (file-exists-p root-dir)
537 (make-directory root-dir)
539 (setq file (concat root-dir "/FULL"))
540 (if (file-exists-p file)
541 (let ((full-buf (get-buffer-create "FULL"))
542 (pwin (or (get-buffer-window mother)
543 (get-largest-window)))
545 (save-window-excursion
546 (set-buffer full-buf)
548 (as-binary-input-file (insert-file-contents file))
549 (setq major-mode 'mime-show-message-mode)
550 (mime-view-buffer (current-buffer) nil mother)
552 (set-window-buffer pwin
554 (set-buffer full-buf)
555 mime-preview-buffer))
558 (setq file (concat root-dir "/" number))
559 (mime-write-entity-body entity file)
560 (let ((total-file (concat root-dir "/CT")))
564 (or (file-exists-p total-file)
567 (get-buffer-create mime-temp-buffer-name))
570 (write-region (point-min)(point-max) total-file)
571 (kill-buffer (current-buffer))
573 (string-to-number total)
575 (and (file-exists-p total-file)
577 (set-buffer (find-file-noselect total-file))
579 (and (re-search-forward "[0-9]+" nil t)
581 (buffer-substring (match-beginning 0)
584 (kill-buffer (current-buffer))
587 (if (and total (> total 0))
590 (set-buffer (get-buffer-create mime-temp-buffer-name))
591 (let ((full-buf (current-buffer)))
595 (setq file (concat root-dir "/" (int-to-string i)))
596 (or (file-exists-p file)
599 (as-binary-input-file (insert-file-contents file))
600 (goto-char (point-max))
603 (as-binary-output-file
604 (write-region (point-min)(point-max)
605 (expand-file-name "FULL" root-dir)))
608 (let ((file (format "%s/%d" root-dir i)))
609 (and (file-exists-p file)
614 (let ((file (expand-file-name "CT" root-dir)))
615 (and (file-exists-p file)
618 (save-window-excursion
619 (setq major-mode 'mime-show-message-mode)
620 (mime-view-buffer (current-buffer) nil mother)
622 (let ((pwin (or (get-buffer-window mother)
623 (get-largest-window)))
624 (pbuf (save-excursion
625 (set-buffer full-buf)
626 mime-preview-buffer)))
627 (set-window-buffer pwin pbuf)
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)
642 (defun mime-raw-dired-function-for-one-frame (dir)
643 (let ((win (or (get-buffer-window mime-preview-buffer)
644 (get-largest-window))))
649 (defun mime-view-message/external-anon-ftp (entity cal)
650 (let* ((site (cdr (assoc "site" cal)))
651 (directory (cdr (assoc "directory" cal)))
652 (name (cdr (assoc "name" cal)))
653 (pathname (concat "/anonymous@" site ":" directory)))
654 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
655 (funcall mime-raw-dired-function pathname)
656 (goto-char (point-min))
657 (search-forward name)
660 (defvar mime-raw-browse-url-function mime-browse-url-function)
662 (defun mime-view-message/external-url (entity cal)
663 (let ((url (cdr (assoc "url" cal))))
664 (message (concat "Accessing " url " ..."))
665 (funcall mime-raw-browse-url-function url)))
671 (defun mime-view-caesar (entity situation)
672 "Internal method for mime-view to display ROT13-47-48 message."
673 (let* ((new-name (format "%s-%s" (buffer-name)
674 (mime-entity-number entity)))
675 (mother mime-preview-buffer))
676 (let ((pwin (or (get-buffer-window mother)
677 (get-largest-window)))
678 (buf (get-buffer-create new-name)))
679 (set-window-buffer pwin buf)
683 (setq buffer-read-only nil)
685 (mime-insert-text-content entity)
686 (mule-caesar-region (point-min) (point-max))
687 (set-buffer-modified-p nil)
689 (view-buffer new-name)
698 (let* ((file mime-acting-situation-examples-file)
699 (buffer (get-buffer-create " *mime-example*")))
700 (if (file-readable-p file)
705 (insert-file-contents file)
710 (while (and (> (length mime-acting-situation-example-list)
711 mime-acting-situation-example-list-max-size)
713 (mime-reduce-acting-situation-examples)
716 (error (setq mime-acting-situation-example-list nil)))
718 (kill-buffer buffer))))
720 ;;; mime-play.el ends here