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