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