(mime-preview/decode-content, mime-article/decode-content): Add new
[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.19 1997-03-17 04:41:21 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 (&optional mode)
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) mode)
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 &optional mode)
58   (or mode
59       (setq mode mime-view-decoding-mode)
60       )
61   (let ((beg (mime::content-info/point-min cinfo))
62         (end (mime::content-info/point-max cinfo))
63         (ctype (or (mime::content-info/type cinfo) "text/plain"))
64         (params (mime::content-info/parameters cinfo))
65         (encoding (mime::content-info/encoding cinfo))
66         )
67     ;; Check for VM
68     (if (< beg (point-min))
69         (setq beg (point-min))
70       )
71     (if (< (point-max) end)
72         (setq end (point-max))
73       )
74     (let (method cal ret)
75       (setq cal (list* (cons 'type ctype)
76                        (cons 'encoding encoding)
77                        (cons 'major-mode major-mode)
78                        params))
79       (if mode
80           (setq cal (cons (cons 'mode mode) cal))
81         )
82       (setq ret (mime/get-content-decoding-alist cal))
83       (setq method (cdr (assq 'method ret)))
84       (cond ((and (symbolp method)
85                   (fboundp method))
86              (funcall method beg end ret)
87              )
88             ((and (listp method)(stringp (car method)))
89              (mime-article/start-external-method-region beg end ret)
90              )
91             (t
92              (mime-article/show-output-buffer
93               "No method are specified for %s\n" ctype)
94              ))
95       )
96     ))
97
98
99 ;;; @ method selector
100 ;;;
101
102 (defun mime/get-content-decoding-alist (al)
103   (get-unified-alist mime/content-decoding-condition al)
104   )
105
106
107 ;;; @ external decoder
108 ;;;
109
110 (defun mime-article/start-external-method-region (beg end cal)
111   (save-excursion
112     (save-restriction
113       (narrow-to-region beg end)
114       (goto-char beg)
115       (let ((method (cdr (assoc 'method cal)))
116             (name (mime-article/get-filename cal))
117             )
118         (if method
119             (let ((file (make-temp-name
120                          (expand-file-name "TM" mime-temp-directory)))
121                   b args)
122               (if (nth 1 method)
123                   (setq b beg)
124                 (setq b
125                       (if (re-search-forward "^$" nil t)
126                           (1+ (match-end 0))
127                         (point-min)
128                         ))
129                 )
130               (goto-char b)
131               (write-region b end file)
132               (message "External method is starting...")
133               (setq cal (put-alist
134                          'name (replace-as-filename name) cal))
135               (setq cal (put-alist 'file file cal))
136               (setq args (nconc
137                           (list (car method)
138                                 mime/output-buffer-name (car method)
139                                 )
140                           (mime-article/make-method-args cal
141                                                          (cdr (cdr method)))
142                           ))
143               (apply (function start-process) args)
144               (mime-article/show-output-buffer)
145               ))
146         ))))
147
148 (defun mime-article/make-method-args (cal format)
149   (mapcar (function
150            (lambda (arg)
151              (if (stringp arg)
152                  arg
153                (let* ((item (eval arg))
154                       (ret (cdr (assoc item cal)))
155                       )
156                  (if ret
157                      ret
158                    (if (eq item 'encoding)
159                        "7bit"
160                      ""))
161                  ))
162              ))
163           format))
164
165 (defun mime-article/show-output-buffer (&rest forms)
166   (get-buffer-create mime/output-buffer-name)
167   (let ((the-win (selected-window))
168         (win (get-buffer-window mime/output-buffer-name))
169         )
170     (or win
171         (if (and mime/output-buffer-window-is-shared-with-bbdb
172                  (boundp 'bbdb-buffer-name)
173                  (setq win (get-buffer-window bbdb-buffer-name))
174                  )
175             (set-window-buffer win mime/output-buffer-name)
176           (select-window (get-buffer-window mime::article/preview-buffer))
177           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
178           (set-window-buffer win mime/output-buffer-name)
179           ))
180     (select-window win)
181     (goto-char (point-max))
182     (if forms
183         (insert (apply (function format) forms))
184       )
185     (select-window the-win)
186     ))
187
188
189 ;;; @ file name
190 ;;;
191
192 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
193
194 (defvar mime-view-file-name-regexp-1
195   (concat mime-view-file-name-char-regexp "+\\."
196           mime-view-file-name-char-regexp "+"))
197
198 (defvar mime-view-file-name-regexp-2
199   (concat (regexp-* mime-view-file-name-char-regexp)
200           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
201
202 (defun mime-article/get-original-filename (param &optional encoding)
203   (or (mime-article/get-uu-filename param encoding)
204       (let (ret)
205         (or (if (or (and (setq ret (mime/Content-Disposition))
206                          (setq ret (assoc "filename" (cdr ret)))
207                          )
208                     (setq ret (assoc "name" param))
209                     (setq ret (assoc "x-name" param))
210                     )
211                 (std11-strip-quoted-string (cdr ret))
212               )
213             (if (setq ret
214                       (std11-find-field-body '("Content-Description"
215                                                "Subject")))
216                 (if (or (string-match mime-view-file-name-regexp-1 ret)
217                         (string-match mime-view-file-name-regexp-2 ret))
218                     (substring ret (match-beginning 0)(match-end 0))
219                   ))
220             ))
221       ))
222
223 (defun mime-article/get-filename (param)
224   (replace-as-filename (mime-article/get-original-filename param))
225   )
226
227
228 ;;; @ mail/news message
229 ;;;
230
231 (defun mime-view-quitting-method-for-mime/show-message-mode ()
232   (let ((mother mime::preview/mother-buffer)
233         (win-conf mime::preview/original-window-configuration)
234         )
235     (kill-buffer
236      (mime::preview-content-info/buffer (car mime::preview/content-list)))
237     (mime-view-kill-buffer)
238     (set-window-configuration win-conf)
239     (pop-to-buffer mother)
240     ;;(goto-char (point-min))
241     ;;(mime-view-up-content)
242     ))
243
244 (defun mime-article/view-message/rfc822 (beg end cal)
245   (let* ((cnum (mime-article/point-content-number beg))
246          (new-name (format "%s-%s" (buffer-name) cnum))
247          (mother mime::article/preview-buffer)
248          (text-decoder
249           (cdr (or (assq major-mode mime-text-decoder-alist)
250                    (assq t mime-text-decoder-alist))))
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-text-decoder text-decoder)
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* ((site (cdr (assoc "site" cal)))
415          (directory (cdr (assoc "directory" cal)))
416          (name (cdr (assoc "name" cal)))
417          ;;(mode (cdr (assoc "mode" cal)))
418          (pathname (concat "/anonymous@" site ":" directory))
419          )
420     (message (concat "Accessing " (expand-file-name name pathname) "..."))
421     (funcall mime-article/dired-function pathname)
422     (goto-char (point-min))
423     (search-forward name)
424     ))
425
426
427 ;;; @ rot13-47
428 ;;;
429
430 (require 'view)
431
432 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
433 (define-key mime-view-text/plain-mode-map
434   "q" (function mime-view-text/plain-exit))
435
436 (defun mime-view-text/plain-mode ()
437   "\\{mime-view-text/plain-mode-map}"
438   (setq buffer-read-only t)
439   (setq major-mode 'mime-view-text/plain-mode)
440   (setq mode-name "MIME-View text/plain")
441   (use-local-map mime-view-text/plain-mode-map)
442   )
443
444 (defun mime-view-text/plain-exit ()
445   (interactive)
446   (kill-buffer (current-buffer))
447   )
448
449 (defun mime-article/decode-caesar (beg end cal)
450   (let* ((cnum (mime-article/point-content-number beg))
451          (new-name (format "%s-%s" (buffer-name) cnum))
452          (mother mime::article/preview-buffer)
453          (charset (cdr (assoc "charset" cal)))
454          (encoding (cdr (assq 'encoding cal)))
455          (mode major-mode)
456          str)
457     (setq str (buffer-substring beg end))
458     (let ((pwin (or (get-buffer-window mother)
459                     (get-largest-window)))
460           (buf (get-buffer-create new-name))
461           )
462       (set-window-buffer pwin buf)
463       (set-buffer buf)
464       (select-window pwin)
465       )
466     (setq buffer-read-only nil)
467     (erase-buffer)
468     (insert str)
469     (goto-char (point-min))
470     (if (re-search-forward "^\n" nil t)
471         (delete-region (point-min) (match-end 0))
472       )
473     (let ((m (cdr (or (assq mode mime-text-decoder-alist)
474                       (assq t mime-text-decoder-alist)))))
475       (and (functionp m)
476            (funcall m charset encoding)
477            ))
478     (save-excursion
479       (set-mark (point-min))
480       (goto-char (point-max))
481       (tm:caesar-region)
482       )
483     (set-buffer-modified-p nil)
484     (mime-view-text/plain-mode)
485     ))
486
487
488 ;;; @ end
489 ;;;
490
491 (provide 'mime-play)
492
493 ;;; mime-play.el ends here