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