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-play-find-every-situations t
47 "*Find every available situations if non-nil.")
49 (defvar mime-play-messages-coding-system nil
50 "Coding system to be used for external MIME playback method.")
57 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
59 It decodes current entity to call internal or external method. The
60 method is selected from variable `mime-acting-condition'.
61 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
62 `mime-acting-situation-example-list'.
63 If MODE is specified, play as it. Default MODE is \"play\"."
65 (let ((entity (get-text-property (point) 'mime-view-entity)))
68 (get-text-property (point) 'mime-view-situation)))
72 (if (assq 'mode situation)
73 (put-alist 'mode mode (copy-alist situation))
74 (cons (cons 'mode mode)
78 (cons (cons 'ignore-examples ignore-examples)
80 (mime-play-entity entity situation)
84 (defun mime-play-entity (entity &optional situation ignored-method)
85 "Play entity specified by ENTITY.
86 It decodes the entity to call internal or external method. The method
87 is selected from variable `mime-acting-condition'. If MODE is
88 specified, play as it. Default MODE is \"play\"."
90 (mime-unify-situations (mime-entity-situation entity situation)
92 mime-acting-situation-example-list
93 'method ignored-method
94 mime-play-find-every-situations))
96 (setq mime-acting-situation-example-list (cdr ret)
99 (setq ret (select-menu-alist
105 (cdr (assq 'method situation)))
108 (setq ret (mime-sort-situation ret))
109 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
114 (setq method (cdr (assq 'method ret)))
115 (cond ((and (symbolp method)
117 (funcall method entity ret)
120 (mime-activate-mailcap-method entity ret)
122 ;; ((and (listp method)(stringp (car method)))
123 ;; (mime-activate-external-method entity ret)
126 (mime-show-echo-buffer "No method are specified for %s\n"
127 (mime-type/subtype-string
128 (cdr (assq 'type situation))
129 (cdr (assq 'subtype situation))))
130 (if (y-or-n-p "Do you want to save current entity to disk?")
131 (mime-save-content entity situation))
136 ;;; @ external decoder
139 (defvar mime-mailcap-method-filename-alist nil)
141 (defun mime-activate-mailcap-method (entity situation)
142 (let ((method (cdr (assoc 'method situation)))
143 (name (mime-entity-safe-filename entity)))
145 (if (and name (not (string= name "")))
146 (expand-file-name name temporary-file-directory)
148 (expand-file-name "EMI" temporary-file-directory))))
149 (mime-write-entity-content entity name)
150 (message "External method is starting...")
153 (mime-format-mailcap-command
155 (cons (cons 'filename name) situation))))
156 (binary-to-text-funcall
157 mime-play-messages-coding-system
158 #'start-process command mime-echo-buffer-name
159 shell-file-name shell-command-switch command))))
160 (set-alist 'mime-mailcap-method-filename-alist process name)
161 (set-process-sentinel process 'mime-mailcap-method-sentinel))))
163 (defun mime-mailcap-method-sentinel (process event)
164 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
165 (if (file-exists-p file)
168 (remove-alist 'mime-mailcap-method-filename-alist process)
169 (message (format "%s %s" process event)))
171 (defvar mime-echo-window-is-shared-with-bbdb
172 (module-installed-p 'bbdb)
173 "*If non-nil, mime-echo window is shared with BBDB window.")
175 (defvar mime-echo-window-height
178 (/ (window-height) 5)
180 "*Size of mime-echo window.
181 It allows function or integer. If it is function,
182 `mime-show-echo-buffer' calls it to get height of mime-echo window.
183 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
186 (defun mime-show-echo-buffer (&rest forms)
187 "Show mime-echo buffer to display MIME-playing information."
188 (get-buffer-create mime-echo-buffer-name)
189 (let ((the-win (selected-window))
190 (win (get-buffer-window mime-echo-buffer-name)))
192 (unless (and mime-echo-window-is-shared-with-bbdb
194 (setq win (get-buffer-window bbdb-buffer-name))
196 (select-window (get-buffer-window (or mime-preview-buffer
198 (setq win (split-window-vertically
200 (if (functionp mime-echo-window-height)
201 (funcall mime-echo-window-height)
202 mime-echo-window-height)
205 (set-window-buffer win mime-echo-buffer-name)
208 (goto-char (point-max))
210 (let ((buffer-read-only nil))
211 (insert (apply (function format) forms))
213 (select-window the-win)
220 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
222 (defvar mime-view-file-name-regexp-1
223 (concat mime-view-file-name-char-regexp "+\\."
224 mime-view-file-name-char-regexp "+"))
226 (defvar mime-view-file-name-regexp-2
227 (concat (regexp-* mime-view-file-name-char-regexp)
228 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
230 (defun mime-entity-safe-filename (entity)
232 (or (mime-entity-filename entity)
234 (or (mime-entity-read-field entity 'Content-Description)
235 (mime-entity-read-field entity 'Subject))))
237 (or (string-match mime-view-file-name-regexp-1 subj)
238 (string-match mime-view-file-name-regexp-2 subj)))
239 (substring subj (match-beginning 0)(match-end 0))
242 (replace-as-filename filename)
246 ;;; @ file extraction
249 (defun mime-save-content (entity situation)
250 (let ((name (or (mime-entity-safe-filename entity)
251 (format "%s" (mime-entity-media-type entity))))
252 (dir (if (eq t mime-save-directory)
254 mime-save-directory))
256 (setq filename (read-file-name
257 (concat "File name: (default "
258 (file-name-nondirectory name) ") ")
260 (concat (file-name-as-directory dir)
261 (file-name-nondirectory name))))
262 (if (file-directory-p filename)
263 (setq filename (concat (file-name-as-directory filename)
264 (file-name-nondirectory name))))
265 (if (file-exists-p filename)
266 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
268 (mime-write-entity-content entity (expand-file-name filename))
275 (defvar mime-magic-type-alist
276 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
277 ("^\211PNG" image png)
278 ("^GIF8[79]" image gif)
279 ("^II\\*\000" image tiff)
280 ("^MM\000\\*" image tiff)
282 ("^\000\000\001\263" video mpeg)
284 "*Alist of regexp about magic-number vs. corresponding media-types.
285 Each element looks like (REGEXP TYPE SUBTYPE).
286 REGEXP is a regular expression to match against the beginning of the
288 TYPE is symbol to indicate primary type of media-type.
289 SUBTYPE is symbol to indicate subtype of media-type.")
291 (defun mime-detect-content (entity situation)
293 (let ((mdata (mime-entity-content entity))
294 (rest mime-magic-type-alist))
295 (while (not (let ((cell (car rest)))
297 (if (string-match (car cell) mdata)
298 (setq type (nth 1 cell)
299 subtype (nth 2 cell))
302 (setq rest (cdr rest))))
303 (setq situation (del-alist 'method (copy-alist situation)))
304 (mime-play-entity entity
306 (put-alist 'type type
307 (put-alist 'subtype subtype
310 'mime-detect-content)))
313 ;;; @ mail/news message
316 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
317 "Quitting method for mime-view.
318 It is registered to variable `mime-preview-quitting-method-alist'."
319 (let ((mother mime-mother-buffer)
320 (win-conf mime-preview-original-window-configuration))
321 (if (and (boundp 'mime-view-temp-message-buffer)
322 (buffer-live-p mime-view-temp-message-buffer))
323 (kill-buffer mime-view-temp-message-buffer))
324 (mime-preview-kill-buffer)
325 (set-window-configuration win-conf)
326 (pop-to-buffer mother)))
328 (defun mime-view-message/rfc822 (entity situation)
330 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
331 (mother (current-buffer))
332 (children (car (mime-entity-children entity)))
334 (mime-display-message
335 children new-name mother nil
336 (cdr (assq 'major-mode
337 (get-text-property (point) 'mime-view-situation))))))
338 (or (get-buffer-window preview-buffer)
339 (let ((m-win (get-buffer-window mother)))
341 (set-window-buffer m-win preview-buffer)
342 (switch-to-buffer preview-buffer)
346 ;;; @ message/partial
349 (defun mime-store-message/partial-piece (entity cal)
352 (concat "m-prts-" (user-login-name)) temporary-file-directory))
353 (id (cdr (assoc "id" cal)))
354 (number (cdr (assoc "number" cal)))
355 (total (cdr (assoc "total" cal)))
357 (mother (current-buffer)))
358 (or (file-exists-p root-dir)
359 (make-directory root-dir))
360 (setq id (replace-as-filename id))
361 (setq root-dir (concat root-dir "/" id))
362 (or (file-exists-p root-dir)
363 (make-directory root-dir))
364 (setq file (concat root-dir "/FULL"))
365 (if (file-exists-p file)
366 (let ((full-buf (get-buffer-create "FULL"))
367 (pwin (or (get-buffer-window mother)
368 (get-largest-window)))
370 (save-window-excursion
371 (set-buffer full-buf)
373 (binary-insert-encoded-file file)
374 (setq major-mode 'mime-show-message-mode)
375 (mime-view-buffer (current-buffer) nil mother)
376 (setq pbuf (current-buffer))
377 (make-local-variable 'mime-view-temp-message-buffer)
378 (setq mime-view-temp-message-buffer full-buf))
379 (set-window-buffer pwin pbuf)
380 (select-window pwin))
381 (setq file (concat root-dir "/" number))
382 (mime-write-entity-body entity file)
383 (let ((total-file (concat root-dir "/CT")))
387 (or (file-exists-p total-file)
390 (get-buffer-create mime-temp-buffer-name))
393 (write-region (point-min)(point-max) total-file)
394 (kill-buffer (current-buffer))
396 (string-to-number total)
398 (and (file-exists-p total-file)
400 (set-buffer (find-file-noselect total-file))
402 (and (re-search-forward "[0-9]+" nil t)
404 (buffer-substring (match-beginning 0)
407 (kill-buffer (current-buffer))
410 (if (and total (> total 0)
411 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
415 (set-buffer (get-buffer-create mime-temp-buffer-name))
416 (let ((full-buf (current-buffer)))
420 (setq file (concat root-dir "/" (int-to-string i)))
421 (or (file-exists-p file)
424 (binary-insert-encoded-file file)
425 (goto-char (point-max))
427 (binary-write-decoded-region
428 (point-min)(point-max)
429 (expand-file-name "FULL" root-dir))
432 (let ((file (format "%s/%d" root-dir i)))
433 (and (file-exists-p file)
436 (let ((file (expand-file-name "CT" root-dir)))
437 (and (file-exists-p file)
439 (let ((buf (current-buffer))
440 (pwin (or (get-buffer-window mother)
441 (get-largest-window)))
442 (pbuf (mime-display-message
443 (mime-open-entity 'buffer (current-buffer))
444 nil mother nil 'mime-show-message-mode)))
445 (with-current-buffer pbuf
446 (make-local-variable 'mime-view-temp-message-buffer)
447 (setq mime-view-temp-message-buffer buf))
448 (set-window-buffer pwin pbuf)
454 ;;; @ message/external-body
457 (defvar mime-raw-dired-function
458 (if (and (>= emacs-major-version 19) window-system)
459 (function dired-other-frame)
460 (function mime-raw-dired-function-for-one-frame)
463 (defun mime-raw-dired-function-for-one-frame (dir)
464 (let ((win (or (get-buffer-window mime-preview-buffer)
465 (get-largest-window))))
470 (defun mime-view-message/external-anon-ftp (entity cal)
471 (let* ((site (cdr (assoc "site" cal)))
472 (directory (cdr (assoc "directory" cal)))
473 (name (cdr (assoc "name" cal)))
474 (pathname (concat "/anonymous@" site ":" directory)))
475 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
476 (funcall mime-raw-dired-function pathname)
477 (goto-char (point-min))
478 (search-forward name)
481 (defvar mime-raw-browse-url-function mime-browse-url-function)
483 (defun mime-view-message/external-url (entity cal)
484 (let ((url (cdr (assoc "url" cal))))
485 (message (concat "Accessing " url " ..."))
486 (funcall mime-raw-browse-url-function url)))
492 (defun mime-view-caesar (entity situation)
493 "Internal method for mime-view to display ROT13-47-48 message."
494 (let ((buf (get-buffer-create
495 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
496 (with-current-buffer buf
497 (setq buffer-read-only nil)
499 (mime-insert-text-content entity)
500 (mule-caesar-region (point-min) (point-max))
501 (set-buffer-modified-p nil)
503 (let ((win (get-buffer-window (current-buffer))))
504 (or (eq (selected-window) win)
505 (select-window (or win (get-largest-window)))
508 (goto-char (point-min))
517 ;;; mime-play.el ends here