require tl-atype.
[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.2 1997-02-24 10:00:50 tmorioka Exp $
9 ;; Keywords: mail, news, MIME, multimedia
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 'tl-atype)
32
33   
34 ;;; @ content decoder
35 ;;;
36
37 (defvar mime-preview/after-decoded-position nil)
38
39 (defun mime-preview/decode-content ()
40   (interactive)
41   (let ((pc (mime-preview/point-pcinfo (point))))
42     (if pc
43         (let ((the-buf (current-buffer)))
44           (setq mime-preview/after-decoded-position (point))
45           (set-buffer (mime::preview-content-info/buffer pc))
46           (mime-article/decode-content
47            (mime::preview-content-info/content-info pc))
48           (if (eq (current-buffer)
49                   (mime::preview-content-info/buffer pc))
50               (progn
51                 (set-buffer the-buf)
52                 (goto-char mime-preview/after-decoded-position)
53                 ))
54           ))))
55
56 (defun mime-article/decode-content (cinfo)
57   (let ((beg (mime::content-info/point-min cinfo))
58         (end (mime::content-info/point-max cinfo))
59         (ctype (or (mime::content-info/type cinfo) "text/plain"))
60         (params (mime::content-info/parameters cinfo))
61         (encoding (mime::content-info/encoding cinfo))
62         )
63     ;; Check for VM
64     (if (< beg (point-min))
65         (setq beg (point-min))
66       )
67     (if (< (point-max) end)
68         (setq end (point-max))
69       )
70     (let (method cal ret)
71       (setq cal (list* (cons 'type ctype)
72                        (cons 'encoding encoding)
73                        (cons 'major-mode major-mode)
74                        params))
75       (if mime-view-decoding-mode
76           (setq cal (cons
77                      (cons 'mode mime-view-decoding-mode)
78                      cal))
79         )
80       (setq ret (mime/get-content-decoding-alist cal))
81       (setq method (cdr (assq 'method ret)))
82       (cond ((and (symbolp method)
83                   (fboundp method))
84              (funcall method beg end ret)
85              )
86             ((and (listp method)(stringp (car method)))
87              (mime-article/start-external-method-region beg end ret)
88              )
89             (t
90              (mime-article/show-output-buffer
91               "No method are specified for %s\n" ctype)
92              ))
93       )
94     ))
95
96 (defun field-unifier-for-mode (a b)
97   (let ((va (cdr a)))
98     (if (if (consp va)
99             (member (cdr b) va)
100           (equal va (cdr b))
101           )
102         (list nil b nil)
103       )))
104
105 (defun mime/get-content-decoding-alist (al)
106   (get-unified-alist mime/content-decoding-condition al)
107   )
108
109
110 ;;; @ external decoder
111 ;;;
112
113 (defun mime-article/start-external-method-region (beg end cal)
114   (save-excursion
115     (save-restriction
116       (narrow-to-region beg end)
117       (goto-char beg)
118       (let ((method (cdr (assoc 'method cal)))
119             (name (mime-article/get-filename cal))
120             )
121         (if method
122             (let ((file (make-temp-name
123                          (expand-file-name "TM" mime/tmp-dir)))
124                   b args)
125               (if (nth 1 method)
126                   (setq b beg)
127                 (setq b
128                       (if (re-search-forward "^$" nil t)
129                           (1+ (match-end 0))
130                         (point-min)
131                         ))
132                 )
133               (goto-char b)
134               (write-region b end file)
135               (message "External method is starting...")
136               (setq cal (put-alist
137                          'name (replace-as-filename name) cal))
138               (setq cal (put-alist 'file file cal))
139               (setq args (nconc
140                           (list (car method)
141                                 mime/output-buffer-name (car method)
142                                 )
143                           (mime-article/make-method-args cal
144                                                          (cdr (cdr method)))
145                           ))
146               (apply (function start-process) args)
147               (mime-article/show-output-buffer)
148               ))
149         ))))
150
151 (defun mime-article/make-method-args (cal format)
152   (mapcar (function
153            (lambda (arg)
154              (if (stringp arg)
155                  arg
156                (let* ((item (eval arg))
157                       (ret (cdr (assoc item cal)))
158                       )
159                  (if ret
160                      ret
161                    (if (eq item 'encoding)
162                        "7bit"
163                      ""))
164                  ))
165              ))
166           format))
167
168 (defun mime-article/show-output-buffer (&rest forms)
169   (get-buffer-create mime/output-buffer-name)
170   (let ((the-win (selected-window))
171         (win (get-buffer-window mime/output-buffer-name))
172         )
173     (or win
174         (if (and mime/output-buffer-window-is-shared-with-bbdb
175                  (boundp 'bbdb-buffer-name)
176                  (setq win (get-buffer-window bbdb-buffer-name))
177                  )
178             (set-window-buffer win mime/output-buffer-name)
179           (select-window (get-buffer-window mime::article/preview-buffer))
180           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
181           (set-window-buffer win mime/output-buffer-name)
182           ))
183     (select-window win)
184     (goto-char (point-max))
185     (if forms
186         (insert (apply (function format) forms))
187       )
188     (select-window the-win)
189     ))
190
191
192 ;;; @ file name
193 ;;;
194
195 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
196
197 (defvar mime-view-file-name-regexp-1
198   (concat mime-view-file-name-char-regexp "+\\."
199           mime-view-file-name-char-regexp "+"))
200
201 (defvar mime-view-file-name-regexp-2
202   (concat (regexp-* mime-view-file-name-char-regexp)
203           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
204
205 (defun mime-article/get-original-filename (param &optional encoding)
206   (or (mime-article/get-uu-filename param encoding)
207       (let (ret)
208         (or (if (or (and (setq ret (mime/Content-Disposition))
209                          (setq ret (assoc "filename" (cdr ret)))
210                          )
211                     (setq ret (assoc "name" param))
212                     (setq ret (assoc "x-name" param))
213                     )
214                 (std11-strip-quoted-string (cdr ret))
215               )
216             (if (setq ret
217                       (std11-find-field-body '("Content-Description"
218                                                "Subject")))
219                 (if (or (string-match mime-view-file-name-regexp-1 ret)
220                         (string-match mime-view-file-name-regexp-2 ret))
221                     (substring ret (match-beginning 0)(match-end 0))
222                   ))
223             ))
224       ))
225
226 (defun mime-article/get-filename (param)
227   (replace-as-filename (mime-article/get-original-filename param))
228   )
229
230
231 ;;; @ mail/news message
232 ;;;
233
234 (defun mime-view-quitting-method-for-mime/show-message-mode ()
235   (let ((mother mime::preview/mother-buffer)
236         (win-conf mime::preview/original-window-configuration)
237         )
238     (kill-buffer
239      (mime::preview-content-info/buffer (car mime::preview/content-list)))
240     (mime-view-kill-buffer)
241     (set-window-configuration win-conf)
242     (pop-to-buffer mother)
243     ;;(goto-char (point-min))
244     ;;(mime-view-up-content)
245     ))
246
247 (defun mime-article/view-message/rfc822 (beg end cal)
248   (let* ((cnum (mime-article/point-content-number beg))
249          (cur-buf (current-buffer))
250          (new-name (format "%s-%s" (buffer-name) cnum))
251          (mother mime::article/preview-buffer)
252          (code-converter
253           (or (cdr (assq major-mode mime-text-decoder-alist))
254               'mime-view-default-code-convert-region))
255          str)
256     (setq str (buffer-substring beg end))
257     (switch-to-buffer new-name)
258     (erase-buffer)
259     (insert str)
260     (goto-char (point-min))
261     (if (re-search-forward "^\n" nil t)
262         (delete-region (point-min) (match-end 0))
263       )
264     (setq major-mode 'mime/show-message-mode)
265     (setq mime::article/code-converter code-converter)
266     (mime/viewer-mode mother)
267     ))
268
269
270 ;;; @ message/partial
271 ;;;
272
273 (defvar mime-article/coding-system-alist
274   (list (cons 'mh-show-mode *noconv*)
275         (cons t (mime-charset-to-coding-system default-mime-charset))
276         ))
277
278 (cond (running-mule-merged-emacs
279        (defun mime-article::write-region (start end file)
280          (let ((coding-system-for-write
281                 (cdr
282                  (or (assq major-mode mime-article/coding-system-alist)
283                      (assq t mime-article/coding-system-alist)
284                      ))))
285            (write-region start end file)
286            ))
287        )
288       ((or (boundp 'MULE)
289            running-xemacs-with-mule)
290        (defun mime-article::write-region (start end file)
291          (let ((file-coding-system
292                 (cdr
293                  (or (assq major-mode mime-article/coding-system-alist)
294                      (assq t mime-article/coding-system-alist)
295                      ))))
296            (write-region start end file)
297            ))
298        )
299       ((boundp 'NEMACS)
300        (defun mime-article::write-region (start end file)
301          (let ((kanji-fileio-code
302                 (cdr
303                  (or (assq major-mode mime-article/kanji-code-alist)
304                      (assq t mime-article/kanji-code-alist)
305                      ))))
306            (write-region start end file)
307            ))
308        )
309       (t
310        (defalias 'mime-article::write-region 'write-region)
311        ))
312
313 (defun mime-article/decode-message/partial (beg end cal)
314   (goto-char beg)
315   (let* ((root-dir (expand-file-name
316                     (concat "m-prts-" (user-login-name)) mime/tmp-dir))
317          (id (cdr (assoc "id" cal)))
318          (number (cdr (assoc "number" cal)))
319          (total (cdr (assoc "total" cal)))
320          file
321          (mother mime::article/preview-buffer)
322          )
323     (or (file-exists-p root-dir)
324         (make-directory root-dir)
325         )
326     (setq id (replace-as-filename id))
327     (setq root-dir (concat root-dir "/" id))
328     (or (file-exists-p root-dir)
329         (make-directory root-dir)
330         )
331     (setq file (concat root-dir "/FULL"))
332     (if (file-exists-p file)
333         (let ((full-buf (get-buffer-create "FULL"))
334               (pwin (or (get-buffer-window mother)
335                         (get-largest-window)))
336               )
337           (save-window-excursion
338             (set-buffer full-buf)
339             (erase-buffer)
340             (as-binary-input-file (insert-file-contents file))
341             (setq major-mode 'mime/show-message-mode)
342             (mime/viewer-mode mother)
343             )
344           (set-window-buffer pwin
345                              (save-excursion
346                                (set-buffer full-buf)
347                                mime::article/preview-buffer))
348           (select-window pwin)
349           )
350       (re-search-forward "^$")
351       (goto-char (1+ (match-end 0)))
352       (setq file (concat root-dir "/" number))
353       (mime-article::write-region (point) (point-max) file)
354       (let ((total-file (concat root-dir "/CT")))
355         (setq total
356               (if total
357                   (progn
358                     (or (file-exists-p total-file)
359                         (save-excursion
360                           (set-buffer
361                            (get-buffer-create mime/temp-buffer-name))
362                           (erase-buffer)
363                           (insert total)
364                           (write-file total-file)
365                           (kill-buffer (current-buffer))
366                           ))
367                     (string-to-number total)
368                     )
369                 (and (file-exists-p total-file)
370                      (save-excursion
371                        (set-buffer (find-file-noselect total-file))
372                        (prog1
373                            (and (re-search-forward "[0-9]+" nil t)
374                                 (string-to-number
375                                  (buffer-substring (match-beginning 0)
376                                                    (match-end 0)))
377                                 )
378                          (kill-buffer (current-buffer))
379                          )))
380                 )))
381       (if (and total (> total 0))
382           (catch 'tag
383             (save-excursion
384               (set-buffer (get-buffer-create mime/temp-buffer-name))
385               (let ((full-buf (current-buffer)))
386                 (erase-buffer)
387                 (let ((i 1))
388                   (while (<= i total)
389                     (setq file (concat root-dir "/" (int-to-string i)))
390                     (or (file-exists-p file)
391                         (throw 'tag nil)
392                         )
393                     (as-binary-input-file (insert-file-contents file))
394                     (goto-char (point-max))
395                     (setq i (1+ i))
396                     ))
397                 (as-binary-output-file (write-file (concat root-dir "/FULL")))
398                 (let ((i 1))
399                   (while (<= i total)
400                     (let ((file (format "%s/%d" root-dir i)))
401                       (and (file-exists-p file)
402                            (delete-file file)
403                            ))
404                     (setq i (1+ i))
405                     ))
406                 (let ((file (expand-file-name "CT" root-dir)))
407                   (and (file-exists-p file)
408                        (delete-file file)
409                        ))
410                 (save-window-excursion
411                   (setq major-mode 'mime/show-message-mode)
412                   (mime/viewer-mode mother)
413                   )
414                 (let ((pwin (or (get-buffer-window mother)
415                                 (get-largest-window)
416                                 ))
417                       (pbuf (save-excursion
418                               (set-buffer full-buf)
419                               mime::article/preview-buffer)))
420                   (set-window-buffer pwin pbuf)
421                   (select-window pwin)
422                   )))))
423       )))
424
425
426 ;;; @ rot13-47
427 ;;;
428
429 (require 'view)
430
431 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
432 (define-key mime-view-text/plain-mode-map
433   "q" (function mime-view-text/plain-exit))
434
435 (defun mime-view-text/plain-mode ()
436   "\\{mime-view-text/plain-mode-map}"
437   (setq buffer-read-only t)
438   (setq major-mode 'mime-view-text/plain-mode)
439   (setq mode-name "MIME-View text/plain")
440   (use-local-map mime-view-text/plain-mode-map)
441   )
442
443 (defun mime-view-text/plain-exit ()
444   (interactive)
445   (kill-buffer (current-buffer))
446   )
447
448 (defun mime-article/decode-caesar (beg end cal)
449   (let* ((cnum (mime-article/point-content-number beg))
450          (cur-buf (current-buffer))
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