`mime::article/preview-buffer' -> `mime-view-buffer'.
[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.27 1997-03-18 14:46:40 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-article/show-output-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/output-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-article/show-output-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 (defun mime-article/show-output-buffer (&rest forms)
170   (get-buffer-create mime/output-buffer-name)
171   (let ((the-win (selected-window))
172         (win (get-buffer-window mime/output-buffer-name))
173         )
174     (or win
175         (if (and mime/output-buffer-window-is-shared-with-bbdb
176                  (boundp 'bbdb-buffer-name)
177                  (setq win (get-buffer-window bbdb-buffer-name))
178                  )
179             (set-window-buffer win mime/output-buffer-name)
180           (select-window (get-buffer-window mime-view-buffer))
181           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
182           (set-window-buffer win mime/output-buffer-name)
183           ))
184     (select-window win)
185     (goto-char (point-max))
186     (if forms
187         (insert (apply (function format) forms))
188       )
189     (select-window the-win)
190     ))
191
192
193 ;;; @ file name
194 ;;;
195
196 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
197
198 (defvar mime-view-file-name-regexp-1
199   (concat mime-view-file-name-char-regexp "+\\."
200           mime-view-file-name-char-regexp "+"))
201
202 (defvar mime-view-file-name-regexp-2
203   (concat (regexp-* mime-view-file-name-char-regexp)
204           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
205
206 (defun mime-article/get-original-filename (param &optional encoding)
207   (or (mime-article/get-uu-filename param encoding)
208       (let (ret)
209         (or (if (or (and (setq ret (mime/Content-Disposition))
210                          (setq ret (assoc "filename" (cdr ret)))
211                          )
212                     (setq ret (assoc "name" param))
213                     (setq ret (assoc "x-name" param))
214                     )
215                 (std11-strip-quoted-string (cdr ret))
216               )
217             (if (setq ret
218                       (std11-find-field-body '("Content-Description"
219                                                "Subject")))
220                 (if (or (string-match mime-view-file-name-regexp-1 ret)
221                         (string-match mime-view-file-name-regexp-2 ret))
222                     (substring ret (match-beginning 0)(match-end 0))
223                   ))
224             ))
225       ))
226
227 (defun mime-article/get-filename (param)
228   (replace-as-filename (mime-article/get-original-filename param))
229   )
230
231
232 ;;; @ mail/news message
233 ;;;
234
235 (defun mime-view-quitting-method-for-mime-show-message-mode ()
236   "Quitting method for mime-view.
237 It is registered to variable `mime-view-quitting-method-alist'."
238   (let ((mother mime::preview/mother-buffer)
239         (win-conf mime::preview/original-window-configuration)
240         )
241     (kill-buffer mime::preview/article-buffer)
242     (mime-view-kill-buffer)
243     (set-window-configuration win-conf)
244     (pop-to-buffer mother)
245     ))
246
247 (defun mime-article/view-message/rfc822 (beg end cal)
248   (let* ((cnum (mime-article/point-content-number beg))
249          (new-name (format "%s-%s" (buffer-name) cnum))
250          (mother mime-view-buffer)
251          (text-decoder
252           (cdr (or (assq major-mode mime-text-decoder-alist)
253                    (assq t mime-text-decoder-alist))))
254          str)
255     (setq str (buffer-substring beg end))
256     (switch-to-buffer new-name)
257     (erase-buffer)
258     (insert str)
259     (goto-char (point-min))
260     (if (re-search-forward "^\n" nil t)
261         (delete-region (point-min) (match-end 0))
262       )
263     (setq major-mode 'mime-show-message-mode)
264     (setq mime-text-decoder text-decoder)
265     (mime-view-mode mother)
266     ))
267
268
269 ;;; @ message/partial
270 ;;;
271
272 (defvar mime-article/coding-system-alist
273   (list '(mh-show-mode . no-conversion)
274         (cons t (mime-charset-to-coding-system default-mime-charset))
275         ))
276
277 (defun mime-article::write-region (start end file)
278   (let ((coding-system-for-write
279          (cdr
280           (or (assq major-mode mime-article/coding-system-alist)
281               (assq t mime-article/coding-system-alist)
282               ))))
283     (write-region start end file)
284     ))
285
286 (defun mime-article/decode-message/partial (beg end cal)
287   (goto-char beg)
288   (let* ((root-dir
289           (expand-file-name
290            (concat "m-prts-" (user-login-name)) mime-temp-directory))
291          (id (cdr (assoc "id" cal)))
292          (number (cdr (assoc "number" cal)))
293          (total (cdr (assoc "total" cal)))
294          file
295          (mother mime-view-buffer)
296          )
297     (or (file-exists-p root-dir)
298         (make-directory root-dir)
299         )
300     (setq id (replace-as-filename id))
301     (setq root-dir (concat root-dir "/" id))
302     (or (file-exists-p root-dir)
303         (make-directory root-dir)
304         )
305     (setq file (concat root-dir "/FULL"))
306     (if (file-exists-p file)
307         (let ((full-buf (get-buffer-create "FULL"))
308               (pwin (or (get-buffer-window mother)
309                         (get-largest-window)))
310               )
311           (save-window-excursion
312             (set-buffer full-buf)
313             (erase-buffer)
314             (as-binary-input-file (insert-file-contents file))
315             (setq major-mode 'mime-show-message-mode)
316             (mime-view-mode mother)
317             )
318           (set-window-buffer pwin
319                              (save-excursion
320                                (set-buffer full-buf)
321                                mime-view-buffer))
322           (select-window pwin)
323           )
324       (re-search-forward "^$")
325       (goto-char (1+ (match-end 0)))
326       (setq file (concat root-dir "/" number))
327       (mime-article::write-region (point) (point-max) file)
328       (let ((total-file (concat root-dir "/CT")))
329         (setq total
330               (if total
331                   (progn
332                     (or (file-exists-p total-file)
333                         (save-excursion
334                           (set-buffer
335                            (get-buffer-create mime/temp-buffer-name))
336                           (erase-buffer)
337                           (insert total)
338                           (write-file total-file)
339                           (kill-buffer (current-buffer))
340                           ))
341                     (string-to-number total)
342                     )
343                 (and (file-exists-p total-file)
344                      (save-excursion
345                        (set-buffer (find-file-noselect total-file))
346                        (prog1
347                            (and (re-search-forward "[0-9]+" nil t)
348                                 (string-to-number
349                                  (buffer-substring (match-beginning 0)
350                                                    (match-end 0)))
351                                 )
352                          (kill-buffer (current-buffer))
353                          )))
354                 )))
355       (if (and total (> total 0))
356           (catch 'tag
357             (save-excursion
358               (set-buffer (get-buffer-create mime/temp-buffer-name))
359               (let ((full-buf (current-buffer)))
360                 (erase-buffer)
361                 (let ((i 1))
362                   (while (<= i total)
363                     (setq file (concat root-dir "/" (int-to-string i)))
364                     (or (file-exists-p file)
365                         (throw 'tag nil)
366                         )
367                     (as-binary-input-file (insert-file-contents file))
368                     (goto-char (point-max))
369                     (setq i (1+ i))
370                     ))
371                 (as-binary-output-file (write-file (concat root-dir "/FULL")))
372                 (let ((i 1))
373                   (while (<= i total)
374                     (let ((file (format "%s/%d" root-dir i)))
375                       (and (file-exists-p file)
376                            (delete-file file)
377                            ))
378                     (setq i (1+ i))
379                     ))
380                 (let ((file (expand-file-name "CT" root-dir)))
381                   (and (file-exists-p file)
382                        (delete-file file)
383                        ))
384                 (save-window-excursion
385                   (setq major-mode 'mime-show-message-mode)
386                   (mime-view-mode mother)
387                   )
388                 (let ((pwin (or (get-buffer-window mother)
389                                 (get-largest-window)
390                                 ))
391                       (pbuf (save-excursion
392                               (set-buffer full-buf)
393                               mime-view-buffer)))
394                   (set-window-buffer pwin pbuf)
395                   (select-window pwin)
396                   )))))
397       )))
398
399
400 ;;; @ message/external-body
401 ;;;
402
403 (defvar mime-article/dired-function
404   (if mime/use-multi-frame
405       (function dired-other-frame)
406     (function mime-article/dired-function-for-one-frame)
407     ))
408
409 (defun mime-article/dired-function-for-one-frame (dir)
410   (let ((win (or (get-buffer-window mime-view-buffer)
411                  (get-largest-window))))
412     (select-window win)
413     (dired dir)
414     ))
415
416 (defun mime-article/decode-message/external-ftp (beg end cal)
417   (let* ((site (cdr (assoc "site" cal)))
418          (directory (cdr (assoc "directory" cal)))
419          (name (cdr (assoc "name" cal)))
420          ;;(mode (cdr (assoc "mode" cal)))
421          (pathname (concat "/anonymous@" site ":" directory))
422          )
423     (message (concat "Accessing " (expand-file-name name pathname) "..."))
424     (funcall mime-article/dired-function pathname)
425     (goto-char (point-min))
426     (search-forward name)
427     ))
428
429
430 ;;; @ rot13-47
431 ;;;
432
433 (require 'view)
434
435 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
436 (define-key mime-view-text/plain-mode-map
437   "q" (function mime-view-text/plain-exit))
438
439 (defun mime-view-text/plain-mode ()
440   "\\{mime-view-text/plain-mode-map}"
441   (setq buffer-read-only t)
442   (setq major-mode 'mime-view-text/plain-mode)
443   (setq mode-name "MIME-View text/plain")
444   (use-local-map mime-view-text/plain-mode-map)
445   )
446
447 (defun mime-view-text/plain-exit ()
448   (interactive)
449   (kill-buffer (current-buffer))
450   )
451
452 (defun mime-article/decode-caesar (beg end cal)
453   (let* ((cnum (mime-article/point-content-number beg))
454          (new-name (format "%s-%s" (buffer-name) cnum))
455          (mother mime-view-buffer)
456          (charset (cdr (assoc "charset" cal)))
457          (encoding (cdr (assq 'encoding cal)))
458          (mode major-mode)
459          str)
460     (setq str (buffer-substring beg end))
461     (let ((pwin (or (get-buffer-window mother)
462                     (get-largest-window)))
463           (buf (get-buffer-create new-name))
464           )
465       (set-window-buffer pwin buf)
466       (set-buffer buf)
467       (select-window pwin)
468       )
469     (setq buffer-read-only nil)
470     (erase-buffer)
471     (insert str)
472     (goto-char (point-min))
473     (if (re-search-forward "^\n" nil t)
474         (delete-region (point-min) (match-end 0))
475       )
476     (let ((m (cdr (or (assq mode mime-text-decoder-alist)
477                       (assq t mime-text-decoder-alist)))))
478       (and (functionp m)
479            (funcall m charset encoding)
480            ))
481     (save-excursion
482       (set-mark (point-min))
483       (goto-char (point-max))
484       (tm:caesar-region)
485       )
486     (set-buffer-modified-p nil)
487     (mime-view-text/plain-mode)
488     ))
489
490
491 ;;; @ end
492 ;;;
493
494 (provide 'mime-play)
495
496 ;;; mime-play.el ends here