1 ;;; mime.el --- MIME library module
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: MIME, multimedia, mail, news
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
30 (require 'eword-decode)
32 (autoload 'eword-encode-field "eword-encode"
33 "Encode header field STRING, and return the result.")
34 (autoload 'eword-encode-header "eword-encode"
35 "Encode header fields to network representation, such as MIME encoded-word.")
38 (autoload 'mime-parse-Content-Type "mime-parse"
39 "Parse STRING as field-body of Content-Type field.")
40 (autoload 'mime-read-Content-Type "mime-parse"
41 "Read field-body of Content-Type field from current-buffer,
42 and return parsed it.")
44 (autoload 'mime-parse-Content-Disposition "mime-parse"
45 "Parse STRING as field-body of Content-Disposition field.")
46 (autoload 'mime-read-Content-Disposition "mime-parse"
47 "Read field-body of Content-Disposition field from current-buffer,
48 and return parsed it.")
50 (autoload 'mime-parse-Content-Transfer-Encoding "mime-parse"
51 "Parse STRING as field-body of Content-Transfer-Encoding field.")
52 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
53 "Read field-body of Content-Transfer-Encoding field from
54 current-buffer, and return it.")
56 (autoload 'mime-parse-message "mime-parse"
57 "Parse current-buffer as a MIME message.")
59 (autoload 'mime-parse-buffer "mime-parse"
60 "Parse BUFFER as a MIME message.")
63 ;;; @ Entity Representation and Implementation
66 (defvar mime-entity-implementation-alist nil)
68 (defsubst mime-find-function (service type)
69 (let ((imps (cdr (assq type mime-entity-implementation-alist))))
71 (let ((func (cdr (assq service imps))))
73 (setq func (intern (format "mm%s-%s" type service)))
74 (set-alist 'mime-entity-implementation-alist
75 type (put-alist service func imps))
78 (let ((prefix (format "mm%s" type)))
79 (require (intern prefix))
80 (let ((func (intern (format "%s-%s" prefix service))))
81 (set-alist 'mime-entity-implementation-alist
83 (list (cons service func)))
86 (defsubst mime-entity-function (entity service)
87 (mime-find-function service
88 (mime-entity-representation-type-internal entity)))
90 (defsubst mime-entity-send (entity service &rest args)
91 (apply (mime-find-function
92 service (mime-entity-representation-type-internal entity))
96 (defun mime-open-entity (type location)
97 "Open an entity and return it.
98 TYPE is representation-type.
99 LOCATION is location of entity. Specification of it is depended on
100 representation-type."
101 (funcall (mime-find-function 'open-entity type) location)
104 (defun mime-entity-cooked-p (entity)
105 "Return non-nil if contents of ENTITY has been already code-converted."
106 (funcall (mime-entity-function entity 'cooked-p))
110 ;;; @ Entity as node of message
113 (defalias 'mime-entity-children 'mime-entity-children-internal)
115 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
117 (defun mime-entity-number (entity)
118 "Return entity-number of ENTITY."
119 (reverse (mime-entity-node-id-internal entity)))
121 (defun mime-find-entity-from-number (entity-number &optional message)
122 "Return entity from ENTITY-NUMBER in MESSAGE.
123 If MESSAGE is not specified, `mime-message-structure' is used."
125 (setq message mime-message-structure))
126 (let ((sn (car entity-number)))
129 (let ((rc (nth sn (mime-entity-children message))))
131 (mime-find-entity-from-number (cdr entity-number) rc)
135 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
136 "Return entity from ENTITY-NODE-ID in MESSAGE.
137 If MESSAGE is not specified, `mime-message-structure' is used."
138 (mime-find-entity-from-number (reverse entity-node-id) message))
140 (defun mime-entity-parent (entity &optional message)
141 "Return mother entity of ENTITY.
142 If MESSAGE is not specified, `mime-message-structure' in the buffer of
144 (mime-find-entity-from-node-id
145 (cdr (mime-entity-node-id entity))
148 (set-buffer (mime-entity-buffer entity))
149 mime-message-structure))))
151 (defun mime-root-entity-p (entity)
152 "Return t if ENTITY is root-entity (message)."
153 (null (mime-entity-node-id entity)))
159 (defun mime-entity-buffer (entity)
160 (or (mime-entity-buffer-internal entity)
161 (mime-entity-send entity 'entity-buffer)))
163 (defun mime-entity-point-min (entity)
164 (mime-entity-send entity 'entity-point-min))
166 (defun mime-entity-point-max (entity)
167 (mime-entity-send entity 'entity-point-max))
169 (defun mime-entity-header-start (entity)
170 (or (mime-entity-header-start-internal entity)
171 (mime-entity-send entity 'entity-header-start)))
173 (defun mime-entity-header-end (entity)
174 (or (mime-entity-header-end-internal entity)
175 (mime-entity-send entity 'entity-header-end)))
177 (defun mime-entity-body-start (entity)
178 (or (mime-entity-body-start-internal entity)
179 (mime-entity-send entity 'entity-body-start)))
181 (defun mime-entity-body-end (entity)
182 (or (mime-entity-body-end-internal entity)
183 (mime-entity-send entity 'entity-body-end)))
189 (defun mime-fetch-field (field-name &optional entity)
190 (or (symbolp field-name)
191 (setq field-name (intern (capitalize (capitalize field-name)))))
193 (setq entity mime-message-structure))
194 (let* ((header (mime-entity-original-header-internal entity))
195 (field-body (cdr (assq field-name header))))
199 (mime-entity-send entity 'fetch-field
200 (symbol-name field-name)))
201 (mime-entity-set-original-header-internal
202 entity (put-alist field-name field-body header))
206 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
208 (defun mime-entity-content-disposition (entity)
209 (or (mime-entity-content-disposition-internal entity)
210 (let ((ret (mime-fetch-field 'Content-Disposition entity)))
212 (let ((disposition (mime-parse-Content-Disposition ret)))
214 (mime-entity-set-content-disposition-internal
218 (defun mime-entity-encoding (entity)
219 (or (mime-entity-encoding-internal entity)
220 (let ((ret (mime-fetch-field 'Content-Transfer-Encoding entity)))
222 (let ((encoding (mime-parse-Content-Transfer-Encoding ret)))
224 (mime-entity-set-encoding-internal entity encoding)
227 (defun mime-read-field (field-name &optional entity)
228 (or (symbolp field-name)
229 (setq field-name (capitalize (capitalize field-name))))
231 (setq entity mime-message-structure))
232 (cond ((eq field-name 'Content-Type)
233 (mime-entity-content-type entity)
235 ((eq field-name 'Content-Disposition)
236 (mime-entity-content-disposition entity)
238 ((eq field-name 'Content-Transfer-Encoding)
239 (mime-entity-encoding entity)
242 (let* ((header (mime-entity-parsed-header-internal entity))
243 (field (cdr (assq field-name header))))
245 (let ((field-body (mime-fetch-field field-name entity)))
247 (cond ((memq field-name '(From Resent-From
251 Reply-To Resent-Reply-To))
252 (setq field (std11-parse-addresses
253 (eword-lexical-analyze field-body)))
255 ((memq field-name '(Sender Resent-Sender))
256 (setq field (std11-parse-address
257 (eword-lexical-analyze field-body)))
259 ((memq field-name eword-decode-ignored-field-list)
260 (setq field field-body))
261 ((memq field-name eword-decode-structured-field-list)
262 (setq field (eword-decode-structured-field-body
265 (setq field (eword-decode-unstructured-field-body
268 (mime-entity-set-parsed-header-internal
269 entity (put-alist field-name field header))
272 (defun eword-visible-field-p (field-name visible-fields invisible-fields)
274 (while visible-fields
275 (let ((regexp (car visible-fields)))
276 (if (string-match regexp field-name)
279 (setq visible-fields (cdr visible-fields))
282 (while invisible-fields
283 (let ((regexp (car invisible-fields)))
284 (if (string-match regexp field-name)
287 (setq invisible-fields (cdr invisible-fields))
291 (defun mime-insert-decoded-header (entity
292 &optional invisible-fields visible-fields
294 "Insert before point a decoded header of ENTITY."
295 (let ((default-charset
297 (if (mime-charset-to-coding-system code-conversion)
299 default-mime-charset))))
301 (narrow-to-region (point)(point))
302 (let ((the-buf (current-buffer))
303 (src-buf (mime-entity-buffer entity))
304 (h-end (mime-entity-header-end entity))
305 beg p end field-name len field)
308 (goto-char (mime-entity-header-start entity))
310 (narrow-to-region (point) h-end)
311 (while (re-search-forward std11-field-head-regexp nil t)
312 (setq beg (match-beginning 0)
314 field-name (buffer-substring beg (1- p))
315 len (string-width field-name)
316 end (std11-field-end))
317 (when (eword-visible-field-p field-name
318 visible-fields invisible-fields)
319 (setq field (intern (capitalize field-name)))
324 (cond ((memq field eword-decode-ignored-field-list)
326 (insert-buffer-substring src-buf p end)
328 ((memq field eword-decode-structured-field-list)
329 ;; Decode as structured field
330 (let ((body (save-excursion
332 (buffer-substring p end)))
333 (default-mime-charset default-charset))
334 (insert (eword-decode-and-fold-structured-field
338 ;; Decode as unstructured field
339 (let ((body (save-excursion
341 (buffer-substring p end)))
342 (default-mime-charset default-charset))
343 (insert (eword-decode-unstructured-field-body
350 ;;; @ Entity Attributes
353 (defun mime-entity-uu-filename (entity)
354 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
356 (set-buffer (mime-entity-buffer entity))
357 (goto-char (mime-entity-body-start entity))
358 (if (re-search-forward "^begin [0-9]+ "
359 (mime-entity-body-end entity) t)
360 (if (looking-at ".+$")
361 (buffer-substring (match-beginning 0)(match-end 0))
364 (defun mime-entity-filename (entity)
365 "Return filename of ENTITY."
366 (or (mime-entity-uu-filename entity)
367 (mime-content-disposition-filename
368 (mime-entity-content-disposition entity))
369 (cdr (let ((param (mime-content-type-parameters
370 (mime-entity-content-type entity))))
371 (or (assoc "name" param)
372 (assoc "x-name" param))
376 (defsubst mime-entity-media-type (entity)
377 (mime-content-type-primary-type (mime-entity-content-type entity)))
378 (defsubst mime-entity-media-subtype (entity)
379 (mime-content-type-subtype (mime-entity-content-type entity)))
380 (defsubst mime-entity-parameters (entity)
381 (mime-content-type-parameters (mime-entity-content-type entity)))
382 (defsubst mime-entity-type/subtype (entity-info)
383 (mime-type/subtype-string (mime-entity-media-type entity-info)
384 (mime-entity-media-subtype entity-info)))
390 (defun mime-entity-content (entity)
391 (mime-entity-send entity 'entity-content))
393 (defun mime-write-entity-content (entity filename)
394 "Write content of ENTITY into FILENAME."
395 (mime-entity-send entity 'write-entity-content filename))
397 (defun mime-write-entity (entity filename)
398 "Write ENTITY into FILENAME."
400 (set-buffer (mime-entity-buffer entity))
401 (if (mime-entity-cooked-p entity)
402 (write-region (mime-entity-point-min entity)
403 (mime-entity-point-max entity) filename)
404 (write-region-as-binary (mime-entity-point-min entity)
405 (mime-entity-point-max entity) filename)
408 (defun mime-write-entity-body (entity filename)
409 "Write body of ENTITY into FILENAME."
411 (set-buffer (mime-entity-buffer entity))
412 (if (mime-entity-cooked-p entity)
413 (write-region (mime-entity-body-start entity)
414 (mime-entity-body-end entity) filename)
415 (write-region-as-binary (mime-entity-body-start entity)
416 (mime-entity-body-end entity) filename)
425 ;;; mime.el ends here