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