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