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