1 ;;; mime-play.el --- Playback processing module for mime-view.el
3 ;; Copyright (C) 1994,1995,1996,1997,1998 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.
37 (error (defvar bbdb-buffer-name nil))
40 (defvar mime-acting-situation-examples nil)
42 (defun mime-save-acting-situation-examples ()
43 (let* ((file mime-acting-situation-examples-file)
44 (buffer (get-buffer-create " *mime-example*")))
48 (setq buffer-file-name file)
50 (insert ";;; " (file-name-nondirectory file) "\n")
51 (insert "\n;; This file is generated automatically by "
52 mime-view-version-string "\n\n")
53 (insert ";;; Code:\n\n")
54 (pp `(setq mime-acting-situation-examples
55 ',mime-acting-situation-examples)
58 (file-name-nondirectory file)
61 (kill-buffer buffer))))
63 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
69 (defvar mime-preview-after-decoded-position nil)
71 (defun mime-preview-play-current-entity (&optional mode)
73 It decodes current entity to call internal or external method. The
74 method is selected from variable `mime-acting-condition'.
75 If MODE is specified, play as it. Default MODE is \"play\"."
79 (let ((entity (get-text-property (point) 'mime-view-entity)))
81 (let ((the-buf (current-buffer))
82 (raw-buffer (mime-entity-buffer entity)))
83 (setq mime-preview-after-decoded-position (point))
84 (set-buffer raw-buffer)
85 (mime-raw-play-entity entity mode)
86 (when (eq (current-buffer) raw-buffer)
88 (goto-char mime-preview-after-decoded-position)
91 (defun mime-sort-situation (situation)
101 (disposition-type . 6)
105 (let ((ret (assq a-t order)))
107 (setq a-order (cdr ret))
113 (let ((ret (assq b-t order)))
115 (setq b-order (cdr ret))
120 (if (= a-order b-order)
121 (string< (format "%s" a-t)(format "%s" b-t))
126 (defsubst mime-delq-null-situation (situations field)
129 (let ((situation (car situations)))
130 (if (assq field situation)
131 (setq dest (cons situation dest))
133 (setq situations (cdr situations)))
136 (defun mime-raw-play-entity (entity &optional mode situation)
137 "Play entity specified by ENTITY.
138 It decodes the entity to call internal or external method. The method
139 is selected from variable `mime-acting-condition'. If MODE is
140 specified, play as it. Default MODE is \"play\"."
143 (setq situation (mime-entity-situation entity)))
145 (setq situation (cons (cons 'mode mode) situation))
148 (or (ctree-match-calist mime-acting-situation-examples situation)
149 (ctree-match-calist-partially mime-acting-situation-examples
153 (or (mime-delq-null-situation
154 (ctree-find-calist mime-acting-condition ret
155 mime-view-find-every-acting-situation)
157 (mime-delq-null-situation
158 (ctree-find-calist mime-acting-condition situation
159 mime-view-find-every-acting-situation)
163 (setq ret (select-menu-alist
169 (cdr (assq 'method situation)))
172 (setq ret (mime-sort-situation ret))
173 (ctree-set-calist-strictly 'mime-acting-situation-examples ret)
178 (setq method (cdr (assq 'method ret)))
179 (cond ((and (symbolp method)
181 (funcall method entity ret)
184 (mime-activate-mailcap-method entity ret)
186 ;; ((and (listp method)(stringp (car method)))
187 ;; (mime-activate-external-method entity ret)
190 (mime-show-echo-buffer "No method are specified for %s\n"
191 (mime-entity-type/subtype entity))
196 ;;; @ external decoder
199 (defvar mime-mailcap-method-filename-alist nil)
201 (defun mime-activate-mailcap-method (entity situation)
204 (let ((start (mime-entity-point-min entity))
205 (end (mime-entity-point-max entity)))
206 (narrow-to-region start end)
208 (let ((method (cdr (assoc 'method situation)))
209 (name (expand-file-name (mime-raw-get-filename situation)
210 mime-temp-directory)))
211 (mime-write-decoded-region (mime-entity-body-start entity) end
212 name (cdr (assq 'encoding situation)))
213 (message "External method is starting...")
216 (mailcap-format-command
218 (cons (cons 'filename name) situation))))
219 (start-process command mime-echo-buffer-name
220 shell-file-name shell-command-switch command)
222 (set-alist 'mime-mailcap-method-filename-alist process name)
223 (set-process-sentinel process 'mime-mailcap-method-sentinel)
227 (defun mime-mailcap-method-sentinel (process event)
228 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
229 (if (file-exists-p file)
232 (remove-alist 'mime-mailcap-method-filename-alist process)
233 (message (format "%s %s" process event)))
235 ;; (defun mime-activate-external-method (entity cal)
238 ;; (let ((beg (mime-entity-point-min entity))
239 ;; (end (mime-entity-point-max entity)))
240 ;; (narrow-to-region beg end)
242 ;; (let ((method (cdr (assoc 'method cal)))
243 ;; (name (mime-raw-get-filename cal)))
245 ;; (let ((file (make-temp-name
246 ;; (expand-file-name "TM" mime-temp-directory)))
248 ;; (if (nth 1 method)
250 ;; (setq b (mime-entity-body-start entity)))
252 ;; (write-region b end file)
253 ;; (message "External method is starting...")
254 ;; (setq cal (put-alist
255 ;; 'name (replace-as-filename name) cal))
256 ;; (setq cal (put-alist 'file file cal))
258 ;; (list (car method)
259 ;; mime-echo-buffer-name (car method))
260 ;; (mime-make-external-method-args
261 ;; cal (cdr (cdr method)))
263 ;; (apply (function start-process) args)
264 ;; (mime-show-echo-buffer)
268 ;; (defun mime-make-external-method-args (cal format)
273 ;; (let* ((item (eval arg))
274 ;; (ret (cdr (assoc item cal))))
276 ;; (if (eq item 'encoding)
282 (defvar mime-echo-window-is-shared-with-bbdb t
283 "*If non-nil, mime-echo window is shared with BBDB window.")
285 (defvar mime-echo-window-height
288 (/ (window-height) 5)
290 "*Size of mime-echo window.
291 It allows function or integer. If it is function,
292 `mime-show-echo-buffer' calls it to get height of mime-echo window.
293 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
296 (defun mime-show-echo-buffer (&rest forms)
297 "Show mime-echo buffer to display MIME-playing information."
298 (get-buffer-create mime-echo-buffer-name)
299 (let ((the-win (selected-window))
300 (win (get-buffer-window mime-echo-buffer-name))
303 (if (and mime-echo-window-is-shared-with-bbdb
304 (boundp 'bbdb-buffer-name)
305 (setq win (get-buffer-window bbdb-buffer-name))
307 (set-window-buffer win mime-echo-buffer-name)
308 (select-window (get-buffer-window mime-preview-buffer))
309 (setq win (split-window-vertically
311 (if (functionp mime-echo-window-height)
312 (funcall mime-echo-window-height)
313 mime-echo-window-height)
315 (set-window-buffer win mime-echo-buffer-name)
318 (goto-char (point-max))
320 (insert (apply (function format) forms))
322 (select-window the-win)
329 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
331 (defvar mime-view-file-name-regexp-1
332 (concat mime-view-file-name-char-regexp "+\\."
333 mime-view-file-name-char-regexp "+"))
335 (defvar mime-view-file-name-regexp-2
336 (concat (regexp-* mime-view-file-name-char-regexp)
337 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
339 (defun mime-raw-get-original-filename (param)
340 (or (if (member (cdr (assq 'encoding param))
341 mime-view-uuencode-encoding-name-list)
342 (mime-raw-get-uu-filename))
344 (or (if (or (and (setq ret (mime-read-Content-Disposition))
348 (mime-content-disposition-parameters ret)))
350 (setq ret (assoc "name" param))
351 (setq ret (assoc "x-name" param))
353 (std11-strip-quoted-string (cdr ret))
356 (std11-find-field-body '("Content-Description"
358 (if (or (string-match mime-view-file-name-regexp-1 ret)
359 (string-match mime-view-file-name-regexp-2 ret))
360 (substring ret (match-beginning 0)(match-end 0))
365 (defun mime-raw-get-filename (param)
366 (replace-as-filename (mime-raw-get-original-filename param))
370 ;;; @ file extraction
373 (defun mime-method-to-save (entity cal)
374 (let ((beg (mime-entity-point-min entity))
375 (end (mime-entity-point-max entity)))
377 (let* ((name (save-restriction
378 (narrow-to-region beg end)
379 (mime-raw-get-filename cal)
381 (encoding (or (cdr (assq 'encoding cal)) "7bit"))
382 (filename (if (and name (not (string-equal name "")))
383 (expand-file-name name
384 (save-window-excursion
388 (interactive "DDirectory: ")
390 (save-window-excursion
394 (interactive "FFilename: ")
395 (expand-file-name file)))))))
397 (if (file-exists-p filename)
398 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
400 (re-search-forward "\n\n")
401 (mime-write-decoded-region (match-end 0) end filename encoding)
408 (defvar mime-file-type-regexp-type-subtype-alist
409 '(("JPEG" image jpeg)))
411 (defun mime-method-to-detect (entity situation)
412 (let ((beg (mime-entity-point-min entity))
413 (end (mime-entity-point-max entity)))
415 (let* ((name (save-restriction
416 (narrow-to-region beg end)
417 (mime-raw-get-filename situation)
419 (encoding (or (cdr (assq 'encoding situation)) "7bit"))
420 (filename (if (and name (not (string-equal name "")))
421 (expand-file-name name mime-temp-directory)
423 (expand-file-name "EMI" mime-temp-directory)))))
424 (mime-write-decoded-region (mime-entity-body-start entity) end
428 (call-process "file" nil t nil filename)
429 (goto-char (point-min))
430 (if (search-forward (concat filename ": ") nil t)
431 (let ((rest mime-file-type-regexp-type-subtype-alist))
432 (while (not (let ((cell (car rest)))
433 (if (looking-at (car cell))
434 (setq type (nth 1 cell)
435 subtype (nth 2 cell))
437 (setq rest (cdr rest))))))
439 (mime-raw-play-entity
441 (put-alist 'type type
442 (put-alist 'subtype subtype
443 (mime-entity-situation entity))))
448 ;;; @ mail/news message
451 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
452 "Quitting method for mime-view.
453 It is registered to variable `mime-preview-quitting-method-alist'."
454 (let ((mother mime-mother-buffer)
455 (win-conf mime-preview-original-window-configuration)
457 (kill-buffer mime-raw-buffer)
458 (mime-preview-kill-buffer)
459 (set-window-configuration win-conf)
460 (pop-to-buffer mother)
463 (defun mime-method-to-display-message/rfc822 (entity cal)
464 (let* ((beg (mime-entity-point-min entity))
465 (end (mime-entity-point-max entity))
466 (cnum (mime-raw-point-to-entity-number beg))
467 (new-name (format "%s-%s" (buffer-name) cnum))
468 (mother mime-preview-buffer)
470 (cdr (or (assq major-mode mime-raw-representation-type-alist)
471 (assq t mime-raw-representation-type-alist))))
473 (setq str (buffer-substring beg end))
474 (switch-to-buffer new-name)
477 (goto-char (point-min))
478 (if (re-search-forward "^\n" nil t)
479 (delete-region (point-min) (match-end 0))
481 (setq major-mode 'mime-show-message-mode)
482 (setq mime-raw-representation-type representation-type)
483 (mime-view-mode mother)
487 ;;; @ message/partial
490 (defun mime-raw-write-region (start end filename)
491 "Write current region into specified file.
492 When called from a program, takes three arguments:
493 START, END and FILENAME. START and END are buffer positions.
494 It refer `mime-raw-representation-type' or `major-mode
495 mime-raw-representation-type-alist'. If it is `binary', region is
496 saved as binary. Otherwise the region is saved by `write-region'."
497 (let ((presentation-type
498 (or mime-raw-representation-type
499 (cdr (or (assq major-mode mime-raw-representation-type-alist)
500 (assq t mime-raw-representation-type-alist))))))
501 (if (eq presentation-type 'binary)
502 (write-region-as-binary start end filename)
503 (write-region start end filename)
506 (defun mime-method-to-store-message/partial (entity cal)
507 (goto-char (mime-entity-point-min entity))
510 (concat "m-prts-" (user-login-name)) mime-temp-directory))
511 (id (cdr (assoc "id" cal)))
512 (number (cdr (assoc "number" cal)))
513 (total (cdr (assoc "total" cal)))
515 (mother mime-preview-buffer)
517 (or (file-exists-p root-dir)
518 (make-directory root-dir)
520 (setq id (replace-as-filename id))
521 (setq root-dir (concat root-dir "/" id))
522 (or (file-exists-p root-dir)
523 (make-directory root-dir)
525 (setq file (concat root-dir "/FULL"))
526 (if (file-exists-p file)
527 (let ((full-buf (get-buffer-create "FULL"))
528 (pwin (or (get-buffer-window mother)
529 (get-largest-window)))
531 (save-window-excursion
532 (set-buffer full-buf)
534 (as-binary-input-file (insert-file-contents file))
535 (setq major-mode 'mime-show-message-mode)
536 (mime-view-mode mother)
538 (set-window-buffer pwin
540 (set-buffer full-buf)
541 mime-preview-buffer))
544 (re-search-forward "^$")
545 (goto-char (1+ (match-end 0)))
546 (setq file (concat root-dir "/" number))
547 (mime-raw-write-region (point) (mime-entity-point-max entity) file)
548 (let ((total-file (concat root-dir "/CT")))
552 (or (file-exists-p total-file)
555 (get-buffer-create mime-temp-buffer-name))
558 (write-region (point-min)(point-max) total-file)
559 (kill-buffer (current-buffer))
561 (string-to-number total)
563 (and (file-exists-p total-file)
565 (set-buffer (find-file-noselect total-file))
567 (and (re-search-forward "[0-9]+" nil t)
569 (buffer-substring (match-beginning 0)
572 (kill-buffer (current-buffer))
575 (if (and total (> total 0))
578 (set-buffer (get-buffer-create mime-temp-buffer-name))
579 (let ((full-buf (current-buffer)))
583 (setq file (concat root-dir "/" (int-to-string i)))
584 (or (file-exists-p file)
587 (as-binary-input-file (insert-file-contents file))
588 (goto-char (point-max))
591 (as-binary-output-file
592 (write-region (point-min)(point-max)
593 (expand-file-name "FULL" root-dir)))
596 (let ((file (format "%s/%d" root-dir i)))
597 (and (file-exists-p file)
602 (let ((file (expand-file-name "CT" root-dir)))
603 (and (file-exists-p file)
606 (save-window-excursion
607 (setq major-mode 'mime-show-message-mode)
608 (mime-view-mode mother)
610 (let ((pwin (or (get-buffer-window mother)
613 (pbuf (save-excursion
614 (set-buffer full-buf)
615 mime-preview-buffer)))
616 (set-window-buffer pwin pbuf)
622 ;;; @ message/external-body
625 (defvar mime-raw-dired-function
626 (if (and (>= emacs-major-version 19) window-system)
627 (function dired-other-frame)
628 (function mime-raw-dired-function-for-one-frame)
631 (defun mime-raw-dired-function-for-one-frame (dir)
632 (let ((win (or (get-buffer-window mime-preview-buffer)
633 (get-largest-window))))
638 (defun mime-method-to-display-message/external-ftp (entity cal)
639 (let* ((site (cdr (assoc "site" cal)))
640 (directory (cdr (assoc "directory" cal)))
641 (name (cdr (assoc "name" cal)))
642 (pathname (concat "/anonymous@" site ":" directory)))
643 (message (concat "Accessing " (expand-file-name name pathname) "..."))
644 (funcall mime-raw-dired-function pathname)
645 (goto-char (point-min))
646 (search-forward name)
653 (defun mime-method-to-display-caesar (entity situation)
654 "Internal method for mime-view to display ROT13-47-48 message."
655 (let* ((new-name (format "%s-%s" (buffer-name)
656 (mime-entity-number entity)))
657 (mother mime-preview-buffer))
658 (let ((pwin (or (get-buffer-window mother)
659 (get-largest-window)))
660 (buf (get-buffer-create new-name)))
661 (set-window-buffer pwin buf)
665 (setq buffer-read-only nil)
667 (mime-text-insert-decoded-body entity)
668 (mule-caesar-region (point-min) (point-max))
669 (set-buffer-modified-p nil)
671 (view-buffer new-name)
680 (let* ((file mime-acting-situation-examples-file)
681 (buffer (get-buffer-create " *mime-example*")))
682 (if (file-readable-p file)
687 (insert-file-contents file)
690 (or (eq (car mime-acting-situation-examples) 'type)
691 (setq mime-acting-situation-examples nil))
693 (kill-buffer buffer))))
695 ;;; mime-play.el ends here