1 ;;; mime-play.el --- Playback processing module for mime-view.el
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003,
4 ;; 2004, 2010 Free Software Foundation, Inc.
6 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
7 ;; Created: 1995/9/26 (separated from tm-view.el)
8 ;; Renamed: 1997/2/21 from tm-play.el
9 ;; Keywords: MIME, multimedia, mail, news
11 ;; This file is part of SEMI (Secretariat of Emacs MIME Interfaces).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
37 (error (defvar bbdb-buffer-name nil)))
40 (defcustom mime-save-directory "~/"
41 "*Name of the directory where MIME entity will be saved in.
42 If t, it means current directory."
44 :type '(choice (const :tag "Current directory" t)
47 (defcustom mime-play-delete-file-immediately t
48 "If non-nil, delete played file immediately."
52 (defvar mime-play-find-every-situations t
53 "*Find every available situations if non-nil.")
55 (defvar mime-play-messages-coding-system nil
56 "Coding system to be used for external MIME playback method.")
63 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
65 It decodes current entity to call internal or external method. The
66 method is selected from variable `mime-acting-condition'.
67 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
68 `mime-acting-situation-example-list'.
69 If MODE is specified, play as it. Default MODE is \"play\"."
71 (let ((entity (get-text-property (point) 'mime-view-entity)))
74 (get-text-property (point) 'mime-view-situation)))
78 (if (assq 'mode situation)
79 (put-alist 'mode mode (copy-alist situation))
80 (cons (cons 'mode mode)
84 (cons (cons 'ignore-examples ignore-examples)
86 (mime-play-entity entity situation)
90 (defun mime-play-entity (entity &optional situation ignored-method)
91 "Play entity specified by ENTITY.
92 It decodes the entity to call internal or external method. The method
93 is selected from variable `mime-acting-condition'. If MODE is
94 specified, play as it. Default MODE is \"play\"."
96 (mime-unify-situations (mime-entity-situation entity situation)
98 mime-acting-situation-example-list
99 'method ignored-method
100 mime-play-find-every-situations))
102 (setq mime-acting-situation-example-list (cdr ret)
105 (setq ret (mime-select-menu-alist
111 (cdr (assq 'method situation)))
114 (setq ret (mime-sort-situation ret))
115 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
120 (setq method (cdr (assq 'method ret)))
121 (cond ((and (symbolp method)
123 (funcall method entity ret)
126 (mime-activate-mailcap-method entity ret)
128 ;; ((and (listp method)(stringp (car method)))
129 ;; (mime-activate-external-method entity ret)
132 (mime-show-echo-buffer "No method are specified for %s\n"
133 (mime-type/subtype-string
134 (cdr (assq 'type situation))
135 (cdr (assq 'subtype situation))))
136 (if (y-or-n-p "Do you want to save current entity to disk?")
137 (mime-save-content entity situation))
142 ;;; @ external decoder
145 (defvar mime-mailcap-method-filename-alist nil)
147 (defun mime-activate-mailcap-method (entity situation)
148 (let ((method (cdr (assoc 'method situation)))
149 (name (mime-entity-safe-filename entity)))
150 (setq name (expand-file-name (if (and name (not (string= name "")))
152 (make-temp-name "EMI"))
153 (make-temp-file "EMI" 'directory)))
154 (mime-write-entity-content entity name)
155 (message "External method is starting...")
158 (mime-format-mailcap-command
160 (cons (cons 'filename name) situation)))
161 (coding-system-for-read mime-play-messages-coding-system))
162 (start-process command mime-echo-buffer-name
163 shell-file-name shell-command-switch command))))
164 (set-alist 'mime-mailcap-method-filename-alist process name)
165 (set-process-sentinel process 'mime-mailcap-method-sentinel))))
167 (defun mime-mailcap-method-sentinel (process event)
168 (when mime-play-delete-file-immediately
169 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
170 (when (file-exists-p file)
173 (delete-directory (file-name-directory file)))))
174 (remove-alist 'mime-mailcap-method-filename-alist process))
175 (message "%s %s" process event))
177 (defun mime-mailcap-delete-played-files ()
178 (dolist (elem mime-mailcap-method-filename-alist)
179 (when (file-exists-p (cdr elem))
181 (delete-file (cdr elem))
182 (delete-directory (file-name-directory (cdr elem)))))))
184 (add-hook 'kill-emacs-hook 'mime-mailcap-delete-played-files)
186 (defvar mime-echo-window-is-shared-with-bbdb
187 (module-installed-p 'bbdb)
188 "*If non-nil, mime-echo window is shared with BBDB window.")
190 (defvar mime-echo-window-height
193 (/ (window-height) 5)
195 "*Size of mime-echo window.
196 It allows function or integer. If it is function,
197 `mime-show-echo-buffer' calls it to get height of mime-echo window.
198 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
201 (defun mime-show-echo-buffer (&rest forms)
202 "Show mime-echo buffer to display MIME-playing information."
203 (get-buffer-create mime-echo-buffer-name)
204 (let ((the-win (selected-window))
205 (win (get-buffer-window mime-echo-buffer-name)))
207 (unless (and mime-echo-window-is-shared-with-bbdb
209 (setq win (get-buffer-window bbdb-buffer-name))
211 (select-window (get-buffer-window (or mime-preview-buffer
213 (setq win (split-window-vertically
215 (if (functionp mime-echo-window-height)
216 (funcall mime-echo-window-height)
217 mime-echo-window-height)
220 (set-window-buffer win mime-echo-buffer-name)
223 (goto-char (point-max))
225 (let ((buffer-read-only nil))
226 (insert (apply (function format) forms))
228 (select-window the-win)
235 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
237 (defvar mime-view-file-name-regexp-1
238 (concat mime-view-file-name-char-regexp "+\\."
239 mime-view-file-name-char-regexp "+"))
241 (defvar mime-view-file-name-regexp-2
242 (concat (regexp-* mime-view-file-name-char-regexp)
243 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
245 (defun mime-entity-safe-filename (entity)
247 (or (mime-entity-filename entity)
249 (or (mime-entity-read-field entity 'Content-Description)
250 (mime-entity-read-field entity 'Subject))))
252 (or (string-match mime-view-file-name-regexp-1 subj)
253 (string-match mime-view-file-name-regexp-2 subj)))
254 (substring subj (match-beginning 0)(match-end 0))
257 (replace-as-filename filename)
261 ;;; @ file extraction
264 (defun mime-save-content (entity situation)
265 (let ((name (or (mime-entity-safe-filename entity)
266 (format "%s" (mime-entity-media-type entity))))
267 (dir (if (eq t mime-save-directory)
269 mime-save-directory))
271 (setq filename (read-file-name
272 (concat "File name: (default "
273 (file-name-nondirectory name) ") ")
275 (concat (file-name-as-directory dir)
276 (file-name-nondirectory name))))
277 (if (file-directory-p filename)
278 (setq filename (concat (file-name-as-directory filename)
279 (file-name-nondirectory name))))
280 (if (file-exists-p filename)
281 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
283 (mime-write-entity-content entity (expand-file-name filename))
290 (defvar mime-magic-type-alist
291 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
292 ("^\211PNG" image png)
293 ("^GIF8[79]" image gif)
294 ("^II\\*\000" image tiff)
295 ("^MM\000\\*" image tiff)
297 ("^\000\000\001\263" video mpeg)
299 "*Alist of regexp about magic-number vs. corresponding media-types.
300 Each element looks like (REGEXP TYPE SUBTYPE).
301 REGEXP is a regular expression to match against the beginning of the
303 TYPE is symbol to indicate primary type of media-type.
304 SUBTYPE is symbol to indicate subtype of media-type.")
306 (defun mime-detect-content (entity situation)
308 (let ((mdata (mime-entity-content entity))
309 (rest mime-magic-type-alist))
310 (while (not (let ((cell (car rest)))
312 (if (string-match (car cell) mdata)
313 (setq type (nth 1 cell)
314 subtype (nth 2 cell))
317 (setq rest (cdr rest))))
318 (setq situation (del-alist 'method (copy-alist situation)))
319 (mime-play-entity entity
321 (put-alist 'type type
322 (put-alist 'subtype subtype
325 'mime-detect-content)))
328 ;;; @ mail/news message
331 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
332 "Quitting method for mime-view.
333 It is registered to variable `mime-preview-quitting-method-alist'."
334 (let ((mother mime-mother-buffer)
335 (win-conf mime-preview-original-window-configuration))
336 (if (buffer-live-p mime-view-temp-message-buffer)
337 (kill-buffer mime-view-temp-message-buffer))
338 (mime-preview-kill-buffer)
339 (set-window-configuration win-conf)
340 (pop-to-buffer mother)))
342 (defun mime-view-message/rfc822 (entity situation)
344 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
345 (mother (current-buffer))
346 (children (car (mime-entity-children entity)))
348 (mime-display-message
349 children new-name mother nil
350 (cdr (assq 'major-mode
351 (get-text-property (point) 'mime-view-situation))))))
352 (or (get-buffer-window preview-buffer)
353 (let ((m-win (get-buffer-window mother)))
355 (set-window-buffer m-win preview-buffer)
356 (switch-to-buffer preview-buffer)
360 ;;; @ message/partial
363 (defun mime-require-safe-directory (dir)
364 "Create a directory DIR safely.
365 The permission of the created directory becomes `700' (for the owner only).
366 If the directory already exists and is writable by other users, an error
368 (let ((attr (file-attributes dir))
369 (orig-modes (default-file-modes)))
370 (if (and attr (eq (car attr) t)) ; directory already exists.
371 (unless (or (memq system-type '(windows-nt ms-dos OS/2 emx))
372 (and (eq (nth 2 attr) (user-real-uid))
373 (eq (file-modes dir) 448)))
374 (error "Invalid owner or permission for %s" dir))
377 (set-default-file-modes 448)
378 (make-directory dir))
379 (set-default-file-modes orig-modes)))))
381 (defvar mime-view-temp-message-buffer nil) ; buffer local variable
383 (defun mime-store-message/partial-piece (entity cal)
386 (concat "m-prts-" (user-login-name)) temporary-file-directory))
387 (id (cdr (assoc "id" cal)))
388 (number (cdr (assoc "number" cal)))
389 (total (cdr (assoc "total" cal)))
391 (mother (current-buffer))
392 (orig-modes (default-file-modes)))
393 (mime-require-safe-directory root-dir)
394 (or (file-exists-p root-dir)
397 (set-default-file-modes 448)
398 (make-directory root-dir))
399 (set-default-file-modes orig-modes)))
400 (setq id (replace-as-filename id))
401 (setq root-dir (concat root-dir "/" id))
403 (or (file-exists-p root-dir)
406 (set-default-file-modes 448)
407 (make-directory root-dir))
408 (set-default-file-modes orig-modes)))
410 (setq file (concat root-dir "/FULL"))
411 (if (file-exists-p file)
412 (let ((full-buf (get-buffer-create "FULL"))
413 (pwin (or (get-buffer-window mother)
414 (get-largest-window)))
416 (save-window-excursion
417 (set-buffer full-buf)
419 (binary-insert-encoded-file file)
420 (setq major-mode 'mime-show-message-mode)
421 (mime-view-buffer (current-buffer) nil mother)
422 (setq pbuf (current-buffer))
423 (make-local-variable 'mime-view-temp-message-buffer)
424 (setq mime-view-temp-message-buffer full-buf))
425 (set-window-buffer pwin pbuf)
426 (select-window pwin))
427 (setq file (concat root-dir "/" number))
428 (mime-write-entity-body entity file)
429 (let ((total-file (concat root-dir "/CT")))
433 (or (file-exists-p total-file)
436 (get-buffer-create mime-temp-buffer-name))
439 (write-region (point-min)(point-max) total-file)
440 (kill-buffer (current-buffer))
442 (string-to-number total)
444 (and (file-exists-p total-file)
446 (set-buffer (find-file-noselect total-file))
448 (and (re-search-forward "[0-9]+" nil t)
450 (buffer-substring (match-beginning 0)
453 (kill-buffer (current-buffer))
456 (if (and total (> total 0)
457 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
461 (set-buffer (get-buffer-create mime-temp-buffer-name))
462 (let ((full-buf (current-buffer)))
466 (setq file (concat root-dir "/" (int-to-string i)))
467 (or (file-exists-p file)
470 (binary-insert-encoded-file file)
471 (goto-char (point-max))
473 (binary-write-decoded-region
474 (point-min)(point-max)
475 (expand-file-name "FULL" root-dir))
478 (let ((file (format "%s/%d" root-dir i)))
479 (and (file-exists-p file)
482 (let ((file (expand-file-name "CT" root-dir)))
483 (and (file-exists-p file)
485 (let ((buf (current-buffer))
486 (pwin (or (get-buffer-window mother)
487 (get-largest-window)))
488 (pbuf (mime-display-message
489 (mime-open-entity 'buffer (current-buffer))
490 nil mother nil 'mime-show-message-mode)))
491 (with-current-buffer pbuf
492 (make-local-variable 'mime-view-temp-message-buffer)
493 (setq mime-view-temp-message-buffer buf))
494 (set-window-buffer pwin pbuf)
500 ;;; @ message/external-body
503 (defvar mime-raw-dired-function
504 (if (and (>= emacs-major-version 19) window-system)
505 (function dired-other-frame)
506 (function mime-raw-dired-function-for-one-frame)
509 (defun mime-raw-dired-function-for-one-frame (dir)
510 (let ((win (or (get-buffer-window mime-preview-buffer)
511 (get-largest-window))))
516 (defun mime-view-message/external-anon-ftp (entity cal)
517 (let* ((site (cdr (assoc "site" cal)))
518 (directory (cdr (assoc "directory" cal)))
519 (name (cdr (assoc "name" cal)))
520 (pathname (concat "/anonymous@" site ":" directory)))
521 (message "%s" (concat "Accessing " (expand-file-name name pathname) "..."))
522 (funcall mime-raw-dired-function pathname)
523 (goto-char (point-min))
524 (search-forward name)
527 (defvar mime-raw-browse-url-function mime-browse-url-function)
529 (defun mime-view-message/external-url (entity cal)
530 (let ((url (cdr (assoc "url" cal))))
531 (message "%s" (concat "Accessing " url "..."))
532 (funcall mime-raw-browse-url-function url)))
538 (defun mime-view-caesar (entity situation)
539 "Internal method for mime-view to display ROT13-47-48 message."
540 (let ((buf (get-buffer-create
541 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
542 (with-current-buffer buf
543 (setq buffer-read-only nil)
545 (mime-insert-text-content entity)
546 (mule-caesar-region (point-min) (point-max))
547 (set-buffer-modified-p nil)
549 (let ((win (get-buffer-window (current-buffer))))
550 (or (eq (selected-window) win)
551 (select-window (or win (get-largest-window)))
554 (goto-char (point-min))
563 ;;; mime-play.el ends here