6f642c6f9946b9213b69e114e5c85689d6a8b549
[elisp/semi.git] / mime-play.el
1 ;;; mime-play.el --- Playback processing module for mime-view.el
2
3 ;; Copyright (C) 1994,95,96,97,98,99,2000 Free Software Foundation, Inc.
4
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
9
10 ;; This file is part of SEMI (Secretariat of Emacs MIME Interfaces).
11
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.
16
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.
21
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.
26
27 ;;; Code:
28
29 (require 'mime-view)
30 (require 'alist)
31 (require 'filename)
32
33 (eval-when-compile
34   (condition-case nil
35       (require 'bbdb)
36     (error (defvar bbdb-buffer-name nil)))
37   )
38
39 (defcustom mime-save-directory "~/"
40   "*Name of the directory where MIME entity will be saved in.
41 If t, it means current directory."
42   :group 'mime-view
43   :type '(choice (const :tag "Current directory" t)
44                  (directory)))
45
46 (defvar mime-play-find-every-situations t
47   "*Find every available situations if non-nil.")
48
49
50 ;;; @ content decoder
51 ;;;
52
53 ;;;###autoload
54 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
55   "Play current entity.
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\"."
61   (interactive "P")
62   (let ((entity (get-text-property (point) 'mime-view-entity)))
63     (if entity
64         (let ((situation
65                (get-text-property (point) 'mime-view-situation)))
66           (or mode
67               (setq mode "play"))
68           (setq situation 
69                 (if (assq 'mode situation)
70                     (put-alist 'mode mode (copy-alist situation))
71                   (cons (cons 'mode mode)
72                         situation)))
73           (if ignore-examples
74               (setq situation
75                     (cons (cons 'ignore-examples ignore-examples)
76                           situation)))
77           (mime-play-entity entity situation)
78           ))))
79
80 ;;;###autoload
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\"."
86   (let ((ret
87          (mime-unify-situations (mime-entity-situation entity situation)
88                                 mime-acting-condition
89                                 mime-acting-situation-example-list
90                                 'method ignored-method
91                                 mime-play-find-every-situations))
92         method)
93     (setq mime-acting-situation-example-list (cdr ret)
94           ret (car ret))
95     (cond ((cdr ret)
96            (setq ret (select-menu-alist
97                       "Methods"
98                       (mapcar (function
99                                (lambda (situation)
100                                  (cons
101                                   (format "%s"
102                                           (cdr (assq 'method situation)))
103                                   situation)))
104                               ret)))
105            (setq ret (mime-sort-situation ret))
106            (add-to-list 'mime-acting-situation-example-list (cons ret 0))
107            )
108           (t
109            (setq ret (car ret))
110            ))
111     (setq method (cdr (assq 'method ret)))
112     (cond ((and (symbolp method)
113                 (fboundp method))
114            (funcall method entity ret)
115            )
116           ((stringp method)
117            (mime-activate-mailcap-method entity ret)
118            )
119           ;; ((and (listp method)(stringp (car method)))
120           ;;  (mime-activate-external-method entity ret)
121           ;;  )
122           (t
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))
129            ))
130     ))
131
132
133 ;;; @ external decoder
134 ;;;
135
136 (defvar mime-mailcap-method-filename-alist nil)
137
138 (defvar mime-mailcap-method-messages-coding-system 'undecided
139   "Coding system for an external mailcap method.")
140
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
145           (if (and name (not (string= name "")))
146               (expand-file-name name temporary-file-directory)
147             (make-temp-name
148              (expand-file-name "EMI" temporary-file-directory))
149             ))
150     (mime-write-entity-content entity name)
151     (message "External method is starting...")
152     (let ((process
153            (let ((command
154                   (mime-format-mailcap-command
155                    method
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              )))
162       (set-alist 'mime-mailcap-method-filename-alist process name)
163       (set-process-sentinel process 'mime-mailcap-method-sentinel)
164       )
165     ))
166
167 (defun mime-mailcap-method-sentinel (process event)
168   (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
169     (if (file-exists-p file)
170         (delete-file file)
171       ))
172   (remove-alist 'mime-mailcap-method-filename-alist process)
173   (message (format "%s %s" process event)))
174
175 (defvar mime-echo-window-is-shared-with-bbdb
176   (module-installed-p 'bbdb)
177   "*If non-nil, mime-echo window is shared with BBDB window.")
178
179 (defvar mime-echo-window-height
180   (function
181    (lambda ()
182      (/ (window-height) 5)
183      ))
184   "*Size of mime-echo window.
185 It allows function or integer.  If it is function,
186 `mime-show-echo-buffer' calls it to get height of mime-echo window.
187 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
188 window.")
189
190 (defun mime-show-echo-buffer (&rest forms)
191   "Show mime-echo buffer to display MIME-playing information."
192   (get-buffer-create mime-echo-buffer-name)
193   (let ((the-win (selected-window))
194         (win (get-buffer-window mime-echo-buffer-name)))
195     (unless win
196       (unless (and mime-echo-window-is-shared-with-bbdb
197                    (condition-case nil
198                        (setq win (get-buffer-window bbdb-buffer-name))
199                      (error nil)))
200         (select-window (get-buffer-window (or mime-preview-buffer
201                                               (current-buffer))))
202         (setq win (split-window-vertically
203                    (- (window-height)
204                       (if (functionp mime-echo-window-height)
205                           (funcall mime-echo-window-height)
206                         mime-echo-window-height)
207                       )))
208         )
209       (set-window-buffer win mime-echo-buffer-name)
210       )
211     (select-window win)
212     (goto-char (point-max))
213     (if forms
214         (let ((buffer-read-only nil))
215           (insert (apply (function format) forms))
216           ))
217     (select-window the-win)
218     ))
219
220
221 ;;; @ file name
222 ;;;
223
224 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
225
226 (defvar mime-view-file-name-regexp-1
227   (concat mime-view-file-name-char-regexp "+\\."
228           mime-view-file-name-char-regexp "+"))
229
230 (defvar mime-view-file-name-regexp-2
231   (concat (regexp-* mime-view-file-name-char-regexp)
232           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
233
234 (defun mime-entity-safe-filename (entity)
235   (let ((filename
236          (or (mime-entity-filename entity)
237              (let ((subj
238                     (or (mime-entity-read-field entity 'Content-Description)
239                         (mime-entity-read-field entity 'Subject))))
240                (if (and subj
241                         (or (string-match mime-view-file-name-regexp-1 subj)
242                             (string-match mime-view-file-name-regexp-2 subj)))
243                    (substring subj (match-beginning 0)(match-end 0))
244                  )))))
245     (if filename
246         (replace-as-filename filename)
247       )))
248
249
250 ;;; @ file extraction
251 ;;;
252
253 (defun mime-save-content (entity situation)
254   (let ((name (or (mime-entity-safe-filename entity)
255                   (format "%s" (mime-entity-media-type entity))))
256         (dir (if (eq t mime-save-directory)
257                  default-directory
258                mime-save-directory))
259         filename)
260     (setq filename (read-file-name
261                     (concat "File name: (default "
262                             (file-name-nondirectory name) ") ")
263                     dir
264                     (concat (file-name-as-directory dir)
265                             (file-name-nondirectory name))))
266     (if (file-directory-p filename)
267         (setq filename (concat (file-name-as-directory filename)
268                                (file-name-nondirectory name))))
269     (if (file-exists-p filename)
270         (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
271             (error "")))
272     (mime-write-entity-content entity (expand-file-name filename))
273     ))
274
275
276 ;;; @ file detection
277 ;;;
278
279 (defvar mime-magic-type-alist
280   '(("^\377\330\377[\340\356]..JFIF"    image jpeg)
281     ("^\211PNG"                         image png)
282     ("^GIF8[79]"                        image gif)
283     ("^II\\*\000"                       image tiff)
284     ("^MM\000\\*"                       image tiff)
285     ("^MThd"                            audio midi)
286     ("^\000\000\001\263"                video mpeg)
287     )
288   "*Alist of regexp about magic-number vs. corresponding media-types.
289 Each element looks like (REGEXP TYPE SUBTYPE).
290 REGEXP is a regular expression to match against the beginning of the
291 content of entity.
292 TYPE is symbol to indicate primary type of media-type.
293 SUBTYPE is symbol to indicate subtype of media-type.")
294
295 (defun mime-detect-content (entity situation)
296   (let (type subtype)
297     (let ((mdata (mime-entity-content entity))
298           (rest mime-magic-type-alist))
299       (while (not (let ((cell (car rest)))
300                     (if cell
301                         (if (string-match (car cell) mdata)
302                             (setq type (nth 1 cell)
303                                   subtype (nth 2 cell))
304                           )
305                       t)))
306         (setq rest (cdr rest))))
307     (setq situation (del-alist 'method (copy-alist situation)))
308     (mime-play-entity entity
309                       (if type
310                           (put-alist 'type type
311                                      (put-alist 'subtype subtype
312                                                 situation))
313                         situation)
314                       'mime-detect-content)))
315
316
317 ;;; @ mail/news message
318 ;;;
319
320 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
321   "Quitting method for mime-view.
322 It is registered to variable `mime-preview-quitting-method-alist'."
323   (let ((mother mime-mother-buffer)
324         (win-conf mime-preview-original-window-configuration))
325     (if (and (boundp 'mime-view-temp-message-buffer)
326              (buffer-live-p mime-view-temp-message-buffer))
327         (kill-buffer mime-view-temp-message-buffer))
328     (mime-preview-kill-buffer)
329     (set-window-configuration win-conf)
330     (pop-to-buffer mother)))
331
332 (defun mime-view-message/rfc822 (entity situation)
333   (let* ((new-name
334           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
335          (mother (current-buffer))
336          (children (car (mime-entity-children entity)))
337          (preview-buffer
338           (mime-display-message
339            children new-name mother nil
340            (cdr (assq 'major-mode
341                       (get-text-property (point) 'mime-view-situation))))))
342     (or (get-buffer-window preview-buffer)
343         (let ((m-win (get-buffer-window mother)))
344           (if m-win
345               (set-window-buffer m-win preview-buffer)
346             (switch-to-buffer preview-buffer)
347             )))))
348
349
350 ;;; @ message/partial
351 ;;;
352
353 (defun mime-store-message/partial-piece (entity cal)
354   (let* ((root-dir
355           (expand-file-name
356            (concat "m-prts-" (user-login-name)) temporary-file-directory))
357          (id (cdr (assoc "id" cal)))
358          (number (cdr (assoc "number" cal)))
359          (total (cdr (assoc "total" cal)))
360          file
361          (mother (current-buffer)))
362     (or (file-exists-p root-dir)
363         (make-directory root-dir))
364     (setq id (replace-as-filename id))
365     (setq root-dir (concat root-dir "/" id))
366     (or (file-exists-p root-dir)
367         (make-directory root-dir))
368     (setq file (concat root-dir "/FULL"))
369     (if (file-exists-p file)
370         (let ((full-buf (get-buffer-create "FULL"))
371               (pwin (or (get-buffer-window mother)
372                         (get-largest-window)))
373               pbuf)
374           (save-window-excursion
375             (set-buffer full-buf)
376             (erase-buffer)
377             (binary-insert-encoded-file file)
378             (setq major-mode 'mime-show-message-mode)
379             (mime-view-buffer (current-buffer) nil mother)
380             (setq pbuf (current-buffer))
381             (make-local-variable 'mime-view-temp-message-buffer)
382             (setq mime-view-temp-message-buffer full-buf))
383           (set-window-buffer pwin pbuf)
384           (select-window pwin))
385       (setq file (concat root-dir "/" number))
386       (mime-write-entity-body entity file)
387       (let ((total-file (concat root-dir "/CT")))
388         (setq total
389               (if total
390                   (progn
391                     (or (file-exists-p total-file)
392                         (save-excursion
393                           (set-buffer
394                            (get-buffer-create mime-temp-buffer-name))
395                           (erase-buffer)
396                           (insert total)
397                           (write-region (point-min)(point-max) total-file)
398                           (kill-buffer (current-buffer))
399                           ))
400                     (string-to-number total)
401                     )
402                 (and (file-exists-p total-file)
403                      (save-excursion
404                        (set-buffer (find-file-noselect total-file))
405                        (prog1
406                            (and (re-search-forward "[0-9]+" nil t)
407                                 (string-to-number
408                                  (buffer-substring (match-beginning 0)
409                                                    (match-end 0)))
410                                 )
411                          (kill-buffer (current-buffer))
412                          )))
413                 )))
414       (if (and total (> total 0)
415                (>= (length (directory-files root-dir nil "^[0-9]+$" t))
416                    total))
417           (catch 'tag
418             (save-excursion
419               (set-buffer (get-buffer-create mime-temp-buffer-name))
420               (let ((full-buf (current-buffer)))
421                 (erase-buffer)
422                 (let ((i 1))
423                   (while (<= i total)
424                     (setq file (concat root-dir "/" (int-to-string i)))
425                     (or (file-exists-p file)
426                         (throw 'tag nil)
427                         )
428                     (binary-insert-encoded-file file)
429                     (goto-char (point-max))
430                     (setq i (1+ i))))
431                 (binary-write-decoded-region
432                  (point-min)(point-max)
433                  (expand-file-name "FULL" root-dir))
434                 (let ((i 1))
435                   (while (<= i total)
436                     (let ((file (format "%s/%d" root-dir i)))
437                       (and (file-exists-p file)
438                            (delete-file file)))
439                     (setq i (1+ i))))
440                 (let ((file (expand-file-name "CT" root-dir)))
441                   (and (file-exists-p file)
442                        (delete-file file)))
443                 (let ((buf (current-buffer))
444                       (pwin (or (get-buffer-window mother)
445                                 (get-largest-window)))
446                       (pbuf (mime-display-message
447                              (mime-open-entity 'buffer (current-buffer))
448                              nil mother nil 'mime-show-message-mode)))
449                   (with-current-buffer pbuf
450                     (make-local-variable 'mime-view-temp-message-buffer)
451                     (setq mime-view-temp-message-buffer buf))
452                   (set-window-buffer pwin pbuf)
453                   (select-window pwin)
454                   )))))
455       )))
456
457
458 ;;; @ message/external-body
459 ;;;
460
461 (defvar mime-raw-dired-function
462   (if (and (>= emacs-major-version 19) window-system)
463       (function dired-other-frame)
464     (function mime-raw-dired-function-for-one-frame)
465     ))
466
467 (defun mime-raw-dired-function-for-one-frame (dir)
468   (let ((win (or (get-buffer-window mime-preview-buffer)
469                  (get-largest-window))))
470     (select-window win)
471     (dired dir)
472     ))
473
474 (defun mime-view-message/external-anon-ftp (entity cal)
475   (let* ((site (cdr (assoc "site" cal)))
476          (directory (cdr (assoc "directory" cal)))
477          (name (cdr (assoc "name" cal)))
478          (pathname (concat "/anonymous@" site ":" directory)))
479     (message (concat "Accessing " (expand-file-name name pathname) " ..."))
480     (funcall mime-raw-dired-function pathname)
481     (goto-char (point-min))
482     (search-forward name)
483     ))
484
485 (defvar mime-raw-browse-url-function mime-browse-url-function)
486
487 (defun mime-view-message/external-url (entity cal)
488   (let ((url (cdr (assoc "url" cal))))
489     (message (concat "Accessing " url " ..."))
490     (funcall mime-raw-browse-url-function url)))
491
492
493 ;;; @ rot13-47
494 ;;;
495
496 (defun mime-view-caesar (entity situation)
497   "Internal method for mime-view to display ROT13-47-48 message."
498   (let ((buf (get-buffer-create
499               (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
500     (with-current-buffer buf
501       (setq buffer-read-only nil)
502       (erase-buffer)
503       (mime-insert-text-content entity)
504       (mule-caesar-region (point-min) (point-max))
505       (set-buffer-modified-p nil)
506       )
507     (let ((win (get-buffer-window (current-buffer))))
508       (or (eq (selected-window) win)
509           (select-window (or win (get-largest-window)))
510           ))
511     (view-buffer buf)
512     (goto-char (point-min))
513     ))
514
515
516 ;;; @ end
517 ;;;
518
519 (provide 'mime-play)
520
521 ;;; mime-play.el ends here