2 ;;; A MIME viewer for GNU Emacs
4 ;;; by Morioka Tomohiko, 1994/07/13
22 (defconst mime/viewer-RCS-ID
23 "$Id: tm-view.el,v 5.21 1994/11/21 18:38:48 morioka Exp morioka $")
25 (defconst mime/viewer-version (get-version-string mime/viewer-RCS-ID))
31 (defconst mime/tspecials "\000-\040()<>@,;:\\\"/[\093?.=")
32 (defconst mime/token-regexp
33 (concat "[^" mime/tspecials "]*"))
34 (defconst mime/content-type-subtype-regexp
35 (concat mime/token-regexp "/" mime/token-regexp))
36 (defconst mime/content-parameter-value-regexp
38 message/quoted-string-regexp
41 (defconst mime/output-buffer-name "*MIME-out*")
42 (defconst mime/decoding-buffer-name "*MIME-decoding*")
48 (defvar mime/content-decoding-condition
49 ;;(setq mime/content-decoding-condition
50 '(((type . "text/plain")
51 (method "tm-plain" nil 'file 'type 'encoding 'mode 'name))
52 ;;((type . "text/x-latex")
53 ;; (method "tm-latex" nil 'file 'type 'encoding 'mode 'name))
54 ((type . "audio/basic")
55 (method "tm-au" nil 'file 'type 'encoding 'mode 'name))
57 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
58 ((type . "image/jpeg")
59 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
60 ((type . "image/tiff")
61 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
62 ((type . "image/x-tiff")
63 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
64 ((type . "image/x-xbm")
65 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
66 ((type . "image/x-pic")
67 (method "tm-image" nil 'file 'type 'encoding 'mode 'name))
68 ((type . "video/mpeg")
69 (method "tm-mpeg" nil 'file 'type 'encoding 'mode 'name))
70 ((type . "application/octet-stream")
71 (method "tm-file" nil 'file 'type 'encoding 'mode 'name))
72 ;;((type . "message/external-body")
73 ;; (method "xterm" nil
74 ;; "-e" "showexternal"
75 ;; 'file '"access-type" '"name" '"site" '"directory"))
76 ((type . "message/partial")
77 (method . mime/decode-message/partial-region))
79 "-m" "tm" "-x" "-d" "-z" "-e" 'file)(mode . "play"))
82 (defvar mime/content-filter-alist
83 '(("text/plain" . mime/decode-text/plain)))
85 (defvar mime/make-content-subject-function
87 (lambda (cid subj ctype)
89 (format "[%s %s (%s)]\n"
93 (format "%s" (+ num 1))
100 (defvar mime/make-content-header-filter
101 ;;(setq mime/make-content-header-filter
105 (delete-region (goto-char (point-min))
106 (or (and (re-search-forward "^$" nil t)
113 (defvar mime/default-showing-Content-Type-list
114 ;;(setq mime/default-showing-Content-Type-list
115 '("text/plain" "text/richtext" "text/enriched"
116 "text/x-latex" "application/x-latex"
117 "application/octet-stream" nil))
119 (defvar mime/go-to-top-node-method-alist
120 ;;(setq mime/go-to-top-node-method-alist
121 '((gnus-article-mode . (lambda ()
122 (mime/exit-view-mode)
123 (delete-other-windows)
124 (gnus-article-show-summary)
126 (rmail-mode . (lambda ()
127 (mime/exit-view-mode)
129 (delete-other-windows)
131 (mh-show-mode . (lambda ()
132 (let ((win (get-buffer-window
133 mime/output-buffer-name))
135 (nth 2 (car mime/preview-flat-content-list)))
140 (mime/exit-view-mode)
142 (let ((name (buffer-name buf)))
143 (string-match "show-" name)
144 (substring name (match-end 0))
147 (mime/show-message-mode . (lambda ()
148 (set-window-configuration
149 mime/show-mode-old-window-configuration)
150 (let ((mother mime/mother-buffer))
153 mime/preview-flat-content-list)))
154 (mime/exit-view-mode)
155 (pop-to-buffer mother)
156 (goto-char (point-min))
161 (defvar mime/use-internal-decoder nil)
163 (defvar mime/body-decoding-mode "play" "MIME body decoding mode")
169 (defun mime/parse-contents ()
172 (goto-char (point-min))
173 (let* ((ctl (mime/Content-Type))
175 (boundary (assoc "boundary" (cdr ctl)))
178 (setq ctype (downcase ctype))
180 (search-forward "\n\n" nil t)
182 (let ((sep (concat "\n--"
184 (message/strip-quoted-string
188 (setq beg (match-end 0))
189 (search-forward (concat "\n--" boundary "--\n") nil t)
190 (setq end (match-beginning 0))
193 (narrow-to-region beg end)
194 (goto-char (point-min))
195 (search-forward (concat "--" boundary "\n") nil t)
196 (setq cb (match-end 0))
197 (while (search-forward sep nil t)
198 (setq ce (match-beginning 0))
199 (setq ncb (match-end 0))
202 (narrow-to-region cb ce)
203 (setq ret (mime/parse-contents))
205 (setq dest (nconc dest (list ret)))
206 (goto-char (nth 1 ret))
207 (search-forward (concat "--" boundary "\n") nil t)
208 (goto-char (setq cb (match-end 0)))
210 (setq ce (point-max))
213 (narrow-to-region cb ce)
214 (setq ret (mime/parse-contents))
216 (setq dest (append dest (list ret)))
218 (setq beg (point-min))
220 (search-forward (concat "\n--" boundary "--\n") nil t)
221 (setq end (match-beginning 0))
223 ((string= ctype "message/rfc822")
226 (narrow-to-region (match-end 0) (point-max))
227 (setq dest (list (mime/parse-contents)))
229 (setq beg (point-min))
230 (setq end (point-max))
232 (t (setq beg (point-min))
233 (setq end (point-max))
238 (defun mime/Content-Type ()
241 (if (and (re-search-forward "^Content-Type:[ \t]*" nil t)
245 (and (re-search-forward ".*\\(\n[ \t].*\\)*" nil t)
248 (goto-char (point-min))
249 (re-search-forward mime/content-type-subtype-regexp nil t)
253 (buffer-substring (match-beginning 0) (match-end 0))
255 dest attribute value)
256 (while (and (re-search-forward "[ \t\n]*;[ \t\n]*" nil t)
257 (re-search-forward mime/token-regexp nil t)
261 (buffer-substring (match-beginning 0) (match-end 0))
263 (if (and (re-search-forward "=[ \t\n]*" nil t)
264 (re-search-forward mime/content-parameter-value-regexp
269 (message/strip-quoted-string
270 (buffer-substring (match-beginning 0)
278 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
281 (if (and (re-search-forward "^Content-Transfer-Encoding:[ \t]*" nil t)
282 (re-search-forward mime/token-regexp nil t)
284 (downcase (buffer-substring (match-beginning 0) (match-end 0)))
288 (defun mime/get-subject (param)
292 (or (and (setq ret (assoc "name" param))
293 (message/strip-quoted-string (cdr ret))
295 (and (setq ret (assoc "x-name" param))
296 (message/strip-quoted-string (cdr ret))
299 (narrow-to-region (point-min)
300 (or (and (search-forward "\n\n" nil t)
305 (message/get-field-body "Content-Description")
306 (message/get-field-body "Subject")
311 (defun mime/get-name (param)
312 (replace-as-filename (mime/get-subject param))
315 (defun mime/make-preview-buffer (&optional buf cl obuf)
316 (let ((the-buf (current-buffer)) fcl)
318 (setq buf (current-buffer))
319 (setq buf (get-buffer buf))
323 (switch-to-buffer buf)
324 (setq cl mime/content-list)
327 (setq obuf (concat "*Preview-" (buffer-name buf) "*"))
329 (setq fcl (mime/make-flat-content-list cl))
330 (if (get-buffer obuf)
333 (let ((r fcl) cell cid ctype beg end e nb ne subj dest str)
336 (setq beg (car cell))
337 (setq end (nth 1 cell))
338 (setq cid (mime/get-point-content-number beg cl))
339 (switch-to-buffer buf)
342 (narrow-to-region beg end)
344 (setq ctype (mime/Content-Type))
346 (if (not (member (car ctype)
347 mime/default-showing-Content-Type-list))
350 (search-forward "\n\n" nil t)
356 (if (> e (point-max))
359 (setq str (buffer-substring beg e))
360 (switch-to-buffer obuf)
366 (narrow-to-region nb ne)
367 (mime/decode-message-header)
368 (setq subj (mime/get-subject (cdr ctype)))
369 (let ((f (cdr (assoc (car ctype) mime/content-filter-alist))))
370 (if (and f (fboundp f))
373 (funcall mime/make-content-header-filter cid)
375 (funcall mime/make-content-subject-function cid subj ctype)
376 (setq ne (point-max))
377 (setq dest (nconc dest (list (list nb (- ne 1) buf beg end))))
382 (set-buffer-modified-p nil)
383 (setq buffer-read-only t)
384 (switch-to-buffer the-buf)
388 (defun mime/parse-message ()
390 (make-variable-buffer-local 'mime/content-list)
391 (setq mime/content-list (mime/parse-contents))
392 (let ((ret (mime/make-preview-buffer)))
393 (make-variable-buffer-local 'mime/preview-buffer)
394 (setq mime/preview-buffer (car ret))
397 ;;; @ content information
400 (defun mime/get-point-content-number (p &optional cl)
402 (setq cl mime/content-list)
408 (if (and (<= b p)(<= p e))
409 (or (let (co ret (sn 0))
413 (setq ret (mime/get-point-content-number p co))
414 (cond ((eq ret t) (throw 'tag (list sn)))
415 (ret (throw 'tag (cons sn ret)))
422 (defun mime/get-content-region (cn &optional cl)
424 (setq cl mime/content-list)
431 (let ((rcl (nth sn (nth 2 cl))))
433 (mime/get-content-region (cdr cn) rcl)
437 (defun mime/make-flat-content-list (&optional cl)
439 (setq cl mime/content-list)
441 (let ((dest (list cl))
445 (setq dest (append dest (mime/make-flat-content-list (car rcl))))
450 (defun mime/get-point-preview-content (p &optional fcl)
452 (setq fcl mime/preview-flat-content-list)
458 (if (and (<= (car cell) p)(<= p (nth 1 cell)))
470 (defun mime/Quoted-Printable-decode-region (beg end)
474 (narrow-to-region beg end)
475 (goto-char (point-min))
476 (while (re-search-forward "=\n" nil t)
479 (goto-char (point-min))
481 (while (re-search-forward mime/Quoted-Printable-octet-regexp nil t)
482 (setq b (match-beginning 0))
483 (setq e (match-end 0))
484 (setq str (buffer-substring b e))
486 (insert (mime/Quoted-Printable-decode-string str))
490 (defun mime/Base64-decode-region (beg end)
494 (narrow-to-region beg end)
495 (goto-char (point-min))
496 (while (search-forward "\n" nil t)
499 (let ((str (buffer-substring (point-min)(point-max))))
500 (delete-region (point-min)(point-max))
501 (insert (mime/base64-decode-string str))
504 (defun mime/make-method-args (cal format)
509 (let ((ret (cdr (assoc (eval arg) cal))))
517 (defun mime/start-external-method-region (beg end cal)
519 (if (< end (point-max))
524 (narrow-to-region beg e)
526 (let ((method (cdr (assoc 'method cal)))
527 (name (mime/get-name cal))
530 (let ((file (make-temp-name
531 (expand-file-name "TM" mime/tmp-dir)))
535 (search-forward "\n\n" nil t)
536 (setq b (match-end 0))
539 (write-region b e file)
541 'name (replace-as-filename name) cal))
542 (setq cal (put-alist 'file file cal))
545 mime/output-buffer-name (car method)
547 (mime/make-method-args cal (cdr (cdr method)))
549 (apply (function start-process) args)
550 (mime/show-output-buffer)
554 (defun mime/decode-message/partial-region (beg end cal)
556 (let* ((root-dir (expand-file-name
557 (concat "m-prts-" (user-login-name)) mime/tmp-dir))
558 (id (cdr (assoc "id" cal)))
559 (number (cdr (assoc "number" cal)))
560 (total (cdr (assoc "total" cal)))
561 (the-buf (current-buffer))
563 (mother mime/preview-buffer))
564 (if (not (file-exists-p root-dir))
565 (shell-command (concat "mkdir " root-dir))
567 (setq id (replace-as-filename id))
568 (setq root-dir (concat root-dir "/" id))
569 (if (not (file-exists-p root-dir))
570 (shell-command (concat "mkdir " root-dir))
572 (setq file (concat root-dir "/FULL"))
573 (if (not (file-exists-p file))
575 (re-search-forward "^$")
576 (goto-char (+ (match-end 0) 1))
577 (setq file (concat root-dir "/" number))
578 (write-region (point) (point-max) file)
579 (if (get-buffer "*MIME-temp*")
580 (kill-buffer "*MIME-temp*")
582 (switch-to-buffer "*MIME-temp*")
584 (max (string-to-int total))
588 (setq file (concat root-dir "/" (int-to-string i)))
589 (if (not (file-exists-p file))
591 (switch-to-buffer the-buf)
594 (insert-file-contents file)
595 (goto-char (point-max))
598 (delete-other-windows)
599 (write-file (concat root-dir "/FULL"))
600 (setq major-mode 'mime/show-message-mode)
601 (mime/viewer-mode mother)
602 (pop-to-buffer (current-buffer))
606 (delete-other-windows)
608 (setq major-mode 'mime/show-message-mode)
609 (mime/viewer-mode mother)
610 (pop-to-buffer (current-buffer))
614 (defun mime/get-content-decoding-alist (al)
615 (get-unified-alist mime/content-decoding-condition al)
618 (defun mime/decode-content-region (beg end)
623 (narrow-to-region beg end)
625 (setq ctl (mime/Content-Type))
627 (setq encoding (mime/Content-Transfer-Encoding "7bit"))
630 (let ((ctype (downcase (car ctl))) method cal ret)
632 (setq cal (nconc (list (cons 'type ctype)
633 (cons 'encoding encoding)
634 (cons 'major-mode major-mode)
637 (if mime/body-decoding-mode
639 (cons 'mode mime/body-decoding-mode)
642 (setq ret (mime/get-content-decoding-alist cal))
643 (setq method (cdr (assoc 'method ret)))
644 (cond ((and (symbolp method)
646 (funcall method beg end ret)
648 ((and (listp method)(stringp (car method)))
649 (mime/start-external-method-region beg end ret)
651 (t (mime/show-output-buffer
652 "No method are specified for %s\n" ctype)
657 (defun mime/show-output-buffer (&rest forms)
658 (let ((the-buf (current-buffer)))
659 (if (null (get-buffer-window mime/output-buffer-name))
660 (split-window-vertically (/ (* (window-height) 3) 4))
662 (pop-to-buffer mime/output-buffer-name)
663 (goto-char (point-max))
665 (insert (apply (function format) forms))
667 (pop-to-buffer the-buf)
674 (defun mime/decode-text/plain (ctl)
678 (let ((charset (cdr (assoc "charset" (cdr ctl))))
682 (goto-char (point-min))
683 (narrow-to-region (point-min)
684 (or (and (search-forward "\n\n" nil t)
687 (goto-char (point-min))
688 (mime/Content-Transfer-Encoding "7bit")
690 (beg (point-min)) (end (point-max))
692 (goto-char (point-min))
693 (if (search-forward "\n\n" nil t)
694 (setq beg (match-end 0))
696 (if (cond ((string= encoding "quoted-printable")
697 (mime/Quoted-Printable-decode-region beg end)
699 ((string= encoding "base64")
700 (mime/Base64-decode-region beg end)
702 (mime/code-convert-region-to-emacs beg (point-max) charset)
707 ;;; @ MIME viewer mode
710 (defvar mime/viewer-mode-map nil)
711 (if (null mime/viewer-mode-map)
713 (setq mime/viewer-mode-map (make-keymap))
714 (suppress-keymap mime/viewer-mode-map)
715 (define-key mime/viewer-mode-map "u" 'mime/up-content)
716 (define-key mime/viewer-mode-map "p" 'mime/previous-content)
717 (define-key mime/viewer-mode-map "n" 'mime/next-content)
718 (define-key mime/viewer-mode-map " " 'mime/scroll-up-content)
719 (define-key mime/viewer-mode-map "\M- " 'mime/scroll-down-content)
720 (define-key mime/viewer-mode-map "\177" 'mime/scroll-down-content)
721 (define-key mime/viewer-mode-map "\C-m" 'mime/next-line-content)
722 (define-key mime/viewer-mode-map "\C-\M-m" 'mime/previous-line-content)
723 (define-key mime/viewer-mode-map "v" 'mime/play-content)
724 (define-key mime/viewer-mode-map "e" 'mime/extract-content)
725 (define-key mime/viewer-mode-map "\C-c\C-p" 'mime/print-content)
726 (define-key mime/viewer-mode-map "q" 'mime/quit-view-mode)
727 (define-key mime/viewer-mode-map "\C-c\C-x" 'mime/exit-view-mode)
730 (defun mime/viewer-mode (&optional mother)
731 "Major mode for viewing MIME message.
733 u Move to upper content
734 p Move to previous content
735 n Move to next content
739 RET Move to next line
740 M-RET Move to previous line
741 v Decode the content as `play mode'
742 e Decode the content as `extract mode'
743 C-c C-p Decode the content as `print mode'
747 (let ((buf (get-buffer mime/output-buffer-name))
748 (the-buf (current-buffer))
752 (switch-to-buffer buf)
754 (switch-to-buffer the-buf)
756 (let ((ret (mime/parse-message))
758 (switch-to-buffer (car ret))
759 (setq major-mode 'mime/viewer-mode)
760 (setq mode-name "MIME-View")
761 (make-variable-buffer-local 'mime/viewer-original-major-mode)
762 (setq mime/viewer-original-major-mode
765 (make-variable-buffer-local
766 'mime/show-mode-old-window-configuration)
767 (setq mime/show-mode-old-window-configuration
768 (current-window-configuration))
769 (make-variable-buffer-local 'mime/mother-buffer)
770 (setq mime/mother-buffer mother)
771 'mime/show-message-mode)
773 (use-local-map mime/viewer-mode-map)
774 (make-variable-buffer-local 'mime/preview-flat-content-list)
775 (setq mime/preview-flat-content-list (nth 1 ret))
777 (let ((ce (nth 1 (car mime/preview-flat-content-list)))
779 (goto-char (point-min))
780 (search-forward "\n\n" nil t)
781 (setq e (match-end 0))
785 (run-hooks 'mime/viewer-mode-hook)
788 (defun mime/decode-content ()
790 (let ((pc (mime/get-point-preview-content (point))))
792 (let ((the-buf (current-buffer)))
793 (switch-to-buffer (nth 2 pc))
794 (mime/decode-content-region (nth 3 pc)(nth 4 pc))
795 (if (eq (current-buffer) (nth 2 pc))
796 (switch-to-buffer the-buf)
800 (defun mime/play-content ()
802 (let ((mime/body-decoding-mode "play"))
803 (mime/decode-content)
806 (defun mime/extract-content ()
808 (let ((mime/body-decoding-mode "extract"))
809 (mime/decode-content)
812 (defun mime/print-content ()
814 (let ((mime/body-decoding-mode "print"))
815 (mime/decode-content)
818 (defun mime/up-content ()
820 (let ((pc (mime/get-point-preview-content (point)))
821 (the-buf (current-buffer))
823 (switch-to-buffer (nth 2 pc))
824 (setq cn (mime/get-point-content-number (nth 3 pc)))
826 (mime/quit-view-mode the-buf (nth 2 pc))
827 (setq r (mime/get-content-region (butlast cn)))
828 (switch-to-buffer the-buf)
830 (let ((rfcl mime/preview-flat-content-list) cell)
832 (setq cell (car rfcl))
833 (if (and (= (car r)(nth 3 cell))
834 (= (nth 1 r)(nth 4 cell))
837 (goto-char (nth 0 cell))
840 (setq rfcl (cdr rfcl))
844 (defun mime/previous-content ()
846 (let* ((fcl mime/preview-flat-content-list)
848 (i (- (length fcl) 1))
852 (if (> p (car (nth i fcl)))
853 (throw 'tag (goto-char (car (nth i fcl))))
859 (defun mime/next-content ()
861 (let ((fcl mime/preview-flat-content-list)
866 (if (< p (car (car fcl)))
867 (throw 'tag (goto-char (car (car fcl))))
873 (defun mime/scroll-up-content (&optional h)
876 (setq h (- (window-height) 1))
878 (let ((fcl mime/preview-flat-content-list)
881 (setq np (or (catch 'tag
883 (if (< p (car (car fcl)))
884 (throw 'tag (car (car fcl)))
894 (defun mime/scroll-down-content (&optional h)
897 (setq h (- (window-height) 1))
899 (let ((fcl mime/preview-flat-content-list)
902 (setq pp (or (let ((i (- (length fcl) 1)))
905 (if (> p (nth 1 (nth i fcl)))
906 (throw 'tag (nth 1 (nth i fcl)))
916 (defun mime/next-line-content ()
918 (mime/scroll-up-content 1)
921 (defun mime/previous-line-content ()
923 (mime/scroll-down-content 1)
926 (defun mime/quit-view-mode (&optional the-buf buf)
929 (setq the-buf (current-buffer))
932 (setq buf (nth 2 (mime/get-point-preview-content (point))))
935 (switch-to-buffer buf)
936 (assoc major-mode mime/go-to-top-node-method-alist)
940 (switch-to-buffer the-buf)
945 (defun mime/exit-view-mode ()
947 (kill-buffer (current-buffer))
950 (fset 'mime/view-mode 'mime/viewer-mode)
952 (run-hooks 'tm-view-load-hook)