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