b5f1ac09cc33ea53cbbbe2538543f6fba77d5975
[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)
77   (or mode
78       (setq mode "play"))
79   (let ((entity (get-text-property (point) 'mime-view-entity)))
80     (if entity
81         (let ((the-buf (current-buffer))
82               (raw-buffer (mime-entity-buffer entity)))
83           (setq mime-preview-after-decoded-position (point))
84           (set-buffer raw-buffer)
85           (mime-raw-play-entity entity mode)
86           (when (eq (current-buffer) raw-buffer)
87             (set-buffer the-buf)
88             (goto-char mime-preview-after-decoded-position)
89             )))))
90
91 (defun mime-sort-situation (situation)
92   (sort situation
93         #'(lambda (a b)
94             (let ((a-t (car a))
95                   (b-t (car b))
96                   (order '((type . 1)
97                            (subtype . 2)
98                            (mode . 3)
99                            (method . 4)
100                            (major-mode . 5)
101                            (disposition-type . 6)
102                            ))
103                   a-order b-order)
104               (if (symbolp a-t)
105                   (let ((ret (assq a-t order)))
106                     (if ret
107                         (setq a-order (cdr ret))
108                       (setq a-order 7)
109                       ))
110                 (setq a-order 8)
111                 )
112               (if (symbolp b-t)
113                   (let ((ret (assq b-t order)))
114                     (if ret
115                         (setq b-order (cdr ret))
116                       (setq b-order 7)
117                       ))
118                 (setq b-order 8)
119                 )
120               (if (= a-order b-order)
121                   (string< (format "%s" a-t)(format "%s" b-t))
122                 (< a-order b-order))
123               )))
124   )
125
126 (defsubst mime-delq-null-situation (situations field)
127   (let (dest)
128     (while situations
129       (let ((situation (car situations)))
130         (if (assq field situation)
131             (setq dest (cons situation dest))
132           ))
133       (setq situations (cdr situations)))
134     dest))
135
136 (defun mime-raw-play-entity (entity &optional mode situation)
137   "Play entity specified by ENTITY.
138 It decodes the entity to call internal or external method.  The method
139 is selected from variable `mime-acting-condition'.  If MODE is
140 specified, play as it.  Default MODE is \"play\"."
141   (let (method ret)
142     (or situation
143         (setq situation (mime-entity-situation entity)))
144     (if mode
145         (setq situation (cons (cons 'mode mode) situation))
146       )
147     (setq ret
148           (or (ctree-match-calist mime-acting-situation-examples situation)
149               (ctree-match-calist-partially mime-acting-situation-examples
150                                             situation)
151               situation))
152     (setq ret
153           (or (mime-delq-null-situation
154                (ctree-find-calist mime-acting-condition ret
155                                   mime-view-find-every-acting-situation)
156                'method)
157               (mime-delq-null-situation
158                (ctree-find-calist mime-acting-condition situation
159                                   mime-view-find-every-acting-situation)
160                'method)
161               ))
162     (cond ((cdr ret)
163            (setq ret (select-menu-alist
164                       "Methods"
165                       (mapcar (function
166                                (lambda (situation)
167                                  (cons
168                                   (format "%s"
169                                           (cdr (assq 'method situation)))
170                                   situation)))
171                               ret)))
172            (setq ret (mime-sort-situation ret))
173            (ctree-set-calist-strictly 'mime-acting-situation-examples ret)
174            )
175           (t
176            (setq ret (car ret))
177            ))
178     (setq method (cdr (assq 'method ret)))
179     (cond ((and (symbolp method)
180                 (fboundp method))
181            (funcall method entity ret)
182            )
183           ((stringp method)
184            (mime-activate-mailcap-method entity ret)
185            )
186           ;; ((and (listp method)(stringp (car method)))
187           ;;  (mime-activate-external-method entity ret)
188           ;;  )
189           (t
190            (mime-show-echo-buffer "No method are specified for %s\n"
191                                   (mime-entity-type/subtype entity))
192            ))
193     ))
194
195
196 ;;; @ external decoder
197 ;;;
198
199 (defvar mime-mailcap-method-filename-alist nil)
200
201 (defun mime-activate-mailcap-method (entity situation)
202   (save-excursion
203     (save-restriction
204       (let ((start (mime-entity-point-min entity))
205             (end (mime-entity-point-max entity)))
206         (narrow-to-region start end)
207         (goto-char start)
208         (let ((method (cdr (assoc 'method situation)))
209               (name (mime-entity-safe-filename entity)))
210           (setq name
211                 (if name
212                     (expand-file-name name mime-temp-directory)
213                   (make-temp-name
214                    (expand-file-name "EMI" mime-temp-directory))
215                   ))
216           (mime-write-decoded-region (mime-entity-body-start entity) end
217                                      name (cdr (assq 'encoding situation)))
218           (message "External method is starting...")
219           (let ((process
220                  (let ((command
221                         (mailcap-format-command
222                          method
223                          (cons (cons 'filename name) situation))))
224                    (start-process command mime-echo-buffer-name
225                                   shell-file-name shell-command-switch command)
226                    )))
227             (set-alist 'mime-mailcap-method-filename-alist process name)
228             (set-process-sentinel process 'mime-mailcap-method-sentinel)
229             )
230           )))))
231
232 (defun mime-mailcap-method-sentinel (process event)
233   (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
234     (if (file-exists-p file)
235         (delete-file file)
236       ))
237   (remove-alist 'mime-mailcap-method-filename-alist process)
238   (message (format "%s %s" process event)))
239
240 (defvar mime-echo-window-is-shared-with-bbdb t
241   "*If non-nil, mime-echo window is shared with BBDB window.")
242
243 (defvar mime-echo-window-height
244   (function
245    (lambda ()
246      (/ (window-height) 5)
247      ))
248   "*Size of mime-echo window.
249 It allows function or integer.  If it is function,
250 `mime-show-echo-buffer' calls it to get height of mime-echo window.
251 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
252 window.")
253
254 (defun mime-show-echo-buffer (&rest forms)
255   "Show mime-echo buffer to display MIME-playing information."
256   (get-buffer-create mime-echo-buffer-name)
257   (let ((the-win (selected-window))
258         (win (get-buffer-window mime-echo-buffer-name))
259         )
260     (or win
261         (if (and mime-echo-window-is-shared-with-bbdb
262                  (boundp 'bbdb-buffer-name)
263                  (setq win (get-buffer-window bbdb-buffer-name))
264                  )
265             (set-window-buffer win mime-echo-buffer-name)
266           (select-window (get-buffer-window mime-preview-buffer))
267           (setq win (split-window-vertically
268                      (- (window-height)
269                         (if (functionp mime-echo-window-height)
270                             (funcall mime-echo-window-height)
271                           mime-echo-window-height)
272                         )))
273           (set-window-buffer win mime-echo-buffer-name)
274           ))
275     (select-window win)
276     (goto-char (point-max))
277     (if forms
278         (insert (apply (function format) forms))
279       )
280     (select-window the-win)
281     ))
282
283
284 ;;; @ file name
285 ;;;
286
287 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
288
289 (defvar mime-view-file-name-regexp-1
290   (concat mime-view-file-name-char-regexp "+\\."
291           mime-view-file-name-char-regexp "+"))
292
293 (defvar mime-view-file-name-regexp-2
294   (concat (regexp-* mime-view-file-name-char-regexp)
295           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
296
297 (defun mime-entity-safe-filename (entity)
298   (replace-as-filename
299    (or (mime-entity-filename entity)
300        (let ((ret (or (mime-entity-read-field entity 'Content-Description)
301                       (mime-entity-read-field entity 'Subject))))
302          (if (or (string-match mime-view-file-name-regexp-1 ret)
303                  (string-match mime-view-file-name-regexp-2 ret))
304              (substring ret (match-beginning 0)(match-end 0))
305            )))))
306
307
308 ;;; @ file extraction
309 ;;;
310
311 (defun mime-save-content (entity situation)
312   (let* ((name (mime-entity-safe-filename entity))
313          (encoding (or (mime-entity-encoding entity) "7bit"))
314          (filename (if (and name (not (string-equal name "")))
315                        (expand-file-name name
316                                          (save-window-excursion
317                                            (call-interactively
318                                             (function
319                                              (lambda (dir)
320                                                (interactive "DDirectory: ")
321                                                dir)))))
322                      (save-window-excursion
323                        (call-interactively
324                         (function
325                          (lambda (file)
326                            (interactive "FFilename: ")
327                            (expand-file-name file)))))))
328          )
329     (if (file-exists-p filename)
330         (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
331             (error "")))
332     (mime-write-decoded-region (mime-entity-body-start entity)
333                                (mime-entity-body-end entity)
334                                filename encoding)
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 cal)
404   (let* ((beg (mime-entity-point-min entity))
405          (end (mime-entity-point-max entity))
406          (cnum (mime-raw-point-to-entity-number beg))
407          (new-name (format "%s-%s" (buffer-name) cnum))
408          (mother mime-preview-buffer)
409          (representation-type
410           (cdr (or (assq major-mode mime-raw-representation-type-alist)
411                    (assq t mime-raw-representation-type-alist))))
412          str)
413     (setq str (buffer-substring beg end))
414     (switch-to-buffer new-name)
415     (erase-buffer)
416     (insert str)
417     (goto-char (point-min))
418     (if (re-search-forward "^\n" nil t)
419         (delete-region (point-min) (match-end 0))
420       )
421     (setq major-mode 'mime-show-message-mode)
422     (setq mime-raw-representation-type representation-type)
423     (mime-view-mode mother)
424     ))
425
426
427 ;;; @ message/partial
428 ;;;
429
430 (defun mime-raw-write-region (start end filename)
431   "Write current region into specified file.
432 When called from a program, takes three arguments:
433 START, END and FILENAME.  START and END are buffer positions.
434 It refer `mime-raw-representation-type' or `major-mode
435 mime-raw-representation-type-alist'.  If it is `binary', region is
436 saved as binary.  Otherwise the region is saved by `write-region'."
437   (let ((presentation-type
438          (or mime-raw-representation-type
439              (cdr (or (assq major-mode mime-raw-representation-type-alist)
440                       (assq t mime-raw-representation-type-alist))))))
441     (if (eq presentation-type 'binary)
442         (write-region-as-binary start end filename)
443       (write-region start end filename)
444       )))
445
446 (defun mime-store-message/partial-piece (entity cal)
447   (goto-char (mime-entity-point-min entity))
448   (let* ((root-dir
449           (expand-file-name
450            (concat "m-prts-" (user-login-name)) mime-temp-directory))
451          (id (cdr (assoc "id" cal)))
452          (number (cdr (assoc "number" cal)))
453          (total (cdr (assoc "total" cal)))
454          file
455          (mother mime-preview-buffer)
456          )
457     (or (file-exists-p root-dir)
458         (make-directory root-dir)
459         )
460     (setq id (replace-as-filename id))
461     (setq root-dir (concat root-dir "/" id))
462     (or (file-exists-p root-dir)
463         (make-directory root-dir)
464         )
465     (setq file (concat root-dir "/FULL"))
466     (if (file-exists-p file)
467         (let ((full-buf (get-buffer-create "FULL"))
468               (pwin (or (get-buffer-window mother)
469                         (get-largest-window)))
470               )
471           (save-window-excursion
472             (set-buffer full-buf)
473             (erase-buffer)
474             (as-binary-input-file (insert-file-contents file))
475             (setq major-mode 'mime-show-message-mode)
476             (mime-view-mode mother)
477             )
478           (set-window-buffer pwin
479                              (save-excursion
480                                (set-buffer full-buf)
481                                mime-preview-buffer))
482           (select-window pwin)
483           )
484       (re-search-forward "^$")
485       (goto-char (1+ (match-end 0)))
486       (setq file (concat root-dir "/" number))
487       (mime-raw-write-region (point) (mime-entity-point-max entity) file)
488       (let ((total-file (concat root-dir "/CT")))
489         (setq total
490               (if total
491                   (progn
492                     (or (file-exists-p total-file)
493                         (save-excursion
494                           (set-buffer
495                            (get-buffer-create mime-temp-buffer-name))
496                           (erase-buffer)
497                           (insert total)
498                           (write-region (point-min)(point-max) total-file)
499                           (kill-buffer (current-buffer))
500                           ))
501                     (string-to-number total)
502                     )
503                 (and (file-exists-p total-file)
504                      (save-excursion
505                        (set-buffer (find-file-noselect total-file))
506                        (prog1
507                            (and (re-search-forward "[0-9]+" nil t)
508                                 (string-to-number
509                                  (buffer-substring (match-beginning 0)
510                                                    (match-end 0)))
511                                 )
512                          (kill-buffer (current-buffer))
513                          )))
514                 )))
515       (if (and total (> total 0))
516           (catch 'tag
517             (save-excursion
518               (set-buffer (get-buffer-create mime-temp-buffer-name))
519               (let ((full-buf (current-buffer)))
520                 (erase-buffer)
521                 (let ((i 1))
522                   (while (<= i total)
523                     (setq file (concat root-dir "/" (int-to-string i)))
524                     (or (file-exists-p file)
525                         (throw 'tag nil)
526                         )
527                     (as-binary-input-file (insert-file-contents file))
528                     (goto-char (point-max))
529                     (setq i (1+ i))
530                     ))
531                 (as-binary-output-file
532                  (write-region (point-min)(point-max)
533                                (expand-file-name "FULL" root-dir)))
534                 (let ((i 1))
535                   (while (<= i total)
536                     (let ((file (format "%s/%d" root-dir i)))
537                       (and (file-exists-p file)
538                            (delete-file file)
539                            ))
540                     (setq i (1+ i))
541                     ))
542                 (let ((file (expand-file-name "CT" root-dir)))
543                   (and (file-exists-p file)
544                        (delete-file file)
545                        ))
546                 (save-window-excursion
547                   (setq major-mode 'mime-show-message-mode)
548                   (mime-view-mode mother)
549                   )
550                 (let ((pwin (or (get-buffer-window mother)
551                                 (get-largest-window)
552                                 ))
553                       (pbuf (save-excursion
554                               (set-buffer full-buf)
555                               mime-preview-buffer)))
556                   (set-window-buffer pwin pbuf)
557                   (select-window pwin)
558                   )))))
559       )))
560
561
562 ;;; @ message/external-body
563 ;;;
564
565 (defvar mime-raw-dired-function
566   (if (and (>= emacs-major-version 19) window-system)
567       (function dired-other-frame)
568     (function mime-raw-dired-function-for-one-frame)
569     ))
570
571 (defun mime-raw-dired-function-for-one-frame (dir)
572   (let ((win (or (get-buffer-window mime-preview-buffer)
573                  (get-largest-window))))
574     (select-window win)
575     (dired dir)
576     ))
577
578 (defun mime-view-message/external-ftp (entity cal)
579   (let* ((site (cdr (assoc "site" cal)))
580          (directory (cdr (assoc "directory" cal)))
581          (name (cdr (assoc "name" cal)))
582          (pathname (concat "/anonymous@" site ":" directory)))
583     (message (concat "Accessing " (expand-file-name name pathname) "..."))
584     (funcall mime-raw-dired-function pathname)
585     (goto-char (point-min))
586     (search-forward name)
587     ))
588
589
590 ;;; @ rot13-47
591 ;;;
592
593 (defun mime-view-caesar (entity situation)
594   "Internal method for mime-view to display ROT13-47-48 message."
595   (let* ((new-name (format "%s-%s" (buffer-name)
596                            (mime-entity-number entity)))
597          (mother mime-preview-buffer))
598     (let ((pwin (or (get-buffer-window mother)
599                     (get-largest-window)))
600           (buf (get-buffer-create new-name)))
601       (set-window-buffer pwin buf)
602       (set-buffer buf)
603       (select-window pwin)
604       )
605     (setq buffer-read-only nil)
606     (erase-buffer)
607     (mime-text-insert-decoded-body entity)
608     (mule-caesar-region (point-min) (point-max))
609     (set-buffer-modified-p nil)
610     (set-buffer mother)
611     (view-buffer new-name)
612     ))
613
614
615 ;;; @ end
616 ;;;
617
618 (provide 'mime-play)
619
620 (let* ((file mime-acting-situation-examples-file)
621        (buffer (get-buffer-create " *mime-example*")))
622   (if (file-readable-p file)
623       (unwind-protect
624           (save-excursion
625             (set-buffer buffer)
626             (erase-buffer)
627             (insert-file-contents file)
628             (eval-buffer)
629             ;; format check
630             (or (eq (car mime-acting-situation-examples) 'type)
631                 (setq mime-acting-situation-examples nil))
632             )
633         (kill-buffer buffer))))
634
635 ;;; mime-play.el ends here