1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
33 (autoload 'mm-inline-partial "mm-partial"))
35 (defgroup mime-display ()
36 "Display of MIME in mail and news articles."
37 :link '(custom-manual "(emacs-mime)Customization")
41 ;;; Convenience macros.
43 (defmacro mm-handle-buffer (handle)
45 (defmacro mm-handle-type (handle)
47 (defsubst mm-handle-media-type (handle)
48 (if (stringp (car handle))
50 (car (mm-handle-type handle))))
51 (defsubst mm-handle-media-supertype (handle)
52 (car (split-string (mm-handle-media-type handle) "/")))
53 (defsubst mm-handle-media-subtype (handle)
54 (cadr (split-string (mm-handle-media-type handle) "/")))
55 (defmacro mm-handle-encoding (handle)
57 (defmacro mm-handle-undisplayer (handle)
59 (defmacro mm-handle-set-undisplayer (handle function)
60 `(setcar (nthcdr 3 ,handle) ,function))
61 (defmacro mm-handle-disposition (handle)
63 (defmacro mm-handle-description (handle)
65 (defmacro mm-handle-cache (handle)
67 (defmacro mm-handle-set-cache (handle contents)
68 `(setcar (nthcdr 6 ,handle) ,contents))
69 (defmacro mm-handle-id (handle)
71 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
72 disposition description cache
74 `(list ,buffer ,type ,encoding ,undisplayer
75 ,disposition ,description ,cache ,id))
77 (defcustom mm-inline-media-tests
81 (mm-valid-and-fit-image-p 'jpeg handle)))
85 (mm-valid-and-fit-image-p 'png handle)))
89 (mm-valid-and-fit-image-p 'gif handle)))
93 (mm-valid-and-fit-image-p 'tiff handle)) )
97 (mm-valid-and-fit-image-p 'xbm handle)))
101 (mm-valid-and-fit-image-p 'xbm handle)))
105 (mm-valid-and-fit-image-p 'xpm handle)))
109 (mm-valid-and-fit-image-p 'xpm handle)))
113 (mm-valid-and-fit-image-p 'bmp handle)))
114 ("text/plain" mm-inline-text identity)
115 ("text/enriched" mm-inline-text identity)
116 ("text/richtext" mm-inline-text identity)
117 ("text/x-patch" mm-display-patch-inline
119 (locate-library "diff-mode")))
123 (locate-library "w3")))
127 (or (featurep 'vcard)
128 (locate-library "vcard"))))
129 ("message/delivery-status" mm-inline-text identity)
130 ("message/rfc822" mm-inline-message identity)
131 ("message/partial" mm-inline-partial identity)
132 ("text/.*" mm-inline-text identity)
133 ("audio/wav" mm-inline-audio
135 (and (or (featurep 'nas-sound) (featurep 'native-sound))
136 (device-sound-enabled-p))))
140 (and (or (featurep 'nas-sound) (featurep 'native-sound))
141 (device-sound-enabled-p))))
142 ("application/pgp-signature" ignore identity)
143 ("multipart/alternative" ignore identity)
144 ("multipart/mixed" ignore identity)
145 ("multipart/related" ignore identity))
146 "Alist of media types/tests saying whether types can be displayed inline."
147 :type '(repeat (list (string :tag "MIME type")
148 (function :tag "Display function")
149 (function :tag "Display test")))
150 :group 'mime-display)
152 (defcustom mm-inlined-types
153 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
155 "application/pgp-signature")
156 "List of media types that are to be displayed inline."
157 :type '(repeat string)
158 :group 'mime-display)
160 (defcustom mm-automatic-display
161 '("text/plain" "text/enriched" "text/richtext" "text/html"
162 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
163 "message/rfc822" "text/x-patch" "application/pgp-signature")
164 "A list of MIME types to be displayed automatically."
165 :type '(repeat string)
166 :group 'mime-display)
168 (defcustom mm-attachment-override-types '("text/x-vcard")
169 "Types to have \"attachment\" ignored if they can be displayed inline."
170 :type '(repeat string)
171 :group 'mime-display)
173 (defcustom mm-inline-override-types nil
174 "Types to be treated as attachments even if they can be displayed inline."
175 :type '(repeat string)
176 :group 'mime-display)
178 (defcustom mm-automatic-external-display nil
179 "List of MIME type regexps that will be displayed externally automatically."
180 :type '(repeat string)
181 :group 'mime-display)
183 (defcustom mm-discouraged-alternatives nil
184 "List of MIME types that are discouraged when viewing multipart/alternative.
185 Viewing agents are supposed to view the last possible part of a message,
186 as that is supposed to be the richest. However, users may prefer other
187 types instead, and this list says what types are most unwanted. If,
188 for instance, text/html parts are very unwanted, and text/richtext are
189 somewhat unwanted, then the value of this variable should be set
192 (\"text/html\" \"text/richtext\")"
193 :type '(repeat string)
194 :group 'mime-display)
196 (defvar mm-tmp-directory
197 (cond ((fboundp 'temp-directory) (temp-directory))
198 ((boundp 'temporary-file-directory) temporary-file-directory)
200 "Where mm will store its temporary files.")
202 (defcustom mm-inline-large-images nil
203 "If non-nil, then all images fit in the buffer."
205 :group 'mime-display)
207 ;;; Internal variables.
209 (defvar mm-dissection-list nil)
210 (defvar mm-last-shell-command "")
211 (defvar mm-content-id-alist nil)
213 ;; According to RFC2046, in particular, in a digest, the default
214 ;; Content-Type value for a body part is changed from "text/plain" to
216 (defvar mm-dissect-default-type "text/plain")
220 (defun mm-dissect-buffer (&optional no-strict-mime)
221 "Dissect the current buffer and return a list of MIME handles."
223 (let (ct ctl type subtype cte cd description id result)
225 (mail-narrow-to-head)
226 (when (or no-strict-mime
227 (mail-fetch-field "mime-version"))
228 (setq ct (mail-fetch-field "content-type")
229 ctl (ignore-errors (mail-header-parse-content-type ct))
230 cte (mail-fetch-field "content-transfer-encoding")
231 cd (mail-fetch-field "content-disposition")
232 description (mail-fetch-field "content-description")
233 id (mail-fetch-field "content-id"))))
235 (setq cte (mail-header-strip cte)))
237 (not (string-match "/" (car ctl))))
238 (mm-dissect-singlepart
239 (list mm-dissect-default-type)
240 (and cte (intern (downcase (mail-header-remove-whitespace
241 (mail-header-remove-comments
244 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
246 (setq type (split-string (car ctl) "/"))
247 (setq subtype (cadr type)
252 ((equal type "multipart")
253 (let ((mm-dissect-default-type (if (equal subtype "digest")
256 (cons (car ctl) (mm-dissect-multipart ctl))))
258 (mm-dissect-singlepart
260 (and cte (intern (downcase (mail-header-remove-whitespace
261 (mail-header-remove-comments
264 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
267 (when (string-match " *<\\(.*\\)> *" id)
268 (setq id (match-string 1 id)))
269 (push (cons id result) mm-content-id-alist))
272 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
274 (if (equal "text/plain" (car ctl))
277 (let ((res (mm-make-handle
278 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
279 (push (car res) mm-dissection-list)
282 (defun mm-remove-all-parts ()
283 "Remove all MIME handles."
285 (mapcar 'mm-remove-part mm-dissection-list)
286 (setq mm-dissection-list nil))
288 (defun mm-dissect-multipart (ctl)
289 (goto-char (point-min))
290 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
291 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
294 (goto-char (point-max))
295 (if (re-search-backward close-delimiter nil t)
298 (while (search-forward boundary end t)
299 (goto-char (match-beginning 0))
303 (narrow-to-region start (point))
304 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
306 (setq start (point)))
310 (narrow-to-region start end)
311 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
314 (defun mm-copy-to-buffer ()
315 "Copy the contents of the current buffer to a fresh buffer."
317 (let ((obuf (current-buffer))
319 (goto-char (point-min))
320 (search-forward-regexp "^\n" nil t)
322 (set-buffer (generate-new-buffer " *mm*"))
323 (insert-buffer-substring obuf beg)
326 (defun mm-display-part (handle &optional no-default)
327 "Display the MIME part represented by HANDLE.
328 Returns nil if the part is removed; inline if displayed inline;
329 external if displayed external."
331 (mailcap-parse-mailcaps)
332 (if (mm-handle-displayed-p handle)
333 (mm-remove-part handle)
334 (let* ((type (mm-handle-media-type handle))
335 (method (mailcap-mime-info type)))
336 (if (mm-inlined-p handle)
339 (mm-display-inline handle)
343 (if (and (not method)
344 (equal "text" (car (split-string type))))
347 (mm-insert-inline handle (mm-get-part handle))
350 handle (or method 'mailcap-save-binary-file)))))))))
352 (defun mm-display-external (handle method)
353 "Display HANDLE using METHOD."
354 (let ((outbuf (current-buffer)))
355 (mm-with-unibyte-buffer
356 (if (functionp method)
357 (let ((cur (current-buffer)))
358 (if (eq method 'mailcap-save-binary-file)
360 (set-buffer (generate-new-buffer "*mm*"))
362 (mm-insert-part handle)
363 (let ((win (get-buffer-window cur t)))
365 (select-window win)))
366 (switch-to-buffer (generate-new-buffer "*mm*")))
367 (buffer-disable-undo)
368 (mm-set-buffer-file-coding-system mm-binary-coding-system)
369 (insert-buffer-substring cur)
370 (message "Viewing with %s" method)
371 (let ((mm (current-buffer))
372 (non-viewer (assq 'non-viewer
374 (mm-handle-media-type handle) t))))
378 (mm-save-part handle))
379 (when (and (not non-viewer)
381 (mm-handle-set-undisplayer handle mm)))))
382 ;; The function is a string to be executed.
383 (mm-insert-part handle)
384 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
385 (filename (mail-content-type-get
386 (mm-handle-disposition handle) 'filename))
387 (mime-info (mailcap-mime-info
388 (mm-handle-media-type handle) t))
389 (needsterm (or (assoc "needsterm" mime-info)
390 (assoc "needsterminal" mime-info)))
391 (copiousoutput (assoc "copiousoutput" mime-info))
393 ;; We create a private sub-directory where we store our files.
395 (set-file-modes dir 448)
397 (setq file (expand-file-name (file-name-nondirectory filename)
399 (setq file (make-temp-name (expand-file-name "mm." dir))))
400 (let ((coding-system-for-write mm-binary-coding-system))
401 (write-region (point-min) (point-max) file nil 'nomesg))
402 (message "Viewing with %s" method)
405 (start-process "*display*" nil
410 method file (mm-handle-type handle)))
411 (mm-handle-set-undisplayer handle (cons file buffer)))
412 (message "Displaying %s..." (format method file))
415 (with-current-buffer outbuf
421 (call-process shell-file-name nil
423 (generate-new-buffer "*mm*"))
427 method file (mm-handle-type handle)))
428 (if (buffer-live-p buffer)
433 (ignore-errors (delete-file file))
434 (ignore-errors (delete-directory
435 (file-name-directory file)))
436 (ignore-errors (kill-buffer buffer))))))
440 (start-process "*display*"
442 (generate-new-buffer "*mm*"))
446 method file (mm-handle-type handle)))
447 (mm-handle-set-undisplayer handle (cons file buffer)))
448 (message "Displaying %s..." (format method file))
451 (defun mm-mailcap-command (method file type-list)
452 (let ((ctl (cdr type-list))
456 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
457 (push (substring method beg (match-beginning 0)) out)
458 (setq beg (match-end 0)
459 total (match-string 0 method)
460 sub (match-string 1 method))
462 ((string= total "%%")
464 ((string= total "%s")
465 (setq uses-stdin nil)
466 (push (mm-quote-arg file) out))
467 ((string= total "%t")
468 (push (mm-quote-arg (car type-list)) out))
470 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
471 (push (substring method beg (length method)) out)
475 (push (mm-quote-arg file) out)))
476 (mapconcat 'identity (nreverse out) "")))
478 (defun mm-remove-parts (handles)
479 "Remove the displayed MIME parts represented by HANDLES."
480 (if (and (listp handles)
481 (bufferp (car handles)))
482 (mm-remove-part handles)
484 (while (setq handle (pop handles))
490 (stringp (car handle)))
491 (mm-remove-parts (cdr handle)))
493 (mm-remove-part handle)))))))
495 (defun mm-destroy-parts (handles)
496 "Remove the displayed MIME parts represented by HANDLES."
497 (if (and (listp handles)
498 (bufferp (car handles)))
499 (mm-destroy-part handles)
501 (while (setq handle (pop handles))
507 (stringp (car handle)))
508 (mm-destroy-parts (cdr handle)))
510 (mm-destroy-part handle)))))))
512 (defun mm-remove-part (handle)
513 "Remove the displayed MIME part represented by HANDLE."
515 (let ((object (mm-handle-undisplayer handle)))
518 ;; Internally displayed part.
519 ((mm-annotationp object)
520 (delete-annotation object))
521 ((or (functionp object)
523 (eq (car object) 'lambda)))
525 ;; Externally displayed part.
527 (ignore-errors (delete-file (car object)))
528 (ignore-errors (delete-directory (file-name-directory (car object))))
529 (ignore-errors (kill-buffer (cdr object))))
531 (when (buffer-live-p object)
532 (kill-buffer object)))))
533 (mm-handle-set-undisplayer handle nil))))
535 (defun mm-display-inline (handle)
536 (let* ((type (mm-handle-media-type handle))
537 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
538 (funcall function handle)
539 (goto-char (point-min))))
541 (defun mm-assoc-string-match (alist type)
543 (when (string-match (car elem) type)
546 (defun mm-inlinable-p (handle)
547 "Say whether HANDLE can be displayed inline."
548 (let ((alist mm-inline-media-tests)
549 (type (mm-handle-media-type handle))
552 (when (string-match (caar alist) type)
553 (setq test (caddar alist)
555 (setq test (funcall test handle)))
559 (defun mm-automatic-display-p (handle)
560 "Say whether the user wants HANDLE to be displayed automatically."
561 (let ((methods mm-automatic-display)
562 (type (mm-handle-media-type handle))
564 (while (setq method (pop methods))
565 (when (and (not (mm-inline-override-p handle))
566 (string-match method type)
567 (mm-inlinable-p handle))
572 (defun mm-inlined-p (handle)
573 "Say whether the user wants HANDLE to be displayed automatically."
574 (let ((methods mm-inlined-types)
575 (type (mm-handle-media-type handle))
577 (while (setq method (pop methods))
578 (when (and (not (mm-inline-override-p handle))
579 (string-match method type)
580 (mm-inlinable-p handle))
585 (defun mm-attachment-override-p (handle)
586 "Say whether HANDLE should have attachment behavior overridden."
587 (let ((types mm-attachment-override-types)
588 (type (mm-handle-media-type handle))
591 (while (setq ty (pop types))
592 (when (and (string-match ty type)
593 (mm-inlinable-p handle))
594 (throw 'found t))))))
596 (defun mm-inline-override-p (handle)
597 "Say whether HANDLE should have inline behavior overridden."
598 (let ((types mm-inline-override-types)
599 (type (mm-handle-media-type handle))
602 (while (setq ty (pop types))
603 (when (string-match ty type)
604 (throw 'found t))))))
606 (defun mm-automatic-external-display-p (type)
607 "Return the user-defined method for TYPE."
608 (let ((methods mm-automatic-external-display)
610 (while (setq method (pop methods))
611 (when (string-match method type)
616 (defun mm-destroy-part (handle)
617 "Destroy the data structures connected to HANDLE."
619 (mm-remove-part handle)
620 (when (buffer-live-p (mm-handle-buffer handle))
621 (kill-buffer (mm-handle-buffer handle)))))
623 (defun mm-handle-displayed-p (handle)
624 "Say whether HANDLE is displayed or not."
625 (mm-handle-undisplayer handle))
628 ;;; Functions for outputting parts
631 (defun mm-get-part (handle)
632 "Return the contents of HANDLE as a string."
633 (mm-with-unibyte-buffer
634 (mm-insert-part handle)
637 (defun mm-insert-part (handle)
638 "Insert the contents of HANDLE in the current buffer."
639 (let ((cur (current-buffer)))
641 (if (member (mm-handle-media-supertype handle) '("text" "message"))
643 (if (eq (or (mm-handle-encoding handle)
644 (with-current-buffer (mm-handle-buffer handle)
647 ;; Emacs MULE can not handle some 8bit characters in
648 ;; multibyte character!!
649 (let ((text (with-current-buffer
650 (mm-handle-buffer handle)
651 (mm-with-unibyte-current-buffer
653 (mm-with-unibyte-current-buffer
655 (insert-buffer-substring (mm-handle-buffer handle)))
656 (mm-decode-content-transfer-encoding
657 (mm-handle-encoding handle)
658 (mm-handle-media-type handle))
659 (let ((temp (current-buffer)))
661 (insert-buffer-substring temp)))
662 (mm-with-unibyte-buffer
663 (insert-buffer-substring (mm-handle-buffer handle))
664 (mm-decode-content-transfer-encoding
665 (mm-handle-encoding handle)
666 (mm-handle-media-type handle))
667 (let ((temp (current-buffer)))
669 (insert-buffer-substring temp)))))))
671 (defvar mm-default-directory nil)
673 (defun mm-save-part (handle)
674 "Write HANDLE to a file."
675 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
676 (filename (mail-content-type-get
677 (mm-handle-disposition handle) 'filename))
680 (setq filename (file-name-nondirectory filename)))
682 (read-file-name "Save MIME part to: "
684 (or filename name "")
685 (or mm-default-directory default-directory))))
686 (setq mm-default-directory (file-name-directory file))
687 (when (or (not (file-exists-p file))
688 (yes-or-no-p (format "File %s already exists; overwrite? "
690 (mm-save-part-to-file handle file))))
692 (defun mm-save-part-to-file (handle file)
693 (mm-with-unibyte-buffer
694 (mm-insert-part handle)
695 (let ((coding-system-for-write 'binary)
696 ;; Don't re-compress .gz & al. Arguably we should make
697 ;; `file-name-handler-alist' nil, but that would chop
698 ;; ange-ftp, which is reasonable to use here.
699 (inhibit-file-name-operation 'write-region)
700 (inhibit-file-name-handlers
701 (cons 'jka-compr-handler inhibit-file-name-handlers)))
702 (write-region (point-min) (point-max) file))))
704 (defun mm-pipe-part (handle)
705 "Pipe HANDLE to a process."
706 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
708 (read-string "Shell command on MIME part: " mm-last-shell-command)))
709 (mm-with-unibyte-buffer
710 (mm-insert-part handle)
711 (shell-command-on-region (point-min) (point-max) command nil))))
713 (defun mm-interactively-view-part (handle)
714 "Display HANDLE using METHOD."
715 (let* ((type (mm-handle-media-type handle))
717 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
718 (mailcap-mime-info type 'all)))
719 (method (completing-read "Viewer: " methods)))
720 (when (string= method "")
721 (error "No method given"))
722 (if (string-match "^[^% \t]+$" method)
723 (setq method (concat method " %s")))
724 (mm-display-external (copy-sequence handle) method)))
726 (defun mm-preferred-alternative (handles &optional preferred)
727 "Say which of HANDLES are preferred."
728 (let ((prec (if preferred (list preferred)
729 (mm-preferred-alternative-precedence handles)))
730 p h result type handle)
731 (while (setq p (pop prec))
734 (setq handle (car h))
735 (setq type (mm-handle-media-type handle))
736 (when (and (equal p type)
737 (mm-automatic-display-p handle)
738 (or (stringp (car handle))
739 (not (mm-handle-disposition handle))
740 (equal (car (mm-handle-disposition handle))
748 (defun mm-preferred-alternative-precedence (handles)
749 "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
750 (let ((seq (nreverse (mapcar #'mm-handle-media-type
752 (dolist (disc (reverse mm-discouraged-alternatives))
753 (dolist (elem (copy-sequence seq))
754 (when (string-match disc elem)
755 (setq seq (nconc (delete elem seq) (list elem))))))
758 (defun mm-get-content-id (id)
759 "Return the handle(s) referred to by ID."
760 (cdr (assoc id mm-content-id-alist)))
762 (defun mm-get-image (handle)
763 "Return an image instance based on HANDLE."
764 (let ((type (mm-handle-media-subtype handle))
766 ;; Allow some common translations.
769 ((equal type "x-pixmap")
771 ((equal type "x-xbitmap")
774 (or (mm-handle-cache handle)
775 (mm-with-unibyte-buffer
776 (mm-insert-part handle)
780 (if (fboundp 'make-glyph)
783 ;; xbm images require special handling, since
784 ;; the only way to create glyphs from these
785 ;; (without a ton of work) is to write them
786 ;; out to a file, and then create a file
788 (let ((file (make-temp-name
789 (expand-file-name "emm.xbm"
793 (write-region (point-min) (point-max) file)
794 (make-glyph (list (cons 'x file))))
796 (delete-file file)))))
799 (vector (intern type) :data (buffer-string)))))
800 (create-image (buffer-string) (intern type) 'data-p))))
801 (mm-handle-set-cache handle spec))))))
803 (defun mm-image-fit-p (handle)
804 "Say whether the image in HANDLE will fit the current window."
805 (let ((image (mm-get-image handle)))
806 (if (fboundp 'glyph-width)
807 ;; XEmacs' glyphs can actually tell us about their width, so
808 ;; lets be nice and smart about them.
809 (or mm-inline-large-images
810 (and (< (glyph-width image) (window-pixel-width))
811 (< (glyph-height image) (window-pixel-height))))
812 ;; Let's just inline everything under Emacs 21, since the image
813 ;; specification there doesn't actually get the width/height
814 ;; until you render the image.
817 (defun mm-valid-image-format-p (format)
818 "Say whether FORMAT can be displayed natively by Emacs."
821 ((fboundp 'valid-image-instantiator-format-p)
822 (valid-image-instantiator-format-p format))
824 ((fboundp 'image-type-available-p)
825 (and (display-graphic-p)
826 (image-type-available-p format)))
827 ;; Nobody else can do images yet.
831 (defun mm-valid-and-fit-image-p (format handle)
832 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
834 (mm-valid-image-format-p format)
835 (mm-image-fit-p handle)))
839 ;;; mm-decode.el ends here