(mime-article/dired-function): New variable; copied from tm-ftp.el.
[elisp/semi.git] / mime-play.el
1 ;;; mime-play.el --- decoder for mime-view.el
2
3 ;; Copyright (C) 1994,1995,1996,1997 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 ;; Version: $Id: mime-play.el,v 0.13 1997-03-10 13:39:57 morioka Exp $
9 ;; Keywords: MIME, multimedia, mail, news
10
11 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Code:
29
30 (require 'mime-view)
31 (require 'alist)
32 (require 'filename)
33
34   
35 ;;; @ content decoder
36 ;;;
37
38 (defvar mime-preview/after-decoded-position nil)
39
40 (defun mime-preview/decode-content ()
41   (interactive)
42   (let ((pc (mime-preview/point-pcinfo (point))))
43     (if pc
44         (let ((the-buf (current-buffer)))
45           (setq mime-preview/after-decoded-position (point))
46           (set-buffer (mime::preview-content-info/buffer pc))
47           (mime-article/decode-content
48            (mime::preview-content-info/content-info pc))
49           (if (eq (current-buffer)
50                   (mime::preview-content-info/buffer pc))
51               (progn
52                 (set-buffer the-buf)
53                 (goto-char mime-preview/after-decoded-position)
54                 ))
55           ))))
56
57 (defun mime-article/decode-content (cinfo)
58   (let ((beg (mime::content-info/point-min cinfo))
59         (end (mime::content-info/point-max cinfo))
60         (ctype (or (mime::content-info/type cinfo) "text/plain"))
61         (params (mime::content-info/parameters cinfo))
62         (encoding (mime::content-info/encoding cinfo))
63         )
64     ;; Check for VM
65     (if (< beg (point-min))
66         (setq beg (point-min))
67       )
68     (if (< (point-max) end)
69         (setq end (point-max))
70       )
71     (let (method cal ret)
72       (setq cal (list* (cons 'type ctype)
73                        (cons 'encoding encoding)
74                        (cons 'major-mode major-mode)
75                        params))
76       (if mime-view-decoding-mode
77           (setq cal (cons
78                      (cons 'mode mime-view-decoding-mode)
79                      cal))
80         )
81       (setq ret (mime/get-content-decoding-alist cal))
82       (setq method (cdr (assq 'method ret)))
83       (cond ((and (symbolp method)
84                   (fboundp method))
85              (funcall method beg end ret)
86              )
87             ((and (listp method)(stringp (car method)))
88              (mime-article/start-external-method-region beg end ret)
89              )
90             (t
91              (mime-article/show-output-buffer
92               "No method are specified for %s\n" ctype)
93              ))
94       )
95     ))
96
97
98 ;;; @ method selector
99 ;;;
100
101 (defun mime/get-content-decoding-alist (al)
102   (get-unified-alist mime/content-decoding-condition al)
103   )
104
105
106 ;;; @ external decoder
107 ;;;
108
109 (defun mime-article/start-external-method-region (beg end cal)
110   (save-excursion
111     (save-restriction
112       (narrow-to-region beg end)
113       (goto-char beg)
114       (let ((method (cdr (assoc 'method cal)))
115             (name (mime-article/get-filename cal))
116             )
117         (if method
118             (let ((file (make-temp-name
119                          (expand-file-name "TM" mime-temp-directory)))
120                   b args)
121               (if (nth 1 method)
122                   (setq b beg)
123                 (setq b
124                       (if (re-search-forward "^$" nil t)
125                           (1+ (match-end 0))
126                         (point-min)
127                         ))
128                 )
129               (goto-char b)
130               (write-region b end file)
131               (message "External method is starting...")
132               (setq cal (put-alist
133                          'name (replace-as-filename name) cal))
134               (setq cal (put-alist 'file file cal))
135               (setq args (nconc
136                           (list (car method)
137                                 mime/output-buffer-name (car method)
138                                 )
139                           (mime-article/make-method-args cal
140                                                          (cdr (cdr method)))
141                           ))
142               (apply (function start-process) args)
143               (mime-article/show-output-buffer)
144               ))
145         ))))
146
147 (defun mime-article/make-method-args (cal format)
148   (mapcar (function
149            (lambda (arg)
150              (if (stringp arg)
151                  arg
152                (let* ((item (eval arg))
153                       (ret (cdr (assoc item cal)))
154                       )
155                  (if ret
156                      ret
157                    (if (eq item 'encoding)
158                        "7bit"
159                      ""))
160                  ))
161              ))
162           format))
163
164 (defun mime-article/show-output-buffer (&rest forms)
165   (get-buffer-create mime/output-buffer-name)
166   (let ((the-win (selected-window))
167         (win (get-buffer-window mime/output-buffer-name))
168         )
169     (or win
170         (if (and mime/output-buffer-window-is-shared-with-bbdb
171                  (boundp 'bbdb-buffer-name)
172                  (setq win (get-buffer-window bbdb-buffer-name))
173                  )
174             (set-window-buffer win mime/output-buffer-name)
175           (select-window (get-buffer-window mime::article/preview-buffer))
176           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
177           (set-window-buffer win mime/output-buffer-name)
178           ))
179     (select-window win)
180     (goto-char (point-max))
181     (if forms
182         (insert (apply (function format) forms))
183       )
184     (select-window the-win)
185     ))
186
187
188 ;;; @ file name
189 ;;;
190
191 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
192
193 (defvar mime-view-file-name-regexp-1
194   (concat mime-view-file-name-char-regexp "+\\."
195           mime-view-file-name-char-regexp "+"))
196
197 (defvar mime-view-file-name-regexp-2
198   (concat (regexp-* mime-view-file-name-char-regexp)
199           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
200
201 (defun mime-article/get-original-filename (param &optional encoding)
202   (or (mime-article/get-uu-filename param encoding)
203       (let (ret)
204         (or (if (or (and (setq ret (mime/Content-Disposition))
205                          (setq ret (assoc "filename" (cdr ret)))
206                          )
207                     (setq ret (assoc "name" param))
208                     (setq ret (assoc "x-name" param))
209                     )
210                 (std11-strip-quoted-string (cdr ret))
211               )
212             (if (setq ret
213                       (std11-find-field-body '("Content-Description"
214                                                "Subject")))
215                 (if (or (string-match mime-view-file-name-regexp-1 ret)
216                         (string-match mime-view-file-name-regexp-2 ret))
217                     (substring ret (match-beginning 0)(match-end 0))
218                   ))
219             ))
220       ))
221
222 (defun mime-article/get-filename (param)
223   (replace-as-filename (mime-article/get-original-filename param))
224   )
225
226
227 ;;; @ mail/news message
228 ;;;
229
230 (defun mime-view-quitting-method-for-mime/show-message-mode ()
231   (let ((mother mime::preview/mother-buffer)
232         (win-conf mime::preview/original-window-configuration)
233         )
234     (kill-buffer
235      (mime::preview-content-info/buffer (car mime::preview/content-list)))
236     (mime-view-kill-buffer)
237     (set-window-configuration win-conf)
238     (pop-to-buffer mother)
239     ;;(goto-char (point-min))
240     ;;(mime-view-up-content)
241     ))
242
243 (defun mime-article/view-message/rfc822 (beg end cal)
244   (let* ((cnum (mime-article/point-content-number beg))
245          (cur-buf (current-buffer))
246          (new-name (format "%s-%s" (buffer-name) cnum))
247          (mother mime::article/preview-buffer)
248          (code-converter
249           (or (cdr (assq major-mode mime-text-decoder-alist))
250               'mime-view-default-code-convert-region))
251          str)
252     (setq str (buffer-substring beg end))
253     (switch-to-buffer new-name)
254     (erase-buffer)
255     (insert str)
256     (goto-char (point-min))
257     (if (re-search-forward "^\n" nil t)
258         (delete-region (point-min) (match-end 0))
259       )
260     (setq major-mode 'mime/show-message-mode)
261     (setq mime::article/code-converter code-converter)
262     (mime-view-mode mother)
263     ))
264
265
266 ;;; @ message/partial
267 ;;;
268
269 (defvar mime-article/coding-system-alist
270   (list '(mh-show-mode . no-conversion)
271         (cons t (mime-charset-to-coding-system default-mime-charset))
272         ))
273
274 (defun mime-article::write-region (start end file)
275   (let ((coding-system-for-write
276          (cdr
277           (or (assq major-mode mime-article/coding-system-alist)
278               (assq t mime-article/coding-system-alist)
279               ))))
280     (write-region start end file)
281     ))
282
283 (defun mime-article/decode-message/partial (beg end cal)
284   (goto-char beg)
285   (let* ((root-dir
286           (expand-file-name
287            (concat "m-prts-" (user-login-name)) mime-temp-directory))
288          (id (cdr (assoc "id" cal)))
289          (number (cdr (assoc "number" cal)))
290          (total (cdr (assoc "total" cal)))
291          file
292          (mother mime::article/preview-buffer)
293          )
294     (or (file-exists-p root-dir)
295         (make-directory root-dir)
296         )
297     (setq id (replace-as-filename id))
298     (setq root-dir (concat root-dir "/" id))
299     (or (file-exists-p root-dir)
300         (make-directory root-dir)
301         )
302     (setq file (concat root-dir "/FULL"))
303     (if (file-exists-p file)
304         (let ((full-buf (get-buffer-create "FULL"))
305               (pwin (or (get-buffer-window mother)
306                         (get-largest-window)))
307               )
308           (save-window-excursion
309             (set-buffer full-buf)
310             (erase-buffer)
311             (as-binary-input-file (insert-file-contents file))
312             (setq major-mode 'mime/show-message-mode)
313             (mime-view-mode mother)
314             )
315           (set-window-buffer pwin
316                              (save-excursion
317                                (set-buffer full-buf)
318                                mime::article/preview-buffer))
319           (select-window pwin)
320           )
321       (re-search-forward "^$")
322       (goto-char (1+ (match-end 0)))
323       (setq file (concat root-dir "/" number))
324       (mime-article::write-region (point) (point-max) file)
325       (let ((total-file (concat root-dir "/CT")))
326         (setq total
327               (if total
328                   (progn
329                     (or (file-exists-p total-file)
330                         (save-excursion
331                           (set-buffer
332                            (get-buffer-create mime/temp-buffer-name))
333                           (erase-buffer)
334                           (insert total)
335                           (write-file total-file)
336                           (kill-buffer (current-buffer))
337                           ))
338                     (string-to-number total)
339                     )
340                 (and (file-exists-p total-file)
341                      (save-excursion
342                        (set-buffer (find-file-noselect total-file))
343                        (prog1
344                            (and (re-search-forward "[0-9]+" nil t)
345                                 (string-to-number
346                                  (buffer-substring (match-beginning 0)
347                                                    (match-end 0)))
348                                 )
349                          (kill-buffer (current-buffer))
350                          )))
351                 )))
352       (if (and total (> total 0))
353           (catch 'tag
354             (save-excursion
355               (set-buffer (get-buffer-create mime/temp-buffer-name))
356               (let ((full-buf (current-buffer)))
357                 (erase-buffer)
358                 (let ((i 1))
359                   (while (<= i total)
360                     (setq file (concat root-dir "/" (int-to-string i)))
361                     (or (file-exists-p file)
362                         (throw 'tag nil)
363                         )
364                     (as-binary-input-file (insert-file-contents file))
365                     (goto-char (point-max))
366                     (setq i (1+ i))
367                     ))
368                 (as-binary-output-file (write-file (concat root-dir "/FULL")))
369                 (let ((i 1))
370                   (while (<= i total)
371                     (let ((file (format "%s/%d" root-dir i)))
372                       (and (file-exists-p file)
373                            (delete-file file)
374                            ))
375                     (setq i (1+ i))
376                     ))
377                 (let ((file (expand-file-name "CT" root-dir)))
378                   (and (file-exists-p file)
379                        (delete-file file)
380                        ))
381                 (save-window-excursion
382                   (setq major-mode 'mime/show-message-mode)
383                   (mime-view-mode mother)
384                   )
385                 (let ((pwin (or (get-buffer-window mother)
386                                 (get-largest-window)
387                                 ))
388                       (pbuf (save-excursion
389                               (set-buffer full-buf)
390                               mime::article/preview-buffer)))
391                   (set-window-buffer pwin pbuf)
392                   (select-window pwin)
393                   )))))
394       )))
395
396
397 ;;; @ message/external-body
398 ;;;
399
400 (defvar mime-article/dired-function
401   (if mime/use-multi-frame
402       (function dired-other-frame)
403     (function mime-article/dired-function-for-one-frame)
404     ))
405
406 (defun mime-article/dired-function-for-one-frame (dir)
407   (let ((win (or (get-buffer-window mime::article/preview-buffer)
408                  (get-largest-window))))
409     (select-window win)
410     (dired dir)
411     ))
412
413 (defun mime-article/decode-message/external-ftp (beg end cal)
414   (let* ((access-type (cdr (assoc "access-type" cal)))
415          (site (cdr (assoc "site" cal)))
416          (directory (cdr (assoc "directory" cal)))
417          (name (cdr (assoc "name" cal)))
418          (mode (cdr (assoc "mode" cal)))
419          (pathname (concat "/anonymous@" site ":" directory))
420          )
421     (message (concat "Accessing " (expand-file-name name pathname) "..."))
422     (funcall mime-article/dired-function pathname)
423     (goto-char (point-min))
424     (search-forward name)
425     ))
426
427
428 ;;; @ rot13-47
429 ;;;
430
431 (require 'view)
432
433 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
434 (define-key mime-view-text/plain-mode-map
435   "q" (function mime-view-text/plain-exit))
436
437 (defun mime-view-text/plain-mode ()
438   "\\{mime-view-text/plain-mode-map}"
439   (setq buffer-read-only t)
440   (setq major-mode 'mime-view-text/plain-mode)
441   (setq mode-name "MIME-View text/plain")
442   (use-local-map mime-view-text/plain-mode-map)
443   )
444
445 (defun mime-view-text/plain-exit ()
446   (interactive)
447   (kill-buffer (current-buffer))
448   )
449
450 (defun mime-article/decode-caesar (beg end cal)
451   (let* ((cnum (mime-article/point-content-number beg))
452          (cur-buf (current-buffer))
453          (new-name (format "%s-%s" (buffer-name) cnum))
454          (mother mime::article/preview-buffer)
455          (charset (cdr (assoc "charset" cal)))
456          (encoding (cdr (assq 'encoding cal)))
457          (mode major-mode)
458          str)
459     (setq str (buffer-substring beg end))
460     (let ((pwin (or (get-buffer-window mother)
461                     (get-largest-window)))
462           (buf (get-buffer-create new-name))
463           )
464       (set-window-buffer pwin buf)
465       (set-buffer buf)
466       (select-window pwin)
467       )
468     (setq buffer-read-only nil)
469     (erase-buffer)
470     (insert str)
471     (goto-char (point-min))
472     (if (re-search-forward "^\n" nil t)
473         (delete-region (point-min) (match-end 0))
474       )
475     (let ((m (cdr (or (assq mode mime-text-decoder-alist)
476                       (assq t mime-text-decoder-alist)))))
477       (and (functionp m)
478            (funcall m charset encoding)
479            ))
480     (save-excursion
481       (set-mark (point-min))
482       (goto-char (point-max))
483       (tm:caesar-region)
484       )
485     (set-buffer-modified-p nil)
486     (mime-view-text/plain-mode)
487     ))
488
489
490 ;;; @ end
491 ;;;
492
493 (provide 'mime-play)
494
495 ;;; mime-play.el ends here