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