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