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.
31 (defvar mm-xemacs-p (string-match "XEmacs" (emacs-version)))
33 (defgroup mime-display ()
34 "Display of MIME in mail and news articles."
35 :link '(custom-manual "(emacs-mime)Customization")
39 ;;; Convenience macros.
41 (defmacro mm-handle-buffer (handle)
43 (defmacro mm-handle-type (handle)
45 (defsubst mm-handle-media-type (handle)
46 (if (stringp (car handle))
48 (car (mm-handle-type handle))))
49 (defsubst mm-handle-media-supertype (handle)
50 (car (split-string (mm-handle-media-type handle) "/")))
51 (defsubst mm-handle-media-subtype (handle)
52 (cadr (split-string (mm-handle-media-type handle) "/")))
53 (defmacro mm-handle-encoding (handle)
55 (defmacro mm-handle-undisplayer (handle)
57 (defmacro mm-handle-set-undisplayer (handle function)
58 `(setcar (nthcdr 3 ,handle) ,function))
59 (defmacro mm-handle-disposition (handle)
61 (defmacro mm-handle-description (handle)
63 (defmacro mm-handle-cache (handle)
65 (defmacro mm-handle-set-cache (handle contents)
66 `(setcar (nthcdr 6 ,handle) ,contents))
67 (defmacro mm-handle-id (handle)
69 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
70 disposition description cache
72 `(list ,buffer ,type ,encoding ,undisplayer
73 ,disposition ,description ,cache ,id))
75 (defcustom mm-inline-media-tests
79 (mm-valid-and-fit-image-p 'jpeg handle)))
83 (mm-valid-and-fit-image-p 'png handle)))
87 (mm-valid-and-fit-image-p 'gif handle)))
91 (mm-valid-and-fit-image-p 'tiff handle)) )
95 (mm-valid-and-fit-image-p 'xbm handle)))
99 (mm-valid-and-fit-image-p 'xbm handle)))
103 (mm-valid-and-fit-image-p 'xpm handle)))
107 (mm-valid-and-fit-image-p 'xpm handle)))
111 (mm-valid-and-fit-image-p 'bmp handle)))
112 ("text/plain" mm-inline-text identity)
113 ("text/enriched" mm-inline-text identity)
114 ("text/richtext" mm-inline-text identity)
115 ("text/x-patch" mm-display-patch-inline
117 (locate-library "diff-mode")))
121 (locate-library "w3")))
125 (or (featurep 'vcard)
126 (locate-library "vcard"))))
127 ("message/delivery-status" mm-inline-text identity)
128 ("message/rfc822" mm-inline-message identity)
129 ("text/.*" mm-inline-text identity)
130 ("audio/wav" mm-inline-audio
132 (and (or (featurep 'nas-sound) (featurep 'native-sound))
133 (device-sound-enabled-p))))
137 (and (or (featurep 'nas-sound) (featurep 'native-sound))
138 (device-sound-enabled-p))))
139 ("application/pgp-signature" ignore identity)
140 ("multipart/alternative" ignore identity)
141 ("multipart/mixed" ignore identity)
142 ("multipart/related" ignore identity))
143 "Alist of media types/tests saying whether types can be displayed inline."
144 :type '(repeat (list (string :tag "MIME type")
145 (function :tag "Display function")
146 (function :tag "Display test")))
147 :group 'mime-display)
149 (defcustom mm-inlined-types
150 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
151 "application/pgp-signature")
152 "List of media types that are to be displayed inline."
153 :type '(repeat string)
154 :group 'mime-display)
156 (defcustom mm-automatic-display
157 '("text/plain" "text/enriched" "text/richtext" "text/html"
158 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
159 "message/rfc822" "text/x-patch" "application/pgp-signature")
160 "A list of MIME types to be displayed automatically."
161 :type '(repeat string)
162 :group 'mime-display)
164 (defcustom mm-attachment-override-types '("text/x-vcard")
165 "Types to have \"attachment\" ignored if they can be displayed inline."
166 :type '(repeat string)
167 :group 'mime-display)
169 (defcustom mm-inline-override-types nil
170 "Types to be treated as attachments even if they can be displayed inline."
171 :type '(repeat string)
172 :group 'mime-display)
174 (defcustom mm-automatic-external-display nil
175 "List of MIME type regexps that will be displayed externally automatically."
176 :type '(repeat string)
177 :group 'mime-display)
179 (defcustom mm-discouraged-alternatives nil
180 "List of MIME types that are discouraged when viewing multipart/alternative.
181 Viewing agents are supposed to view the last possible part of a message,
182 as that is supposed to be the richest. However, users may prefer other
183 types instead, and this list says what types are most unwanted. If,
184 for instance, text/html parts are very unwanted, and text/richtech are
185 somewhat unwanted, then the value of this variable should be set
188 (\"text/html\" \"text/richtext\")"
189 :type '(repeat string)
190 :group 'mime-display)
192 (defvar mm-tmp-directory
193 (cond ((fboundp 'temp-directory) (temp-directory))
194 ((boundp 'temporary-file-directory) temporary-file-directory)
196 "Where mm will store its temporary files.")
198 (defcustom mm-inline-large-images nil
199 "If non-nil, then all images fit in the buffer."
201 :group 'mime-display)
203 ;;; Internal variables.
205 (defvar mm-dissection-list nil)
206 (defvar mm-last-shell-command "")
207 (defvar mm-content-id-alist nil)
211 (defun mm-dissect-buffer (&optional no-strict-mime)
212 "Dissect the current buffer and return a list of MIME handles."
214 (let (ct ctl type subtype cte cd description id result)
216 (mail-narrow-to-head)
217 (when (or no-strict-mime
218 (mail-fetch-field "mime-version"))
219 (setq ct (mail-fetch-field "content-type")
220 ctl (ignore-errors (mail-header-parse-content-type ct))
221 cte (mail-fetch-field "content-transfer-encoding")
222 cd (mail-fetch-field "content-disposition")
223 description (mail-fetch-field "content-description")
224 id (mail-fetch-field "content-id"))))
226 (setq cte (mail-header-strip cte)))
228 (not (string-match "/" (car ctl))))
229 (mm-dissect-singlepart
231 (and cte (intern (downcase (mail-header-remove-whitespace
232 (mail-header-remove-comments
235 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
237 (setq type (split-string (car ctl) "/"))
238 (setq subtype (cadr type)
243 ((equal type "multipart")
244 (cons (car ctl) (mm-dissect-multipart ctl)))
246 (mm-dissect-singlepart
248 (and cte (intern (downcase (mail-header-remove-whitespace
249 (mail-header-remove-comments
252 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
255 (when (string-match " *<\\(.*\\)> *" id)
256 (setq id (match-string 1 id)))
257 (push (cons id result) mm-content-id-alist))
260 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
262 (if (equal "text/plain" (car ctl))
265 (let ((res (mm-make-handle
266 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
267 (push (car res) mm-dissection-list)
270 (defun mm-remove-all-parts ()
271 "Remove all MIME handles."
273 (mapcar 'mm-remove-part mm-dissection-list)
274 (setq mm-dissection-list nil))
276 (defun mm-dissect-multipart (ctl)
277 (goto-char (point-min))
278 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
279 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
282 (goto-char (point-max))
283 (if (re-search-backward close-delimiter nil t)
286 (while (search-forward boundary end t)
287 (goto-char (match-beginning 0))
291 (narrow-to-region start (point))
292 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
294 (setq start (point)))
298 (narrow-to-region start end)
299 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
302 (defun mm-copy-to-buffer ()
303 "Copy the contents of the current buffer to a fresh buffer."
305 (let ((obuf (current-buffer))
307 (goto-char (point-min))
308 (search-forward-regexp "^\n" nil t)
310 (set-buffer (generate-new-buffer " *mm*"))
311 (insert-buffer-substring obuf beg)
314 (defun mm-display-part (handle &optional no-default)
315 "Display the MIME part represented by HANDLE.
316 Returns nil if the part is removed; inline if displayed inline;
317 external if displayed external."
319 (mailcap-parse-mailcaps)
320 (if (mm-handle-displayed-p handle)
321 (mm-remove-part handle)
322 (let* ((type (mm-handle-media-type handle))
323 (method (mailcap-mime-info type)))
324 (if (mm-inlined-p handle)
327 (mm-display-inline handle)
331 (if (and (not method)
332 (equal "text" (car (split-string type))))
335 (mm-insert-inline handle (mm-get-part handle))
338 handle (or method 'mailcap-save-binary-file)))))))))
340 (defun mm-display-external (handle method)
341 "Display HANDLE using METHOD."
342 (let ((outbuf (current-buffer)))
343 (mm-with-unibyte-buffer
344 (if (functionp method)
345 (let ((cur (current-buffer)))
346 (if (eq method 'mailcap-save-binary-file)
348 (set-buffer (generate-new-buffer "*mm*"))
350 (mm-insert-part handle)
351 (let ((win (get-buffer-window cur t)))
353 (select-window win)))
354 (switch-to-buffer (generate-new-buffer "*mm*")))
355 (buffer-disable-undo)
356 (mm-set-buffer-file-coding-system mm-binary-coding-system)
357 (insert-buffer-substring cur)
358 (message "Viewing with %s" method)
359 (let ((mm (current-buffer))
360 (non-viewer (assq 'non-viewer
362 (mm-handle-media-type handle) t))))
366 (mm-save-part handle))
367 (when (and (not non-viewer)
369 (mm-handle-set-undisplayer handle mm)))))
370 ;; The function is a string to be executed.
371 (mm-insert-part handle)
372 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
373 (filename (mail-content-type-get
374 (mm-handle-disposition handle) 'filename))
375 (mime-info (mailcap-mime-info
376 (mm-handle-media-type handle) t))
377 (needsterm (or (assoc "needsterm" mime-info)
378 (assoc "needsterminal" mime-info)))
379 (copiousoutput (assoc "copiousoutput" mime-info))
381 ;; We create a private sub-directory where we store our files.
383 (set-file-modes dir 448)
385 (setq file (expand-file-name (file-name-nondirectory filename)
387 (setq file (make-temp-name (expand-file-name "mm." dir))))
388 (let ((coding-system-for-write mm-binary-coding-system))
389 (write-region (point-min) (point-max) file nil 'nomesg))
390 (message "Viewing with %s" method)
393 (start-process "*display*" nil
398 method file (mm-handle-type handle)))
399 (mm-handle-set-undisplayer handle (cons file buffer)))
400 (message "Displaying %s..." (format method file))
403 (with-current-buffer outbuf
409 (call-process shell-file-name nil
411 (generate-new-buffer "*mm*"))
415 method file (mm-handle-type handle)))
416 (if (buffer-live-p buffer)
421 (ignore-errors (delete-file file))
422 (ignore-errors (delete-directory
423 (file-name-directory file)))
424 (ignore-errors (kill-buffer buffer))))))
428 (start-process "*display*"
430 (generate-new-buffer "*mm*"))
434 method file (mm-handle-type handle)))
435 (mm-handle-set-undisplayer handle (cons file buffer)))
436 (message "Displaying %s..." (format method file))
439 (defun mm-mailcap-command (method file type-list)
440 (let ((ctl (cdr type-list))
444 (while (string-match "%{\\([^}]+\\)}\\|%s\\|%t\\|%%" method beg)
445 (push (substring method beg (match-beginning 0)) out)
446 (setq beg (match-end 0)
447 total (match-string 0 method)
448 sub (match-string 1 method))
450 ((string= total "%%")
452 ((string= total "%s")
453 (setq uses-stdin nil)
454 (push (mm-quote-arg file) out))
455 ((string= total "%t")
456 (push (mm-quote-arg (car type-list)) out))
458 (push (mm-quote-arg (or (cdr (assq (intern sub) ctl)) "")) out))))
459 (push (substring method beg (length method)) out)
463 (push (mm-quote-arg file) out)))
464 (mapconcat 'identity (nreverse out) "")))
466 (defun mm-remove-parts (handles)
467 "Remove the displayed MIME parts represented by HANDLE."
468 (if (and (listp handles)
469 (bufferp (car handles)))
470 (mm-remove-part handles)
472 (while (setq handle (pop handles))
478 (stringp (car handle)))
479 (mm-remove-parts (cdr handle)))
481 (mm-remove-part handle)))))))
483 (defun mm-destroy-parts (handles)
484 "Remove the displayed MIME parts represented by HANDLE."
485 (if (and (listp handles)
486 (bufferp (car handles)))
487 (mm-destroy-part handles)
489 (while (setq handle (pop handles))
495 (stringp (car handle)))
496 (mm-destroy-parts (cdr handle)))
498 (mm-destroy-part handle)))))))
500 (defun mm-remove-part (handle)
501 "Remove the displayed MIME part represented by HANDLE."
503 (let ((object (mm-handle-undisplayer handle)))
506 ;; Internally displayed part.
507 ((mm-annotationp object)
508 (delete-annotation object))
509 ((or (functionp object)
511 (eq (car object) 'lambda)))
513 ;; Externally displayed part.
515 (ignore-errors (delete-file (car object)))
516 (ignore-errors (delete-directory (file-name-directory (car object))))
517 (ignore-errors (kill-buffer (cdr object))))
519 (when (buffer-live-p object)
520 (kill-buffer object)))))
521 (mm-handle-set-undisplayer handle nil))))
523 (defun mm-display-inline (handle)
524 (let* ((type (mm-handle-media-type handle))
525 (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
526 (funcall function handle)
527 (goto-char (point-min))))
529 (defun mm-assoc-string-match (alist type)
531 (when (string-match (car elem) type)
534 (defun mm-inlinable-p (handle)
535 "Say whether HANDLE can be displayed inline."
536 (let ((alist mm-inline-media-tests)
537 (type (mm-handle-media-type handle))
540 (when (string-match (caar alist) type)
541 (setq test (caddar alist)
543 (setq test (funcall test handle)))
547 (defun mm-automatic-display-p (handle)
548 "Say whether the user wants HANDLE to be displayed automatically."
549 (let ((methods mm-automatic-display)
550 (type (mm-handle-media-type handle))
552 (while (setq method (pop methods))
553 (when (and (not (mm-inline-override-p handle))
554 (string-match method type)
555 (mm-inlinable-p handle))
560 (defun mm-inlined-p (handle)
561 "Say whether the user wants HANDLE to be displayed automatically."
562 (let ((methods mm-inlined-types)
563 (type (mm-handle-media-type handle))
565 (while (setq method (pop methods))
566 (when (and (not (mm-inline-override-p handle))
567 (string-match method type)
568 (mm-inlinable-p handle))
573 (defun mm-attachment-override-p (handle)
574 "Say whether HANDLE should have attachment behavior overridden."
575 (let ((types mm-attachment-override-types)
576 (type (mm-handle-media-type handle))
579 (while (setq ty (pop types))
580 (when (and (string-match ty type)
581 (mm-inlinable-p handle))
582 (throw 'found t))))))
584 (defun mm-inline-override-p (handle)
585 "Say whether HANDLE should have inline behavior overridden."
586 (let ((types mm-inline-override-types)
587 (type (mm-handle-media-type handle))
590 (while (setq ty (pop types))
591 (when (string-match ty type)
592 (throw 'found t))))))
594 (defun mm-automatic-external-display-p (type)
595 "Return the user-defined method for TYPE."
596 (let ((methods mm-automatic-external-display)
598 (while (setq method (pop methods))
599 (when (string-match method type)
604 (defun mm-destroy-part (handle)
605 "Destroy the data structures connected to HANDLE."
607 (mm-remove-part handle)
608 (when (buffer-live-p (mm-handle-buffer handle))
609 (kill-buffer (mm-handle-buffer handle)))))
611 (defun mm-handle-displayed-p (handle)
612 "Say whether HANDLE is displayed or not."
613 (mm-handle-undisplayer handle))
616 ;;; Functions for outputting parts
619 (defun mm-get-part (handle)
620 "Return the contents of HANDLE as a string."
621 (mm-with-unibyte-buffer
622 (mm-insert-part handle)
625 (defun mm-insert-part (handle)
626 "Insert the contents of HANDLE in the current buffer."
627 (let ((cur (current-buffer)))
629 (if (member (mm-handle-media-supertype handle) '("text" "message"))
631 (insert-buffer-substring (mm-handle-buffer handle))
632 (mm-decode-content-transfer-encoding
633 (mm-handle-encoding handle)
634 (mm-handle-media-type handle))
635 (let ((temp (current-buffer)))
637 (insert-buffer-substring temp)))
638 (mm-with-unibyte-buffer
639 (insert-buffer-substring (mm-handle-buffer handle))
640 (mm-decode-content-transfer-encoding
641 (mm-handle-encoding handle)
642 (mm-handle-media-type handle))
643 (let ((temp (current-buffer)))
645 (insert-buffer-substring temp)))))))
647 (defvar mm-default-directory nil)
649 (defun mm-save-part (handle)
650 "Write HANDLE to a file."
651 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
652 (filename (mail-content-type-get
653 (mm-handle-disposition handle) 'filename))
656 (setq filename (file-name-nondirectory filename)))
658 (read-file-name "Save MIME part to: "
660 (or filename name "")
661 (or mm-default-directory default-directory))))
662 (setq mm-default-directory (file-name-directory file))
663 (when (or (not (file-exists-p file))
664 (yes-or-no-p (format "File %s already exists; overwrite? "
666 (mm-save-part-to-file handle file))))
668 (defun mm-save-part-to-file (handle file)
669 (mm-with-unibyte-buffer
670 (mm-insert-part handle)
671 (let ((coding-system-for-write 'binary)
672 ;; Don't re-compress .gz & al. Arguably we should make
673 ;; `file-name-handler-alist' nil, but that would chop
674 ;; ange-ftp, which is reasonable to use here.
675 (inhibit-file-name-operation 'write-region)
676 (inhibit-file-name-handlers
677 (cons 'jka-compr-handler inhibit-file-name-handlers)))
678 (write-region (point-min) (point-max) file))))
680 (defun mm-pipe-part (handle)
681 "Pipe HANDLE to a process."
682 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
684 (read-string "Shell command on MIME part: " mm-last-shell-command)))
685 (mm-with-unibyte-buffer
686 (mm-insert-part handle)
687 (shell-command-on-region (point-min) (point-max) command nil))))
689 (defun mm-interactively-view-part (handle)
690 "Display HANDLE using METHOD."
691 (let* ((type (mm-handle-media-type handle))
693 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
694 (mailcap-mime-info type 'all)))
695 (method (completing-read "Viewer: " methods)))
696 (when (string= method "")
697 (error "No method given"))
698 (mm-display-external (copy-sequence handle) method)))
700 (defun mm-preferred-alternative (handles &optional preferred)
701 "Say which of HANDLES are preferred."
702 (let ((prec (if preferred (list preferred)
703 (mm-preferred-alternative-precedence handles)))
704 p h result type handle)
705 (while (setq p (pop prec))
708 (setq handle (car h))
709 (setq type (mm-handle-media-type handle))
710 (when (and (equal p type)
711 (mm-automatic-display-p handle)
712 (or (stringp (car handle))
713 (not (mm-handle-disposition handle))
714 (equal (car (mm-handle-disposition handle))
722 (defun mm-preferred-alternative-precedence (handles)
723 "Return the precedence based on HANDLES and mm-discouraged-alternatives."
724 (let ((seq (nreverse (mapcar (lambda (h)
725 (mm-handle-media-type h))
727 (dolist (disc (reverse mm-discouraged-alternatives))
728 (dolist (elem (copy-sequence seq))
729 (when (string-match disc elem)
730 (setq seq (nconc (delete elem seq) (list elem))))))
733 (defun mm-get-content-id (id)
734 "Return the handle(s) referred to by ID."
735 (cdr (assoc id mm-content-id-alist)))
737 (defun mm-get-image-emacs (handle)
738 "Return an image instance based on HANDLE."
739 (let ((type (mm-handle-media-subtype handle))
741 ;; Allow some common translations.
744 ((equal type "x-pixmap")
746 ((equal type "x-xbitmap")
749 (or (mm-handle-cache handle)
750 (mm-with-unibyte-buffer
751 (mm-insert-part handle)
757 ;; xbm images require special handling, since
758 ;; the only way to create glyphs from these
759 ;; (without a ton of work) is to write them
760 ;; out to a file, and then create a file
762 (error "Don't know what to do for XBMs right now."))
764 (list 'image :type (intern type) :data (buffer-string))))))
765 (mm-handle-set-cache handle spec))))))
767 (defun mm-get-image-xemacs (handle)
768 "Return an image instance based on HANDLE."
769 (let ((type (mm-handle-media-subtype handle))
771 ;; Allow some common translations.
774 ((equal type "x-pixmap")
776 ((equal type "x-xbitmap")
779 (or (mm-handle-cache handle)
780 (mm-with-unibyte-buffer
781 (mm-insert-part handle)
787 ;; xbm images require special handling, since
788 ;; the only way to create glyphs from these
789 ;; (without a ton of work) is to write them
790 ;; out to a file, and then create a file
792 (let ((file (make-temp-name
793 (expand-file-name "emm.xbm"
797 (write-region (point-min) (point-max) file)
798 (make-glyph (list (cons 'x file))))
800 (delete-file file)))))
803 (vector (intern type) :data (buffer-string)))))))
804 (mm-handle-set-cache handle spec))))))
806 (defun mm-get-image (handle)
808 (mm-get-image-xemacs handle)
809 (mm-get-image-emacs handle)))
811 (defun mm-image-fit-p (handle)
812 "Say whether the image in HANDLE will fit the current window."
813 (let ((image (mm-get-image handle)))
814 (if (fboundp 'glyph-width)
815 ;; XEmacs' glyphs can actually tell us about their width, so
816 ;; lets be nice and smart about them.
817 (or mm-inline-large-images
818 (and (< (glyph-width image) (window-pixel-width))
819 (< (glyph-height image) (window-pixel-height))))
820 ;; Let's just inline everything under Emacs 21, since the image
821 ;; specification there doesn't actually get the width/height
822 ;; until you render the image.
825 (defun mm-valid-image-format-p (format)
826 "Say whether FORMAT can be displayed natively by Emacs."
829 ((fboundp 'valid-image-instantiator-format-p)
830 (valid-image-instantiator-format-p format))
832 ((fboundp 'image-type-available-p)
833 (image-type-available-p format))
834 ;; Nobody else can do images yet.
838 (defun mm-valid-and-fit-image-p (format handle)
839 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
841 (mm-valid-image-format-p format)
842 (mm-image-fit-p handle)))
846 ;; mm-decode.el ends here