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)
51 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
53 It decodes current entity to call internal or external method. The
54 method is selected from variable `mime-acting-condition'.
55 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
56 `mime-acting-situation-example-list'.
57 If MODE is specified, play as it. Default MODE is \"play\"."
59 (let ((entity (get-text-property (point) 'mime-view-entity)))
62 (get-text-property (point) 'mime-view-situation)))
66 (if (assq 'mode situation)
67 (put-alist 'mode mode (copy-alist situation))
68 (cons (cons 'mode mode)
72 (cons (cons 'ignore-examples ignore-examples)
74 (mime-play-entity entity situation)
78 (defun mime-play-entity (entity &optional situation ignored-method)
79 "Play entity specified by ENTITY.
80 It decodes the entity to call internal or external method. The method
81 is selected from variable `mime-acting-condition'. If MODE is
82 specified, play as it. Default MODE is \"play\"."
84 (mime-unify-situations (mime-entity-situation entity situation)
86 mime-acting-situation-example-list
89 (setq mime-acting-situation-example-list (cdr ret)
92 (setq ret (select-menu-alist
98 (cdr (assq 'method situation)))
101 (setq ret (mime-sort-situation ret))
102 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
107 (setq method (cdr (assq 'method ret)))
108 (cond ((and (symbolp method)
110 (funcall method entity ret)
113 (mime-activate-mailcap-method entity ret)
115 ;; ((and (listp method)(stringp (car method)))
116 ;; (mime-activate-external-method entity ret)
119 (mime-show-echo-buffer "No method are specified for %s\n"
120 (mime-type/subtype-string
121 (cdr (assq 'type situation))
122 (cdr (assq 'subtype situation))))
123 (if (y-or-n-p "Do you want to save current entity to disk?")
124 (mime-save-content entity situation))
129 ;;; @ external decoder
132 (defvar mime-mailcap-method-filename-alist nil)
134 (defun mime-activate-mailcap-method (entity situation)
135 (let ((method (cdr (assoc 'method situation)))
136 (name (mime-entity-safe-filename entity)))
138 (if (and name (not (string= name "")))
139 (expand-file-name name temporary-file-directory)
141 (expand-file-name "EMI" temporary-file-directory))
143 (mime-write-entity-content entity name)
144 (message "External method is starting...")
147 (mailcap-format-command
149 (cons (cons 'filename name) situation))))
150 (start-process command mime-echo-buffer-name
151 shell-file-name shell-command-switch command)
153 (set-alist 'mime-mailcap-method-filename-alist process name)
154 (set-process-sentinel process 'mime-mailcap-method-sentinel)
158 (defun mime-mailcap-method-sentinel (process event)
159 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
160 (if (file-exists-p file)
163 (remove-alist 'mime-mailcap-method-filename-alist process)
164 (message (format "%s %s" process event)))
166 (defvar mime-echo-window-is-shared-with-bbdb
167 (module-installed-p 'bbdb)
168 "*If non-nil, mime-echo window is shared with BBDB window.")
170 (defvar mime-echo-window-height
173 (/ (window-height) 5)
175 "*Size of mime-echo window.
176 It allows function or integer. If it is function,
177 `mime-show-echo-buffer' calls it to get height of mime-echo window.
178 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
181 (defun mime-show-echo-buffer (&rest forms)
182 "Show mime-echo buffer to display MIME-playing information."
183 (get-buffer-create mime-echo-buffer-name)
184 (let ((the-win (selected-window))
185 (win (get-buffer-window mime-echo-buffer-name)))
187 (unless (and mime-echo-window-is-shared-with-bbdb
189 (setq win (get-buffer-window bbdb-buffer-name))
191 (select-window (get-buffer-window (or mime-preview-buffer
193 (setq win (split-window-vertically
195 (if (functionp mime-echo-window-height)
196 (funcall mime-echo-window-height)
197 mime-echo-window-height)
200 (set-window-buffer win mime-echo-buffer-name)
203 (goto-char (point-max))
205 (let ((buffer-read-only nil))
206 (insert (apply (function format) forms))
208 (select-window the-win)
215 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
217 (defvar mime-view-file-name-regexp-1
218 (concat mime-view-file-name-char-regexp "+\\."
219 mime-view-file-name-char-regexp "+"))
221 (defvar mime-view-file-name-regexp-2
222 (concat (regexp-* mime-view-file-name-char-regexp)
223 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
225 (defun mime-entity-safe-filename (entity)
227 (or (mime-entity-filename entity)
229 (or (mime-entity-read-field entity 'Content-Description)
230 (mime-entity-read-field entity 'Subject))))
232 (or (string-match mime-view-file-name-regexp-1 subj)
233 (string-match mime-view-file-name-regexp-2 subj)))
234 (substring subj (match-beginning 0)(match-end 0))
237 (replace-as-filename filename)
241 ;;; @ file extraction
244 (defun mime-save-content (entity situation)
245 (let ((name (or (mime-entity-safe-filename entity)
246 (format "%s" (mime-entity-media-type entity))))
247 (dir (if (eq t mime-save-directory)
249 mime-save-directory))
251 (setq filename (read-file-name
252 (concat "File name: (default "
253 (file-name-nondirectory name) ") ")
255 (concat (file-name-as-directory dir)
256 (file-name-nondirectory name))))
257 (if (file-directory-p filename)
258 (setq filename (concat (file-name-as-directory filename)
259 (file-name-nondirectory name))))
260 (if (file-exists-p filename)
261 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
263 (mime-write-entity-content entity (expand-file-name filename))
270 (defvar mime-magic-type-alist
271 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
272 ("^\211PNG" image png)
273 ("^GIF8[79]" image gif)
274 ("^II\\*\000" image tiff)
275 ("^MM\000\\*" image tiff)
277 ("^\000\000\001\263" video mpeg)
279 "*Alist of regexp about magic-number vs. corresponding media-types.
280 Each element looks like (REGEXP TYPE SUBTYPE).
281 REGEXP is a regular expression to match against the beginning of the
283 TYPE is symbol to indicate primary type of media-type.
284 SUBTYPE is symbol to indicate subtype of media-type.")
286 (defun mime-detect-content (entity situation)
288 (let ((mdata (mime-entity-content entity))
289 (rest mime-magic-type-alist))
290 (while (not (let ((cell (car rest)))
292 (if (string-match (car cell) mdata)
293 (setq type (nth 1 cell)
294 subtype (nth 2 cell))
297 (setq rest (cdr rest))))
298 (setq situation (del-alist 'method (copy-alist situation)))
299 (mime-play-entity entity
301 (put-alist 'type type
302 (put-alist 'subtype subtype
305 'mime-detect-content)))
308 ;;; @ mail/news message
311 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
312 "Quitting method for mime-view.
313 It is registered to variable `mime-preview-quitting-method-alist'."
314 (let ((raw-buffer (mime-entity-buffer
315 (get-text-property (point-min) 'mime-view-entity)))
316 (mother mime-mother-buffer)
317 (win-conf mime-preview-original-window-configuration))
318 (kill-buffer raw-buffer)
319 (mime-preview-kill-buffer)
320 (set-window-configuration win-conf)
321 (pop-to-buffer mother)
324 (defun mime-view-message/rfc822 (entity situation)
326 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
327 (mother (current-buffer))
328 (children (car (mime-entity-children entity)))
330 (mime-display-message
331 children new-name mother nil
332 (cdr (assq 'major-mode
333 (get-text-property (point) 'mime-view-situation))))))
334 (or (get-buffer-window preview-buffer)
335 (let ((m-win (get-buffer-window mother)))
337 (set-window-buffer m-win preview-buffer)
338 (switch-to-buffer preview-buffer)
342 ;;; @ message/partial
345 (defun mime-store-message/partial-piece (entity cal)
348 (concat "m-prts-" (user-login-name)) temporary-file-directory))
349 (id (cdr (assoc "id" cal)))
350 (number (cdr (assoc "number" cal)))
351 (total (cdr (assoc "total" cal)))
353 (mother (current-buffer)))
354 (or (file-exists-p root-dir)
355 (make-directory root-dir))
356 (setq id (replace-as-filename id))
357 (setq root-dir (concat root-dir "/" id))
358 (or (file-exists-p root-dir)
359 (make-directory root-dir))
360 (setq file (concat root-dir "/FULL"))
361 (if (file-exists-p file)
362 (let ((full-buf (get-buffer-create "FULL"))
363 (pwin (or (get-buffer-window mother)
364 (get-largest-window)))
366 (save-window-excursion
367 (set-buffer full-buf)
369 (as-binary-input-file (insert-file-contents file))
370 (setq major-mode 'mime-show-message-mode)
371 (mime-view-buffer (current-buffer) nil mother)
372 (setq pbuf (current-buffer))
374 (set-window-buffer pwin pbuf)
377 (setq file (concat root-dir "/" number))
378 (mime-write-entity-body entity file)
379 (let ((total-file (concat root-dir "/CT")))
383 (or (file-exists-p total-file)
386 (get-buffer-create mime-temp-buffer-name))
389 (write-region (point-min)(point-max) total-file)
390 (kill-buffer (current-buffer))
392 (string-to-number total)
394 (and (file-exists-p total-file)
396 (set-buffer (find-file-noselect total-file))
398 (and (re-search-forward "[0-9]+" nil t)
400 (buffer-substring (match-beginning 0)
403 (kill-buffer (current-buffer))
406 (if (and total (> total 0)
407 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
411 (set-buffer (get-buffer-create mime-temp-buffer-name))
412 (let ((full-buf (current-buffer)))
416 (setq file (concat root-dir "/" (int-to-string i)))
417 (or (file-exists-p file)
420 (as-binary-input-file (insert-file-contents file))
421 (goto-char (point-max))
424 (as-binary-output-file
425 (write-region (point-min)(point-max)
426 (expand-file-name "FULL" root-dir)))
429 (let ((file (format "%s/%d" root-dir i)))
430 (and (file-exists-p file)
435 (let ((file (expand-file-name "CT" root-dir)))
436 (and (file-exists-p file)
439 (let ((pwin (or (get-buffer-window mother)
440 (get-largest-window)))
441 (pbuf (mime-display-message
442 (mime-open-entity 'buffer (current-buffer))
443 nil mother nil 'mime-show-message-mode)))
444 (set-window-buffer pwin pbuf)
450 ;;; @ message/external-body
453 (defvar mime-raw-dired-function
454 (if (and (>= emacs-major-version 19) window-system)
455 (function dired-other-frame)
456 (function mime-raw-dired-function-for-one-frame)
459 (defun mime-raw-dired-function-for-one-frame (dir)
460 (let ((win (or (get-buffer-window mime-preview-buffer)
461 (get-largest-window))))
466 (defun mime-view-message/external-anon-ftp (entity cal)
467 (let* ((site (cdr (assoc "site" cal)))
468 (directory (cdr (assoc "directory" cal)))
469 (name (cdr (assoc "name" cal)))
470 (pathname (concat "/anonymous@" site ":" directory)))
471 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
472 (funcall mime-raw-dired-function pathname)
473 (goto-char (point-min))
474 (search-forward name)
477 (defvar mime-raw-browse-url-function mime-browse-url-function)
479 (defun mime-view-message/external-url (entity cal)
480 (let ((url (cdr (assoc "url" cal))))
481 (message (concat "Accessing " url " ..."))
482 (funcall mime-raw-browse-url-function url)))
488 (defun mime-view-caesar (entity situation)
489 "Internal method for mime-view to display ROT13-47-48 message."
490 (let ((buf (get-buffer-create
491 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
492 (with-current-buffer buf
493 (setq buffer-read-only nil)
495 (mime-insert-text-content entity)
496 (mule-caesar-region (point-min) (point-max))
497 (set-buffer-modified-p nil)
499 (let ((win (get-buffer-window (current-buffer))))
500 (or (eq (selected-window) win)
501 (select-window (or win (get-largest-window)))
504 (goto-char (point-min))
513 ;;; mime-play.el ends here