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