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