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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, 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 (buffer-live-p mime-view-temp-message-buffer)
336 (kill-buffer mime-view-temp-message-buffer))
337 (mime-preview-kill-buffer)
338 (set-window-configuration win-conf)
339 (pop-to-buffer mother)))
341 (defun mime-view-message/rfc822 (entity situation)
343 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
344 (mother (current-buffer))
345 (children (car (mime-entity-children entity)))
347 (mime-display-message
348 children new-name mother nil
349 (cdr (assq 'major-mode
350 (get-text-property (point) 'mime-view-situation))))))
351 (or (get-buffer-window preview-buffer)
352 (let ((m-win (get-buffer-window mother)))
354 (set-window-buffer m-win preview-buffer)
355 (switch-to-buffer preview-buffer)
359 ;;; @ message/partial
362 (defun mime-require-safe-directory (dir)
363 "Create a directory DIR safely.
364 The permission of the created directory becomes `700' (for the owner only).
365 If the directory already exists and is writable by other users, an error
367 (let ((attr (file-attributes dir))
368 (orig-modes (default-file-modes)))
369 (if (and attr (eq (car attr) t)) ; directory already exists.
370 (unless (or (memq system-type '(windows-nt ms-dos OS/2 emx))
371 (and (eq (nth 2 attr) (user-real-uid))
372 (eq (file-modes dir) 448)))
373 (error "Invalid owner or permission for %s" dir))
376 (set-default-file-modes 448)
377 (make-directory dir))
378 (set-default-file-modes orig-modes)))))
380 (defvar mime-view-temp-message-buffer nil) ; buffer local variable
382 (defun mime-store-message/partial-piece (entity cal)
385 (concat "m-prts-" (user-login-name)) temporary-file-directory))
386 (id (cdr (assoc "id" cal)))
387 (number (cdr (assoc "number" cal)))
388 (total (cdr (assoc "total" cal)))
390 (mother (current-buffer))
391 (orig-modes (default-file-modes)))
392 (mime-require-safe-directory root-dir)
393 (or (file-exists-p root-dir)
396 (set-default-file-modes 448)
397 (make-directory root-dir))
398 (set-default-file-modes orig-modes)))
399 (setq id (replace-as-filename id))
400 (setq root-dir (concat root-dir "/" id))
402 (or (file-exists-p root-dir)
405 (set-default-file-modes 448)
406 (make-directory root-dir))
407 (set-default-file-modes orig-modes)))
409 (setq file (concat root-dir "/FULL"))
410 (if (file-exists-p file)
411 (let ((full-buf (get-buffer-create "FULL"))
412 (pwin (or (get-buffer-window mother)
413 (get-largest-window)))
415 (save-window-excursion
416 (set-buffer full-buf)
418 (binary-insert-encoded-file file)
419 (setq major-mode 'mime-show-message-mode)
420 (mime-view-buffer (current-buffer) nil mother)
421 (setq pbuf (current-buffer))
422 (make-local-variable 'mime-view-temp-message-buffer)
423 (setq mime-view-temp-message-buffer full-buf))
424 (set-window-buffer pwin pbuf)
425 (select-window pwin))
426 (setq file (concat root-dir "/" number))
427 (mime-write-entity-body entity file)
428 (let ((total-file (concat root-dir "/CT")))
432 (or (file-exists-p total-file)
435 (get-buffer-create mime-temp-buffer-name))
438 (write-region (point-min)(point-max) total-file)
439 (kill-buffer (current-buffer))
441 (string-to-number total)
443 (and (file-exists-p total-file)
445 (set-buffer (find-file-noselect total-file))
447 (and (re-search-forward "[0-9]+" nil t)
449 (buffer-substring (match-beginning 0)
452 (kill-buffer (current-buffer))
455 (if (and total (> total 0)
456 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
460 (set-buffer (get-buffer-create mime-temp-buffer-name))
461 (let ((full-buf (current-buffer)))
465 (setq file (concat root-dir "/" (int-to-string i)))
466 (or (file-exists-p file)
469 (binary-insert-encoded-file file)
470 (goto-char (point-max))
472 (binary-write-decoded-region
473 (point-min)(point-max)
474 (expand-file-name "FULL" root-dir))
477 (let ((file (format "%s/%d" root-dir i)))
478 (and (file-exists-p file)
481 (let ((file (expand-file-name "CT" root-dir)))
482 (and (file-exists-p file)
484 (let ((buf (current-buffer))
485 (pwin (or (get-buffer-window mother)
486 (get-largest-window)))
487 (pbuf (mime-display-message
488 (mime-open-entity 'buffer (current-buffer))
489 nil mother nil 'mime-show-message-mode)))
490 (with-current-buffer pbuf
491 (make-local-variable 'mime-view-temp-message-buffer)
492 (setq mime-view-temp-message-buffer buf))
493 (set-window-buffer pwin pbuf)
499 ;;; @ message/external-body
502 (defvar mime-raw-dired-function
503 (if (and (>= emacs-major-version 19) window-system)
504 (function dired-other-frame)
505 (function mime-raw-dired-function-for-one-frame)
508 (defun mime-raw-dired-function-for-one-frame (dir)
509 (let ((win (or (get-buffer-window mime-preview-buffer)
510 (get-largest-window))))
515 (defun mime-view-message/external-anon-ftp (entity cal)
516 (let* ((site (cdr (assoc "site" cal)))
517 (directory (cdr (assoc "directory" cal)))
518 (name (cdr (assoc "name" cal)))
519 (pathname (concat "/anonymous@" site ":" directory)))
520 (message "%s" (concat "Accessing " (expand-file-name name pathname) "..."))
521 (funcall mime-raw-dired-function pathname)
522 (goto-char (point-min))
523 (search-forward name)
526 (defvar mime-raw-browse-url-function mime-browse-url-function)
528 (defun mime-view-message/external-url (entity cal)
529 (let ((url (cdr (assoc "url" cal))))
530 (message "%s" (concat "Accessing " url "..."))
531 (funcall mime-raw-browse-url-function url)))
537 (defun mime-view-caesar (entity situation)
538 "Internal method for mime-view to display ROT13-47-48 message."
539 (let ((buf (get-buffer-create
540 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
541 (with-current-buffer buf
542 (setq buffer-read-only nil)
544 (mime-insert-text-content entity)
545 (mule-caesar-region (point-min) (point-max))
546 (set-buffer-modified-p nil)
548 (let ((win (get-buffer-window (current-buffer))))
549 (or (eq (selected-window) win)
550 (select-window (or win (get-largest-window)))
553 (goto-char (point-min))
562 ;;; mime-play.el ends here