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 ;;; 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 (eq (device-type) 'x))
54 ("image/xpm" mm-inline-image (featurep 'xpm))
55 ("image/bmp" mm-inline-image (featurep 'bmp))
56 ("text/plain" mm-inline-text t)
57 ("text/enriched" mm-inline-text t)
58 ("text/richtext" mm-inline-text t)
59 ("text/html" mm-inline-text (locate-library "w3"))
60 ("message/delivery-status" mm-inline-text t)
61 ("audio/wav" mm-inline-audio
62 (and (or (featurep 'nas-sound) (featurep 'native-sound))
63 (device-sound-enabled-p)))
64 ("audio/au" mm-inline-audio
65 (and (or (featurep 'nas-sound) (featurep 'native-sound))
66 (device-sound-enabled-p))))
67 "Alist of media types/test that say whether the media types can be displayed inline.")
69 (defvar mm-user-display-methods
70 '(("image/.*" . inline)
72 ("message/delivery-status" . inline)))
74 (defvar mm-user-automatic-display
75 '("text/plain" "text/enriched" "text/richtext" "text/html" "image/gif"
76 "message/delivery-status" "multipart/.*"))
78 (defvar mm-alternative-precedence
79 '("text/plain" "text/enriched" "text/richtext" "text/html")
80 "List that describes the precedence of alternative parts.")
82 (defvar mm-tmp-directory "/tmp/"
83 "Where mm will store its temporary files.")
85 ;;; Internal variables.
87 (defvar mm-dissection-list nil)
88 (defvar mm-last-shell-command "")
89 (defvar mm-content-id-alist nil)
93 (defun mm-dissect-buffer (&optional no-strict-mime)
94 "Dissect the current buffer and return a list of MIME handles."
96 (let (ct ctl type subtype cte cd description id result)
99 (when (and (or no-strict-mime
100 (mail-fetch-field "mime-version"))
101 (setq ct (mail-fetch-field "content-type")))
102 (setq ctl (condition-case () (mail-header-parse-content-type ct)
104 cte (mail-fetch-field "content-transfer-encoding")
105 cd (mail-fetch-field "content-disposition")
106 description (mail-fetch-field "content-description")
107 id (mail-fetch-field "content-id"))))
109 (mm-dissect-singlepart '("text/plain") nil no-strict-mime nil nil)
110 (setq type (split-string (car ctl) "/"))
111 (setq subtype (cadr type)
116 ((equal type "multipart")
117 (cons (car ctl) (mm-dissect-multipart ctl)))
119 (mm-dissect-singlepart
121 (and cte (intern (downcase (mail-header-remove-whitespace
122 (mail-header-remove-comments
125 (and cd (condition-case ()
126 (mail-header-parse-content-disposition cd)
129 (push (cons id result) mm-content-id-alist))
132 (defun mm-dissect-singlepart (ctl cte &optional force cdl description)
134 (not (equal "text/plain" (car ctl))))
135 (let ((res (list (mm-copy-to-buffer) ctl cte nil cdl description)))
136 (push (car res) mm-dissection-list)
139 (defun mm-remove-all-parts ()
140 "Remove all MIME handles."
142 (mapcar 'mm-remove-part mm-dissection-list)
143 (setq mm-dissection-list nil))
145 (defun mm-dissect-multipart (ctl)
146 (goto-char (point-min))
147 (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
148 (close-delimiter (concat boundary "--[ \t]*$"))
151 (goto-char (point-max))
152 (if (re-search-backward close-delimiter nil t)
155 (while (search-forward boundary end t)
156 (goto-char (match-beginning 0))
160 (narrow-to-region start (point))
161 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
163 (setq start (point)))
167 (narrow-to-region start end)
168 (setq parts (nconc (list (mm-dissect-buffer t)) parts)))))
171 (defun mm-copy-to-buffer ()
172 "Copy the contents of the current buffer to a fresh buffer."
174 (let ((obuf (current-buffer))
176 (goto-char (point-min))
177 (search-forward "\n\n" nil t)
179 (set-buffer (generate-new-buffer " *mm*"))
180 (insert-buffer-substring obuf beg)
183 (defun mm-inlinable-part-p (type)
184 "Say whether TYPE can be displayed inline."
185 (eq (mm-user-method type) 'inline))
187 (defun mm-display-part (handle &optional no-default)
188 "Display the MIME part represented by HANDLE."
190 (mailcap-parse-mailcaps)
191 (if (mm-handle-displayed-p handle)
192 (mm-remove-part handle)
193 (let* ((type (car (mm-handle-type handle)))
194 (method (mailcap-mime-info type))
195 (user-method (mm-user-method type)))
196 (if (eq user-method 'inline)
199 (mm-display-inline handle))
200 (when (or user-method
203 (if (and (not user-method)
205 (equal "text" (car (split-string type))))
206 (mm-insert-inline handle (mm-get-part handle))
208 handle (or user-method method
209 'mailcap-save-binary-file)))))))))
211 (defun mm-display-external (handle method)
212 "Display HANDLE using METHOD."
213 (mm-with-unibyte-buffer
214 (insert-buffer-substring (mm-handle-buffer handle))
215 (mm-decode-content-transfer-encoding
216 (mm-handle-encoding handle) (car (mm-handle-type handle)))
217 (if (functionp method)
218 (let ((cur (current-buffer)))
219 (if (eq method 'mailcap-save-binary-file)
221 (set-buffer (generate-new-buffer "*mm*"))
223 (let ((win (get-buffer-window cur t)))
225 (select-window win)))
226 (switch-to-buffer (generate-new-buffer "*mm*")))
227 (buffer-disable-undo)
228 (mm-set-buffer-file-coding-system 'no-conversion)
229 (insert-buffer-substring cur)
230 (message "Viewing with %s" method)
231 (let ((mm (current-buffer)))
235 (mm-save-part handle))
236 (mm-handle-set-undisplayer handle mm))))
237 (let* ((dir (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
238 (filename (mail-content-type-get
239 (mm-handle-disposition handle) 'filename))
240 (needsterm (assoc "needsterm"
242 (car (mm-handle-type handle)) t)))
244 ;; We create a private sub-directory where we store our files.
246 (set-file-modes dir 448)
248 (setq file (expand-file-name (file-name-nondirectory filename)
250 (setq file (make-temp-name (expand-file-name "mm." dir))))
251 (write-region (point-min) (point-max)
252 file nil 'nomesg nil 'no-conversion)
253 (message "Viewing with %s" method)
257 (start-process "*display*" nil
259 "-e" shell-file-name "-c"
261 (mm-quote-arg file)))
262 (start-process "*display*" (generate-new-buffer "*mm*")
265 (mm-quote-arg file)))))
266 (mm-handle-set-undisplayer handle (cons file process)))
267 (message "Displaying %s..." (format method file))))))
269 (defun mm-remove-parts (handles)
270 "Remove the displayed MIME parts represented by HANDLE."
271 (if (and (listp handles)
272 (bufferp (car handles)))
273 (mm-remove-part handles)
275 (while (setq handle (pop handles))
280 (stringp (car handle)))
281 (mm-remove-parts (cdr handle)))
283 (mm-remove-part handle)))))))
285 (defun mm-destroy-parts (handles)
286 "Remove the displayed MIME parts represented by HANDLE."
287 (if (and (listp handles)
288 (bufferp (car handles)))
289 (mm-destroy-part handles)
291 (while (setq handle (pop handles))
296 (stringp (car handle)))
297 (mm-destroy-parts (cdr handle)))
299 (mm-destroy-part handle)))))))
301 (defun mm-remove-part (handle)
302 "Remove the displayed MIME part represented by HANDLE."
304 (let ((object (mm-handle-undisplayer handle)))
307 ;; Internally displayed part.
308 ((mm-annotationp object)
309 (delete-annotation object))
310 ((or (functionp object)
312 (eq (car object) 'lambda)))
314 ;; Externally displayed part.
317 (delete-file (car object))
320 (delete-directory (file-name-directory (car object)))
323 (kill-process (cdr object))
326 (when (buffer-live-p object)
327 (kill-buffer object))))
329 (mm-handle-set-undisplayer handle nil))))
331 (defun mm-display-inline (handle)
332 (let* ((type (car (mm-handle-type handle)))
333 (function (cadr (assoc type mm-inline-media-tests))))
334 (funcall function handle)
335 (goto-char (point-min))))
337 (defun mm-inlinable-p (type)
338 "Say whether TYPE can be displayed inline."
339 (let ((alist mm-inline-media-tests)
342 (when (equal type (caar alist))
343 (setq test (caddar alist)
345 (setq test (eval test)))
349 (defun mm-user-method (type)
350 "Return the user-defined method for TYPE."
351 (let ((methods mm-user-display-methods)
353 (while (setq method (pop methods))
354 (when (string-match (car method) type)
355 (when (or (not (eq (cdr method) 'inline))
356 (mm-inlinable-p type))
357 (setq result (cdr method)
361 (defun mm-automatic-display-p (type)
362 "Return the user-defined method for TYPE."
363 (let ((methods mm-user-automatic-display)
365 (while (setq method (pop methods))
366 (when (string-match method type)
371 (defun add-mime-display-method (type method)
372 "Make parts of TYPE be displayed with METHOD.
373 This overrides entries in the mailcap file."
374 (push (cons type method) mm-user-display-methods))
376 (defun mm-destroy-part (handle)
377 "Destroy the data structures connected to HANDLE."
379 (mm-remove-part handle)
380 (when (buffer-live-p (mm-handle-buffer handle))
381 (kill-buffer (mm-handle-buffer handle)))))
383 (defun mm-handle-displayed-p (handle)
384 "Say whether HANDLE is displayed or not."
385 (mm-handle-undisplayer handle))
387 (defun mm-quote-arg (arg)
388 "Return a version of ARG that is safe to evaluate in a shell."
389 (let ((pos 0) new-pos accum)
390 ;; *** bug: we don't handle newline characters properly
391 (while (setq new-pos (string-match "[!`\"$\\& \t{} ]" arg pos))
392 (push (substring arg pos new-pos) accum)
394 (push (list (aref arg new-pos)) accum)
395 (setq pos (1+ new-pos)))
398 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
401 ;;; Functions for outputting parts
404 (defun mm-get-part (handle)
405 "Return the contents of HANDLE as a string."
406 (mm-with-unibyte-buffer
407 (insert-buffer-substring (mm-handle-buffer handle))
408 (mm-decode-content-transfer-encoding
409 (mm-handle-encoding handle)
410 (car (mm-handle-type handle)))
413 (defvar mm-default-directory nil)
415 (defun mm-save-part (handle)
416 "Write HANDLE to a file."
417 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
418 (filename (mail-content-type-get
419 (mm-handle-disposition handle) 'filename))
422 (setq filename (file-name-nondirectory filename)))
424 (read-file-name "Save MIME part to: "
426 (or filename name "")
427 (or mm-default-directory default-directory))))
428 (setq mm-default-directory (file-name-directory file))
429 (mm-with-unibyte-buffer
430 (insert-buffer-substring (mm-handle-buffer handle))
431 (mm-decode-content-transfer-encoding
432 (mm-handle-encoding handle)
433 (car (mm-handle-type handle)))
434 (when (or (not (file-exists-p file))
435 (yes-or-no-p (format "File %s already exists; overwrite? "
437 (write-region (point-min) (point-max) file)))))
439 (defun mm-pipe-part (handle)
440 "Pipe HANDLE to a process."
441 (let* ((name (mail-content-type-get (mm-handle-type handle) 'name))
443 (read-string "Shell command on MIME part: " mm-last-shell-command)))
444 (mm-with-unibyte-buffer
445 (insert-buffer-substring (mm-handle-buffer handle))
446 (mm-decode-content-transfer-encoding
447 (mm-handle-encoding handle)
448 (car (mm-handle-type handle)))
449 (shell-command-on-region (point-min) (point-max) command nil))))
451 (defun mm-interactively-view-part (handle)
452 "Display HANDLE using METHOD."
453 (let* ((type (car (mm-handle-type handle)))
455 (mapcar (lambda (i) (list (cdr (assoc 'viewer i))))
456 (mailcap-mime-info type 'all)))
457 (method (completing-read "Viewer: " methods)))
458 (mm-display-external (copy-sequence handle) method)))
460 (defun mm-preferred-alternative (handles &optional preferred)
461 "Say which of HANDLES are preferred."
462 (let ((prec (if preferred (list preferred) mm-alternative-precedence))
464 (while (setq p (pop prec))
468 (if (stringp (caar h))
470 (car (mm-handle-type (car h)))))
471 (when (and (equal p type)
472 (mm-automatic-display-p type)
473 (or (stringp (caar h))
474 (not (mm-handle-disposition (car h)))
475 (equal (car (mm-handle-disposition (car h)))
483 (defun mm-get-content-id (id)
484 "Return the handle(s) referred to by ID."
485 (cdr (assoc id mm-content-id-alist)))
489 ;; mm-decode.el ends here