1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998,99 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 ;;; Convenience macros.
33 (defmacro mm-handle-buffer (handle)
35 (defmacro mm-handle-type (handle)
37 (defsubst mm-handle-media-type (handle)
38 (if (stringp (car handle))
40 (car (mm-handle-type handle))))
41 (defsubst mm-handle-media-supertype (handle)
42 (car (split-string (mm-handle-media-type handle) "/")))
43 (defsubst mm-handle-media-subtype (handle)
44 (cadr (split-string (mm-handle-media-type handle) "/")))
45 (defmacro mm-handle-encoding (handle)
47 (defmacro mm-handle-undisplayer (handle)
49 (defmacro mm-handle-set-undisplayer (handle function)
50 `(setcar (nthcdr 3 ,handle) ,function))
51 (defmacro mm-handle-disposition (handle)
53 (defmacro mm-handle-description (handle)
55 (defmacro mm-handle-cache (handle)
57 (defmacro mm-handle-set-cache (handle contents)
58 `(setcar (nthcdr 6 ,handle) ,contents))
59 (defmacro mm-handle-id (handle)
61 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
62 disposition description cache
64 `(list ,buffer ,type ,encoding ,undisplayer
65 ,disposition ,description ,cache ,id))
67 (defvar mm-inline-media-tests
71 (mm-valid-and-fit-image-p 'jpeg handle)))
75 (mm-valid-and-fit-image-p 'png handle)))
79 (mm-valid-and-fit-image-p 'gif handle)))
83 (mm-valid-and-fit-image-p 'tiff handle)) )
87 (mm-valid-and-fit-image-p 'xbm handle)))
91 (mm-valid-and-fit-image-p 'xbm handle)))
95 (mm-valid-and-fit-image-p 'xpm handle)))
99 (mm-valid-and-fit-image-p 'xpm handle)))
103 (mm-valid-and-fit-image-p 'bmp handle)))
104 ("text/plain" mm-inline-text identity)
105 ("text/enriched" mm-inline-text identity)
106 ("text/richtext" mm-inline-text identity)
110 (locate-library "w3")))
114 (locate-library "vcard")))
115 ("message/delivery-status" mm-inline-text identity)
116 ("message/rfc822" mm-inline-message identity)
117 ("text/.*" mm-inline-text identity)
118 ("audio/wav" mm-inline-audio
120 (and (or (featurep 'nas-sound) (featurep 'native-sound))
121 (device-sound-enabled-p))))
125 (and (or (featurep 'nas-sound) (featurep 'native-sound))
126 (device-sound-enabled-p))))
127 ("multipart/alternative" ignore identity)
128 ("multipart/mixed" ignore identity)
129 ("multipart/related" ignore identity))
130 "Alist of media types/test that say whether the media types can be displayed inline.")
132 (defvar mm-inlined-types
133 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822")
134 "List of media types that are to be displayed inline.")
136 (defvar mm-automatic-display
137 '("text/plain" "text/enriched" "text/richtext" "text/html"
138 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
140 "A list of MIME types to be displayed automatically.")
142 (defvar mm-attachment-override-types
143 '("text/plain" "text/x-vcard")
144 "Types that should have \"attachment\" ignored if they can be displayed inline.")
146 (defvar mm-automatic-external-display nil
147 "List of MIME type regexps that will be displayed externally automatically.")
149 (defvar mm-discouraged-alternatives nil
150 "List of MIME types that are discouraged when viewing multipart/alternative.
151 Viewing agents are supposed to view the last possible part of a message,
152 as that is supposed to be the richest. However, users may prefer other
153 types instead, and this list says what types are most unwanted. If,
154 for instance, text/html parts are very unwanted, and text/richtech are
155 somewhat unwanted, then the value of this variable should be set
158 (\"text/html\" \"text/richtext\")")
160 (defvar mm-tmp-directory
161 (cond ((fboundp 'temp-directory) (temp-directory))
162 ((boundp 'temporary-file-directory) temporary-file-directory)
164 "Where mm will store its temporary files.")
166 (defvar mm-inline-large-images nil
167 "If non-nil, then all images fit in the buffer.")
169 ;;; Internal variables.
171 (defvar mm-dissection-list nil)
172 (defvar mm-last-shell-command "")
173 (defvar mm-content-id-alist nil)
177 (defun mm-dissect-buffer (&optional no-strict-mime)
178 "Dissect the current buffer and return a list of MIME handles."
180 (let (ct ctl type subtype cte cd description id result)
182 (mail-narrow-to-head)
183 (when (or no-strict-mime
184 (mail-fetch-field "mime-version"))
185 (setq ct (mail-fetch-field "content-type")
186 ctl (ignore-errors (mail-header-parse-content-type ct))
187 cte (mail-fetch-field "content-transfer-encoding")
188 cd (mail-fetch-field "content-disposition")
189 description (mail-fetch-field "content-description")
190 id (mail-fetch-field "content-id"))))
192 (not (string-match "/" (car ctl))))
193 (mm-dissect-singlepart
194 '("text/plain") nil no-strict-mime
195 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
197 (setq type (split-string (car ctl) "/"))
198 (setq subtype (cadr type)
203 ((equal type "multipart")
204 (cons (car ctl) (mm-dissect-multipart ctl)))
206 (mm-dissect-singlepart
208 (and cte (intern (downcase (mail-header-remove-whitespace
209 (mail-header-remove-comments
212 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
215 (when (string-match " *<\\(.*\\)> *" id)
216 (setq id (match-string 1 id)))
217 (push (cons id result) mm-content-id-alist))
220 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
222 (not (equal "text/plain" (car ctl))))
223 (let ((res (mm-make-handle
224 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
225 (push (car res) mm-dissection-list)
228 (defun mm-remove-all-parts ()
229 "Remove all MIME handles."
231 (mapcar 'mm-remove-part mm-dissection-list)
232 (setq mm-dissection-list nil))
234 (defun mm-dissect-multipart (ctl)
235 (goto-char (point-min))
236 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
237 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
240 (goto-char (point-max))
241 (if (re-search-backward close-delimiter nil t)
244 (while (search-forward boundary end t)
245 (goto-char (match-beginning 0))
249 (narrow-to-region start (point))
250 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
252 (setq start (point)))
256 (narrow-to-region start end)
257 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
260 (defun mm-copy-to-buffer ()
261 "Copy the contents of the current buffer to a fresh buffer."
263 (let ((obuf (current-buffer))
265 (goto-char (point-min))
266 (search-forward-regexp "^\n" nil t)
268 (set-buffer (generate-new-buffer " *mm*"))
269 (insert-buffer-substring obuf beg)
272 (defun mm-display-part (handle &optional no-default)
273 "Display the MIME part represented by HANDLE.
274 Returns nil if the part is removed; inline if displayed inline;
275 external if displayed external."
277 (mailcap-parse-mailcaps)
278 (if (mm-handle-displayed-p handle)
279 (mm-remove-part handle)
280 (let* ((type (mm-handle-media-type handle))
281 (method (mailcap-mime-info type)))
282 (if (mm-inlined-p handle)
285 (mm-display-inline handle)
289 (if (and (not method)
290 (equal "text" (car (split-string type))))
293 (mm-insert-inline handle (mm-get-part handle))
296 handle (or method 'mailcap-save-binary-file))
299 (defun mm-display-external (handle method)
300 "Display HANDLE using METHOD."
301 (mm-with-unibyte-buffer
302 (if (functionp method)
303 (let ((cur (current-buffer)))
304 (if (eq method 'mailcap-save-binary-file)
306 (set-buffer (generate-new-buffer "*mm*"))
308 (mm-insert-part handle)
309 (let ((win (get-buffer-window cur t)))
311 (select-window win)))
312 (switch-to-buffer (generate-new-buffer "*mm*")))
313 (buffer-disable-undo)
314 (mm-set-buffer-file-coding-system mm-binary-coding-system)
315 (insert-buffer-substring cur)
316 (message "Viewing with %s" method)
317 (let ((mm (current-buffer))
318 (non-viewer (assoc "non-viewer"
320 (mm-handle-media-type handle)) t)))
324 (mm-save-part handle))
325 (when (and (not non-viewer)
327 (mm-handle-set-undisplayer handle mm)))))
328 ;; The function is a string to be executed.
329 (mm-insert-part handle)
330 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
331 (filename (mail-content-type-get
332 (mm-handle-disposition handle) 'filename))
333 (needsterm (assoc "needsterm"
335 (mm-handle-media-type handle)) t))
337 ;; We create a private sub-directory where we store our files.
339 (set-file-modes dir 448)
341 (setq file (expand-file-name (file-name-nondirectory filename)
343 (setq file (make-temp-name (expand-file-name "mm." dir))))
344 (write-region (point-min) (point-max) file nil 'nomesg)
345 (message "Viewing with %s" method)
349 (start-process "*display*" nil
351 "-e" shell-file-name "-c"
353 (mm-quote-arg file)))
354 (start-process "*display*"
355 (setq buffer (generate-new-buffer "*mm*"))
358 (mm-quote-arg file)))))
359 (mm-handle-set-undisplayer handle (cons file buffer)))
360 (message "Displaying %s..." (format method file))))))
362 (defun mm-remove-parts (handles)
363 "Remove the displayed MIME parts represented by HANDLE."
364 (if (and (listp handles)
365 (bufferp (car handles)))
366 (mm-remove-part handles)
368 (while (setq handle (pop handles))
373 (stringp (car handle)))
374 (mm-remove-parts (cdr handle)))
376 (mm-remove-part handle)))))))
378 (defun mm-destroy-parts (handles)
379 "Remove the displayed MIME parts represented by HANDLE."
380 (if (and (listp handles)
381 (bufferp (car handles)))
382 (mm-destroy-part handles)
384 (while (setq handle (pop handles))
389 (stringp (car handle)))
390 (mm-destroy-parts (cdr handle)))
392 (mm-destroy-part handle)))))))
394 (defun mm-remove-part (handle)
395 "Remove the displayed MIME part represented by HANDLE."
397 (let ((object (mm-handle-undisplayer handle)))
400 ;; Internally displayed part.
401 ((mm-annotationp object)
402 (delete-annotation object))
403 ((or (functionp object)
405 (eq (car object) 'lambda)))
407 ;; Externally displayed part.
409 (ignore-errors (delete-file (car object)))
410 (ignore-errors (delete-directory (file-name-directory (car object))))
411 (ignore-errors (kill-buffer (cdr object))))
413 (when (buffer-live-p object)
414 (kill-buffer object)))))
415 (mm-handle-set-undisplayer handle nil))))
417 (defun mm-display-inline (handle)
418 (let* ((type (mm-handle-media-type handle))
419 (function (cadr (assoc type mm-inline-media-tests))))
420 (funcall function handle)
421 (goto-char (point-min))))
423 (defun mm-inlinable-p (handle)
424 "Say whether HANDLE can be displayed inline."
425 (let ((alist mm-inline-media-tests)
426 (type (mm-handle-media-type handle))
429 (when (equal type (caar alist))
430 (setq test (caddar alist)
432 (setq test (funcall test handle)))
436 (defun mm-automatic-display-p (handle)
437 "Say whether the user wants HANDLE to be displayed automatically."
438 (let ((methods mm-automatic-display)
439 (type (mm-handle-media-type handle))
441 (while (setq method (pop methods))
442 (when (and (string-match method type)
443 (mm-inlinable-p handle))
448 (defun mm-inlined-p (handle)
449 "Say whether the user wants HANDLE to be displayed automatically."
450 (let ((methods mm-inlined-types)
451 (type (mm-handle-media-type handle))
453 (while (setq method (pop methods))
454 (when (and (string-match method type)
455 (mm-inlinable-p handle))
460 (defun mm-attachment-override-p (handle)
461 "Say whether HANDLE should have attachment behavior overridden."
462 (let ((types mm-attachment-override-types)
463 (type (mm-handle-media-type handle))
466 (while (setq ty (pop types))
467 (when (and (string-match ty type)
468 (mm-inlinable-p handle))
469 (throw 'found t))))))
471 (defun mm-automatic-external-display-p (type)
472 "Return the user-defined method for TYPE."
473 (let ((methods mm-automatic-external-display)
475 (while (setq method (pop methods))
476 (when (string-match method type)
481 (defun mm-destroy-part (handle)
482 "Destroy the data structures connected to HANDLE."
484 (mm-remove-part handle)
485 (when (buffer-live-p (mm-handle-buffer handle))
486 (kill-buffer (mm-handle-buffer handle)))))
488 (defun mm-handle-displayed-p (handle)
489 "Say whether HANDLE is displayed or not."
490 (mm-handle-undisplayer handle))
493 ;;; Functions for outputting parts
496 (defun mm-get-part (handle)
497 "Return the contents of HANDLE as a string."
498 (mm-with-unibyte-buffer
499 (mm-insert-part handle)
502 (defun mm-insert-part (handle)
503 "Insert the contents of HANDLE in the current buffer."
504 (let ((cur (current-buffer)))
506 (if (member (mm-handle-media-supertype handle) '("text" "message"))
508 (insert-buffer-substring (mm-handle-buffer handle))
509 (mm-decode-content-transfer-encoding
510 (mm-handle-encoding handle)
511 (mm-handle-media-type handle))
512 (let ((temp (current-buffer)))
514 (insert-buffer-substring temp)))
515 (mm-with-unibyte-buffer
516 (insert-buffer-substring (mm-handle-buffer handle))
517 (mm-decode-content-transfer-encoding
518 (mm-handle-encoding handle)
519 (mm-handle-media-type handle))
520 (let ((temp (current-buffer)))
522 (insert-buffer-substring temp)))))))
524 (defvar mm-default-directory nil)
526 (defun mm-save-part (handle)
527 "Write HANDLE to a file."
528 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
529 (filename (mail-content-type-get
530 (mm-handle-disposition handle) 'filename))
533 (setq filename (file-name-nondirectory filename)))
535 (read-file-name "Save MIME part to: "
537 (or filename name "")
538 (or mm-default-directory default-directory))))
539 (setq mm-default-directory (file-name-directory file))
540 (when (or (not (file-exists-p file))
541 (yes-or-no-p (format "File %s already exists; overwrite? "
543 (mm-save-part-to-file handle file))))
545 (defun mm-save-part-to-file (handle file)
546 (mm-with-unibyte-buffer
547 (mm-insert-part handle)
548 ;; Now every coding system is 100% binary within mm-with-unibyte-buffer
549 ;; Is text still special?
550 (let ((coding-system-for-write
551 (if (equal "text" (mm-handle-media-supertype handle))
552 buffer-file-coding-system
554 ;; Don't re-compress .gz & al. Arguably we should make
555 ;; `file-name-handler-alist' nil, but that would chop
556 ;; ange-ftp which it's reasonable to use here.
557 (inhibit-file-name-operation 'write-region)
558 (inhibit-file-name-handlers
559 (if (equal (mm-handle-media-type handle)
560 "application/octet-stream")
561 (cons 'jka-compr-handler inhibit-file-name-handlers)
562 inhibit-file-name-handlers)))
563 (write-region (point-min) (point-max) file))))
565 (defun mm-pipe-part (handle)
566 "Pipe HANDLE to a process."
567 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
569 (read-string "Shell command on MIME part: " mm-last-shell-command)))
570 (mm-with-unibyte-buffer
571 (mm-insert-part handle)
572 (shell-command-on-region (point-min) (point-max) command nil))))
574 (defun mm-interactively-view-part (handle)
575 "Display HANDLE using METHOD."
576 (let* ((type (mm-handle-media-type handle))
578 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
579 (mailcap-mime-info type 'all)))
580 (method (completing-read "Viewer: " methods)))
581 (mm-display-external (copy-sequence handle) method)))
583 (defun mm-preferred-alternative (handles &optional preferred)
584 "Say which of HANDLES are preferred."
585 (let ((prec (if preferred (list preferred)
586 (mm-preferred-alternative-precedence handles)))
587 p h result type handle)
588 (while (setq p (pop prec))
591 (setq handle (car h))
592 (setq type (mm-handle-media-type handle))
593 (when (and (equal p type)
594 (mm-automatic-display-p handle)
595 (or (stringp (car handle))
596 (not (mm-handle-disposition handle))
597 (equal (car (mm-handle-disposition handle))
605 (defun mm-preferred-alternative-precedence (handles)
606 "Return the precedence based on HANDLES and mm-discouraged-alternatives."
607 (let ((seq (nreverse (mapcar (lambda (h)
608 (mm-handle-media-type h))
610 (dolist (disc (reverse mm-discouraged-alternatives))
611 (dolist (elem (copy-sequence seq))
612 (when (string-match disc elem)
613 (setq seq (nconc (delete elem seq) (list elem))))))
616 (defun mm-get-content-id (id)
617 "Return the handle(s) referred to by ID."
618 (cdr (assoc id mm-content-id-alist)))
620 (defun mm-get-image (handle)
621 "Return an image instance based on HANDLE."
622 (let ((type (mm-handle-media-subtype handle))
624 ;; Allow some common translations.
627 ((equal type "x-pixmap")
629 ((equal type "x-xbitmap")
632 (or (mm-handle-cache handle)
633 (mm-with-unibyte-buffer
634 (mm-insert-part handle)
640 ;; xbm images require special handling, since
641 ;; the only way to create glyphs from these
642 ;; (without a ton of work) is to write them
643 ;; out to a file, and then create a file
645 (let ((file (make-temp-name
646 (expand-file-name "emm.xbm"
650 (write-region (point-min) (point-max) file)
651 (make-glyph (list (cons 'x file))))
653 (delete-file file)))))
656 (vector (intern type) :data (buffer-string)))))))
657 (mm-handle-set-cache handle spec))))))
659 (defun mm-image-fit-p (handle)
660 "Say whether the image in HANDLE will fit the current window."
661 (let ((image (mm-get-image handle)))
662 (or mm-inline-large-images
663 (and (< (glyph-width image) (window-pixel-width))
664 (< (glyph-height image) (window-pixel-height))))))
666 (defun mm-valid-image-format-p (format)
667 "Say whether FORMAT can be displayed natively by Emacs."
668 (and (fboundp 'valid-image-instantiator-format-p)
669 (valid-image-instantiator-format-p format)))
671 (defun mm-valid-and-fit-image-p (format handle)
672 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
674 (mm-valid-image-format-p format)
675 (mm-image-fit-p handle)))
679 ;; mm-decode.el ends here