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