Merge remi-1_8.
[elisp/semi.git] / mime-play.el
1 ;;; mime-play.el --- Playback processing module for mime-view.el
2
3 ;; Copyright (C) 1994,1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
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   (require 'mime-text)
35   (condition-case nil
36       (require 'bbdb)
37     (error (defvar bbdb-buffer-name nil)))
38   )
39
40 (defvar mime-acting-situation-examples nil)
41
42 (defun mime-save-acting-situation-examples ()
43   (let* ((file mime-acting-situation-examples-file)
44          (buffer (get-buffer-create " *mime-example*")))
45     (unwind-protect
46         (save-excursion
47           (set-buffer buffer)
48           (setq buffer-file-name file)
49           (erase-buffer)
50           (insert ";;; " (file-name-nondirectory file) "\n")
51           (insert "\n;; This file is generated automatically by "
52                   mime-view-version-string "\n\n")
53           (insert ";;; Code:\n\n")
54           (pp `(setq mime-acting-situation-examples
55                      ',mime-acting-situation-examples)
56               (current-buffer))
57           (insert "\n;;; "
58                   (file-name-nondirectory file)
59                   " ends here.\n")
60           (save-buffer))
61       (kill-buffer buffer))))
62
63 (add-hook 'kill-emacs-hook 'mime-save-acting-situation-examples)
64
65   
66 ;;; @ content decoder
67 ;;;
68
69 (defvar mime-preview-after-decoded-position nil)
70
71 (defun mime-preview-play-current-entity (&optional mode)
72   "Play current entity.
73 It decodes current entity to call internal or external method.  The
74 method is selected from variable `mime-acting-condition'.
75 If MODE is specified, play as it.  Default MODE is \"play\"."
76   (interactive (list "play"))
77   (let ((entity (get-text-property (point) 'mime-view-entity)))
78     (if entity
79         (let ((the-buf (current-buffer))
80               (raw-buffer (mime-entity-buffer entity)))
81           (setq mime-preview-after-decoded-position (point))
82           (set-buffer raw-buffer)
83           (mime-raw-play-entity entity mode)
84           (when (eq (current-buffer) raw-buffer)
85             (set-buffer the-buf)
86             (goto-char mime-preview-after-decoded-position)
87             )))))
88
89 (defun mime-sort-situation (situation)
90   (sort situation
91         #'(lambda (a b)
92             (let ((a-t (car a))
93                   (b-t (car b))
94                   (order '((type . 1)
95                            (subtype . 2)
96                            (mode . 3)
97                            (method . 4)
98                            (major-mode . 5)
99                            (disposition-type . 6)
100                            ))
101                   a-order b-order)
102               (if (symbolp a-t)
103                   (let ((ret (assq a-t order)))
104                     (if ret
105                         (setq a-order (cdr ret))
106                       (setq a-order 7)
107                       ))
108                 (setq a-order 8)
109                 )
110               (if (symbolp b-t)
111                   (let ((ret (assq b-t order)))
112                     (if ret
113                         (setq b-order (cdr ret))
114                       (setq b-order 7)
115                       ))
116                 (setq b-order 8)
117                 )
118               (if (= a-order b-order)
119                   (string< (format "%s" a-t)(format "%s" b-t))
120                 (< a-order b-order))
121               )))
122   )
123
124 (defsubst mime-delq-null-situation (situations field)
125   (let (dest)
126     (while situations
127       (let ((situation (car situations)))
128         (if (assq field situation)
129             (setq dest (cons situation dest))
130           ))
131       (setq situations (cdr situations)))
132     dest))
133
134 (defun mime-raw-play-entity (entity &optional mode situation)
135   "Play entity specified by ENTITY.
136 It decodes the entity to call internal or external method.  The method
137 is selected from variable `mime-acting-condition'.  If MODE is
138 specified, play as it.  Default MODE is \"play\"."
139   (let (method ret)
140     (or situation
141         (setq situation (mime-entity-situation entity)))
142     (if mode
143         (setq situation (cons (cons 'mode mode) situation))
144       )
145     (setq ret
146           (or (ctree-match-calist mime-acting-situation-examples situation)
147               (ctree-match-calist-partially mime-acting-situation-examples
148                                             situation)
149               situation))
150     (setq ret
151           (or (mime-delq-null-situation
152                (ctree-find-calist mime-acting-condition ret
153                                   mime-view-find-every-acting-situation)
154                'method)
155               (mime-delq-null-situation
156                (ctree-find-calist mime-acting-condition situation
157                                   mime-view-find-every-acting-situation)
158                'method)
159               ))
160     (cond ((cdr ret)
161            (setq ret (select-menu-alist
162                       "Methods"
163                       (mapcar (function
164                                (lambda (situation)
165                                  (cons
166                                   (format "%s"
167                                           (cdr (assq 'method situation)))
168                                   situation)))
169                               ret)))
170            (setq ret (mime-sort-situation ret))
171            (ctree-set-calist-strictly 'mime-acting-situation-examples ret)
172            )
173           (t
174            (setq ret (car ret))
175            ))
176     (setq method (cdr (assq 'method ret)))
177     (cond ((and (symbolp method)
178                 (fboundp method))
179            (funcall method entity ret)
180            )
181           ((stringp method)
182            (mime-activate-mailcap-method entity ret)
183            )
184           ;; ((and (listp method)(stringp (car method)))
185           ;;  (mime-activate-external-method entity ret)
186           ;;  )
187           (t
188            (mime-show-echo-buffer "No method are specified for %s\n"
189                                   (mime-entity-type/subtype entity))
190            ))
191     ))
192
193
194 ;;; @ external decoder
195 ;;;
196
197 (defvar mime-mailcap-method-filename-alist nil)
198
199 (defun mime-activate-mailcap-method (entity situation)
200   (save-excursion
201     (save-restriction
202       (let ((start (mime-entity-point-min entity))
203             (end (mime-entity-point-max entity)))
204         (narrow-to-region start end)
205         (goto-char start)
206         (let ((method (cdr (assoc 'method situation)))
207               (name (mime-entity-safe-filename entity)))
208           (setq name
209                 (if name
210                     (expand-file-name name mime-temp-directory)
211                   (make-temp-name
212                    (expand-file-name "EMI" mime-temp-directory))
213                   ))
214           (mime-write-decoded-region (mime-entity-body-start entity) end
215                                      name (cdr (assq 'encoding situation)))
216           (message "External method is starting...")
217           (let ((process
218                  (let ((command
219                         (mailcap-format-command
220                          method
221                          (cons (cons 'filename name) situation))))
222                    (start-process command mime-echo-buffer-name
223                                   shell-file-name shell-command-switch command)
224                    )))
225             (set-alist 'mime-mailcap-method-filename-alist process name)
226             (set-process-sentinel process 'mime-mailcap-method-sentinel)
227             )
228           )))))
229
230 (defun mime-mailcap-method-sentinel (process event)
231   (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
232     (if (file-exists-p file)
233         (delete-file file)
234       ))
235   (remove-alist 'mime-mailcap-method-filename-alist process)
236   (message (format "%s %s" process event)))
237
238 (defvar mime-echo-window-is-shared-with-bbdb t
239   "*If non-nil, mime-echo window is shared with BBDB window.")
240
241 (defvar mime-echo-window-height
242   (function
243    (lambda ()
244      (/ (window-height) 5)
245      ))
246   "*Size of mime-echo window.
247 It allows function or integer.  If it is function,
248 `mime-show-echo-buffer' calls it to get height of mime-echo window.
249 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
250 window.")
251
252 (defun mime-show-echo-buffer (&rest forms)
253   "Show mime-echo buffer to display MIME-playing information."
254   (get-buffer-create mime-echo-buffer-name)
255   (let ((the-win (selected-window))
256         (win (get-buffer-window mime-echo-buffer-name))
257         )
258     (or win
259         (if (and mime-echo-window-is-shared-with-bbdb
260                  (boundp 'bbdb-buffer-name)
261                  (setq win (get-buffer-window bbdb-buffer-name))
262                  )
263             (set-window-buffer win mime-echo-buffer-name)
264           (select-window (get-buffer-window mime-preview-buffer))
265           (setq win (split-window-vertically
266                      (- (window-height)
267                         (if (functionp mime-echo-window-height)
268                             (funcall mime-echo-window-height)
269                           mime-echo-window-height)
270                         )))
271           (set-window-buffer win mime-echo-buffer-name)
272           ))
273     (select-window win)
274     (goto-char (point-max))
275     (if forms
276         (insert (apply (function format) forms))
277       )
278     (select-window the-win)
279     ))
280
281
282 ;;; @ file name
283 ;;;
284
285 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
286
287 (defvar mime-view-file-name-regexp-1
288   (concat mime-view-file-name-char-regexp "+\\."
289           mime-view-file-name-char-regexp "+"))
290
291 (defvar mime-view-file-name-regexp-2
292   (concat (regexp-* mime-view-file-name-char-regexp)
293           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
294
295 (defun mime-entity-safe-filename (entity)
296   (let ((filename
297          (or (mime-entity-filename entity)
298              (let ((subj
299                     (or (mime-read-field 'Content-Description entity)
300                         (mime-read-field 'Subject entity))))
301                (if (and subj
302                         (or (string-match mime-view-file-name-regexp-1 subj)
303                             (string-match mime-view-file-name-regexp-2 subj)))
304                    (substring subj (match-beginning 0)(match-end 0))
305                  )))))
306     (if filename
307         (replace-as-filename filename)
308       )))
309
310
311 ;;; @ file extraction
312 ;;;
313
314 (defun mime-save-content (entity situation)
315   (let* ((name (mime-entity-safe-filename entity))
316          (filename (if (and name (not (string-equal name "")))
317                        (expand-file-name name
318                                          (save-window-excursion
319                                            (call-interactively
320                                             (function
321                                              (lambda (dir)
322                                                (interactive "DDirectory: ")
323                                                dir)))))
324                      (save-window-excursion
325                        (call-interactively
326                         (function
327                          (lambda (file)
328                            (interactive "FFilename: ")
329                            (expand-file-name file)))))))
330          )
331     (if (file-exists-p filename)
332         (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
333             (error "")))
334     (mime-write-entity-content entity filename)
335     ))
336
337
338 ;;; @ file detection
339 ;;;
340
341 (defvar mime-file-content-type-alist
342   '(("JPEG"     image jpeg)
343     ("GIF"      image gif)
344     )
345   "*Alist of \"file\" output patterns vs. corresponding media-types.
346 Each element looks like (REGEXP TYPE SUBTYPE).
347 REGEXP is pattern for \"file\" command output.
348 TYPE is symbol to indicate primary type of media-type.
349 SUBTYPE is symbol to indicate subtype of media-type.")
350
351 (defun mime-detect-content (entity situation)
352   (let ((beg (mime-entity-point-min entity))
353         (end (mime-entity-point-max entity)))
354     (goto-char beg)
355     (let* ((name (save-restriction
356                    (narrow-to-region beg end)
357                    (mime-entity-safe-filename entity)
358                    ))
359            (encoding (or (cdr (assq 'encoding situation)) "7bit"))
360            (filename (if (and name (not (string-equal name "")))
361                          (expand-file-name name mime-temp-directory)
362                        (make-temp-name
363                         (expand-file-name "EMI" mime-temp-directory)))))
364       (mime-write-decoded-region (mime-entity-body-start entity) end
365                                  filename encoding)
366       (let (type subtype)
367         (with-temp-buffer
368           (call-process "file" nil t nil filename)
369           (goto-char (point-min))
370           (if (search-forward (concat filename ": ") nil t)
371               (let ((rest mime-file-content-type-alist))
372                 (while (not (let ((cell (car rest)))
373                               (if (looking-at (car cell))
374                                   (setq type (nth 1 cell)
375                                         subtype (nth 2 cell))
376                                 )))
377                   (setq rest (cdr rest))))))
378         (if type
379             (mime-raw-play-entity
380              entity "play"
381              (put-alist 'type type
382                         (put-alist 'subtype subtype
383                                    (mime-entity-situation entity))))
384           ))
385       )))
386
387
388 ;;; @ mail/news message
389 ;;;
390
391 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
392   "Quitting method for mime-view.
393 It is registered to variable `mime-preview-quitting-method-alist'."
394   (let ((mother mime-mother-buffer)
395         (win-conf mime-preview-original-window-configuration)
396         )
397     (kill-buffer mime-raw-buffer)
398     (mime-preview-kill-buffer)
399     (set-window-configuration win-conf)
400     (pop-to-buffer mother)
401     ))
402
403 (defun mime-view-message/rfc822 (entity situation)
404   (let* ((new-name
405           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
406          (mother mime-preview-buffer)
407          (children (car (mime-entity-children entity))))
408     (set-buffer (get-buffer-create new-name))
409     (erase-buffer)
410     (insert-buffer-substring (mime-entity-buffer children)
411                              (mime-entity-point-min children)
412                              (mime-entity-point-max children))
413     (setq mime-message-structure children)
414     (setq major-mode 'mime-show-message-mode)
415     (mime-view-buffer (current-buffer) nil mother
416                       nil (if (mime-entity-cooked-p entity) 'cooked))
417     ))
418
419
420 ;;; @ message/partial
421 ;;;
422
423 (defun mime-store-message/partial-piece (entity cal)
424   (goto-char (mime-entity-point-min entity))
425   (let* ((root-dir
426           (expand-file-name
427            (concat "m-prts-" (user-login-name)) mime-temp-directory))
428          (id (cdr (assoc "id" cal)))
429          (number (cdr (assoc "number" cal)))
430          (total (cdr (assoc "total" cal)))
431          file
432          (mother mime-preview-buffer)
433          )
434     (or (file-exists-p root-dir)
435         (make-directory root-dir)
436         )
437     (setq id (replace-as-filename id))
438     (setq root-dir (concat root-dir "/" id))
439     (or (file-exists-p root-dir)
440         (make-directory root-dir)
441         )
442     (setq file (concat root-dir "/FULL"))
443     (if (file-exists-p file)
444         (let ((full-buf (get-buffer-create "FULL"))
445               (pwin (or (get-buffer-window mother)
446                         (get-largest-window)))
447               )
448           (save-window-excursion
449             (set-buffer full-buf)
450             (erase-buffer)
451             (as-binary-input-file (insert-file-contents file))
452             (setq major-mode 'mime-show-message-mode)
453             (mime-view-mode mother)
454             )
455           (set-window-buffer pwin
456                              (save-excursion
457                                (set-buffer full-buf)
458                                mime-preview-buffer))
459           (select-window pwin)
460           )
461       (setq file (concat root-dir "/" number))
462       (mime-write-entity-body entity file)
463       (let ((total-file (concat root-dir "/CT")))
464         (setq total
465               (if total
466                   (progn
467                     (or (file-exists-p total-file)
468                         (save-excursion
469                           (set-buffer
470                            (get-buffer-create mime-temp-buffer-name))
471                           (erase-buffer)
472                           (insert total)
473                           (write-region (point-min)(point-max) total-file)
474                           (kill-buffer (current-buffer))
475                           ))
476                     (string-to-number total)
477                     )
478                 (and (file-exists-p total-file)
479                      (save-excursion
480                        (set-buffer (find-file-noselect total-file))
481                        (prog1
482                            (and (re-search-forward "[0-9]+" nil t)
483                                 (string-to-number
484                                  (buffer-substring (match-beginning 0)
485                                                    (match-end 0)))
486                                 )
487                          (kill-buffer (current-buffer))
488                          )))
489                 )))
490       (if (and total (> total 0))
491           (catch 'tag
492             (save-excursion
493               (set-buffer (get-buffer-create mime-temp-buffer-name))
494               (let ((full-buf (current-buffer)))
495                 (erase-buffer)
496                 (let ((i 1))
497                   (while (<= i total)
498                     (setq file (concat root-dir "/" (int-to-string i)))
499                     (or (file-exists-p file)
500                         (throw 'tag nil)
501                         )
502                     (as-binary-input-file (insert-file-contents file))
503                     (goto-char (point-max))
504                     (setq i (1+ i))
505                     ))
506                 (as-binary-output-file
507                  (write-region (point-min)(point-max)
508                                (expand-file-name "FULL" root-dir)))
509                 (let ((i 1))
510                   (while (<= i total)
511                     (let ((file (format "%s/%d" root-dir i)))
512                       (and (file-exists-p file)
513                            (delete-file file)
514                            ))
515                     (setq i (1+ i))
516                     ))
517                 (let ((file (expand-file-name "CT" root-dir)))
518                   (and (file-exists-p file)
519                        (delete-file file)
520                        ))
521                 (save-window-excursion
522                   (setq major-mode 'mime-show-message-mode)
523                   (mime-view-mode mother)
524                   )
525                 (let ((pwin (or (get-buffer-window mother)
526                                 (get-largest-window)
527                                 ))
528                       (pbuf (save-excursion
529                               (set-buffer full-buf)
530                               mime-preview-buffer)))
531                   (set-window-buffer pwin pbuf)
532                   (select-window pwin)
533                   )))))
534       )))
535
536
537 ;;; @ message/external-body
538 ;;;
539
540 (defvar mime-raw-dired-function
541   (if (and (>= emacs-major-version 19) window-system)
542       (function dired-other-frame)
543     (function mime-raw-dired-function-for-one-frame)
544     ))
545
546 (defun mime-raw-dired-function-for-one-frame (dir)
547   (let ((win (or (get-buffer-window mime-preview-buffer)
548                  (get-largest-window))))
549     (select-window win)
550     (dired dir)
551     ))
552
553 (defun mime-view-message/external-anon-ftp (entity cal)
554   (let* ((site (cdr (assoc "site" cal)))
555          (directory (cdr (assoc "directory" cal)))
556          (name (cdr (assoc "name" cal)))
557          (pathname (concat "/anonymous@" site ":" directory)))
558     (message (concat "Accessing " (expand-file-name name pathname) " ..."))
559     (funcall mime-raw-dired-function pathname)
560     (goto-char (point-min))
561     (search-forward name)
562     ))
563
564 (defvar mime-raw-browse-url-function (function mime-browse-url))
565
566 (defun mime-view-message/external-url (entity cal)
567   (let ((url (cdr (assoc "url" cal))))
568     (message (concat "Accessing " url " ..."))
569     (funcall mime-raw-browse-url-function url)))
570
571
572 ;;; @ rot13-47
573 ;;;
574
575 (defun mime-view-caesar (entity situation)
576   "Internal method for mime-view to display ROT13-47-48 message."
577   (let* ((new-name (format "%s-%s" (buffer-name)
578                            (mime-entity-number entity)))
579          (mother mime-preview-buffer))
580     (let ((pwin (or (get-buffer-window mother)
581                     (get-largest-window)))
582           (buf (get-buffer-create new-name)))
583       (set-window-buffer pwin buf)
584       (set-buffer buf)
585       (select-window pwin)
586       )
587     (setq buffer-read-only nil)
588     (erase-buffer)
589     (mime-text-insert-decoded-body entity)
590     (mule-caesar-region (point-min) (point-max))
591     (set-buffer-modified-p nil)
592     (set-buffer mother)
593     (view-buffer new-name)
594     ))
595
596
597 ;;; @ end
598 ;;;
599
600 (provide 'mime-play)
601
602 (let* ((file mime-acting-situation-examples-file)
603        (buffer (get-buffer-create " *mime-example*")))
604   (if (file-readable-p file)
605       (unwind-protect
606           (save-excursion
607             (set-buffer buffer)
608             (erase-buffer)
609             (insert-file-contents file)
610             (eval-buffer)
611             ;; format check
612             (or (eq (car mime-acting-situation-examples) 'type)
613                 (setq mime-acting-situation-examples nil))
614             )
615         (kill-buffer buffer))))
616
617 ;;; mime-play.el ends here