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