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 (defcustom mime-play-delete-file-immediately t
47 "If non-nil, delete played file immediately."
51 (defvar mime-play-find-every-situations t
52 "*Find every available situations if non-nil.")
54 (defvar mime-play-messages-coding-system nil
55 "Coding system to be used for external MIME playback method.")
62 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
64 It decodes current entity to call internal or external method. The
65 method is selected from variable `mime-acting-condition'.
66 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
67 `mime-acting-situation-example-list'.
68 If MODE is specified, play as it. Default MODE is \"play\"."
70 (let ((entity (get-text-property (point) 'mime-view-entity)))
73 (get-text-property (point) 'mime-view-situation)))
77 (if (assq 'mode situation)
78 (put-alist 'mode mode (copy-alist situation))
79 (cons (cons 'mode mode)
83 (cons (cons 'ignore-examples ignore-examples)
85 (mime-play-entity entity situation)
89 (defun mime-play-entity (entity &optional situation ignored-method)
90 "Play entity specified by ENTITY.
91 It decodes the entity to call internal or external method. The method
92 is selected from variable `mime-acting-condition'. If MODE is
93 specified, play as it. Default MODE is \"play\"."
95 (mime-unify-situations (mime-entity-situation entity situation)
97 mime-acting-situation-example-list
98 'method ignored-method
99 mime-play-find-every-situations))
101 (setq mime-acting-situation-example-list (cdr ret)
104 (setq ret (mime-select-menu-alist
110 (cdr (assq 'method situation)))
113 (setq ret (mime-sort-situation ret))
114 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
119 (setq method (cdr (assq 'method ret)))
120 (cond ((and (symbolp method)
122 (funcall method entity ret)
125 (mime-activate-mailcap-method entity ret)
127 ;; ((and (listp method)(stringp (car method)))
128 ;; (mime-activate-external-method entity ret)
131 (mime-show-echo-buffer "No method are specified for %s\n"
132 (mime-type/subtype-string
133 (cdr (assq 'type situation))
134 (cdr (assq 'subtype situation))))
135 (if (y-or-n-p "Do you want to save current entity to disk?")
136 (mime-save-content entity situation))
141 ;;; @ external decoder
144 (defvar mime-mailcap-method-filename-alist nil)
146 (defun mime-activate-mailcap-method (entity situation)
147 (let ((method (cdr (assoc 'method situation)))
148 (name (mime-entity-safe-filename entity)))
149 (setq name (expand-file-name (if (and name (not (string= name "")))
151 (make-temp-name "EMI"))
152 (make-temp-file "EMI" 'directory)))
153 (mime-write-entity-content entity name)
154 (message "External method is starting...")
157 (mime-format-mailcap-command
159 (cons (cons 'filename name) situation)))
160 (coding-system-for-read mime-play-messages-coding-system))
161 (start-process command mime-echo-buffer-name
162 shell-file-name shell-command-switch command))))
163 (set-alist 'mime-mailcap-method-filename-alist process name)
164 (set-process-sentinel process 'mime-mailcap-method-sentinel))))
166 (defun mime-mailcap-method-sentinel (process event)
167 (when mime-play-delete-file-immediately
168 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
169 (when (file-exists-p file)
172 (delete-directory (file-name-directory file)))))
173 (remove-alist 'mime-mailcap-method-filename-alist process))
174 (message "%s %s" process event))
176 (defun mime-mailcap-delete-played-files ()
177 (dolist (elem mime-mailcap-method-filename-alist)
178 (when (file-exists-p (cdr elem))
180 (delete-file (cdr elem))
181 (delete-directory (file-name-directory (cdr elem)))))))
183 (add-hook 'kill-emacs-hook 'mime-mailcap-delete-played-files)
185 (defvar mime-echo-window-is-shared-with-bbdb
186 (module-installed-p 'bbdb)
187 "*If non-nil, mime-echo window is shared with BBDB window.")
189 (defvar mime-echo-window-height
192 (/ (window-height) 5)
194 "*Size of mime-echo window.
195 It allows function or integer. If it is function,
196 `mime-show-echo-buffer' calls it to get height of mime-echo window.
197 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
200 (defun mime-show-echo-buffer (&rest forms)
201 "Show mime-echo buffer to display MIME-playing information."
202 (get-buffer-create mime-echo-buffer-name)
203 (let ((the-win (selected-window))
204 (win (get-buffer-window mime-echo-buffer-name)))
206 (unless (and mime-echo-window-is-shared-with-bbdb
208 (setq win (get-buffer-window bbdb-buffer-name))
210 (select-window (get-buffer-window (or mime-preview-buffer
212 (setq win (split-window-vertically
214 (if (functionp mime-echo-window-height)
215 (funcall mime-echo-window-height)
216 mime-echo-window-height)
219 (set-window-buffer win mime-echo-buffer-name)
222 (goto-char (point-max))
224 (let ((buffer-read-only nil))
225 (insert (apply (function format) forms))
227 (select-window the-win)
234 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
236 (defvar mime-view-file-name-regexp-1
237 (concat mime-view-file-name-char-regexp "+\\."
238 mime-view-file-name-char-regexp "+"))
240 (defvar mime-view-file-name-regexp-2
241 (concat (regexp-* mime-view-file-name-char-regexp)
242 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
244 (defun mime-entity-safe-filename (entity)
246 (or (mime-entity-filename entity)
248 (or (mime-entity-read-field entity 'Content-Description)
249 (mime-entity-read-field entity 'Subject))))
251 (or (string-match mime-view-file-name-regexp-1 subj)
252 (string-match mime-view-file-name-regexp-2 subj)))
253 (substring subj (match-beginning 0)(match-end 0))
256 (replace-as-filename filename)
260 ;;; @ file extraction
263 (defun mime-save-content (entity situation)
264 (let ((name (or (mime-entity-safe-filename entity)
265 (format "%s" (mime-entity-media-type entity))))
266 (dir (if (eq t mime-save-directory)
268 mime-save-directory))
270 (setq filename (read-file-name
271 (concat "File name: (default "
272 (file-name-nondirectory name) ") ")
274 (concat (file-name-as-directory dir)
275 (file-name-nondirectory name))))
276 (if (file-directory-p filename)
277 (setq filename (concat (file-name-as-directory filename)
278 (file-name-nondirectory name))))
279 (if (file-exists-p filename)
280 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
282 (mime-write-entity-content entity (expand-file-name filename))
289 (defvar mime-magic-type-alist
290 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
291 ("^\211PNG" image png)
292 ("^GIF8[79]" image gif)
293 ("^II\\*\000" image tiff)
294 ("^MM\000\\*" image tiff)
296 ("^\000\000\001\263" video mpeg)
298 "*Alist of regexp about magic-number vs. corresponding media-types.
299 Each element looks like (REGEXP TYPE SUBTYPE).
300 REGEXP is a regular expression to match against the beginning of the
302 TYPE is symbol to indicate primary type of media-type.
303 SUBTYPE is symbol to indicate subtype of media-type.")
305 (defun mime-detect-content (entity situation)
307 (let ((mdata (mime-entity-content entity))
308 (rest mime-magic-type-alist))
309 (while (not (let ((cell (car rest)))
311 (if (string-match (car cell) mdata)
312 (setq type (nth 1 cell)
313 subtype (nth 2 cell))
316 (setq rest (cdr rest))))
317 (setq situation (del-alist 'method (copy-alist situation)))
318 (mime-play-entity entity
320 (put-alist 'type type
321 (put-alist 'subtype subtype
324 'mime-detect-content)))
327 ;;; @ mail/news message
330 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
331 "Quitting method for mime-view.
332 It is registered to variable `mime-preview-quitting-method-alist'."
333 (let ((mother mime-mother-buffer)
334 (win-conf mime-preview-original-window-configuration))
335 (if (and (boundp 'mime-view-temp-message-buffer)
336 (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 (defun mime-store-message/partial-piece (entity cal)
384 (concat "m-prts-" (user-login-name)) temporary-file-directory))
385 (id (cdr (assoc "id" cal)))
386 (number (cdr (assoc "number" cal)))
387 (total (cdr (assoc "total" cal)))
389 (mother (current-buffer))
390 (orig-modes (default-file-modes)))
391 (mime-require-safe-directory root-dir)
392 (or (file-exists-p root-dir)
395 (set-default-file-modes 448)
396 (make-directory root-dir))
397 (set-default-file-modes orig-modes)))
398 (setq id (replace-as-filename id))
399 (setq root-dir (concat root-dir "/" id))
401 (or (file-exists-p root-dir)
404 (set-default-file-modes 448)
405 (make-directory root-dir))
406 (set-default-file-modes orig-modes)))
408 (setq file (concat root-dir "/FULL"))
409 (if (file-exists-p file)
410 (let ((full-buf (get-buffer-create "FULL"))
411 (pwin (or (get-buffer-window mother)
412 (get-largest-window)))
414 (save-window-excursion
415 (set-buffer full-buf)
417 (binary-insert-encoded-file file)
418 (setq major-mode 'mime-show-message-mode)
419 (mime-view-buffer (current-buffer) nil mother)
420 (setq pbuf (current-buffer))
421 (make-local-variable 'mime-view-temp-message-buffer)
422 (setq mime-view-temp-message-buffer full-buf))
423 (set-window-buffer pwin pbuf)
424 (select-window pwin))
425 (setq file (concat root-dir "/" number))
426 (mime-write-entity-body entity file)
427 (let ((total-file (concat root-dir "/CT")))
431 (or (file-exists-p total-file)
434 (get-buffer-create mime-temp-buffer-name))
437 (write-region (point-min)(point-max) total-file)
438 (kill-buffer (current-buffer))
440 (string-to-number total)
442 (and (file-exists-p total-file)
444 (set-buffer (find-file-noselect total-file))
446 (and (re-search-forward "[0-9]+" nil t)
448 (buffer-substring (match-beginning 0)
451 (kill-buffer (current-buffer))
454 (if (and total (> total 0)
455 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
459 (set-buffer (get-buffer-create mime-temp-buffer-name))
460 (let ((full-buf (current-buffer)))
464 (setq file (concat root-dir "/" (int-to-string i)))
465 (or (file-exists-p file)
468 (binary-insert-encoded-file file)
469 (goto-char (point-max))
471 (binary-write-decoded-region
472 (point-min)(point-max)
473 (expand-file-name "FULL" root-dir))
476 (let ((file (format "%s/%d" root-dir i)))
477 (and (file-exists-p file)
480 (let ((file (expand-file-name "CT" root-dir)))
481 (and (file-exists-p file)
483 (let ((buf (current-buffer))
484 (pwin (or (get-buffer-window mother)
485 (get-largest-window)))
486 (pbuf (mime-display-message
487 (mime-open-entity 'buffer (current-buffer))
488 nil mother nil 'mime-show-message-mode)))
489 (with-current-buffer pbuf
490 (make-local-variable 'mime-view-temp-message-buffer)
491 (setq mime-view-temp-message-buffer buf))
492 (set-window-buffer pwin pbuf)
498 ;;; @ message/external-body
501 (defvar mime-raw-dired-function
502 (if (and (>= emacs-major-version 19) window-system)
503 (function dired-other-frame)
504 (function mime-raw-dired-function-for-one-frame)
507 (defun mime-raw-dired-function-for-one-frame (dir)
508 (let ((win (or (get-buffer-window mime-preview-buffer)
509 (get-largest-window))))
514 (defun mime-view-message/external-anon-ftp (entity cal)
515 (let* ((site (cdr (assoc "site" cal)))
516 (directory (cdr (assoc "directory" cal)))
517 (name (cdr (assoc "name" cal)))
518 (pathname (concat "/anonymous@" site ":" directory)))
519 (message "%s" (concat "Accessing " (expand-file-name name pathname) "..."))
520 (funcall mime-raw-dired-function pathname)
521 (goto-char (point-min))
522 (search-forward name)
525 (defvar mime-raw-browse-url-function mime-browse-url-function)
527 (defun mime-view-message/external-url (entity cal)
528 (let ((url (cdr (assoc "url" cal))))
529 (message "%s" (concat "Accessing " url "..."))
530 (funcall mime-raw-browse-url-function url)))
536 (defun mime-view-caesar (entity situation)
537 "Internal method for mime-view to display ROT13-47-48 message."
538 (let ((buf (get-buffer-create
539 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
540 (with-current-buffer buf
541 (setq buffer-read-only nil)
543 (mime-insert-text-content entity)
544 (mule-caesar-region (point-min) (point-max))
545 (set-buffer-modified-p nil)
547 (let ((win (get-buffer-window (current-buffer))))
548 (or (eq (selected-window) win)
549 (select-window (or win (get-largest-window)))
552 (goto-char (point-min))
561 ;;; mime-play.el ends here