1 ;;; mm-decode.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998 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)
48 (defvar mm-inline-media-tests
49 '(("image/jpeg" mm-inline-image (featurep 'jpeg))
50 ("image/png" mm-inline-image (featurep 'png))
51 ("image/gif" mm-inline-image (featurep 'gif))
52 ("image/tiff" mm-inline-image (featurep 'tiff))
53 ("image/xbm" mm-inline-image (and (fboundp 'device-type)
54 (eq (device-type) 'x)))
55 ("image/xpm" mm-inline-image (featurep 'xpm))
56 ("image/bmp" mm-inline-image (featurep 'bmp))
57 ("text/plain" mm-inline-text t)
58 ("text/enriched" mm-inline-text t)
59 ("text/richtext" mm-inline-text t)
60 ("text/html" mm-inline-text (locate-library "w3"))
61 ("message/delivery-status" mm-inline-text t)
62 ("audio/wav" mm-inline-audio
63 (and (or (featurep 'nas-sound) (featurep 'native-sound))
64 (device-sound-enabled-p)))
65 ("audio/au" mm-inline-audio
66 (and (or (featurep 'nas-sound) (featurep 'native-sound))
67 (device-sound-enabled-p))))
68 "Alist of media types/test that say whether the media types can be displayed inline.")
70 (defvar mm-user-display-methods
71 '(("image/.*" . inline)
73 ("message/delivery-status" . inline)))
75 (defvar mm-user-automatic-display
76 '("text/plain" "text/enriched" "text/richtext" "text/html"
77 "image/.*" "message/delivery-status" "multipart/.*"))
79 (defvar mm-alternative-precedence
80 '("text/html" "text/enriched" "text/richtext" "text/plain")
81 "List that describes the precedence of alternative parts.")
83 (defvar mm-tmp-directory "/tmp/"
84 "Where mm will store its temporary files.")
86 ;;; Internal variables.
88 (defvar mm-dissection-list nil)
89 (defvar mm-last-shell-command "")
90 (defvar mm-content-id-alist nil)
94 (defun mm-dissect-buffer (&optional no-strict-mime)
95 "Dissect the current buffer and return a list of MIME handles."
97 (let (ct ctl type subtype cte cd description id result)
100 (when (and (or no-strict-mime
101 (mail-fetch-field "mime-version"))
102 (setq ct (mail-fetch-field "content-type")))
103 (setq ctl (condition-case () (mail-header-parse-content-type ct)
105 cte (mail-fetch-field "content-transfer-encoding")
106 cd (mail-fetch-field "content-disposition")
107 description (mail-fetch-field "content-description")
108 id (mail-fetch-field "content-id"))))
110 (mm-dissect-singlepart
111 '("text/plain") nil no-strict-mime nil description)
112 (setq type (split-string (car ctl) "/"))
113 (setq subtype (cadr type)
118 ((equal type "multipart")
119 (cons (car ctl) (mm-dissect-multipart ctl)))
121 (mm-dissect-singlepart
123 (and cte (intern (downcase (mail-header-remove-whitespace
124 (mail-header-remove-comments
127 (and cd (condition-case ()
128 (mail-header-parse-content-disposition cd)
132 (push (cons id result) mm-content-id-alist))
135 (defun mm-dissect-singlepart (ctl cte &optional force cdl description)
137 (not (equal "text/plain" (car ctl))))
138 (let ((res (list (mm-copy-to-buffer) ctl cte nil cdl description)))
139 (push (car res) mm-dissection-list)
142 (defun mm-remove-all-parts ()
143 "Remove all MIME handles."
145 (mapcar 'mm-remove-part mm-dissection-list)
146 (setq mm-dissection-list nil))
148 (defun mm-dissect-multipart (ctl)
149 (goto-char (point-min))
150 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
151 (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
154 (goto-char (point-max))
155 (if (re-search-backward close-delimiter nil t)
158 (while (search-forward boundary end t)
159 (goto-char (match-beginning 0))
163 (narrow-to-region start (point))
164 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
166 (setq start (point)))
170 (narrow-to-region start end)
171 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
174 (defun mm-copy-to-buffer ()
175 "Copy the contents of the current buffer to a fresh buffer."
177 (let ((obuf (current-buffer))
179 (goto-char (point-min))
180 (search-forward-regexp "^\n" nil t)
182 (set-buffer (generate-new-buffer " *mm*"))
183 (insert-buffer-substring obuf beg)
186 (defun mm-inlinable-part-p (type)
187 "Say whether TYPE can be displayed inline."
188 (eq (mm-user-method type) 'inline))
190 (defun mm-display-part (handle &optional no-default)
191 "Display the MIME part represented by HANDLE.
192 Returns nil if the part is removed; inline if displayed inline;
193 external if displayed external."
195 (mailcap-parse-mailcaps)
196 (if (mm-handle-displayed-p handle)
197 (mm-remove-part handle)
198 (let* ((type (car (mm-handle-type handle)))
199 (method (mailcap-mime-info type))
200 (user-method (mm-user-method type)))
201 (if (eq user-method 'inline)
204 (mm-display-inline handle))
205 (when (or user-method
208 (if (and (not user-method)
210 (equal "text" (car (split-string type))))
212 (mm-insert-inline handle (mm-get-part handle))
215 handle (or user-method method
216 'mailcap-save-binary-file))
219 (defun mm-display-external (handle method)
220 "Display HANDLE using METHOD."
221 (mm-with-unibyte-buffer
222 (insert-buffer-substring (mm-handle-buffer handle))
223 (mm-decode-content-transfer-encoding
224 (mm-handle-encoding handle) (car (mm-handle-type handle)))
225 (if (functionp method)
226 (let ((cur (current-buffer)))
227 (if (eq method 'mailcap-save-binary-file)
229 (set-buffer (generate-new-buffer "*mm*"))
231 (let ((win (get-buffer-window cur t)))
233 (select-window win)))
234 (switch-to-buffer (generate-new-buffer "*mm*")))
235 (buffer-disable-undo)
236 (mm-set-buffer-file-coding-system 'no-conversion)
237 (insert-buffer-substring cur)
238 (message "Viewing with %s" method)
239 (let ((mm (current-buffer)))
243 (mm-save-part handle))
244 (mm-handle-set-undisplayer handle mm))))
245 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
246 (filename (mail-content-type-get
247 (mm-handle-disposition handle) 'filename))
248 (needsterm (assoc "needsterm"
250 (car (mm-handle-type handle)) t)))
252 ;; We create a private sub-directory where we store our files.
254 (set-file-modes dir 448)
256 (setq file (expand-file-name (file-name-nondirectory filename)
258 (setq file (make-temp-name (expand-file-name "mm." dir))))
259 (write-region (point-min) (point-max)
260 file nil 'nomesg nil 'no-conversion)
261 (message "Viewing with %s" method)
265 (start-process "*display*" nil
267 "-e" shell-file-name "-c"
269 (mm-quote-arg file)))
270 (start-process "*display*" (generate-new-buffer "*mm*")
273 (mm-quote-arg file)))))
274 (mm-handle-set-undisplayer handle (cons file process)))
275 (message "Displaying %s..." (format method file))))))
277 (defun mm-remove-parts (handles)
278 "Remove the displayed MIME parts represented by HANDLE."
279 (if (and (listp handles)
280 (bufferp (car handles)))
281 (mm-remove-part handles)
283 (while (setq handle (pop handles))
288 (stringp (car handle)))
289 (mm-remove-parts (cdr handle)))
291 (mm-remove-part handle)))))))
293 (defun mm-destroy-parts (handles)
294 "Remove the displayed MIME parts represented by HANDLE."
295 (if (and (listp handles)
296 (bufferp (car handles)))
297 (mm-destroy-part handles)
299 (while (setq handle (pop handles))
304 (stringp (car handle)))
305 (mm-destroy-parts (cdr handle)))
307 (mm-destroy-part handle)))))))
309 (defun mm-remove-part (handle)
310 "Remove the displayed MIME part represented by HANDLE."
312 (let ((object (mm-handle-undisplayer handle)))
315 ;; Internally displayed part.
316 ((mm-annotationp object)
317 (delete-annotation object))
318 ((or (functionp object)
320 (eq (car object) 'lambda)))
322 ;; Externally displayed part.
325 (delete-file (car object))
328 (delete-directory (file-name-directory (car object)))
331 (kill-process (cdr object))
334 (when (buffer-live-p object)
335 (kill-buffer object))))
337 (mm-handle-set-undisplayer handle nil))))
339 (defun mm-display-inline (handle)
340 (let* ((type (car (mm-handle-type handle)))
341 (function (cadr (assoc type mm-inline-media-tests))))
342 (funcall function handle)
343 (goto-char (point-min))))
345 (defun mm-inlinable-p (type)
346 "Say whether TYPE can be displayed inline."
347 (let ((alist mm-inline-media-tests)
350 (when (equal type (caar alist))
351 (setq test (caddar alist)
353 (setq test (eval test)))
357 (defun mm-user-method (type)
358 "Return the user-defined method for TYPE."
359 (let ((methods mm-user-display-methods)
361 (while (setq method (pop methods))
362 (when (string-match (car method) type)
363 (when (or (not (eq (cdr method) 'inline))
364 (mm-inlinable-p type))
365 (setq result (cdr method)
369 (defun mm-automatic-display-p (type)
370 "Return the user-defined method for TYPE."
371 (let ((methods mm-user-automatic-display)
373 (while (setq method (pop methods))
374 (when (and (string-match method type)
375 (mm-inlinable-p type))
380 (defun add-mime-display-method (type method)
381 "Make parts of TYPE be displayed with METHOD.
382 This overrides entries in the mailcap file."
383 (push (cons type method) mm-user-display-methods))
385 (defun mm-destroy-part (handle)
386 "Destroy the data structures connected to HANDLE."
388 (mm-remove-part handle)
389 (when (buffer-live-p (mm-handle-buffer handle))
390 (kill-buffer (mm-handle-buffer handle)))))
392 (defun mm-handle-displayed-p (handle)
393 "Say whether HANDLE is displayed or not."
394 (mm-handle-undisplayer handle))
396 (defun mm-quote-arg (arg)
397 "Return a version of ARG that is safe to evaluate in a shell."
398 (let ((pos 0) new-pos accum)
399 ;; *** bug: we don't handle newline characters properly
400 (while (setq new-pos (string-match "[;!`\"$\\& \t{} ]" arg pos))
401 (push (substring arg pos new-pos) accum)
403 (push (list (aref arg new-pos)) accum)
404 (setq pos (1+ new-pos)))
407 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
410 ;;; Functions for outputting parts
413 (defun mm-get-part (handle)
414 "Return the contents of HANDLE as a string."
415 (mm-with-unibyte-buffer
416 (insert-buffer-substring (mm-handle-buffer handle))
417 (mm-decode-content-transfer-encoding
418 (mm-handle-encoding handle)
419 (car (mm-handle-type handle)))
422 (defvar mm-default-directory nil)
424 (defun mm-save-part (handle)
425 "Write HANDLE to a file."
426 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
427 (filename (mail-content-type-get
428 (mm-handle-disposition handle) 'filename))
431 (setq filename (file-name-nondirectory filename)))
433 (read-file-name "Save MIME part to: "
435 (or filename name "")
436 (or mm-default-directory default-directory))))
437 (setq mm-default-directory (file-name-directory file))
438 (mm-with-unibyte-buffer
439 (insert-buffer-substring (mm-handle-buffer handle))
440 (mm-decode-content-transfer-encoding
441 (mm-handle-encoding handle)
442 (car (mm-handle-type handle)))
443 (when (or (not (file-exists-p file))
444 (yes-or-no-p (format "File %s already exists; overwrite? "
446 (write-region (point-min) (point-max) file)))))
448 (defun mm-pipe-part (handle)
449 "Pipe HANDLE to a process."
450 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
452 (read-string "Shell command on MIME part: " mm-last-shell-command)))
453 (mm-with-unibyte-buffer
454 (insert-buffer-substring (mm-handle-buffer handle))
455 (mm-decode-content-transfer-encoding
456 (mm-handle-encoding handle)
457 (car (mm-handle-type handle)))
458 (shell-command-on-region (point-min) (point-max) command nil))))
460 (defun mm-interactively-view-part (handle)
461 "Display HANDLE using METHOD."
462 (let* ((type (car (mm-handle-type handle)))
464 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
465 (mailcap-mime-info type 'all)))
466 (method (completing-read "Viewer: " methods)))
467 (mm-display-external (copy-sequence handle) method)))
469 (defun mm-preferred-alternative (handles &optional preferred)
470 "Say which of HANDLES are preferred."
471 (let ((prec (if preferred (list preferred) mm-alternative-precedence))
473 (while (setq p (pop prec))
477 (if (stringp (caar h))
479 (car (mm-handle-type (car h)))))
480 (when (and (equal p type)
481 (mm-automatic-display-p type)
482 (or (stringp (caar h))
483 (not (mm-handle-disposition (car h)))
484 (equal (car (mm-handle-disposition (car h)))
492 (defun mm-get-content-id (id)
493 "Return the handle(s) referred to by ID."
494 (cdr (assoc id mm-content-id-alist)))
498 ;; mm-decode.el ends here