add iso-8859-14 also
[elisp/semi.git] / mime-play.el
1 ;;; mime-play.el --- Playback processing module for mime-view.el
2
3 ;; Copyright (C) 1994,95,96,97,98,99,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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
34   (condition-case nil
35       (require 'bbdb)
36     (error (defvar bbdb-buffer-name nil)))
37   )
38
39 (defcustom mime-save-directory "~/"
40   "*Name of the directory where MIME entity will be saved in.
41 If t, it means current directory."
42   :group 'mime-view
43   :type '(choice (const :tag "Current directory" t)
44                  (directory)))
45
46 (defvar mime-play-find-every-situations t
47   "*Find every available situations if non-nil.")
48
49 (defvar mime-play-messages-coding-system nil
50   "Coding system to be used for external MIME playback method.")
51
52
53 ;;; @ content decoder
54 ;;;
55
56 ;;;###autoload
57 (defun mime-preview-play-current-entity (&optional ignore-examples mode)
58   "Play current entity.
59 It decodes current entity to call internal or external method.  The
60 method is selected from variable `mime-acting-condition'.
61 If IGNORE-EXAMPLES (C-u prefix) is specified, this function ignores
62 `mime-acting-situation-example-list'.
63 If MODE is specified, play as it.  Default MODE is \"play\"."
64   (interactive "P")
65   (let ((entity (get-text-property (point) 'mime-view-entity)))
66     (if entity
67         (let ((situation
68                (get-text-property (point) 'mime-view-situation)))
69           (or mode
70               (setq mode "play"))
71           (setq situation 
72                 (if (assq 'mode situation)
73                     (put-alist 'mode mode (copy-alist situation))
74                   (cons (cons 'mode mode)
75                         situation)))
76           (if ignore-examples
77               (setq situation
78                     (cons (cons 'ignore-examples ignore-examples)
79                           situation)))
80           (mime-play-entity entity situation)
81           ))))
82
83 ;;;###autoload
84 (defun mime-play-entity (entity &optional situation ignored-method)
85   "Play entity specified by ENTITY.
86 It decodes the entity to call internal or external method.  The method
87 is selected from variable `mime-acting-condition'.  If MODE is
88 specified, play as it.  Default MODE is \"play\"."
89   (let ((ret
90          (mime-unify-situations (mime-entity-situation entity situation)
91                                 mime-acting-condition
92                                 mime-acting-situation-example-list
93                                 'method ignored-method
94                                 mime-play-find-every-situations))
95         method)
96     (setq mime-acting-situation-example-list (cdr ret)
97           ret (car ret))
98     (cond ((cdr ret)
99            (setq ret (mime-select-menu-alist
100                       "Methods"
101                       (mapcar (function
102                                (lambda (situation)
103                                  (cons
104                                   (format "%s"
105                                           (cdr (assq 'method situation)))
106                                   situation)))
107                               ret)))
108            (setq ret (mime-sort-situation ret))
109            (add-to-list 'mime-acting-situation-example-list (cons ret 0))
110            )
111           (t
112            (setq ret (car ret))
113            ))
114     (setq method (cdr (assq 'method ret)))
115     (cond ((and (symbolp method)
116                 (fboundp method))
117            (funcall method entity ret)
118            )
119           ((stringp method)
120            (mime-activate-mailcap-method entity ret)
121            )
122           ;; ((and (listp method)(stringp (car method)))
123           ;;  (mime-activate-external-method entity ret)
124           ;;  )
125           (t
126            (mime-show-echo-buffer "No method are specified for %s\n"
127                                   (mime-type/subtype-string
128                                    (cdr (assq 'type situation))
129                                    (cdr (assq 'subtype situation))))
130            (if (y-or-n-p "Do you want to save current entity to disk?")
131                (mime-save-content entity situation))
132            ))
133     ))
134
135
136 ;;; @ external decoder
137 ;;;
138
139 (defvar mime-mailcap-method-filename-alist nil)
140
141 (defun mime-activate-mailcap-method (entity situation)
142   (let ((method (cdr (assoc 'method situation)))
143         (name (mime-entity-safe-filename entity)))
144     (setq name (expand-file-name (if (and name (not (string= name "")))
145                                      name
146                                    (make-temp-name "EMI"))
147                                  (make-temp-file "EMI" 'directory)))
148     (mime-write-entity-content entity name)
149     (message "External method is starting...")
150     (let ((process
151            (let ((command
152                   (mime-format-mailcap-command
153                    method
154                    (cons (cons 'filename name) situation))))
155              (binary-to-text-funcall
156               mime-play-messages-coding-system
157               #'start-process command mime-echo-buffer-name
158               shell-file-name shell-command-switch command))))
159       (set-alist 'mime-mailcap-method-filename-alist process name)
160       (set-process-sentinel process 'mime-mailcap-method-sentinel))))
161
162 (defun mime-mailcap-method-sentinel (process event)
163   (let ((file (cdr (assq process mime-mailcap-method-filename-alist))))
164     (when (file-exists-p file)
165       (ignore-errors
166         (delete-file file)
167         (delete-directory (file-name-directory file)))))
168   (remove-alist 'mime-mailcap-method-filename-alist process)
169   (message "%s %s" process event))
170
171 (defvar mime-echo-window-is-shared-with-bbdb
172   (module-installed-p 'bbdb)
173   "*If non-nil, mime-echo window is shared with BBDB window.")
174
175 (defvar mime-echo-window-height
176   (function
177    (lambda ()
178      (/ (window-height) 5)
179      ))
180   "*Size of mime-echo window.
181 It allows function or integer.  If it is function,
182 `mime-show-echo-buffer' calls it to get height of mime-echo window.
183 Otherwise `mime-show-echo-buffer' uses it as height of mime-echo
184 window.")
185
186 (defun mime-show-echo-buffer (&rest forms)
187   "Show mime-echo buffer to display MIME-playing information."
188   (get-buffer-create mime-echo-buffer-name)
189   (let ((the-win (selected-window))
190         (win (get-buffer-window mime-echo-buffer-name)))
191     (unless win
192       (unless (and mime-echo-window-is-shared-with-bbdb
193                    (condition-case nil
194                        (setq win (get-buffer-window bbdb-buffer-name))
195                      (error nil)))
196         (select-window (get-buffer-window (or mime-preview-buffer
197                                               (current-buffer))))
198         (setq win (split-window-vertically
199                    (- (window-height)
200                       (if (functionp mime-echo-window-height)
201                           (funcall mime-echo-window-height)
202                         mime-echo-window-height)
203                       )))
204         )
205       (set-window-buffer win mime-echo-buffer-name)
206       )
207     (select-window win)
208     (goto-char (point-max))
209     (if forms
210         (let ((buffer-read-only nil))
211           (insert (apply (function format) forms))
212           ))
213     (select-window the-win)
214     ))
215
216
217 ;;; @ file name
218 ;;;
219
220 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
221
222 (defvar mime-view-file-name-regexp-1
223   (concat mime-view-file-name-char-regexp "+\\."
224           mime-view-file-name-char-regexp "+"))
225
226 (defvar mime-view-file-name-regexp-2
227   (concat (regexp-* mime-view-file-name-char-regexp)
228           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
229
230 (defun mime-entity-safe-filename (entity)
231   (let ((filename
232          (or (mime-entity-filename entity)
233              (let ((subj
234                     (or (mime-entity-read-field entity 'Content-Description)
235                         (mime-entity-read-field entity 'Subject))))
236                (if (and subj
237                         (or (string-match mime-view-file-name-regexp-1 subj)
238                             (string-match mime-view-file-name-regexp-2 subj)))
239                    (substring subj (match-beginning 0)(match-end 0))
240                  )))))
241     (if filename
242         (replace-as-filename filename)
243       )))
244
245
246 ;;; @ file extraction
247 ;;;
248
249 (defun mime-save-content (entity situation)
250   (let ((name (or (mime-entity-safe-filename entity)
251                   (format "%s" (mime-entity-media-type entity))))
252         (dir (if (eq t mime-save-directory)
253                  default-directory
254                mime-save-directory))
255         filename)
256     (setq filename (read-file-name
257                     (concat "File name: (default "
258                             (file-name-nondirectory name) ") ")
259                     dir
260                     (concat (file-name-as-directory dir)
261                             (file-name-nondirectory name))))
262     (if (file-directory-p filename)
263         (setq filename (concat (file-name-as-directory filename)
264                                (file-name-nondirectory name))))
265     (if (file-exists-p filename)
266         (or (yes-or-no-p (format "File %s exists. Save anyway? " filename))
267             (error "")))
268     (mime-write-entity-content entity (expand-file-name filename))
269     ))
270
271
272 ;;; @ file detection
273 ;;;
274
275 (defvar mime-magic-type-alist
276   '(("^\377\330\377[\340\356]..JFIF"    image jpeg)
277     ("^\211PNG"                         image png)
278     ("^GIF8[79]"                        image gif)
279     ("^II\\*\000"                       image tiff)
280     ("^MM\000\\*"                       image tiff)
281     ("^MThd"                            audio midi)
282     ("^\000\000\001\263"                video mpeg)
283     )
284   "*Alist of regexp about magic-number vs. corresponding media-types.
285 Each element looks like (REGEXP TYPE SUBTYPE).
286 REGEXP is a regular expression to match against the beginning of the
287 content of entity.
288 TYPE is symbol to indicate primary type of media-type.
289 SUBTYPE is symbol to indicate subtype of media-type.")
290
291 (defun mime-detect-content (entity situation)
292   (let (type subtype)
293     (let ((mdata (mime-entity-content entity))
294           (rest mime-magic-type-alist))
295       (while (not (let ((cell (car rest)))
296                     (if cell
297                         (if (string-match (car cell) mdata)
298                             (setq type (nth 1 cell)
299                                   subtype (nth 2 cell))
300                           )
301                       t)))
302         (setq rest (cdr rest))))
303     (setq situation (del-alist 'method (copy-alist situation)))
304     (mime-play-entity entity
305                       (if type
306                           (put-alist 'type type
307                                      (put-alist 'subtype subtype
308                                                 situation))
309                         situation)
310                       'mime-detect-content)))
311
312
313 ;;; @ mail/news message
314 ;;;
315
316 (defun mime-preview-quitting-method-for-mime-show-message-mode ()
317   "Quitting method for mime-view.
318 It is registered to variable `mime-preview-quitting-method-alist'."
319   (let ((mother mime-mother-buffer)
320         (win-conf mime-preview-original-window-configuration))
321     (if (and (boundp 'mime-view-temp-message-buffer)
322              (buffer-live-p mime-view-temp-message-buffer))
323         (kill-buffer mime-view-temp-message-buffer))
324     (mime-preview-kill-buffer)
325     (set-window-configuration win-conf)
326     (pop-to-buffer mother)))
327
328 (defun mime-view-message/rfc822 (entity situation)
329   (let* ((new-name
330           (format "%s-%s" (buffer-name) (mime-entity-number entity)))
331          (mother (current-buffer))
332          (children (car (mime-entity-children entity)))
333          (preview-buffer
334           (mime-display-message
335            children new-name mother nil
336            (cdr (assq 'major-mode
337                       (get-text-property (point) 'mime-view-situation))))))
338     (or (get-buffer-window preview-buffer)
339         (let ((m-win (get-buffer-window mother)))
340           (if m-win
341               (set-window-buffer m-win preview-buffer)
342             (switch-to-buffer preview-buffer)
343             )))))
344
345
346 ;;; @ message/partial
347 ;;;
348
349 (defun mime-require-safe-directory (dir)
350   "Create a directory DIR safely.
351 The permission of the created directory becomes `700' (for the owner only).
352 If the directory already exists and is writable by other users, an error
353 occurs."
354   (let ((attr (file-attributes dir))
355         (orig-modes (default-file-modes)))
356     (if (and attr (eq (car attr) t)) ; directory already exists.
357         (unless (or (memq system-type '(windows-nt ms-dos OS/2 emx))
358                     (and (eq (nth 2 attr) (user-real-uid))
359                          (eq (file-modes dir) 448)))
360           (error "Invalid owner or permission for %s" dir))
361       (unwind-protect
362           (progn
363             (set-default-file-modes 448)
364             (make-directory dir))
365         (set-default-file-modes orig-modes)))))
366
367 (defun mime-store-message/partial-piece (entity cal)
368   (let* ((root-dir
369           (expand-file-name
370            (concat "m-prts-" (user-login-name)) temporary-file-directory))
371          (id (cdr (assoc "id" cal)))
372          (number (cdr (assoc "number" cal)))
373          (total (cdr (assoc "total" cal)))
374          file
375          (mother (current-buffer))
376          (orig-modes (default-file-modes)))
377     (mime-require-safe-directory root-dir)
378     (or (file-exists-p root-dir)
379         (unwind-protect
380             (progn
381               (set-default-file-modes 448)
382               (make-directory root-dir))
383           (set-default-file-modes orig-modes)))
384     (setq id (replace-as-filename id))
385     (setq root-dir (concat root-dir "/" id))
386
387     (or (file-exists-p root-dir)
388         (unwind-protect
389             (progn
390               (set-default-file-modes 448)
391               (make-directory root-dir))
392           (set-default-file-modes orig-modes)))
393
394     (setq file (concat root-dir "/FULL"))
395     (if (file-exists-p file)
396         (let ((full-buf (get-buffer-create "FULL"))
397               (pwin (or (get-buffer-window mother)
398                         (get-largest-window)))
399               pbuf)
400           (save-window-excursion
401             (set-buffer full-buf)
402             (erase-buffer)
403             (binary-insert-encoded-file file)
404             (setq major-mode 'mime-show-message-mode)
405             (mime-view-buffer (current-buffer) nil mother)
406             (setq pbuf (current-buffer))
407             (make-local-variable 'mime-view-temp-message-buffer)
408             (setq mime-view-temp-message-buffer full-buf))
409           (set-window-buffer pwin pbuf)
410           (select-window pwin))
411       (setq file (concat root-dir "/" number))
412       (mime-write-entity-body entity file)
413       (let ((total-file (concat root-dir "/CT")))
414         (setq total
415               (if total
416                   (progn
417                     (or (file-exists-p total-file)
418                         (save-excursion
419                           (set-buffer
420                            (get-buffer-create mime-temp-buffer-name))
421                           (erase-buffer)
422                           (insert total)
423                           (write-region (point-min)(point-max) total-file)
424                           (kill-buffer (current-buffer))
425                           ))
426                     (string-to-number total)
427                     )
428                 (and (file-exists-p total-file)
429                      (save-excursion
430                        (set-buffer (find-file-noselect total-file))
431                        (prog1
432                            (and (re-search-forward "[0-9]+" nil t)
433                                 (string-to-number
434                                  (buffer-substring (match-beginning 0)
435                                                    (match-end 0)))
436                                 )
437                          (kill-buffer (current-buffer))
438                          )))
439                 )))
440       (if (and total (> total 0)
441                (>= (length (directory-files root-dir nil "^[0-9]+$" t))
442                    total))
443           (catch 'tag
444             (save-excursion
445               (set-buffer (get-buffer-create mime-temp-buffer-name))
446               (let ((full-buf (current-buffer)))
447                 (erase-buffer)
448                 (let ((i 1))
449                   (while (<= i total)
450                     (setq file (concat root-dir "/" (int-to-string i)))
451                     (or (file-exists-p file)
452                         (throw 'tag nil)
453                         )
454                     (binary-insert-encoded-file file)
455                     (goto-char (point-max))
456                     (setq i (1+ i))))
457                 (binary-write-decoded-region
458                  (point-min)(point-max)
459                  (expand-file-name "FULL" root-dir))
460                 (let ((i 1))
461                   (while (<= i total)
462                     (let ((file (format "%s/%d" root-dir i)))
463                       (and (file-exists-p file)
464                            (delete-file file)))
465                     (setq i (1+ i))))
466                 (let ((file (expand-file-name "CT" root-dir)))
467                   (and (file-exists-p file)
468                        (delete-file file)))
469                 (let ((buf (current-buffer))
470                       (pwin (or (get-buffer-window mother)
471                                 (get-largest-window)))
472                       (pbuf (mime-display-message
473                              (mime-open-entity 'buffer (current-buffer))
474                              nil mother nil 'mime-show-message-mode)))
475                   (with-current-buffer pbuf
476                     (make-local-variable 'mime-view-temp-message-buffer)
477                     (setq mime-view-temp-message-buffer buf))
478                   (set-window-buffer pwin pbuf)
479                   (select-window pwin)
480                   )))))
481       )))
482
483
484 ;;; @ message/external-body
485 ;;;
486
487 (defvar mime-raw-dired-function
488   (if (and (>= emacs-major-version 19) window-system)
489       (function dired-other-frame)
490     (function mime-raw-dired-function-for-one-frame)
491     ))
492
493 (defun mime-raw-dired-function-for-one-frame (dir)
494   (let ((win (or (get-buffer-window mime-preview-buffer)
495                  (get-largest-window))))
496     (select-window win)
497     (dired dir)
498     ))
499
500 (defun mime-view-message/external-anon-ftp (entity cal)
501   (let* ((site (cdr (assoc "site" cal)))
502          (directory (cdr (assoc "directory" cal)))
503          (name (cdr (assoc "name" cal)))
504          (pathname (concat "/anonymous@" site ":" directory)))
505     (message "%s" (concat "Accessing " (expand-file-name name pathname) "..."))
506     (funcall mime-raw-dired-function pathname)
507     (goto-char (point-min))
508     (search-forward name)
509     ))
510
511 (defvar mime-raw-browse-url-function mime-browse-url-function)
512
513 (defun mime-view-message/external-url (entity cal)
514   (let ((url (cdr (assoc "url" cal))))
515     (message "%s" (concat "Accessing " url "..."))
516     (funcall mime-raw-browse-url-function url)))
517
518
519 ;;; @ rot13-47
520 ;;;
521
522 (defun mime-view-caesar (entity situation)
523   "Internal method for mime-view to display ROT13-47-48 message."
524   (let ((buf (get-buffer-create
525               (format "%s-%s" (buffer-name) (mime-entity-number entity)))))
526     (with-current-buffer buf
527       (setq buffer-read-only nil)
528       (erase-buffer)
529       (mime-insert-text-content entity)
530       (mule-caesar-region (point-min) (point-max))
531       (set-buffer-modified-p nil)
532       )
533     (let ((win (get-buffer-window (current-buffer))))
534       (or (eq (selected-window) win)
535           (select-window (or win (get-largest-window)))
536           ))
537     (view-buffer buf)
538     (goto-char (point-min))
539     ))
540
541
542 ;;; @ end
543 ;;;
544
545 (provide 'mime-play)
546
547 ;;; mime-play.el ends here