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 (defsubst mime-find-function (service type)
67 (let ((imps (cdr (assq type mime-entity-implementation-alist))))
69 (cdr (assq service imps))
70 (require (intern (format "mm%s" type)))
72 (cdr (assq type mime-entity-implementation-alist))))
75 (defsubst mime-entity-function (entity service)
76 (mime-find-function service
77 (mime-entity-representation-type-internal entity)))
79 (defsubst mime-entity-send (entity service &rest args)
80 (apply (mime-find-function
81 service (mime-entity-representation-type-internal entity))
85 (defun mime-open-entity (type location)
86 "Open an entity and return it.
87 TYPE is representation-type.
88 LOCATION is location of entity. Specification of it is depended on
90 (funcall (mime-find-function 'open-entity type) location))
92 (defun mime-entity-cooked-p (entity)
93 "Return non-nil if contents of ENTITY has been already code-converted."
94 (mime-entity-send entity 'cooked-p))
97 ;;; @ Entity as node of message
100 (defalias 'mime-entity-children 'mime-entity-children-internal)
102 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
104 (defun mime-entity-number (entity)
105 "Return entity-number of ENTITY."
106 (reverse (mime-entity-node-id-internal entity)))
108 (defun mime-find-entity-from-number (entity-number &optional message)
109 "Return entity from ENTITY-NUMBER in MESSAGE.
110 If MESSAGE is not specified, `mime-message-structure' is used."
112 (setq message mime-message-structure))
113 (let ((sn (car entity-number)))
116 (let ((rc (nth sn (mime-entity-children message))))
118 (mime-find-entity-from-number (cdr entity-number) rc)
122 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
123 "Return entity from ENTITY-NODE-ID in MESSAGE.
124 If MESSAGE is not specified, `mime-message-structure' is used."
125 (mime-find-entity-from-number (reverse entity-node-id) message))
127 (defun mime-entity-parent (entity &optional message)
128 "Return mother entity of ENTITY.
129 If MESSAGE is not specified, `mime-message-structure' in the buffer of
131 (mime-find-entity-from-node-id
132 (cdr (mime-entity-node-id entity))
135 (set-buffer (mime-entity-buffer entity))
136 mime-message-structure))))
138 (defun mime-root-entity-p (entity)
139 "Return t if ENTITY is root-entity (message)."
140 (null (mime-entity-node-id entity)))
146 (defun mime-entity-buffer (entity)
147 (or (mime-entity-buffer-internal entity)
148 (mime-entity-send entity 'entity-buffer)))
150 (defun mime-entity-point-min (entity)
151 (mime-entity-send entity 'point-min))
153 (defun mime-entity-point-max (entity)
154 (mime-entity-send entity 'point-max))
156 (defun mime-entity-header-start (entity)
157 (or (mime-entity-header-start-internal entity)
158 (mime-entity-send entity 'header-start)))
160 (defun mime-entity-header-end (entity)
161 (or (mime-entity-header-end-internal entity)
162 (mime-entity-send entity 'header-end)))
164 (defun mime-entity-body-start (entity)
165 (or (mime-entity-body-start-internal entity)
166 (mime-entity-send entity 'body-start)))
168 (defun mime-entity-body-end (entity)
169 (or (mime-entity-body-end-internal entity)
170 (mime-entity-send entity 'body-end)))
176 (defun mime-fetch-field (field-name &optional entity)
177 (or (symbolp field-name)
178 (setq field-name (intern (capitalize (capitalize field-name)))))
180 (setq entity mime-message-structure))
181 (let* ((header (mime-entity-original-header-internal entity))
182 (field-body (cdr (assq field-name header))))
186 (mime-entity-send entity 'fetch-field
187 (symbol-name field-name)))
188 (mime-entity-set-original-header-internal
189 entity (put-alist field-name field-body header))
193 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
195 (defun mime-entity-content-disposition (entity)
196 (or (mime-entity-content-disposition-internal entity)
197 (let ((ret (mime-fetch-field 'Content-Disposition entity)))
199 (let ((disposition (mime-parse-Content-Disposition ret)))
201 (mime-entity-set-content-disposition-internal
205 (defun mime-entity-encoding (entity &optional default-encoding)
206 (or (mime-entity-encoding-internal entity)
208 (or (let ((ret (mime-fetch-field
209 'Content-Transfer-Encoding entity)))
210 (and ret (mime-parse-Content-Transfer-Encoding ret)))
211 default-encoding "7bit")))
212 (mime-entity-set-encoding-internal entity encoding)
215 (defun mime-read-field (field-name &optional entity)
216 (or (symbolp field-name)
217 (setq field-name (capitalize (capitalize field-name))))
219 (setq entity mime-message-structure))
220 (cond ((eq field-name 'Content-Type)
221 (mime-entity-content-type entity)
223 ((eq field-name 'Content-Disposition)
224 (mime-entity-content-disposition entity)
226 ((eq field-name 'Content-Transfer-Encoding)
227 (mime-entity-encoding entity)
230 (let* ((header (mime-entity-parsed-header-internal entity))
231 (field (cdr (assq field-name header))))
233 (let ((field-body (mime-fetch-field field-name entity)))
235 (cond ((memq field-name '(From Resent-From
239 Reply-To Resent-Reply-To))
240 (setq field (std11-parse-addresses
241 (eword-lexical-analyze field-body)))
243 ((memq field-name '(Sender Resent-Sender))
244 (setq field (std11-parse-address
245 (eword-lexical-analyze field-body)))
247 ((memq field-name eword-decode-ignored-field-list)
248 (setq field field-body))
249 ((memq field-name eword-decode-structured-field-list)
250 (setq field (eword-decode-structured-field-body
253 (setq field (eword-decode-unstructured-field-body
256 (mime-entity-set-parsed-header-internal
257 entity (put-alist field-name field header))
260 (defun eword-visible-field-p (field-name visible-fields invisible-fields)
262 (while visible-fields
263 (let ((regexp (car visible-fields)))
264 (if (string-match regexp field-name)
267 (setq visible-fields (cdr visible-fields))
270 (while invisible-fields
271 (let ((regexp (car invisible-fields)))
272 (if (string-match regexp field-name)
275 (setq invisible-fields (cdr invisible-fields))
279 (defun mime-insert-decoded-header (entity &optional invisible-fields
281 "Insert before point a decoded header of ENTITY."
282 (mime-entity-send entity 'insert-decoded-header
283 invisible-fields visible-fields))
286 ;;; @ Entity Attributes
289 (defun mime-entity-uu-filename (entity)
290 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
292 (set-buffer (mime-entity-buffer entity))
293 (goto-char (mime-entity-body-start entity))
294 (if (re-search-forward "^begin [0-9]+ "
295 (mime-entity-body-end entity) t)
296 (if (looking-at ".+$")
297 (buffer-substring (match-beginning 0)(match-end 0))
300 (defun mime-entity-filename (entity)
301 "Return filename of ENTITY."
302 (or (mime-entity-uu-filename entity)
303 (mime-content-disposition-filename
304 (mime-entity-content-disposition entity))
305 (cdr (let ((param (mime-content-type-parameters
306 (mime-entity-content-type entity))))
307 (or (assoc "name" param)
308 (assoc "x-name" param))
312 (defsubst mime-entity-media-type (entity)
313 (mime-content-type-primary-type (mime-entity-content-type entity)))
314 (defsubst mime-entity-media-subtype (entity)
315 (mime-content-type-subtype (mime-entity-content-type entity)))
316 (defsubst mime-entity-parameters (entity)
317 (mime-content-type-parameters (mime-entity-content-type entity)))
318 (defsubst mime-entity-type/subtype (entity-info)
319 (mime-type/subtype-string (mime-entity-media-type entity-info)
320 (mime-entity-media-subtype entity-info)))
326 (defun mime-entity-content (entity)
327 (mime-entity-send entity 'entity-content))
329 (defun mime-write-entity-content (entity filename)
330 "Write content of ENTITY into FILENAME."
331 (mime-entity-send entity 'write-entity-content filename))
333 (defun mime-write-entity (entity filename)
334 "Write ENTITY into FILENAME."
335 (mime-entity-send entity 'write-entity filename))
337 (defun mime-write-entity-body (entity filename)
338 "Write body of ENTITY into FILENAME."
339 (mime-entity-send entity 'write-entity-body filename))
347 ;;; mime.el ends here