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 (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
63 (mm-valid-and-fit-image-p 'jpeg handle)))
67 (mm-valid-and-fit-image-p 'png handle)))
71 (mm-valid-and-fit-image-p 'gif handle)))
75 (mm-valid-and-fit-image-p 'tiff handle)) )
79 (mm-valid-and-fit-image-p 'xbm handle)))
83 (mm-valid-and-fit-image-p 'xbm handle)))
87 (mm-valid-and-fit-image-p 'xpm handle)))
91 (mm-valid-and-fit-image-p 'xpm handle)))
95 (mm-valid-and-fit-image-p 'bmp handle)))
96 ("text/plain" mm-inline-text identity)
97 ("text/enriched" mm-inline-text identity)
98 ("text/richtext" mm-inline-text identity)
102 (locate-library "w3")))
106 (locate-library "vcard")))
107 ("message/delivery-status" mm-inline-text identity)
108 ("message/rfc822" mm-inline-message identity)
109 ("text/.*" mm-inline-text identity)
110 ("audio/wav" mm-inline-audio
112 (and (or (featurep 'nas-sound) (featurep 'native-sound))
113 (device-sound-enabled-p))))
117 (and (or (featurep 'nas-sound) (featurep 'native-sound))
118 (device-sound-enabled-p))))
119 ("multipart/alternative" ignore identity)
120 ("multipart/mixed" ignore identity)
121 ("multipart/related" ignore identity))
122 "Alist of media types/test that say whether the media types can be displayed inline.")
124 (defvar mm-inlined-types
125 '("image/.*" "text/.*" "message/delivery-status" "message/rfc822")
126 "List of media types that are to be displayed inline.")
128 (defvar mm-automatic-display
129 '("text/plain" "text/enriched" "text/richtext" "text/html"
130 "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
132 "A list of MIME types to be displayed automatically.")
134 (defvar mm-attachment-override-types
135 '("text/plain" "text/x-vcard")
136 "Types that should have \"attachment\" ignored if they can be displayed inline.")
138 (defvar mm-automatic-external-display nil
139 "List of MIME type regexps that will be displayed externally automatically.")
141 (defvar mm-discouraged-alternatives nil
142 "List of MIME types that are discouraged when viewing multipart/alternative.
143 Viewing agents are supposed to view the last possible part of a message,
144 as that is supposed to be the richest. However, users may prefer other
145 types instead, and this list says what types are most unwanted. If,
146 for instance, text/html parts are very unwanted, and text/richtech are
147 somewhat unwanted, then the value of this variable should be set
150 (\"text/html\" \"text/richtext\")")
152 (defvar mm-tmp-directory
153 (cond ((fboundp 'temp-directory) (temp-directory))
154 ((boundp 'temporary-file-directory) temporary-file-directory)
156 "Where mm will store its temporary files.")
158 (defvar mm-inline-large-images nil
159 "If non-nil, then all images fit in the buffer.")
161 ;;; Internal variables.
163 (defvar mm-dissection-list nil)
164 (defvar mm-last-shell-command "")
165 (defvar mm-content-id-alist nil)
169 (defun mm-dissect-buffer (&optional no-strict-mime)
170 "Dissect the current buffer and return a list of MIME handles."
172 (let (ct ctl type subtype cte cd description id result)
174 (mail-narrow-to-head)
175 (when (or no-strict-mime
176 (mail-fetch-field "mime-version"))
177 (setq ct (mail-fetch-field "content-type")
178 ctl (ignore-errors (mail-header-parse-content-type ct))
179 cte (mail-fetch-field "content-transfer-encoding")
180 cd (mail-fetch-field "content-disposition")
181 description (mail-fetch-field "content-description")
182 id (mail-fetch-field "content-id"))))
184 (not (string-match "/" (car ctl))))
185 (mm-dissect-singlepart
186 '("text/plain") nil no-strict-mime
187 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
189 (setq type (split-string (car ctl) "/"))
190 (setq subtype (cadr type)
195 ((equal type "multipart")
196 (cons (car ctl) (mm-dissect-multipart ctl)))
198 (mm-dissect-singlepart
200 (and cte (intern (downcase (mail-header-remove-whitespace
201 (mail-header-remove-comments
204 (and cd (ignore-errors (mail-header-parse-content-disposition cd)))
207 (when (string-match " *<\\(.*\\)> *" id)
208 (setq id (match-string 1 id)))
209 (push (cons id result) mm-content-id-alist))
212 (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
214 (not (equal "text/plain" (car ctl))))
215 (let ((res (mm-make-handle
216 (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
217 (push (car res) mm-dissection-list)
220 (defun mm-remove-all-parts ()
221 "Remove all MIME handles."
223 (mapcar 'mm-remove-part mm-dissection-list)
224 (setq mm-dissection-list nil))
226 (defun mm-dissect-multipart (ctl)
227 (goto-char (point-min))
228 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
229 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
232 (goto-char (point-max))
233 (if (re-search-backward close-delimiter nil t)
236 (while (search-forward boundary end t)
237 (goto-char (match-beginning 0))
241 (narrow-to-region start (point))
242 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
244 (setq start (point)))
248 (narrow-to-region start end)
249 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
252 (defun mm-copy-to-buffer ()
253 "Copy the contents of the current buffer to a fresh buffer."
255 (let ((obuf (current-buffer))
257 (goto-char (point-min))
258 (search-forward-regexp "^\n" nil t)
260 (set-buffer (generate-new-buffer " *mm*"))
261 (insert-buffer-substring obuf beg)
264 (defun mm-display-part (handle &optional no-default)
265 "Display the MIME part represented by HANDLE.
266 Returns nil if the part is removed; inline if displayed inline;
267 external if displayed external."
269 (mailcap-parse-mailcaps)
270 (if (mm-handle-displayed-p handle)
271 (mm-remove-part handle)
272 (let* ((type (car (mm-handle-type handle)))
273 (method (mailcap-mime-info type)))
274 (if (mm-inlined-p handle)
277 (mm-display-inline handle)
281 (if (and (not method)
282 (equal "text" (car (split-string type))))
285 (mm-insert-inline handle (mm-get-part handle))
288 handle (or method 'mailcap-save-binary-file))
291 (defun mm-display-external (handle method)
292 "Display HANDLE using METHOD."
293 (mm-with-unibyte-buffer
294 (if (functionp method)
295 (let ((cur (current-buffer)))
296 (if (eq method 'mailcap-save-binary-file)
298 (set-buffer (generate-new-buffer "*mm*"))
300 (mm-insert-part handle)
301 (let ((win (get-buffer-window cur t)))
303 (select-window win)))
304 (switch-to-buffer (generate-new-buffer "*mm*")))
305 (buffer-disable-undo)
306 (mm-set-buffer-file-coding-system mm-binary-coding-system)
307 (insert-buffer-substring cur)
308 (message "Viewing with %s" method)
309 (let ((mm (current-buffer))
310 (non-viewer (assoc "non-viewer"
312 (car (mm-handle-type handle)) t))))
316 (mm-save-part handle))
317 (when (and (not non-viewer)
319 (mm-handle-set-undisplayer handle mm)))))
320 ;; The function is a string to be executed.
321 (mm-insert-part handle)
322 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
323 (filename (mail-content-type-get
324 (mm-handle-disposition handle) 'filename))
325 (needsterm (assoc "needsterm"
327 (car (mm-handle-type handle)) t)))
329 ;; We create a private sub-directory where we store our files.
331 (set-file-modes dir 448)
333 (setq file (expand-file-name (file-name-nondirectory filename)
335 (setq file (make-temp-name (expand-file-name "mm." dir))))
336 (write-region (point-min) (point-max) file nil 'nomesg)
337 (message "Viewing with %s" method)
341 (start-process "*display*" nil
343 "-e" shell-file-name "-c"
345 (mm-quote-arg file)))
346 (start-process "*display*"
347 (setq buffer (generate-new-buffer "*mm*"))
350 (mm-quote-arg file)))))
351 (mm-handle-set-undisplayer handle (cons file buffer)))
352 (message "Displaying %s..." (format method file))))))
354 (defun mm-remove-parts (handles)
355 "Remove the displayed MIME parts represented by HANDLE."
356 (if (and (listp handles)
357 (bufferp (car handles)))
358 (mm-remove-part handles)
360 (while (setq handle (pop handles))
365 (stringp (car handle)))
366 (mm-remove-parts (cdr handle)))
368 (mm-remove-part handle)))))))
370 (defun mm-destroy-parts (handles)
371 "Remove the displayed MIME parts represented by HANDLE."
372 (if (and (listp handles)
373 (bufferp (car handles)))
374 (mm-destroy-part handles)
376 (while (setq handle (pop handles))
381 (stringp (car handle)))
382 (mm-destroy-parts (cdr handle)))
384 (mm-destroy-part handle)))))))
386 (defun mm-remove-part (handle)
387 "Remove the displayed MIME part represented by HANDLE."
389 (let ((object (mm-handle-undisplayer handle)))
392 ;; Internally displayed part.
393 ((mm-annotationp object)
394 (delete-annotation object))
395 ((or (functionp object)
397 (eq (car object) 'lambda)))
399 ;; Externally displayed part.
401 (ignore-errors (delete-file (car object)))
402 (ignore-errors (delete-directory (file-name-directory (car object))))
403 (ignore-errors (kill-buffer (cdr object))))
405 (when (buffer-live-p object)
406 (kill-buffer object)))))
407 (mm-handle-set-undisplayer handle nil))))
409 (defun mm-display-inline (handle)
410 (let* ((type (car (mm-handle-type handle)))
411 (function (cadr (assoc type mm-inline-media-tests))))
412 (funcall function handle)
413 (goto-char (point-min))))
415 (defun mm-inlinable-p (handle)
416 "Say whether HANDLE can be displayed inline."
417 (let ((alist mm-inline-media-tests)
418 (type (car (mm-handle-type handle)))
421 (when (equal type (caar alist))
422 (setq test (caddar alist)
424 (setq test (funcall test handle)))
428 (defun mm-automatic-display-p (handle)
429 "Say whether the user wants HANDLE to be displayed automatically."
430 (let ((methods mm-automatic-display)
431 (type (car (mm-handle-type handle)))
433 (while (setq method (pop methods))
434 (when (and (string-match method type)
435 (mm-inlinable-p handle))
440 (defun mm-inlined-p (handle)
441 "Say whether the user wants HANDLE to be displayed automatically."
442 (let ((methods mm-inlined-types)
443 (type (car (mm-handle-type handle)))
445 (while (setq method (pop methods))
446 (when (and (string-match method type)
447 (mm-inlinable-p handle))
452 (defun mm-attachment-override-p (handle)
453 "Say whether HANDLE should have attachment behavior overridden."
454 (let ((types mm-attachment-override-types)
455 (type (car (mm-handle-type handle)))
458 (while (setq ty (pop types))
459 (when (and (string-match ty type)
460 (mm-inlinable-p handle))
461 (throw 'found t))))))
463 (defun mm-automatic-external-display-p (type)
464 "Return the user-defined method for TYPE."
465 (let ((methods mm-automatic-external-display)
467 (while (setq method (pop methods))
468 (when (string-match method type)
473 (defun mm-destroy-part (handle)
474 "Destroy the data structures connected to HANDLE."
476 (mm-remove-part handle)
477 (when (buffer-live-p (mm-handle-buffer handle))
478 (kill-buffer (mm-handle-buffer handle)))))
480 (defun mm-handle-displayed-p (handle)
481 "Say whether HANDLE is displayed or not."
482 (mm-handle-undisplayer handle))
485 ;;; Functions for outputting parts
488 (defun mm-get-part (handle)
489 "Return the contents of HANDLE as a string."
490 (mm-with-unibyte-buffer
491 (mm-insert-part handle)
494 (defun mm-insert-part (handle)
495 "Insert the contents of HANDLE in the current buffer."
496 (let ((cur (current-buffer)))
498 (if (member (car (split-string (car (mm-handle-type handle)) "/"))
501 (insert-buffer-substring (mm-handle-buffer handle))
502 (mm-decode-content-transfer-encoding
503 (mm-handle-encoding handle)
504 (car (mm-handle-type handle)))
505 (let ((temp (current-buffer)))
507 (insert-buffer-substring temp)))
508 (mm-with-unibyte-buffer
509 (insert-buffer-substring (mm-handle-buffer handle))
510 (mm-decode-content-transfer-encoding
511 (mm-handle-encoding handle)
512 (car (mm-handle-type handle)))
513 (let ((temp (current-buffer)))
515 (insert-buffer-substring temp)))))))
517 (defvar mm-default-directory nil)
519 (defun mm-save-part (handle)
520 "Write HANDLE to a file."
521 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
522 (filename (mail-content-type-get
523 (mm-handle-disposition handle) 'filename))
526 (setq filename (file-name-nondirectory filename)))
528 (read-file-name "Save MIME part to: "
530 (or filename name "")
531 (or mm-default-directory default-directory))))
532 (setq mm-default-directory (file-name-directory file))
533 (when (or (not (file-exists-p file))
534 (yes-or-no-p (format "File %s already exists; overwrite? "
536 (mm-save-part-to-file handle file))))
538 (defun mm-save-part-to-file (handle file)
539 (mm-with-unibyte-buffer
540 (mm-insert-part handle)
541 ;; Now every coding system is 100% binary within mm-with-unibyte-buffer
542 ;; Is text still special?
543 (let ((coding-system-for-write
544 (if (equal "text" (car (split-string
545 (car (mm-handle-type handle)) "/")))
546 buffer-file-coding-system
548 ;; Don't re-compress .gz & al. Arguably we should make
549 ;; `file-name-handler-alist' nil, but that would chop
550 ;; ange-ftp which it's reasonable to use here.
551 (inhibit-file-name-operation 'write-region)
552 (inhibit-file-name-handlers
553 (if (equal (car (mm-handle-type handle))
554 "application/octet-stream")
555 (cons 'jka-compr-handler inhibit-file-name-handlers)
556 inhibit-file-name-handlers)))
557 (write-region (point-min) (point-max) file))))
559 (defun mm-pipe-part (handle)
560 "Pipe HANDLE to a process."
561 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
563 (read-string "Shell command on MIME part: " mm-last-shell-command)))
564 (mm-with-unibyte-buffer
565 (mm-insert-part handle)
566 (shell-command-on-region (point-min) (point-max) command nil))))
568 (defun mm-interactively-view-part (handle)
569 "Display HANDLE using METHOD."
570 (let* ((type (car (mm-handle-type handle)))
572 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
573 (mailcap-mime-info type 'all)))
574 (method (completing-read "Viewer: " methods)))
575 (mm-display-external (copy-sequence handle) method)))
577 (defun mm-preferred-alternative (handles &optional preferred)
578 "Say which of HANDLES are preferred."
579 (let ((prec (if preferred (list preferred)
580 (mm-preferred-alternative-precedence handles)))
581 p h result type handle)
582 (while (setq p (pop prec))
586 (if (stringp (caar h))
588 (car (mm-handle-type (car h)))))
589 (setq handle (car h))
590 (when (and (equal p type)
591 (mm-automatic-display-p (car h))
592 (or (stringp (caar h))
593 (not (mm-handle-disposition (car h)))
594 (equal (car (mm-handle-disposition (car h)))
602 (defun mm-preferred-alternative-precedence (handles)
603 "Return the precedence based on HANDLES and mm-discouraged-alternatives."
604 (let ((seq (nreverse (mapcar (lambda (h)
605 (car (mm-handle-type h))) handles))))
606 (dolist (disc (reverse mm-discouraged-alternatives))
607 (dolist (elem (copy-sequence seq))
608 (when (string-match disc elem)
609 (setq seq (nconc (delete elem seq) (list elem))))))
612 (defun mm-get-content-id (id)
613 "Return the handle(s) referred to by ID."
614 (cdr (assoc id mm-content-id-alist)))
616 (defun mm-get-image (handle)
617 "Return an image instance based on HANDLE."
618 (let ((type (cadr (split-string (car (mm-handle-type handle)) "/")))
620 ;; Allow some common translations.
623 ((equal type "x-pixmap")
625 ((equal type "x-xbitmap")
628 (or (mm-handle-cache handle)
629 (mm-with-unibyte-buffer
630 (mm-insert-part handle)
636 ;; xbm images require special handling, since
637 ;; the only way to create glyphs from these
638 ;; (without a ton of work) is to write them
639 ;; out to a file, and then create a file
641 (let ((file (make-temp-name
642 (expand-file-name "emm.xbm"
646 (write-region (point-min) (point-max) file)
647 (make-glyph (list (cons 'x file))))
649 (delete-file file)))))
652 (vector (intern type) :data (buffer-string)))))))
653 (mm-handle-set-cache handle spec))))))
655 (defun mm-image-fit-p (handle)
656 "Say whether the image in HANDLE will fit the current window."
657 (let ((image (mm-get-image handle)))
658 (or mm-inline-large-images
659 (and (< (glyph-width image) (window-pixel-width))
660 (< (glyph-height image) (window-pixel-height))))))
662 (defun mm-valid-image-format-p (format)
663 "Say whether FORMAT can be displayed natively by Emacs."
664 (and (fboundp 'valid-image-instantiator-format-p)
665 (valid-image-instantiator-format-p format)))
667 (defun mm-valid-and-fit-image-p (format handle)
668 "Say whether FORMAT can be displayed natively and HANDLE fits the window."
670 (mm-valid-image-format-p format)
671 (mm-image-fit-p handle)))
675 ;; mm-decode.el ends here