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.
28 (require 'gnus-mailcap)
31 ;;; Convenience macros.
33 (defmacro mm-handle-buffer (handle)
35 (defmacro mm-handle-type (handle)
37 (defmacro mm-handle-encoding (handle)
39 (defmacro mm-handle-undisplayer (handle)
41 (defmacro mm-handle-set-undisplayer (handle function)
42 `(setcar (nthcdr 3 ,handle) ,function))
43 (defmacro mm-handle-disposition (handle)
45 (defmacro mm-handle-description (handle)
47 (defmacro mm-handle-cache (handle)
49 (defmacro mm-handle-set-cache (handle contents)
50 `(setcar (nthcdr 6 ,handle) ,contents))
51 (defmacro mm-handle-id (handle)
53 (defmacro mm-make-handle (&optional buffer type encoding undisplayer
54 disposition description cache
56 `(list ,buffer ,type ,encoding ,undisplayer
57 ,disposition ,description ,cache ,id))
59 (defvar mm-inline-media-tests
60 '(("image/jpeg" mm-inline-image (mm-valid-and-fit-image-p 'jpeg handle))
61 ("image/png" mm-inline-image (mm-valid-and-fit-image-p 'png handle))
62 ("image/gif" mm-inline-image (mm-valid-and-fit-image-p 'gif handle))
63 ("image/tiff" mm-inline-image (mm-valid-and-fit-image-p 'tiff handle))
64 ("image/xbm" mm-inline-image (mm-valid-and-fit-image-p 'xbm handle))
65 ("image/x-xbitmap" mm-inline-image (mm-valid-and-fit-image-p 'xbm handle))
66 ("image/xpm" mm-inline-image (mm-valid-and-fit-image-p 'xpm handle))
67 ("image/x-pixmap" mm-inline-image (mm-valid-and-fit-image-p 'xpm handle))
68 ("image/bmp" mm-inline-image (mm-valid-and-fit-image-p 'bmp handle))
69 ("text/plain" mm-inline-text t)
70 ("text/enriched" mm-inline-text t)
71 ("text/richtext" mm-inline-text t)
72 ("text/html" mm-inline-text (locate-library "w3"))
73 ("text/x-vcard" mm-inline-text (locate-library "vcard"))
74 ("message/delivery-status" mm-inline-text t)
75 ("message/rfc822" mm-inline-message t)
76 ("text/.*" mm-inline-text t)
77 ("audio/wav" mm-inline-audio
78 (and (or (featurep 'nas-sound) (featurep 'native-sound))
79 (device-sound-enabled-p)))
80 ("audio/au" mm-inline-audio
81 (and (or (featurep 'nas-sound) (featurep 'native-sound))
82 (device-sound-enabled-p)))
83 ("multipart/alternative" ignore t)
84 ("multipart/mixed" ignore t)
85 ("multipart/related" ignore t))
86 "Alist of media types/test that say whether the media types can be displayed inline.")
88 (defvar mm-user-display-methods
89 '(("image/.*" . inline)
91 ("message/delivery-status" . inline)
92 ("message/rfc822" . inline)))
94 (defvar mm-user-automatic-display
95 '("text/plain" "text/enriched" "text/richtext" "text/html"
96 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
99 (defvar mm-attachment-override-types
100 '("text/plain" "text/x-vcard")
101 "Types that should have \"attachment\" ignored if they can be displayed inline.")
103 (defvar mm-user-automatic-external-display nil
104 "List of MIME type regexps that will be displayed externally automatically.")
106 (defvar mm-discouraged-alternatives nil
107 "List of MIME types that are discouraged when viewing multiapart/alternative.
108 Viewing agents are supposed to view the last possible part of a message,
109 as that is supposed to be the richest. However, users may prefer other
110 types instead, and this list says what types are most unwanted. If,
111 for instance, text/html parts are very unwanted, and text/richtech are
112 somewhat unwanted, then the value of this variable should be set
115 (\"text/html\" \"text/richtext\")")
117 (defvar mm-tmp-directory
118 (cond ((fboundp 'temp-directory) (temp-directory))
119 ((boundp 'temporary-file-directory) temporary-file-directory)
121 "Where mm will store its temporary files.")
123 (defvar mm-all-images-fit nil
124 "If non-nil, then all images fit in the buffer.")
126 ;;; Internal variables.
128 (defvar mm-dissection-list nil)
129 (defvar mm-last-shell-command "")
130 (defvar mm-content-id-alist nil)
134 (defun mm-dissect-buffer (&optional no-strict-mime)
135 "Dissect the current buffer and return a list of MIME handles."
137 (let (ct ctl type subtype cte cd description id result)
139 (mail-narrow-to-head)
140 (when (or no-strict-mime
141 (mail-fetch-field "mime-version"))
142 (setq ct (mail-fetch-field "content-type")
143 ctl (ignore-errors (mail-header-parse-content-type ct))
144 cte (mail-fetch-field "content-transfer-encoding")
145 cd (mail-fetch-field "content-disposition")
146 description (mail-fetch-field "content-description")
147 id (mail-fetch-field "content-id"))))
149 (not (string-match "/" (car ctl))))
150 (mm-dissect-singlepart
151 '("text/plain") nil no-strict-mime
152 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
154 (setq type (split-string (car ctl) "/"))
155 (setq subtype (cadr type)
160 ((equal type "multipart")
161 (cons (car ctl) (mm-dissect-multipart ctl)))
163 (mm-dissect-singlepart
165 (and cte (intern (downcase (mail-header-remove-whitespace
166 (mail-header-remove-comments
169 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
172 (when (string-match " *<\\(.*\\)> *" id)
173 (setq id (match-string 1 id)))
174 (push (cons id result) mm-content-id-alist))
177 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
179 (not (equal "text/plain" (car ctl))))
180 (let ((res (mm-make-handle
181 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
182 (push (car res) mm-dissection-list)
185 (defun mm-remove-all-parts ()
186 "Remove all MIME handles."
188 (mapcar 'mm-remove-part mm-dissection-list)
189 (setq mm-dissection-list nil))
191 (defun mm-dissect-multipart (ctl)
192 (goto-char (point-min))
193 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
194 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
197 (goto-char (point-max))
198 (if (re-search-backward close-delimiter nil t)
201 (while (search-forward boundary end t)
202 (goto-char (match-beginning 0))
206 (narrow-to-region start (point))
207 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
209 (setq start (point)))
213 (narrow-to-region start end)
214 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
217 (defun mm-copy-to-buffer ()
218 "Copy the contents of the current buffer to a fresh buffer."
220 (let ((obuf (current-buffer))
222 (goto-char (point-min))
223 (search-forward-regexp "^\n" nil t)
225 (set-buffer (generate-new-buffer " *mm*"))
226 (insert-buffer-substring obuf beg)
229 (defun mm-inlinable-part-p (type)
230 "Say whether TYPE can be displayed inline."
231 (eq (mm-user-method type) 'inline))
233 (defun mm-display-part (handle &optional no-default)
234 "Display the MIME part represented by HANDLE.
235 Returns nil if the part is removed; inline if displayed inline;
236 external if displayed external."
238 (mailcap-parse-mailcaps)
239 (if (mm-handle-displayed-p handle)
240 (mm-remove-part handle)
241 (let* ((type (car (mm-handle-type handle)))
242 (method (mailcap-mime-info type))
243 (user-method (mm-user-method type)))
244 (if (eq user-method 'inline)
247 (mm-display-inline handle)
249 (when (or user-method
252 (if (and (not user-method)
254 (equal "text" (car (split-string type))))
257 (mm-insert-inline handle (mm-get-part handle))
260 handle (or user-method method
261 'mailcap-save-binary-file))
264 (defun mm-display-external (handle method)
265 "Display HANDLE using METHOD."
266 (mm-with-unibyte-buffer
267 (if (functionp method)
268 (let ((cur (current-buffer)))
269 (if (eq method 'mailcap-save-binary-file)
271 (set-buffer (generate-new-buffer "*mm*"))
273 (mm-insert-part handle)
274 (let ((win (get-buffer-window cur t)))
276 (select-window win)))
277 (switch-to-buffer (generate-new-buffer "*mm*")))
278 (buffer-disable-undo)
279 (mm-set-buffer-file-coding-system mm-binary-coding-system)
280 (insert-buffer-substring cur)
281 (message "Viewing with %s" method)
282 (let ((mm (current-buffer))
283 (non-viewer (assoc "non-viewer"
285 (car (mm-handle-type handle)) t))))
289 (mm-save-part handle))
290 (when (and (not non-viewer)
292 (mm-handle-set-undisplayer handle mm)))))
293 ;; The function is a string to be executed.
294 (mm-insert-part handle)
295 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
296 (filename (mail-content-type-get
297 (mm-handle-disposition handle) 'filename))
298 (needsterm (assoc "needsterm"
300 (car (mm-handle-type handle)) t)))
302 ;; We create a private sub-directory where we store our files.
304 (set-file-modes dir 448)
306 (setq file (expand-file-name (file-name-nondirectory filename)
308 (setq file (make-temp-name (expand-file-name "mm." dir))))
309 (write-region (point-min) (point-max) file nil 'nomesg)
310 (message "Viewing with %s" method)
314 (start-process "*display*" nil
316 "-e" shell-file-name "-c"
318 (mm-quote-arg file)))
319 (start-process "*display*"
320 (setq buffer (generate-new-buffer "*mm*"))
323 (mm-quote-arg file)))))
324 (mm-handle-set-undisplayer handle (cons file buffer)))
325 (message "Displaying %s..." (format method file))))))
327 (defun mm-remove-parts (handles)
328 "Remove the displayed MIME parts represented by HANDLE."
329 (if (and (listp handles)
330 (bufferp (car handles)))
331 (mm-remove-part handles)
333 (while (setq handle (pop handles))
338 (stringp (car handle)))
339 (mm-remove-parts (cdr handle)))
341 (mm-remove-part handle)))))))
343 (defun mm-destroy-parts (handles)
344 "Remove the displayed MIME parts represented by HANDLE."
345 (if (and (listp handles)
346 (bufferp (car handles)))
347 (mm-destroy-part handles)
349 (while (setq handle (pop handles))
354 (stringp (car handle)))
355 (mm-destroy-parts (cdr handle)))
357 (mm-destroy-part handle)))))))
359 (defun mm-remove-part (handle)
360 "Remove the displayed MIME part represented by HANDLE."
362 (let ((object (mm-handle-undisplayer handle)))
365 ;; Internally displayed part.
366 ((mm-annotationp object)
367 (delete-annotation object))
368 ((or (functionp object)
370 (eq (car object) 'lambda)))
372 ;; Externally displayed part.
374 (ignore-errors (delete-file (car object)))
375 (ignore-errors (delete-directory (file-name-directory (car object))))
376 (ignore-errors (kill-buffer (cdr object))))
378 (when (buffer-live-p object)
379 (kill-buffer object)))))
380 (mm-handle-set-undisplayer handle nil))))
382 (defun mm-display-inline (handle)
383 (let* ((type (car (mm-handle-type handle)))
384 (function (cadr (assoc type mm-inline-media-tests))))
385 (funcall function handle)
386 (goto-char (point-min))))
388 (defun mm-inlinable-p (type)
389 "Say whether TYPE can be displayed inline."
390 (let ((alist mm-inline-media-tests)
393 (when (equal type (caar alist))
394 (setq test (caddar alist)
396 (setq test (eval test)))
400 (defun mm-user-method (type)
401 "Return the user-defined method for TYPE."
402 (let ((methods mm-user-display-methods)
404 (while (setq method (pop methods))
405 (when (string-match (car method) type)
406 (when (or (not (eq (cdr method) 'inline))
407 (mm-inlinable-p type))
408 (setq result (cdr method)
412 (defun mm-automatic-display-p (type)
413 "Return the user-defined method for TYPE."
414 (let ((methods mm-user-automatic-display)
416 (while (setq method (pop methods))
417 (when (and (string-match method type)
418 (mm-inlinable-p type))
423 (defun mm-attachment-override-p (type)
424 "Say whether TYPE should have attachment behavior overridden."
425 (let ((types mm-attachment-override-types)
428 (while (setq ty (pop types))
429 (when (and (string-match ty type)
430 (mm-inlinable-p type))
431 (throw 'found t))))))
433 (defun mm-automatic-external-display-p (type)
434 "Return the user-defined method for TYPE."
435 (let ((methods mm-user-automatic-external-display)
437 (while (setq method (pop methods))
438 (when (string-match method type)
443 (defun add-mime-display-method (type method)
444 "Make parts of TYPE be displayed with METHOD.
445 This overrides entries in the mailcap file."
446 (push (cons type method) mm-user-display-methods))
448 (defun mm-destroy-part (handle)
449 "Destroy the data structures connected to HANDLE."
451 (mm-remove-part handle)
452 (when (buffer-live-p (mm-handle-buffer handle))
453 (kill-buffer (mm-handle-buffer handle)))))
455 (defun mm-handle-displayed-p (handle)
456 "Say whether HANDLE is displayed or not."
457 (mm-handle-undisplayer handle))
459 (defun mm-quote-arg (arg)
460 "Return a version of ARG that is safe to evaluate in a shell."
461 (let ((pos 0) new-pos accum)
462 ;; *** bug: we don't handle newline characters properly
463 (while (setq new-pos (string-match "[;!'`\"$\\& \t{} |()<>]" arg pos))
464 (push (substring arg pos new-pos) accum)
466 (push (list (aref arg new-pos)) accum)
467 (setq pos (1+ new-pos)))
470 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
473 ;;; Functions for outputting parts
476 (defun mm-get-part (handle)
477 "Return the contents of HANDLE as a string."
478 (mm-with-unibyte-buffer
479 (mm-insert-part handle)
482 (defun mm-insert-part (handle)
483 "Insert the contents of HANDLE in the current buffer."
484 (let ((cur (current-buffer)))
486 (if (member (car (split-string (car (mm-handle-type handle)) "/"))
489 (insert-buffer-substring (mm-handle-buffer handle))
490 (mm-decode-content-transfer-encoding
491 (mm-handle-encoding handle)
492 (car (mm-handle-type handle)))
493 (let ((temp (current-buffer)))
495 (insert-buffer-substring temp)))
496 (mm-with-unibyte-buffer
497 (insert-buffer-substring (mm-handle-buffer handle))
498 (mm-decode-content-transfer-encoding
499 (mm-handle-encoding handle)
500 (car (mm-handle-type handle)))
501 (let ((temp (current-buffer)))
503 (insert-buffer-substring temp)))))))
505 (defvar mm-default-directory nil)
507 (defun mm-save-part (handle)
508 "Write HANDLE to a file."
509 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
510 (filename (mail-content-type-get
511 (mm-handle-disposition handle) 'filename))
514 (setq filename (file-name-nondirectory filename)))
516 (read-file-name "Save MIME part to: "
518 (or filename name "")
519 (or mm-default-directory default-directory))))
520 (setq mm-default-directory (file-name-directory file))
521 (mm-with-unibyte-buffer
522 (mm-insert-part handle)
523 (when (or (not (file-exists-p file))
524 (yes-or-no-p (format "File %s already exists; overwrite? "
526 ;; Now every coding system is 100% binary within mm-with-unibyte-buffer
527 ;; Is text still special?
528 (let ((coding-system-for-write
529 (if (equal "text" (car (split-string
530 (car (mm-handle-type handle)) "/")))
531 buffer-file-coding-system
533 ;; Don't re-compress .gz & al. Arguably we should make
534 ;; `file-name-handler-alist' nil, but that would chop
535 ;; ange-ftp which it's reasonable to use here.
536 (inhibit-file-name-operation 'write-region)
537 (inhibit-file-name-handlers
538 (if (equal (car (mm-handle-type handle))
539 "application/octet-stream")
540 (cons 'jka-compr-handler inhibit-file-name-handlers)
541 inhibit-file-name-handlers)))
542 (write-region (point-min) (point-max) file))))))
544 (defun mm-pipe-part (handle)
545 "Pipe HANDLE to a process."
546 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
548 (read-string "Shell command on MIME part: " mm-last-shell-command)))
549 (mm-with-unibyte-buffer
550 (mm-insert-part handle)
551 (shell-command-on-region (point-min) (point-max) command nil))))
553 (defun mm-interactively-view-part (handle)
554 "Display HANDLE using METHOD."
555 (let* ((type (car (mm-handle-type handle)))
557 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
558 (mailcap-mime-info type 'all)))
559 (method (completing-read "Viewer: " methods)))
560 (mm-display-external (copy-sequence handle) method)))
562 (defun mm-preferred-alternative (handles &optional preferred)
563 "Say which of HANDLES are preferred."
564 (let ((prec (if preferred (list preferred)
565 (mm-preferred-alternative-precedence handles)))
566 p h result type handle)
567 (while (setq p (pop prec))
571 (if (stringp (caar h))
573 (car (mm-handle-type (car h)))))
574 (setq handle (car h))
575 (when (and (equal p type)
576 (mm-automatic-display-p type)
577 (or (stringp (caar h))
578 (not (mm-handle-disposition (car h)))
579 (equal (car (mm-handle-disposition (car h)))
587 (defun mm-preferred-alternative-precedence (handles)
588 "Return the precedence based on HANDLES and mm-discouraged-alternatives."
589 (let ((seq (mapcar (lambda (h) (car (mm-handle-type h))) handles)))
590 (dolist (disc (reverse mm-discouraged-alternatives))
591 (dolist (elem (copy-sequence seq))
592 (when (string-match disc elem)
593 (setq seq (nconc (delete elem seq) (list elem))))))
596 (defun mm-get-content-id (id)
597 "Return the handle(s) referred to by ID."
598 (cdr (assoc id mm-content-id-alist)))
600 (defun mm-get-image (handle)
601 "Return an image instance based on HANDLE."
602 (let ((type (cadr (split-string (car (mm-handle-type handle)) "/")))
604 ;; Allow some common translations.
607 ((equal type "x-pixmap")
609 ((equal type "x-xbitmap")
612 (or (mm-handle-cache handle)
613 (mm-with-unibyte-buffer
614 (mm-insert-part handle)
620 ;; xbm images require special handling, since
621 ;; the only way to create glyphs from these
622 ;; (without a ton of work) is to write them
623 ;; out to a file, and then create a file
625 (let ((file (make-temp-name
626 (expand-file-name "emm.xbm"
630 (write-region (point-min) (point-max) file)
631 (make-glyph (list (cons 'x file))))
633 (delete-file file)))))
636 (vector (intern type) :data (buffer-string)))))))
637 (mm-handle-set-cache handle spec))))))
639 (defun mm-image-fit-p (handle)
640 "Say whether the image in HANDLE will fit the current window."
641 (let ((image (mm-get-image handle)))
642 (or mm-all-images-fit
643 (and (< (glyph-width image) (window-pixel-width))
644 (< (glyph-height image) (window-pixel-height))))))
646 (defun mm-valid-image-format-p (format)
647 "Say whether FORMAT can be displayed natively by Emacs."
648 (and (fboundp 'valid-image-instantiator-format-p)
649 (valid-image-instantiator-format-p format)))
651 (defun mm-valid-and-fit-image-p (format handle)
652 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
654 (mm-valid-image-format-p format)
655 (mm-image-fit-p handle)))
659 ;; mm-decode.el ends here