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" "image/gif"))
56 (defvar mm-tmp-directory "/tmp/"
57 "Where mm will store its temporary files.")
59 ;;; Internal variables.
61 (defvar mm-dissection-list nil)
62 (defvar mm-last-shell-command "")
64 (defun mm-dissect-buffer (&optional no-strict-mime)
65 "Dissect the current buffer and return a list of MIME handles."
67 (let (ct ctl type subtype cte)
69 (drums-narrow-to-header)
70 (when (and (or no-strict-mime
71 (mail-fetch-field "mime-version"))
72 (setq ct (mail-fetch-field "content-type")))
73 (setq ctl (drums-parse-content-type ct))
74 (setq cte (mail-fetch-field "content-transfer-encoding"))))
76 (setq type (split-string (car ctl) "/"))
77 (setq subtype (cadr type)
80 ((equal type "multipart")
81 (mm-dissect-multipart ctl))
83 (mm-dissect-singlepart ctl (and cte (intern cte))
86 (defun mm-dissect-singlepart (ctl cte &optional force)
88 (not (equal "text/plain" (car ctl))))
89 (let ((res (list (list (mm-copy-to-buffer) ctl cte nil))))
90 (push (car res) mm-dissection-list)
93 (defun mm-remove-all-parts ()
94 "Remove all MIME handles."
96 (mapcar 'mm-remove-part mm-dissection-list)
97 (setq mm-dissection-list nil))
99 (defun mm-dissect-multipart (ctl)
100 (goto-char (point-min))
101 (let ((boundary (concat "\n--" (drums-content-type-get ctl 'boundary)))
103 (while (search-forward boundary nil t)
108 (narrow-to-region start (point))
109 (setq parts (nconc (mm-dissect-buffer t) parts)))))
111 (setq start (point)))
114 (defun mm-copy-to-buffer ()
115 "Copy the contents of the current buffer to a fresh buffer."
117 (let ((obuf (current-buffer))
119 (goto-char (point-min))
120 (search-forward "\n\n" nil t)
122 (set-buffer (generate-new-buffer " *mm*"))
123 (insert-buffer-substring obuf beg)
126 (defun mm-display-part (handle)
127 "Display the MIME part represented by HANDLE."
129 (mailcap-parse-mailcaps)
131 (mm-remove-part handle)
132 (let* ((type (caadr handle))
133 (method (mailcap-mime-info type))
134 (user-method (mm-user-method type)))
135 (if (eq user-method 'inline)
138 (mm-display-inline handle))
139 (mm-display-external handle (or user-method method)))))))
141 (defun mm-display-external (handle method)
142 "Display HANDLE using METHOD."
143 (mm-with-unibyte-buffer
144 (insert-buffer-substring (car handle))
145 (mm-decode-content-transfer-encoding (nth 2 handle))
146 (if (functionp method)
147 (let ((cur (current-buffer)))
148 (switch-to-buffer (generate-new-buffer "*mm*"))
149 (insert-buffer-substring cur)
151 (setcar (nthcdr 3 handle) (current-buffer)))
152 (let* ((file (make-temp-name (expand-file-name "emm." mm-tmp-directory)))
154 (write-region (point-min) (point-max)
155 file nil 'nomesg nil 'no-conversion)
157 (start-process "*display*" nil shell-file-name
158 "-c" (format method file)))
159 (setcar (nthcdr 3 handle) (cons file process))
160 (message "Displaying %s..." (format method file))))))
162 (defun mm-remove-part (handle)
163 "Remove the displayed MIME part represented by HANDLE."
164 (let ((object (nth 3 handle)))
166 ;; Internally displayed part.
167 ((mm-annotationp object)
168 (delete-annotation object))
169 ((or (functionp object)
171 (eq (car object) 'lambda)))
173 ;; Externally displayed part.
176 (delete-file (car object))
179 (kill-process (cdr object))
182 (when (buffer-live-p object)
183 (kill-buffer object))))
184 (setcar (nthcdr 3 handle) nil)))
186 (defun mm-display-inline (handle)
187 (let* ((type (caadr handle))
188 (function (cadr (assoc type mm-inline-media-tests))))
189 (funcall function handle)))
191 (defun mm-inlinable-p (type)
192 "Say whether TYPE can be displayed inline."
193 (let ((alist mm-inline-media-tests)
196 (when (equal type (caar alist))
197 (setq test (caddar alist)
199 (setq test (eval test)))
203 (defun mm-user-method (type)
204 "Return the user-defined method for TYPE."
205 (let ((methods mm-user-display-methods)
207 (while (setq method (pop methods))
208 (when (string-match (car method) type)
209 (when (or (not (eq (cdr method) 'inline))
210 (mm-inlinable-p type))
211 (setq result (cdr method)
215 (defun mm-automatic-display-p (type)
216 "Return the user-defined method for TYPE."
217 (let ((methods mm-user-automatic-display)
219 (while (setq method (pop methods))
220 (when (string-match method type)
225 (defun add-mime-display-method (type method)
226 "Make parts of TYPE be displayed with METHOD.
227 This overrides entries in the mailcap file."
228 (push (cons type method) mm-user-display-methods))
230 (defun mm-destroy-part (handle)
231 "Destroy the data structures connected to HANDLE."
232 (mm-remove-part handle)
233 (when (buffer-live-p (car handle))
234 (kill-buffer (car handle))))
236 (defun mm-quote-arg (arg)
237 "Return a version of ARG that is safe to evaluate in a shell."
238 (let ((pos 0) new-pos accum)
239 ;; *** bug: we don't handle newline characters properly
240 (while (setq new-pos (string-match "[!`\"$\\& \t{}]" arg pos))
241 (push (substring arg pos new-pos) accum)
243 (push (list (aref arg new-pos)) accum)
244 (setq pos (1+ new-pos)))
247 (apply 'concat (nconc (nreverse accum) (list (substring arg pos)))))))
250 ;;; Functions for displaying various formats inline
253 (defun mm-inline-image (handle)
254 (let ((type (cadr (split-string (caadr handle) "/")))
256 (mm-with-unibyte-buffer
257 (insert-buffer-substring (car handle))
258 (mm-decode-content-transfer-encoding (nth 2 handle))
259 (setq image (make-image-specifier
260 (vector (intern type) :data (buffer-string)))))
261 (let ((annot (make-annotation image nil 'text)))
262 (set-extent-property annot 'mm t)
263 (set-extent-property annot 'duplicable t)
264 (setcar (nthcdr 3 handle) annot))))
266 (defun mm-inline-text (handle)
267 (let ((type (cadr (split-string (caadr handle) "/")))
268 text buffer-read-only)
269 (mm-with-unibyte-buffer
270 (insert-buffer-substring (car handle))
271 (mm-decode-content-transfer-encoding (nth 2 handle))
272 (setq text (buffer-string)))
274 ((equal type "plain")
278 (narrow-to-region b (point))
279 (let ((charset (drums-content-type-get (nth 1 handle) 'charset)))
281 (mm-decode-body charset nil)))
285 (let (buffer-read-only)
286 (delete-region ,(set-marker (make-marker) (point-min))
287 ,(set-marker (make-marker) (point-max)))))))))
290 (defun mm-inline-audio (handle)
291 (message "Not implemented"))
294 ;;; Functions for outputting parts
297 (defun mm-save-part (handle)
298 "Write HANDLE to a file."
299 (let* ((name (drums-content-type-get (cadr handle) 'name))
300 (file (read-file-name "Save MIME part to: "
302 (or name "") default-directory))))
303 (mm-with-unibyte-buffer
304 (insert-buffer-substring (car handle))
305 (mm-decode-content-transfer-encoding (nth 2 handle))
306 (when (or (not (file-exists-p file))
307 (yes-or-no-p (format "File %s already exists; overwrite? ")))
308 (write-region (point-min) (point-max) file)))))
310 (defun mm-pipe-part (handle)
311 "Pipe HANDLE to a process."
312 (let* ((name (drums-content-type-get (cadr handle) 'name))
314 (read-string "Shell command on MIME part: " mm-last-shell-command)))
315 (mm-with-unibyte-buffer
316 (insert-buffer-substring (car handle))
317 (mm-decode-content-transfer-encoding (nth 2 handle))
318 (shell-command-on-region (point-min) (point-max) command nil))))
320 (defun mm-interactively-view-part (handle)
321 "Display HANDLE using METHOD."
322 (let* ((type (caadr handle))
324 (mapcar (lambda (i) (list (cdr (assoc "viewer" i))))
325 (mailcap-mime-info type 'all)))
326 (method (completing-read "Viewer: " methods)))
327 (mm-display-external (copy-sequence handle) method)))
331 ;; mm-decode.el ends here