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.")
49 (defvar mime-play-messages-coding-system nil
50 "Coding system to be used for external MIME playback method.")
57 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
59 It decodes current entity to call internal or external method. The
60 method is selected from variable `mime-acting-condition'.
61 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
62 `mime-acting-situation-example-list'.
63 If MODE is specified, play as it. Default MODE is \"play\"."
65 (let ((entity (get-text-property (point) 'mime-view-entity)))
68 (get-text-property (point) 'mime-view-situation)))
72 (if (assq 'mode situation)
73 (put-alist 'mode mode (copy-alist situation))
74 (cons (cons 'mode mode)
78 (cons (cons 'ignore-examples ignore-examples)
80 (mime-play-entity entity situation)
84 (defun mime-play-entity (entity &optional situation ignored-method)
85 "Play entity specified by ENTITY.
86 It decodes the entity to call internal or external method. The method
87 is selected from variable `mime-acting-condition'. If MODE is
88 specified, play as it. Default MODE is \"play\"."
90 (mime-unify-situations (mime-entity-situation entity situation)
92 mime-acting-situation-example-list
93 'method ignored-method
94 mime-play-find-every-situations))
96 (setq mime-acting-situation-example-list (cdr ret)
99 (setq ret (mime-select-menu-alist
105 (cdr (assq 'method situation)))
108 (setq ret (mime-sort-situation ret))
109 (add-to-list 'mime-acting-situation-example-list (cons ret 0))
114 (setq method (cdr (assq 'method ret)))
115 (cond ((and (symbolp method)
117 (funcall method entity ret)
120 (mime-activate-mailcap-method entity ret)
122 ;; ((and (listp method)(stringp (car method)))
123 ;; (mime-activate-external-method entity ret)
126 (mime-show-echo-buffer "No method are specified for %s\n"
127 (mime-type/subtype-string
128 (cdr (assq 'type situation))
129 (cdr (assq 'subtype situation))))
130 (if (y-or-n-p "Do you want to save current entity to disk?")
131 (mime-save-content entity situation))
136 ;;; @ external decoder
139 (defvar mime-mailcap-method-filename-alist nil)
141 (defun mime-activate-mailcap-method (entity situation)
142 (let ((method (cdr (assoc 'method situation)))
143 (name (mime-entity-safe-filename entity)))
144 (setq name (expand-file-name (if (and name (not (string= name "")))
146 (make-temp-name "EMI"))
147 (make-temp-file "EMI" 'directory)))
148 (mime-write-entity-content entity name)
149 (message "External method is starting...")
152 (mime-format-mailcap-command
154 (cons (cons 'filename name) situation)))
155 (coding-system-for-read mime-play-messages-coding-system))
156 (start-process command mime-echo-buffer-name
157 shell-file-name shell-command-switch command))))
158 (set-alist 'mime-mailcap-method-filename-alist process name)
159 (set-process-sentinel process 'mime-mailcap-method-sentinel))))
161 (defun mime-mailcap-method-sentinel (process event)
162 (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
163 (when (file-exists-p file)
166 (delete-directory (file-name-directory file)))))
167 (remove-alist 'mime-mailcap-method-filename-alist process)
168 (message "%s %s" process event))
170 (defvar mime-echo-window-is-shared-with-bbdb
171 (module-installed-p 'bbdb)
172 "*If non-nil, mime-echo window is shared with BBDB window.")
174 (defvar mime-echo-window-height
177 (/ (window-height) 5)
179 "*Size of mime-echo window.
180 It allows function or integer. If it is function,
181 `mime-show-echo-buffer' calls it to get height of mime-echo window.
182 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
185 (defun mime-show-echo-buffer (&rest forms)
186 "Show mime-echo buffer to display MIME-playing information."
187 (get-buffer-create mime-echo-buffer-name)
188 (let ((the-win (selected-window))
189 (win (get-buffer-window mime-echo-buffer-name)))
191 (unless (and mime-echo-window-is-shared-with-bbdb
193 (setq win (get-buffer-window bbdb-buffer-name))
195 (select-window (get-buffer-window (or mime-preview-buffer
197 (setq win (split-window-vertically
199 (if (functionp mime-echo-window-height)
200 (funcall mime-echo-window-height)
201 mime-echo-window-height)
204 (set-window-buffer win mime-echo-buffer-name)
207 (goto-char (point-max))
209 (let ((buffer-read-only nil))
210 (insert (apply (function format) forms))
212 (select-window the-win)
219 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
221 (defvar mime-view-file-name-regexp-1
222 (concat mime-view-file-name-char-regexp "+\\."
223 mime-view-file-name-char-regexp "+"))
225 (defvar mime-view-file-name-regexp-2
226 (concat (regexp-* mime-view-file-name-char-regexp)
227 "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
229 (defun mime-entity-safe-filename (entity)
231 (or (mime-entity-filename entity)
233 (or (mime-entity-read-field entity 'Content-Description)
234 (mime-entity-read-field entity 'Subject))))
236 (or (string-match mime-view-file-name-regexp-1 subj)
237 (string-match mime-view-file-name-regexp-2 subj)))
238 (substring subj (match-beginning 0)(match-end 0))
241 (replace-as-filename filename)
245 ;;; @ file extraction
248 (defun mime-save-content (entity situation)
249 (let ((name (or (mime-entity-safe-filename entity)
250 (format "%s" (mime-entity-media-type entity))))
251 (dir (if (eq t mime-save-directory)
253 mime-save-directory))
255 (setq filename (read-file-name
256 (concat "File name: (default "
257 (file-name-nondirectory name) ") ")
259 (concat (file-name-as-directory dir)
260 (file-name-nondirectory name))))
261 (if (file-directory-p filename)
262 (setq filename (concat (file-name-as-directory filename)
263 (file-name-nondirectory name))))
264 (if (file-exists-p filename)
265 (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
267 (mime-write-entity-content entity (expand-file-name filename))
274 (defvar mime-magic-type-alist
275 '(("^\377\330\377[\340\356]..JFIF" image jpeg)
276 ("^\211PNG" image png)
277 ("^GIF8[79]" image gif)
278 ("^II\\*\000" image tiff)
279 ("^MM\000\\*" image tiff)
281 ("^\000\000\001\263" video mpeg)
283 "*Alist of regexp about magic-number vs. corresponding media-types.
284 Each element looks like (REGEXP TYPE SUBTYPE).
285 REGEXP is a regular expression to match against the beginning of the
287 TYPE is symbol to indicate primary type of media-type.
288 SUBTYPE is symbol to indicate subtype of media-type.")
290 (defun mime-detect-content (entity situation)
292 (let ((mdata (mime-entity-content entity))
293 (rest mime-magic-type-alist))
294 (while (not (let ((cell (car rest)))
296 (if (string-match (car cell) mdata)
297 (setq type (nth 1 cell)
298 subtype (nth 2 cell))
301 (setq rest (cdr rest))))
302 (setq situation (del-alist 'method (copy-alist situation)))
303 (mime-play-entity entity
305 (put-alist 'type type
306 (put-alist 'subtype subtype
309 'mime-detect-content)))
312 ;;; @ mail/news message
315 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
316 "Quitting method for mime-view.
317 It is registered to variable `mime-preview-quitting-method-alist'."
318 (let ((mother mime-mother-buffer)
319 (win-conf mime-preview-original-window-configuration))
320 (if (and (boundp 'mime-view-temp-message-buffer)
321 (buffer-live-p mime-view-temp-message-buffer))
322 (kill-buffer mime-view-temp-message-buffer))
323 (mime-preview-kill-buffer)
324 (set-window-configuration win-conf)
325 (pop-to-buffer mother)))
327 (defun mime-view-message/rfc822 (entity situation)
329 (format "%s-%s" (buffer-name) (mime-entity-number entity)))
330 (mother (current-buffer))
331 (children (car (mime-entity-children entity)))
333 (mime-display-message
334 children new-name mother nil
335 (cdr (assq 'major-mode
336 (get-text-property (point) 'mime-view-situation))))))
337 (or (get-buffer-window preview-buffer)
338 (let ((m-win (get-buffer-window mother)))
340 (set-window-buffer m-win preview-buffer)
341 (switch-to-buffer preview-buffer)
345 ;;; @ message/partial
348 (defun mime-require-safe-directory (dir)
349 "Create a directory DIR safely.
350 The permission of the created directory becomes `700' (for the owner only).
351 If the directory already exists and is writable by other users, an error
353 (let ((attr (file-attributes dir))
354 (orig-modes (default-file-modes)))
355 (if (and attr (eq (car attr) t)) ; directory already exists.
356 (unless (or (memq system-type '(windows-nt ms-dos OS/2 emx))
357 (and (eq (nth 2 attr) (user-real-uid))
358 (eq (file-modes dir) 448)))
359 (error "Invalid owner or permission for %s" dir))
362 (set-default-file-modes 448)
363 (make-directory dir))
364 (set-default-file-modes orig-modes)))))
366 (defun mime-store-message/partial-piece (entity cal)
369 (concat "m-prts-" (user-login-name)) temporary-file-directory))
370 (id (cdr (assoc "id" cal)))
371 (number (cdr (assoc "number" cal)))
372 (total (cdr (assoc "total" cal)))
374 (mother (current-buffer))
375 (orig-modes (default-file-modes)))
376 (mime-require-safe-directory root-dir)
377 (or (file-exists-p root-dir)
380 (set-default-file-modes 448)
381 (make-directory root-dir))
382 (set-default-file-modes orig-modes)))
383 (setq id (replace-as-filename id))
384 (setq root-dir (concat root-dir "/" id))
386 (or (file-exists-p root-dir)
389 (set-default-file-modes 448)
390 (make-directory root-dir))
391 (set-default-file-modes orig-modes)))
393 (setq file (concat root-dir "/FULL"))
394 (if (file-exists-p file)
395 (let ((full-buf (get-buffer-create "FULL"))
396 (pwin (or (get-buffer-window mother)
397 (get-largest-window)))
399 (save-window-excursion
400 (set-buffer full-buf)
402 (binary-insert-encoded-file file)
403 (setq major-mode 'mime-show-message-mode)
404 (mime-view-buffer (current-buffer) nil mother)
405 (setq pbuf (current-buffer))
406 (make-local-variable 'mime-view-temp-message-buffer)
407 (setq mime-view-temp-message-buffer full-buf))
408 (set-window-buffer pwin pbuf)
409 (select-window pwin))
410 (setq file (concat root-dir "/" number))
411 (mime-write-entity-body entity file)
412 (let ((total-file (concat root-dir "/CT")))
416 (or (file-exists-p total-file)
419 (get-buffer-create mime-temp-buffer-name))
422 (write-region (point-min)(point-max) total-file)
423 (kill-buffer (current-buffer))
425 (string-to-number total)
427 (and (file-exists-p total-file)
429 (set-buffer (find-file-noselect total-file))
431 (and (re-search-forward "[0-9]+" nil t)
433 (buffer-substring (match-beginning 0)
436 (kill-buffer (current-buffer))
439 (if (and total (> total 0)
440 (>= (length (directory-files root-dir nil "^[0-9]+$" t))
444 (set-buffer (get-buffer-create mime-temp-buffer-name))
445 (let ((full-buf (current-buffer)))
449 (setq file (concat root-dir "/" (int-to-string i)))
450 (or (file-exists-p file)
453 (binary-insert-encoded-file file)
454 (goto-char (point-max))
456 (binary-write-decoded-region
457 (point-min)(point-max)
458 (expand-file-name "FULL" root-dir))
461 (let ((file (format "%s/%d" root-dir i)))
462 (and (file-exists-p file)
465 (let ((file (expand-file-name "CT" root-dir)))
466 (and (file-exists-p file)
468 (let ((buf (current-buffer))
469 (pwin (or (get-buffer-window mother)
470 (get-largest-window)))
471 (pbuf (mime-display-message
472 (mime-open-entity 'buffer (current-buffer))
473 nil mother nil 'mime-show-message-mode)))
474 (with-current-buffer pbuf
475 (make-local-variable 'mime-view-temp-message-buffer)
476 (setq mime-view-temp-message-buffer buf))
477 (set-window-buffer pwin pbuf)
483 ;;; @ message/external-body
486 (defvar mime-raw-dired-function
487 (if (and (>= emacs-major-version 19) window-system)
488 (function dired-other-frame)
489 (function mime-raw-dired-function-for-one-frame)
492 (defun mime-raw-dired-function-for-one-frame (dir)
493 (let ((win (or (get-buffer-window mime-preview-buffer)
494 (get-largest-window))))
499 (defun mime-view-message/external-anon-ftp (entity cal)
500 (let* ((site (cdr (assoc "site" cal)))
501 (directory (cdr (assoc "directory" cal)))
502 (name (cdr (assoc "name" cal)))
503 (pathname (concat "/anonymous@" site ":" directory)))
504 (message "%s" (concat "Accessing " (expand-file-name name pathname) "..."))
505 (funcall mime-raw-dired-function pathname)
506 (goto-char (point-min))
507 (search-forward name)
510 (defvar mime-raw-browse-url-function mime-browse-url-function)
512 (defun mime-view-message/external-url (entity cal)
513 (let ((url (cdr (assoc "url" cal))))
514 (message "%s" (concat "Accessing " url "..."))
515 (funcall mime-raw-browse-url-function url)))
521 (defun mime-view-caesar (entity situation)
522 "Internal method for mime-view to display ROT13-47-48 message."
523 (let ((buf (get-buffer-create
524 (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
525 (with-current-buffer buf
526 (setq buffer-read-only nil)
528 (mime-insert-text-content entity)
529 (mule-caesar-region (point-min) (point-max))
530 (set-buffer-modified-p nil)
532 (let ((win (get-buffer-window (current-buffer))))
533 (or (eq (selected-window) win)
534 (select-window (or win (get-largest-window)))
537 (goto-char (point-min))
546 ;;; mime-play.el ends here