`mime/show-message-mode' -> `mime-show-message-mode'.
[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.24 1997-03-17 15:37: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 ((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   (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::article/preview-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
242      (mime::preview-content-info/buffer (car mime::preview/content-list)))
243     (mime-view-kill-buffer)
244     (set-window-configuration win-conf)
245     (pop-to-buffer mother)
246     ))
247
248 (defun mime-article/view-message/rfc822 (beg end cal)
249   (let* ((cnum (mime-article/point-content-number beg))
250          (new-name (format "%s-%s" (buffer-name) cnum))
251          (mother mime::article/preview-buffer)
252          (text-decoder
253           (cdr (or (assq major-mode mime-text-decoder-alist)
254                    (assq t mime-text-decoder-alist))))
255          str)
256     (setq str (buffer-substring beg end))
257     (switch-to-buffer new-name)
258     (erase-buffer)
259     (insert str)
260     (goto-char (point-min))
261     (if (re-search-forward "^\n" nil t)
262         (delete-region (point-min) (match-end 0))
263       )
264     (setq major-mode 'mime-show-message-mode)
265     (setq mime-text-decoder text-decoder)
266     (mime-view-mode mother)
267     ))
268
269
270 ;;; @ message/partial
271 ;;;
272
273 (defvar mime-article/coding-system-alist
274   (list '(mh-show-mode . no-conversion)
275         (cons t (mime-charset-to-coding-system default-mime-charset))
276         ))
277
278 (defun mime-article::write-region (start end file)
279   (let ((coding-system-for-write
280          (cdr
281           (or (assq major-mode mime-article/coding-system-alist)
282               (assq t mime-article/coding-system-alist)
283               ))))
284     (write-region start end file)
285     ))
286
287 (defun mime-article/decode-message/partial (beg end cal)
288   (goto-char beg)
289   (let* ((root-dir
290           (expand-file-name
291            (concat "m-prts-" (user-login-name)) mime-temp-directory))
292          (id (cdr (assoc "id" cal)))
293          (number (cdr (assoc "number" cal)))
294          (total (cdr (assoc "total" cal)))
295          file
296          (mother mime::article/preview-buffer)
297          )
298     (or (file-exists-p root-dir)
299         (make-directory root-dir)
300         )
301     (setq id (replace-as-filename id))
302     (setq root-dir (concat root-dir "/" id))
303     (or (file-exists-p root-dir)
304         (make-directory root-dir)
305         )
306     (setq file (concat root-dir "/FULL"))
307     (if (file-exists-p file)
308         (let ((full-buf (get-buffer-create "FULL"))
309               (pwin (or (get-buffer-window mother)
310                         (get-largest-window)))
311               )
312           (save-window-excursion
313             (set-buffer full-buf)
314             (erase-buffer)
315             (as-binary-input-file (insert-file-contents file))
316             (setq major-mode 'mime-show-message-mode)
317             (mime-view-mode mother)
318             )
319           (set-window-buffer pwin
320                              (save-excursion
321                                (set-buffer full-buf)
322                                mime::article/preview-buffer))
323           (select-window pwin)
324           )
325       (re-search-forward "^$")
326       (goto-char (1+ (match-end 0)))
327       (setq file (concat root-dir "/" number))
328       (mime-article::write-region (point) (point-max) file)
329       (let ((total-file (concat root-dir "/CT")))
330         (setq total
331               (if total
332                   (progn
333                     (or (file-exists-p total-file)
334                         (save-excursion
335                           (set-buffer
336                            (get-buffer-create mime/temp-buffer-name))
337                           (erase-buffer)
338                           (insert total)
339                           (write-file total-file)
340                           (kill-buffer (current-buffer))
341                           ))
342                     (string-to-number total)
343                     )
344                 (and (file-exists-p total-file)
345                      (save-excursion
346                        (set-buffer (find-file-noselect total-file))
347                        (prog1
348                            (and (re-search-forward "[0-9]+" nil t)
349                                 (string-to-number
350                                  (buffer-substring (match-beginning 0)
351                                                    (match-end 0)))
352                                 )
353                          (kill-buffer (current-buffer))
354                          )))
355                 )))
356       (if (and total (> total 0))
357           (catch 'tag
358             (save-excursion
359               (set-buffer (get-buffer-create mime/temp-buffer-name))
360               (let ((full-buf (current-buffer)))
361                 (erase-buffer)
362                 (let ((i 1))
363                   (while (<= i total)
364                     (setq file (concat root-dir "/" (int-to-string i)))
365                     (or (file-exists-p file)
366                         (throw 'tag nil)
367                         )
368                     (as-binary-input-file (insert-file-contents file))
369                     (goto-char (point-max))
370                     (setq i (1+ i))
371                     ))
372                 (as-binary-output-file (write-file (concat root-dir "/FULL")))
373                 (let ((i 1))
374                   (while (<= i total)
375                     (let ((file (format "%s/%d" root-dir i)))
376                       (and (file-exists-p file)
377                            (delete-file file)
378                            ))
379                     (setq i (1+ i))
380                     ))
381                 (let ((file (expand-file-name "CT" root-dir)))
382                   (and (file-exists-p file)
383                        (delete-file file)
384                        ))
385                 (save-window-excursion
386                   (setq major-mode 'mime-show-message-mode)
387                   (mime-view-mode mother)
388                   )
389                 (let ((pwin (or (get-buffer-window mother)
390                                 (get-largest-window)
391                                 ))
392                       (pbuf (save-excursion
393                               (set-buffer full-buf)
394                               mime::article/preview-buffer)))
395                   (set-window-buffer pwin pbuf)
396                   (select-window pwin)
397                   )))))
398       )))
399
400
401 ;;; @ message/external-body
402 ;;;
403
404 (defvar mime-article/dired-function
405   (if mime/use-multi-frame
406       (function dired-other-frame)
407     (function mime-article/dired-function-for-one-frame)
408     ))
409
410 (defun mime-article/dired-function-for-one-frame (dir)
411   (let ((win (or (get-buffer-window mime::article/preview-buffer)
412                  (get-largest-window))))
413     (select-window win)
414     (dired dir)
415     ))
416
417 (defun mime-article/decode-message/external-ftp (beg end cal)
418   (let* ((site (cdr (assoc "site" cal)))
419          (directory (cdr (assoc "directory" cal)))
420          (name (cdr (assoc "name" cal)))
421          ;;(mode (cdr (assoc "mode" cal)))
422          (pathname (concat "/anonymous@" site ":" directory))
423          )
424     (message (concat "Accessing " (expand-file-name name pathname) "..."))
425     (funcall mime-article/dired-function pathname)
426     (goto-char (point-min))
427     (search-forward name)
428     ))
429
430
431 ;;; @ rot13-47
432 ;;;
433
434 (require 'view)
435
436 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
437 (define-key mime-view-text/plain-mode-map
438   "q" (function mime-view-text/plain-exit))
439
440 (defun mime-view-text/plain-mode ()
441   "\\{mime-view-text/plain-mode-map}"
442   (setq buffer-read-only t)
443   (setq major-mode 'mime-view-text/plain-mode)
444   (setq mode-name "MIME-View text/plain")
445   (use-local-map mime-view-text/plain-mode-map)
446   )
447
448 (defun mime-view-text/plain-exit ()
449   (interactive)
450   (kill-buffer (current-buffer))
451   )
452
453 (defun mime-article/decode-caesar (beg end cal)
454   (let* ((cnum (mime-article/point-content-number beg))
455          (new-name (format "%s-%s" (buffer-name) cnum))
456          (mother mime::article/preview-buffer)
457          (charset (cdr (assoc "charset" cal)))
458          (encoding (cdr (assq 'encoding cal)))
459          (mode major-mode)
460          str)
461     (setq str (buffer-substring beg end))
462     (let ((pwin (or (get-buffer-window mother)
463                     (get-largest-window)))
464           (buf (get-buffer-create new-name))
465           )
466       (set-window-buffer pwin buf)
467       (set-buffer buf)
468       (select-window pwin)
469       )
470     (setq buffer-read-only nil)
471     (erase-buffer)
472     (insert str)
473     (goto-char (point-min))
474     (if (re-search-forward "^\n" nil t)
475         (delete-region (point-min) (match-end 0))
476       )
477     (let ((m (cdr (or (assq mode mime-text-decoder-alist)
478                       (assq t mime-text-decoder-alist)))))
479       (and (functionp m)
480            (funcall m charset encoding)
481            ))
482     (save-excursion
483       (set-mark (point-min))
484       (goto-char (point-max))
485       (tm:caesar-region)
486       )
487     (set-buffer-modified-p nil)
488     (mime-view-text/plain-mode)
489     ))
490
491
492 ;;; @ end
493 ;;;
494
495 (provide 'mime-play)
496
497 ;;; mime-play.el ends here