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.
31 (defvar mm-inline-media-tests
32 '(("image/jpeg" mm-inline-image (featurep 'jpeg))
33 ("image/png" mm-inline-image (featurep 'png))
34 ("image/gif" mm-inline-image (featurep 'gif))
35 ("image/tiff" mm-inline-image (featurep 'tiff))
36 ("image/xbm" mm-inline-image (eq (device-type) 'x))
37 ("image/xpm" mm-inline-image (featurep 'xpm))
38 ("image/bmp" mm-inline-image (featurep 'bmp))
39 ("text/plain" mm-inline-text t)
40 ("text/html" mm-inline-text (featurep 'w3))
41 ("audio/wav" mm-inline-audio
42 (and (or (featurep 'nas-sound) (featurep 'native-sound))
43 (device-sound-enabled-p)))
44 ("audio/au" mm-inline-audio
45 (and (or (featurep 'nas-sound) (featurep 'native-sound))
46 (device-sound-enabled-p))))
47 "Alist of media types/test that say whether the media types can be displayed inline.")
49 (defvar mm-user-display-methods
50 '(("image/.*" . inline)
51 ("text/.*" . inline)))
53 (defvar mm-user-automatic-display
54 '("text/plain" "text/html" "image/gif"))
56 (defvar mm-alternative-precedence '("text/plain" "text/html")
57 "List that describes the precedence of alternative parts.")
59 (defvar mm-tmp-directory "/tmp/"
60 "Where mm will store its temporary files.")
62 ;;; Internal variables.
64 (defvar mm-dissection-list nil)
65 (defvar mm-last-shell-command "")
67 ;;; Convenience macros.
69 (defmacro mm-handle-buffer (handle)
71 (defmacro mm-handle-type (handle)
73 (defmacro mm-handle-encoding (handle)
75 (defmacro mm-handle-undisplayer (handle)
77 (defmacro mm-handle-set-undisplayer (handle function)
78 `(setcar (nthcdr 3 ,handle) ,function))
79 (defmacro mm-handle-disposition (handle)
81 (defmacro mm-handle-description (handle)
86 (defun mm-dissect-buffer (&optional no-strict-mime)
87 "Dissect the current buffer and return a list of MIME handles."
89 (let (ct ctl type subtype cte cd description)
92 (when (and (or no-strict-mime
93 (mail-fetch-field "mime-version"))
94 (setq ct (mail-fetch-field "content-type")))
95 (setq ctl (mail-header-parse-content-type ct)
96 cte (mail-fetch-field "content-transfer-encoding")
97 cd (mail-fetch-field "content-disposition")
98 description (mail-fetch-field "content-description"))))
100 (setq type (split-string (car ctl) "/"))
101 (setq subtype (cadr type)
104 ((equal type "multipart")
105 (mm-dissect-multipart ctl))
107 (mm-dissect-singlepart
109 (and cte (intern (downcase (mail-header-remove-whitespace
110 (mail-header-remove-comments
113 (and cd (mail-header-parse-content-disposition cd)))))))))
115 (defun mm-dissect-singlepart (ctl cte &optional force cdl description)
117 (not (equal "text/plain" (car ctl))))
118 (let ((res (list (list (mm-copy-to-buffer) ctl cte nil cdl description))))
119 (push (car res) mm-dissection-list)
122 (defun mm-remove-all-parts ()
123 "Remove all MIME handles."
125 (mapcar 'mm-remove-part mm-dissection-list)
126 (setq mm-dissection-list nil))
128 (defun mm-dissect-multipart (ctl)
129 (goto-char (point-min))
130 (let ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
132 (while (search-forward boundary nil t)
137 (narrow-to-region start (point))
138 (setq parts (nconc (mm-dissect-buffer t) parts)))))
140 (setq start (point)))
143 (defun mm-copy-to-buffer ()
144 "Copy the contents of the current buffer to a fresh buffer."
146 (let ((obuf (current-buffer))
148 (goto-char (point-min))
149 (search-forward "\n\n" nil t)
151 (set-buffer (generate-new-buffer " *mm*"))
152 (insert-buffer-substring obuf beg)
155 (defun mm-display-part (handle)
156 "Display the MIME part represented by HANDLE."
158 (mailcap-parse-mailcaps)
159 (if (mm-handle-undisplayer handle)
160 (mm-remove-part handle)
161 (let* ((type (car (mm-handle-type handle)))
162 (method (mailcap-mime-info type))
163 (user-method (mm-user-method type)))
164 (if (eq user-method 'inline)
167 (mm-display-inline handle))
169 handle (or user-method method 'mailcap-save-binary-file)))))))
171 (defun mm-display-external (handle method)
172 "Display HANDLE using METHOD."
173 (mm-with-unibyte-buffer
174 (insert-buffer-substring (mm-handle-buffer handle))
175 (mm-decode-content-transfer-encoding (mm-handle-encoding handle))
176 (if (functionp method)
177 (let ((cur (current-buffer)))
178 (switch-to-buffer (generate-new-buffer "*mm*"))
179 (insert-buffer-substring cur)
181 (mm-handle-set-undisplayer handle (current-buffer)))
182 (let* ((file (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
184 (write-region (point-min) (point-max)
185 file nil 'nomesg nil 'no-conversion)
187 (start-process "*display*" nil shell-file-name
188 "-c" (format method file)))
189 (mm-handle-set-undisplayer handle (cons file process))
190 (message "Displaying %s..." (format method file))))))
192 (defun mm-remove-part (handle)
193 "Remove the displayed MIME part represented by HANDLE."
194 (let ((object (mm-handle-undisplayer handle)))
197 ;; Internally displayed part.
198 ((mm-annotationp object)
199 (delete-annotation object))
200 ((or (functionp object)
202 (eq (car object) 'lambda)))
204 ;; Externally displayed part.
207 (delete-file (car object))
210 (kill-process (cdr object))
213 (when (buffer-live-p object)
214 (kill-buffer object))))
216 (mm-handle-set-undisplayer handle nil)))
218 (defun mm-display-inline (handle)
219 (let* ((type (car (mm-handle-type handle)))
220 (function (cadr (assoc type mm-inline-media-tests))))
221 (funcall function handle)))
223 (defun mm-inlinable-p (type)
224 "Say whether TYPE can be displayed inline."
225 (let ((alist mm-inline-media-tests)
228 (when (equal type (caar alist))
229 (setq test (caddar alist)
231 (setq test (eval test)))
235 (defun mm-user-method (type)
236 "Return the user-defined method for TYPE."
237 (let ((methods mm-user-display-methods)
239 (while (setq method (pop methods))
240 (when (string-match (car method) type)
241 (when (or (not (eq (cdr method) 'inline))
242 (mm-inlinable-p type))
243 (setq result (cdr method)
247 (defun mm-automatic-display-p (type)
248 "Return the user-defined method for TYPE."
249 (let ((methods mm-user-automatic-display)
251 (while (setq method (pop methods))
252 (when (string-match method type)
257 (defun add-mime-display-method (type method)
258 "Make parts of TYPE be displayed with METHOD.
259 This overrides entries in the mailcap file."
260 (push (cons type method) mm-user-display-methods))
262 (defun mm-destroy-part (handle)
263 "Destroy the data structures connected to HANDLE."
264 (mm-remove-part handle)
265 (when (buffer-live-p (mm-handle-buffer handle))
266 (kill-buffer (mm-handle-buffer handle))))
268 (defun mm-quote-arg (arg)
269 "Return a version of ARG that is safe to evaluate in a shell."
270 (let ((pos 0) new-pos accum)
271 ;; *** bug: we don't handle newline characters properly
272 (while (setq new-pos (string-match "[!`\"$\\& \t{}]" arg pos))
273 (push (substring arg pos new-pos) accum)
275 (push (list (aref arg new-pos)) accum)
276 (setq pos (1+ new-pos)))
279 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
282 ;;; Functions for outputting parts
285 (defun mm-get-part (handle)
286 "Return the contents of HANDLE as a string."
287 (mm-with-unibyte-buffer
288 (insert-buffer-substring (mm-handle-buffer handle))
289 (mm-decode-content-transfer-encoding (mm-handle-encoding handle))
292 (defun mm-save-part (handle)
293 "Write HANDLE to a file."
294 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
295 (filename (mail-content-type-get
296 (mm-handle-disposition handle) 'filename))
299 (setq filename (file-name-nondirectory filename)))
301 (read-file-name "Save MIME part to: "
303 (or filename name "") default-directory)))
304 (mm-with-unibyte-buffer
305 (insert-buffer-substring (mm-handle-buffer handle))
306 (mm-decode-content-transfer-encoding (mm-handle-encoding handle))
307 (when (or (not (file-exists-p file))
308 (yes-or-no-p (format "File %s already exists; overwrite? ")))
309 (write-region (point-min) (point-max) file)))))
311 (defun mm-pipe-part (handle)
312 "Pipe HANDLE to a process."
313 (let* ((name (mail-content-type-get (car (mm-handle-type handle)) 'name))
315 (read-string "Shell command on MIME part: " mm-last-shell-command)))
316 (mm-with-unibyte-buffer
317 (insert-buffer-substring (mm-handle-buffer handle))
318 (mm-decode-content-transfer-encoding (mm-handle-encoding handle))
319 (shell-command-on-region (point-min) (point-max) command nil))))
321 (defun mm-interactively-view-part (handle)
322 "Display HANDLE using METHOD."
323 (let* ((type (car (mm-handle-type handle)))
325 (mapcar (lambda (i) (list (cdr (assoc "viewer" i))))
326 (mailcap-mime-info type 'all)))
327 (method (completing-read "Viewer: " methods)))
328 (mm-display-external (copy-sequence handle) method)))
330 (defun mm-preferred-alternative (handles &optional preferred)
331 "Say which of HANDLES are preferred."
332 (let ((prec (if preferred (list preferred) mm-alternative-precedence))
334 (while (setq p (pop prec))
337 (setq type (car (mm-handle-type (car h))))
338 (when (and (equal p type)
339 (mm-automatic-display-p type)
340 (or (not (mm-handle-disposition (car h)))
341 (equal (car (mm-handle-disposition (car h)))
351 ;; mm-decode.el ends here