(mime-article/view-message/rfc822): Unused local variable `cur-buf'
[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.14 1997-03-15 20:21:47 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          (new-name (format "%s-%s" (buffer-name) cnum))
246          (mother mime::article/preview-buffer)
247          (code-converter
248           (or (cdr (assq major-mode mime-text-decoder-alist))
249               'mime-view-default-code-convert-region))
250          str)
251     (setq str (buffer-substring beg end))
252     (switch-to-buffer new-name)
253     (erase-buffer)
254     (insert str)
255     (goto-char (point-min))
256     (if (re-search-forward "^\n" nil t)
257         (delete-region (point-min) (match-end 0))
258       )
259     (setq major-mode 'mime/show-message-mode)
260     (setq mime::article/code-converter code-converter)
261     (mime-view-mode mother)
262     ))
263
264
265 ;;; @ message/partial
266 ;;;
267
268 (defvar mime-article/coding-system-alist
269   (list '(mh-show-mode . no-conversion)
270         (cons t (mime-charset-to-coding-system default-mime-charset))
271         ))
272
273 (defun mime-article::write-region (start end file)
274   (let ((coding-system-for-write
275          (cdr
276           (or (assq major-mode mime-article/coding-system-alist)
277               (assq t mime-article/coding-system-alist)
278               ))))
279     (write-region start end file)
280     ))
281
282 (defun mime-article/decode-message/partial (beg end cal)
283   (goto-char beg)
284   (let* ((root-dir
285           (expand-file-name
286            (concat "m-prts-" (user-login-name)) mime-temp-directory))
287          (id (cdr (assoc "id" cal)))
288          (number (cdr (assoc "number" cal)))
289          (total (cdr (assoc "total" cal)))
290          file
291          (mother mime::article/preview-buffer)
292          )
293     (or (file-exists-p root-dir)
294         (make-directory root-dir)
295         )
296     (setq id (replace-as-filename id))
297     (setq root-dir (concat root-dir "/" id))
298     (or (file-exists-p root-dir)
299         (make-directory root-dir)
300         )
301     (setq file (concat root-dir "/FULL"))
302     (if (file-exists-p file)
303         (let ((full-buf (get-buffer-create "FULL"))
304               (pwin (or (get-buffer-window mother)
305                         (get-largest-window)))
306               )
307           (save-window-excursion
308             (set-buffer full-buf)
309             (erase-buffer)
310             (as-binary-input-file (insert-file-contents file))
311             (setq major-mode 'mime/show-message-mode)
312             (mime-view-mode mother)
313             )
314           (set-window-buffer pwin
315                              (save-excursion
316                                (set-buffer full-buf)
317                                mime::article/preview-buffer))
318           (select-window pwin)
319           )
320       (re-search-forward "^$")
321       (goto-char (1+ (match-end 0)))
322       (setq file (concat root-dir "/" number))
323       (mime-article::write-region (point) (point-max) file)
324       (let ((total-file (concat root-dir "/CT")))
325         (setq total
326               (if total
327                   (progn
328                     (or (file-exists-p total-file)
329                         (save-excursion
330                           (set-buffer
331                            (get-buffer-create mime/temp-buffer-name))
332                           (erase-buffer)
333                           (insert total)
334                           (write-file total-file)
335                           (kill-buffer (current-buffer))
336                           ))
337                     (string-to-number total)
338                     )
339                 (and (file-exists-p total-file)
340                      (save-excursion
341                        (set-buffer (find-file-noselect total-file))
342                        (prog1
343                            (and (re-search-forward "[0-9]+" nil t)
344                                 (string-to-number
345                                  (buffer-substring (match-beginning 0)
346                                                    (match-end 0)))
347                                 )
348                          (kill-buffer (current-buffer))
349                          )))
350                 )))
351       (if (and total (> total 0))
352           (catch 'tag
353             (save-excursion
354               (set-buffer (get-buffer-create mime/temp-buffer-name))
355               (let ((full-buf (current-buffer)))
356                 (erase-buffer)
357                 (let ((i 1))
358                   (while (<= i total)
359                     (setq file (concat root-dir "/" (int-to-string i)))
360                     (or (file-exists-p file)
361                         (throw 'tag nil)
362                         )
363                     (as-binary-input-file (insert-file-contents file))
364                     (goto-char (point-max))
365                     (setq i (1+ i))
366                     ))
367                 (as-binary-output-file (write-file (concat root-dir "/FULL")))
368                 (let ((i 1))
369                   (while (<= i total)
370                     (let ((file (format "%s/%d" root-dir i)))
371                       (and (file-exists-p file)
372                            (delete-file file)
373                            ))
374                     (setq i (1+ i))
375                     ))
376                 (let ((file (expand-file-name "CT" root-dir)))
377                   (and (file-exists-p file)
378                        (delete-file file)
379                        ))
380                 (save-window-excursion
381                   (setq major-mode 'mime/show-message-mode)
382                   (mime-view-mode mother)
383                   )
384                 (let ((pwin (or (get-buffer-window mother)
385                                 (get-largest-window)
386                                 ))
387                       (pbuf (save-excursion
388                               (set-buffer full-buf)
389                               mime::article/preview-buffer)))
390                   (set-window-buffer pwin pbuf)
391                   (select-window pwin)
392                   )))))
393       )))
394
395
396 ;;; @ message/external-body
397 ;;;
398
399 (defvar mime-article/dired-function
400   (if mime/use-multi-frame
401       (function dired-other-frame)
402     (function mime-article/dired-function-for-one-frame)
403     ))
404
405 (defun mime-article/dired-function-for-one-frame (dir)
406   (let ((win (or (get-buffer-window mime::article/preview-buffer)
407                  (get-largest-window))))
408     (select-window win)
409     (dired dir)
410     ))
411
412 (defun mime-article/decode-message/external-ftp (beg end cal)
413   (let* ((access-type (cdr (assoc "access-type" cal)))
414          (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          (cur-buf (current-buffer))
452          (new-name (format "%s-%s" (buffer-name) cnum))
453          (mother mime::article/preview-buffer)
454          (charset (cdr (assoc "charset" cal)))
455          (encoding (cdr (assq 'encoding cal)))
456          (mode major-mode)
457          str)
458     (setq str (buffer-substring beg end))
459     (let ((pwin (or (get-buffer-window mother)
460                     (get-largest-window)))
461           (buf (get-buffer-create new-name))
462           )
463       (set-window-buffer pwin buf)
464       (set-buffer buf)
465       (select-window pwin)
466       )
467     (setq buffer-read-only nil)
468     (erase-buffer)
469     (insert str)
470     (goto-char (point-min))
471     (if (re-search-forward "^\n" nil t)
472         (delete-region (point-min) (match-end 0))
473       )
474     (let ((m (cdr (or (assq mode mime-text-decoder-alist)
475                       (assq t mime-text-decoder-alist)))))
476       (and (functionp m)
477            (funcall m charset encoding)
478            ))
479     (save-excursion
480       (set-mark (point-min))
481       (goto-char (point-max))
482       (tm:caesar-region)
483       )
484     (set-buffer-modified-p nil)
485     (mime-view-text/plain-mode)
486     ))
487
488
489 ;;; @ end
490 ;;;
491
492 (provide 'mime-play)
493
494 ;;; mime-play.el ends here