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.")
54 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
56 It decodes current entity to call internal or external method. The
57 method is selected from variable `mime-acting-condition'.
58 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
59 `mime-acting-situation-example-list'.
60 If MODE is specified, play as it. Default MODE is \"play\"."
62 (let ((entity (get-text-property (point) 'mime-view-entity)))
65 (get-text-property (point) 'mime-view-situation)))
69 (if (assq 'mode situation)
70 (put-alist 'mode mode (copy-alist situation))
71 (cons (cons 'mode mode)
75 (cons (cons 'ignore-examples ignore-examples)
77 (mime-play-entity entity situation)
81 (defun mime-play-entity (entity &optional situation ignored-method)
82 "Play entity specified by ENTITY.
83 It decodes the entity to call internal or external method. The method
84 is selected from variable `mime-acting-condition'. If MODE is
85 specified, play as it. Default MODE is \"play\"."
87 (mime-unify-situations (mime-entity-situation entity situation)
89 mime-acting-situation-example-list
90 'method ignored-method
91 mime-play-find-every-situations))
93 (setq mime-acting-situation-example-list (cdr ret)
96 (setq ret (select-menu-alist
102 (cdr (assq 'method situation)))
105 (setq ret (mime-sort-situation ret))
106 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
111 (setq method (cdr (assq 'method ret)))
112 (cond ((and (symbolp method)
114 (funcall method entity ret)
117 (mime-activate-mailcap-method entity ret)
119 ;; ((and (listp method)(stringp (car method)))
120 ;; (mime-activate-external-method entity ret)
123 (mime-show-echo-buffer "No method are specified for %s\n"
124 (mime-type/subtype-string
125 (cdr (assq 'type situation))
126 (cdr (assq 'subtype situation))))
127 (if (y-or-n-p "Do you want to save current entity to disk?")
128 (mime-save-content entity situation))
133 ;;; @ external decoder
136 (defvar mime-mailcap-method-filename-alist nil)
138 (defvar mime-mailcap-method-messages-coding-system 'undecided
139 "Coding system for an external mailcap method.")
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))
150 (mime-write-entity-content entity name)
151 (message "External method is starting...")
154 (mime-format-mailcap-command
156 (cons (cons 'filename name) situation))))
157 (binary-to-text-funcall
158 mime-mailcap-method-messages-coding-system
159 #'start-process command mime-echo-buffer-name
160 shell-file-name shell-command-switch command))))
161 (set-alist 'mime-mailcap-method-filename-alist process name)
162 (set-process-sentinel process 'mime-mailcap-method-sentinel))))
164 (defun mime-mailcap-method-sentinel (process event)
165 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
166 (if (file-exists-p file)
169 (remove-alist 'mime-mailcap-method-filename-alist process)
170 (message (format "%s %s" process event)))
172 (defvar mime-echo-window-is-shared-with-bbdb
173 (module-installed-p 'bbdb)
174 "*If non-nil, mime-echo window is shared with BBDB window.")
176 (defvar mime-echo-window-height
179 (/ (window-height) 5)
181 "*Size of mime-echo window.
182 It allows function or integer. If it is function,
183 `mime-show-echo-buffer' calls it to get height of mime-echo window.
184 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
187 (defun mime-show-echo-buffer (&rest forms)
188 "Show mime-echo buffer to display MIME-playing information."
189 (get-buffer-create mime-echo-buffer-name)
190 (let ((the-win (selected-window))
191 (win (get-buffer-window mime-echo-buffer-name)))
193 (unless (and mime-echo-window-is-shared-with-bbdb
195 (setq win (get-buffer-window bbdb-buffer-name))
197 (select-window (get-buffer-window (or mime-preview-buffer
199 (setq win (split-window-vertically
201 (if (functionp mime-echo-window-height)
202 (funcall mime-echo-window-height)
203 mime-echo-window-height)
206 (set-window-buffer win mime-echo-buffer-name)
209 (goto-char (point-max))
211 (let ((buffer-read-only nil))
212 (insert (apply (function format) forms))
214 (select-window the-win)
221 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
223 (defvar mime-view-file-name-regexp-1
224 (concat mime-view-file-name-char-regexp "+\\."
225 mime-view-file-name-char-regexp "+"))
227 (defvar mime-view-file-name-regexp-2
228 (concat (regexp-* mime-view-file-name-char-regexp)
229 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
231 (defun mime-entity-safe-filename (entity)
233 (or (mime-entity-filename entity)
235 (or (mime-entity-read-field entity 'Content-Description)
236 (mime-entity-read-field entity 'Subject))))
238 (or (string-match mime-view-file-name-regexp-1 subj)
239 (string-match mime-view-file-name-regexp-2 subj)))
240 (substring subj (match-beginning 0)(match-end 0))
243 (replace-as-filename filename)
247 ;;; @ file extraction
250 (defun mime-save-content (entity situation)
251 (let ((name (or (mime-entity-safe-filename entity)
252 (format "%s" (mime-entity-media-type entity))))
253 (dir (if (eq t mime-save-directory)
255 mime-save-directory))
257 (setq filename (read-file-name
258 (concat "File name: (default "
259 (file-name-nondirectory name) ") ")
261 (concat (file-name-as-directory dir)
262 (file-name-nondirectory name))))
263 (if (file-directory-p filename)
264 (setq filename (concat (file-name-as-directory filename)
265 (file-name-nondirectory name))))
266 (if (file-exists-p filename)
267 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
269 (mime-write-entity-content entity (expand-file-name filename))
276 (defvar mime-magic-type-alist
277 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
278 ("^\211PNG" image png)
279 ("^GIF8[79]" image gif)
280 ("^II\\*\000" image tiff)
281 ("^MM\000\\*" image tiff)
283 ("^\000\000\001\263" video mpeg)
285 "*Alist of regexp about magic-number vs. corresponding media-types.
286 Each element looks like (REGEXP TYPE SUBTYPE).
287 REGEXP is a regular expression to match against the beginning of the
289 TYPE is symbol to indicate primary type of media-type.
290 SUBTYPE is symbol to indicate subtype of media-type.")
292 (defun mime-detect-content (entity situation)
294 (let ((mdata (mime-entity-content entity))
295 (rest mime-magic-type-alist))
296 (while (not (let ((cell (car rest)))
298 (if (string-match (car cell) mdata)
299 (setq type (nth 1 cell)
300 subtype (nth 2 cell))
303 (setq rest (cdr rest))))
304 (setq situation (del-alist 'method (copy-alist situation)))
305 (mime-play-entity entity
307 (put-alist 'type type
308 (put-alist 'subtype subtype
311 'mime-detect-content)))
314 ;;; @ mail/news message
317 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
318 "Quitting method for mime-view.
319 It is registered to variable `mime-preview-quitting-method-alist'."
320 (let ((mother mime-mother-buffer)
321 (win-conf mime-preview-original-window-configuration))
322 (if (and (boundp 'mime-view-temp-message-buffer)
323 (buffer-live-p mime-view-temp-message-buffer))
324 (kill-buffer mime-view-temp-message-buffer))
325 (mime-preview-kill-buffer)
326 (set-window-configuration win-conf)
327 (pop-to-buffer mother)))
329 (defun mime-view-message/rfc822 (entity situation)
331 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
332 (mother (current-buffer))
333 (children (car (mime-entity-children entity)))
335 (mime-display-message
336 children new-name mother nil
337 (cdr (assq 'major-mode
338 (get-text-property (point) 'mime-view-situation))))))
339 (or (get-buffer-window preview-buffer)
340 (let ((m-win (get-buffer-window mother)))
342 (set-window-buffer m-win preview-buffer)
343 (switch-to-buffer preview-buffer)
347 ;;; @ message/partial
350 (defun mime-store-message/partial-piece (entity cal)
353 (concat "m-prts-" (user-login-name)) temporary-file-directory))
354 (id (cdr (assoc "id" cal)))
355 (number (cdr (assoc "number" cal)))
356 (total (cdr (assoc "total" cal)))
358 (mother (current-buffer)))
359 (or (file-exists-p root-dir)
360 (make-directory root-dir))
361 (setq id (replace-as-filename id))
362 (setq root-dir (concat root-dir "/" id))
363 (or (file-exists-p root-dir)
364 (make-directory root-dir))
365 (setq file (concat root-dir "/FULL"))
366 (if (file-exists-p file)
367 (let ((full-buf (get-buffer-create "FULL"))
368 (pwin (or (get-buffer-window mother)
369 (get-largest-window)))
371 (save-window-excursion
372 (set-buffer full-buf)
374 (binary-insert-encoded-file file)
375 (setq major-mode 'mime-show-message-mode)
376 (mime-view-buffer (current-buffer) nil mother)
377 (setq pbuf (current-buffer))
378 (make-local-variable 'mime-view-temp-message-buffer)
379 (setq mime-view-temp-message-buffer full-buf))
380 (set-window-buffer pwin pbuf)
381 (select-window pwin))
382 (setq file (concat root-dir "/" number))
383 (mime-write-entity-body entity file)
384 (let ((total-file (concat root-dir "/CT")))
388 (or (file-exists-p total-file)
391 (get-buffer-create mime-temp-buffer-name))
394 (write-region (point-min)(point-max) total-file)
395 (kill-buffer (current-buffer))
397 (string-to-number total)
399 (and (file-exists-p total-file)
401 (set-buffer (find-file-noselect total-file))
403 (and (re-search-forward "[0-9]+" nil t)
405 (buffer-substring (match-beginning 0)
408 (kill-buffer (current-buffer))
411 (if (and total (> total 0)
412 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
416 (set-buffer (get-buffer-create mime-temp-buffer-name))
417 (let ((full-buf (current-buffer)))
421 (setq file (concat root-dir "/" (int-to-string i)))
422 (or (file-exists-p file)
425 (binary-insert-encoded-file file)
426 (goto-char (point-max))
428 (binary-write-decoded-region
429 (point-min)(point-max)
430 (expand-file-name "FULL" root-dir))
433 (let ((file (format "%s/%d" root-dir i)))
434 (and (file-exists-p file)
437 (let ((file (expand-file-name "CT" root-dir)))
438 (and (file-exists-p file)
440 (let ((buf (current-buffer))
441 (pwin (or (get-buffer-window mother)
442 (get-largest-window)))
443 (pbuf (mime-display-message
444 (mime-open-entity 'buffer (current-buffer))
445 nil mother nil 'mime-show-message-mode)))
446 (with-current-buffer pbuf
447 (make-local-variable 'mime-view-temp-message-buffer)
448 (setq mime-view-temp-message-buffer buf))
449 (set-window-buffer pwin pbuf)
455 ;;; @ message/external-body
458 (defvar mime-raw-dired-function
459 (if (and (>= emacs-major-version 19) window-system)
460 (function dired-other-frame)
461 (function mime-raw-dired-function-for-one-frame)
464 (defun mime-raw-dired-function-for-one-frame (dir)
465 (let ((win (or (get-buffer-window mime-preview-buffer)
466 (get-largest-window))))
471 (defun mime-view-message/external-anon-ftp (entity cal)
472 (let* ((site (cdr (assoc "site" cal)))
473 (directory (cdr (assoc "directory" cal)))
474 (name (cdr (assoc "name" cal)))
475 (pathname (concat "/anonymous@" site ":" directory)))
476 (message (concat "Accessing " (expand-file-name name pathname) " ..."))
477 (funcall mime-raw-dired-function pathname)
478 (goto-char (point-min))
479 (search-forward name)
482 (defvar mime-raw-browse-url-function mime-browse-url-function)
484 (defun mime-view-message/external-url (entity cal)
485 (let ((url (cdr (assoc "url" cal))))
486 (message (concat "Accessing " url " ..."))
487 (funcall mime-raw-browse-url-function url)))
493 (defun mime-view-caesar (entity situation)
494 "Internal method for mime-view to display ROT13-47-48 message."
495 (let ((buf (get-buffer-create
496 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
497 (with-current-buffer buf
498 (setq buffer-read-only nil)
500 (mime-insert-text-content entity)
501 (mule-caesar-region (point-min) (point-max))
502 (set-buffer-modified-p nil)
504 (let ((win (get-buffer-window (current-buffer))))
505 (or (eq (selected-window) win)
506 (select-window (or win (get-largest-window)))
509 (goto-char (point-min))
518 ;;; mime-play.el ends here