(field-unify): Fixed.
[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.8 1997-02-28 02:33:20 tmorioka 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
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
97 ;;; @ method selector
98 ;;;
99
100 ;;; @@ field
101 ;;;
102
103 (defun put-fields (tp c)
104   (catch 'tag
105     (let ((r tp) f ret)
106       (while r
107         (setq f (car r))
108         (if (not (if (setq ret (assoc (car f) c))
109                      (equal (cdr ret)(cdr f))
110                    (setq c (cons f c))
111                    ))
112             (throw 'tag 'error))
113         (setq r (cdr r))
114         ))
115     c))
116
117
118 ;;; @@ field unifier
119 ;;;
120
121 (defun field-unifier-for-default (a b)
122   (let ((ret
123          (cond ((equal a b)    a)
124                ((null (cdr b)) a)
125                ((null (cdr a)) b)
126                )))
127     (if ret
128         (list nil ret nil)
129       )))
130
131 (defun field-unifier-for-mode (a b)
132   (let ((va (cdr a)))
133     (if (if (consp va)
134             (member (cdr b) va)
135           (equal va (cdr b))
136           )
137         (list nil b nil)
138       )))
139
140 (defun field-unify (a b)
141   (let ((sym (intern (concat "field-unifier-for-" (symbol-value (car a))))))
142     (or (fboundp sym)
143         (setq sym (function field-unifier-for-default))
144         )
145     (funcall sym a b)
146     ))
147
148
149 ;;; @@ type unifier
150 ;;;
151
152 (defun assoc-unify (class instance)
153   (catch 'tag
154     (let ((cla (copy-alist class))
155           (ins (copy-alist instance))
156           (r class)
157           cell aret ret prev rest)
158       (while r
159         (setq cell (car r))
160         (setq aret (assoc (car cell) ins))
161         (if aret
162             (if (setq ret (field-unify cell aret))
163                 (progn
164                   (if (car ret)
165                       (setq prev (put-alist (car (car ret))
166                                             (cdr (car ret))
167                                             prev))
168                     )
169                   (if (nth 2 ret)
170                       (setq rest (put-alist (car (nth 2 ret))
171                                             (cdr (nth 2 ret))
172                                             rest))
173                     )
174                   (setq cla (put-alist (car cell)(cdr (nth 1 ret)) cla))
175                   (setq ins (del-alist (car cell) ins))
176                   )
177               (throw 'tag nil)
178               ))
179         (setq r (cdr r))
180         )
181       (setq r (copy-alist ins))
182       (while r
183         (setq cell (car r))
184         (setq aret (assoc (car cell) cla))
185         (if aret
186             (if (setq ret (field-unify cell aret))
187                 (progn
188                   (if (car ret)
189                       (setq prev (put-alist (car (car ret))
190                                             (cdr (car ret))
191                                             prev))
192                     )
193                   (if (nth 2 ret)
194                       (setq rest (put-alist (car (nth 2 ret))
195                                             (cdr (nth 2 ret))
196                                             rest))
197                     )
198                   (setq cla (del-alist (car cell) cla))
199                   (setq ins (put-alist (car cell)(cdr (nth 1 ret)) ins))
200                   )
201               (throw 'tag nil)
202               ))
203         (setq r (cdr r))
204         )
205       (list prev (append cla ins) rest)
206       )))
207
208 (defun get-unified-alist (db al)
209   (let ((r db) ret)
210     (catch 'tag
211       (while r
212         (if (setq ret (nth 1 (assoc-unify (car r) al)))
213             (throw 'tag ret)
214           )
215         (setq r (cdr r))
216         ))))
217
218 (defun delete-atype (atl al)
219   (let* ((r atl) ret oal)
220     (setq oal
221           (catch 'tag
222             (while r
223               (if (setq ret (nth 1 (assoc-unify (car r) al)))
224                   (throw 'tag (car r))
225                 )
226               (setq r (cdr r))
227               )))
228     (delete oal atl)
229     ))
230
231 (defun remove-atype (sym al)
232   (and (boundp sym)
233        (set sym (delete-atype (eval sym) al))
234        ))
235
236 (defun replace-atype (atl old-al new-al)
237   (let* ((r atl) ret oal)
238     (if (catch 'tag
239           (while r
240             (if (setq ret (nth 1 (assoc-unify (car r) old-al)))
241                 (throw 'tag (rplaca r new-al))
242               )
243             (setq r (cdr r))
244             ))
245         atl)))
246
247 (defun set-atype (sym al &rest options)
248   (if (null (boundp sym))
249       (set sym al)
250     (let* ((replacement (memq 'replacement options))
251            (ignore-fields (car (cdr (memq 'ignore options))))
252            (remove (or (car (cdr (memq 'remove options)))
253                        (let ((ral (copy-alist al)))
254                          (mapcar (function
255                                   (lambda (type)
256                                     (setq ral (del-alist type ral))
257                                     ))
258                                  ignore-fields)
259                          ral)))
260            )
261       (set sym
262            (or (if replacement
263                    (replace-atype (eval sym) remove al)
264                  )
265                (cons al
266                      (delete-atype (eval sym) remove)
267                      )
268                )))))
269
270
271 ;;; @@ main selector
272 ;;;
273
274 (defun mime/get-content-decoding-alist (al)
275   (get-unified-alist mime/content-decoding-condition al)
276   )
277
278
279 ;;; @ external decoder
280 ;;;
281
282 (defun mime-article/start-external-method-region (beg end cal)
283   (save-excursion
284     (save-restriction
285       (narrow-to-region beg end)
286       (goto-char beg)
287       (let ((method (cdr (assoc 'method cal)))
288             (name (mime-article/get-filename cal))
289             )
290         (if method
291             (let ((file (make-temp-name
292                          (expand-file-name "TM" mime/tmp-dir)))
293                   b args)
294               (if (nth 1 method)
295                   (setq b beg)
296                 (setq b
297                       (if (re-search-forward "^$" nil t)
298                           (1+ (match-end 0))
299                         (point-min)
300                         ))
301                 )
302               (goto-char b)
303               (write-region b end file)
304               (message "External method is starting...")
305               (setq cal (put-alist
306                          'name (replace-as-filename name) cal))
307               (setq cal (put-alist 'file file cal))
308               (setq args (nconc
309                           (list (car method)
310                                 mime/output-buffer-name (car method)
311                                 )
312                           (mime-article/make-method-args cal
313                                                          (cdr (cdr method)))
314                           ))
315               (apply (function start-process) args)
316               (mime-article/show-output-buffer)
317               ))
318         ))))
319
320 (defun mime-article/make-method-args (cal format)
321   (mapcar (function
322            (lambda (arg)
323              (if (stringp arg)
324                  arg
325                (let* ((item (eval arg))
326                       (ret (cdr (assoc item cal)))
327                       )
328                  (if ret
329                      ret
330                    (if (eq item 'encoding)
331                        "7bit"
332                      ""))
333                  ))
334              ))
335           format))
336
337 (defun mime-article/show-output-buffer (&rest forms)
338   (get-buffer-create mime/output-buffer-name)
339   (let ((the-win (selected-window))
340         (win (get-buffer-window mime/output-buffer-name))
341         )
342     (or win
343         (if (and mime/output-buffer-window-is-shared-with-bbdb
344                  (boundp 'bbdb-buffer-name)
345                  (setq win (get-buffer-window bbdb-buffer-name))
346                  )
347             (set-window-buffer win mime/output-buffer-name)
348           (select-window (get-buffer-window mime::article/preview-buffer))
349           (setq win (split-window-vertically (/ (* (window-height) 3) 4)))
350           (set-window-buffer win mime/output-buffer-name)
351           ))
352     (select-window win)
353     (goto-char (point-max))
354     (if forms
355         (insert (apply (function format) forms))
356       )
357     (select-window the-win)
358     ))
359
360
361 ;;; @ file name
362 ;;;
363
364 (defvar mime-view-file-name-char-regexp "[A-Za-z0-9+_-]")
365
366 (defvar mime-view-file-name-regexp-1
367   (concat mime-view-file-name-char-regexp "+\\."
368           mime-view-file-name-char-regexp "+"))
369
370 (defvar mime-view-file-name-regexp-2
371   (concat (regexp-* mime-view-file-name-char-regexp)
372           "\\(\\." mime-view-file-name-char-regexp "+\\)*"))
373
374 (defun mime-article/get-original-filename (param &optional encoding)
375   (or (mime-article/get-uu-filename param encoding)
376       (let (ret)
377         (or (if (or (and (setq ret (mime/Content-Disposition))
378                          (setq ret (assoc "filename" (cdr ret)))
379                          )
380                     (setq ret (assoc "name" param))
381                     (setq ret (assoc "x-name" param))
382                     )
383                 (std11-strip-quoted-string (cdr ret))
384               )
385             (if (setq ret
386                       (std11-find-field-body '("Content-Description"
387                                                "Subject")))
388                 (if (or (string-match mime-view-file-name-regexp-1 ret)
389                         (string-match mime-view-file-name-regexp-2 ret))
390                     (substring ret (match-beginning 0)(match-end 0))
391                   ))
392             ))
393       ))
394
395 (defun mime-article/get-filename (param)
396   (replace-as-filename (mime-article/get-original-filename param))
397   )
398
399
400 ;;; @ mail/news message
401 ;;;
402
403 (defun mime-view-quitting-method-for-mime/show-message-mode ()
404   (let ((mother mime::preview/mother-buffer)
405         (win-conf mime::preview/original-window-configuration)
406         )
407     (kill-buffer
408      (mime::preview-content-info/buffer (car mime::preview/content-list)))
409     (mime-view-kill-buffer)
410     (set-window-configuration win-conf)
411     (pop-to-buffer mother)
412     ;;(goto-char (point-min))
413     ;;(mime-view-up-content)
414     ))
415
416 (defun mime-article/view-message/rfc822 (beg end cal)
417   (let* ((cnum (mime-article/point-content-number beg))
418          (cur-buf (current-buffer))
419          (new-name (format "%s-%s" (buffer-name) cnum))
420          (mother mime::article/preview-buffer)
421          (code-converter
422           (or (cdr (assq major-mode mime-text-decoder-alist))
423               'mime-view-default-code-convert-region))
424          str)
425     (setq str (buffer-substring beg end))
426     (switch-to-buffer new-name)
427     (erase-buffer)
428     (insert str)
429     (goto-char (point-min))
430     (if (re-search-forward "^\n" nil t)
431         (delete-region (point-min) (match-end 0))
432       )
433     (setq major-mode 'mime/show-message-mode)
434     (setq mime::article/code-converter code-converter)
435     (mime-view-mode mother)
436     ))
437
438
439 ;;; @ message/partial
440 ;;;
441
442 (defvar mime-article/coding-system-alist
443   (list '(mh-show-mode . no-conversion)
444         (cons t (mime-charset-to-coding-system default-mime-charset))
445         ))
446
447 (cond (running-mule-merged-emacs
448        (defun mime-article::write-region (start end file)
449          (let ((coding-system-for-write
450                 (cdr
451                  (or (assq major-mode mime-article/coding-system-alist)
452                      (assq t mime-article/coding-system-alist)
453                      ))))
454            (write-region start end file)
455            ))
456        )
457       ((or (boundp 'MULE)
458            running-xemacs-with-mule)
459        (defun mime-article::write-region (start end file)
460          (let ((file-coding-system
461                 (cdr
462                  (or (assq major-mode mime-article/coding-system-alist)
463                      (assq t mime-article/coding-system-alist)
464                      ))))
465            (write-region start end file)
466            ))
467        )
468       ((boundp 'NEMACS)
469        (defun mime-article::write-region (start end file)
470          (let ((kanji-fileio-code
471                 (cdr
472                  (or (assq major-mode mime-article/kanji-code-alist)
473                      (assq t mime-article/kanji-code-alist)
474                      ))))
475            (write-region start end file)
476            ))
477        )
478       (t
479        (defalias 'mime-article::write-region 'write-region)
480        ))
481
482 (defun mime-article/decode-message/partial (beg end cal)
483   (goto-char beg)
484   (let* ((root-dir (expand-file-name
485                     (concat "m-prts-" (user-login-name)) mime/tmp-dir))
486          (id (cdr (assoc "id" cal)))
487          (number (cdr (assoc "number" cal)))
488          (total (cdr (assoc "total" cal)))
489          file
490          (mother mime::article/preview-buffer)
491          )
492     (or (file-exists-p root-dir)
493         (make-directory root-dir)
494         )
495     (setq id (replace-as-filename id))
496     (setq root-dir (concat root-dir "/" id))
497     (or (file-exists-p root-dir)
498         (make-directory root-dir)
499         )
500     (setq file (concat root-dir "/FULL"))
501     (if (file-exists-p file)
502         (let ((full-buf (get-buffer-create "FULL"))
503               (pwin (or (get-buffer-window mother)
504                         (get-largest-window)))
505               )
506           (save-window-excursion
507             (set-buffer full-buf)
508             (erase-buffer)
509             (as-binary-input-file (insert-file-contents file))
510             (setq major-mode 'mime/show-message-mode)
511             (mime-view-mode mother)
512             )
513           (set-window-buffer pwin
514                              (save-excursion
515                                (set-buffer full-buf)
516                                mime::article/preview-buffer))
517           (select-window pwin)
518           )
519       (re-search-forward "^$")
520       (goto-char (1+ (match-end 0)))
521       (setq file (concat root-dir "/" number))
522       (mime-article::write-region (point) (point-max) file)
523       (let ((total-file (concat root-dir "/CT")))
524         (setq total
525               (if total
526                   (progn
527                     (or (file-exists-p total-file)
528                         (save-excursion
529                           (set-buffer
530                            (get-buffer-create mime/temp-buffer-name))
531                           (erase-buffer)
532                           (insert total)
533                           (write-file total-file)
534                           (kill-buffer (current-buffer))
535                           ))
536                     (string-to-number total)
537                     )
538                 (and (file-exists-p total-file)
539                      (save-excursion
540                        (set-buffer (find-file-noselect total-file))
541                        (prog1
542                            (and (re-search-forward "[0-9]+" nil t)
543                                 (string-to-number
544                                  (buffer-substring (match-beginning 0)
545                                                    (match-end 0)))
546                                 )
547                          (kill-buffer (current-buffer))
548                          )))
549                 )))
550       (if (and total (> total 0))
551           (catch 'tag
552             (save-excursion
553               (set-buffer (get-buffer-create mime/temp-buffer-name))
554               (let ((full-buf (current-buffer)))
555                 (erase-buffer)
556                 (let ((i 1))
557                   (while (<= i total)
558                     (setq file (concat root-dir "/" (int-to-string i)))
559                     (or (file-exists-p file)
560                         (throw 'tag nil)
561                         )
562                     (as-binary-input-file (insert-file-contents file))
563                     (goto-char (point-max))
564                     (setq i (1+ i))
565                     ))
566                 (as-binary-output-file (write-file (concat root-dir "/FULL")))
567                 (let ((i 1))
568                   (while (<= i total)
569                     (let ((file (format "%s/%d" root-dir i)))
570                       (and (file-exists-p file)
571                            (delete-file file)
572                            ))
573                     (setq i (1+ i))
574                     ))
575                 (let ((file (expand-file-name "CT" root-dir)))
576                   (and (file-exists-p file)
577                        (delete-file file)
578                        ))
579                 (save-window-excursion
580                   (setq major-mode 'mime/show-message-mode)
581                   (mime-view-mode mother)
582                   )
583                 (let ((pwin (or (get-buffer-window mother)
584                                 (get-largest-window)
585                                 ))
586                       (pbuf (save-excursion
587                               (set-buffer full-buf)
588                               mime::article/preview-buffer)))
589                   (set-window-buffer pwin pbuf)
590                   (select-window pwin)
591                   )))))
592       )))
593
594
595 ;;; @ rot13-47
596 ;;;
597
598 (require 'view)
599
600 (defconst mime-view-text/plain-mode-map (copy-keymap view-mode-map))
601 (define-key mime-view-text/plain-mode-map
602   "q" (function mime-view-text/plain-exit))
603
604 (defun mime-view-text/plain-mode ()
605   "\\{mime-view-text/plain-mode-map}"
606   (setq buffer-read-only t)
607   (setq major-mode 'mime-view-text/plain-mode)
608   (setq mode-name "MIME-View text/plain")
609   (use-local-map mime-view-text/plain-mode-map)
610   )
611
612 (defun mime-view-text/plain-exit ()
613   (interactive)
614   (kill-buffer (current-buffer))
615   )
616
617 (defun mime-article/decode-caesar (beg end cal)
618   (let* ((cnum (mime-article/point-content-number beg))
619          (cur-buf (current-buffer))
620          (new-name (format "%s-%s" (buffer-name) cnum))
621          (mother mime::article/preview-buffer)
622          (charset (cdr (assoc "charset" cal)))
623          (encoding (cdr (assq 'encoding cal)))
624          (mode major-mode)
625          str)
626     (setq str (buffer-substring beg end))
627     (let ((pwin (or (get-buffer-window mother)
628                     (get-largest-window)))
629           (buf (get-buffer-create new-name))
630           )
631       (set-window-buffer pwin buf)
632       (set-buffer buf)
633       (select-window pwin)
634       )
635     (setq buffer-read-only nil)
636     (erase-buffer)
637     (insert str)
638     (goto-char (point-min))
639     (if (re-search-forward "^\n" nil t)
640         (delete-region (point-min) (match-end 0))
641       )
642     (let ((m (cdr (or (assq mode mime-text-decoder-alist)
643                       (assq t mime-text-decoder-alist)))))
644       (and (functionp m)
645            (funcall m charset encoding)
646            ))
647     (save-excursion
648       (set-mark (point-min))
649       (goto-char (point-max))
650       (tm:caesar-region)
651       )
652     (set-buffer-modified-p nil)
653     (mime-view-text/plain-mode)
654     ))
655
656
657 ;;; @ end
658 ;;;
659
660 (provide 'mime-play)
661
662 ;;; mime-play.el ends here