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 &optional invisible-fields
293 "Insert before point a decoded header of ENTITY."
294 (mime-entity-send entity 'insert-decoded-header
295 invisible-fields visible-fields))
298 ;;; @ Entity Attributes
301 (defun mime-entity-uu-filename (entity)
302 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
304 (set-buffer (mime-entity-buffer entity))
305 (goto-char (mime-entity-body-start entity))
306 (if (re-search-forward "^begin [0-9]+ "
307 (mime-entity-body-end entity) t)
308 (if (looking-at ".+$")
309 (buffer-substring (match-beginning 0)(match-end 0))
312 (defun mime-entity-filename (entity)
313 "Return filename of ENTITY."
314 (or (mime-entity-uu-filename entity)
315 (mime-content-disposition-filename
316 (mime-entity-content-disposition entity))
317 (cdr (let ((param (mime-content-type-parameters
318 (mime-entity-content-type entity))))
319 (or (assoc "name" param)
320 (assoc "x-name" param))
324 (defsubst mime-entity-media-type (entity)
325 (mime-content-type-primary-type (mime-entity-content-type entity)))
326 (defsubst mime-entity-media-subtype (entity)
327 (mime-content-type-subtype (mime-entity-content-type entity)))
328 (defsubst mime-entity-parameters (entity)
329 (mime-content-type-parameters (mime-entity-content-type entity)))
330 (defsubst mime-entity-type/subtype (entity-info)
331 (mime-type/subtype-string (mime-entity-media-type entity-info)
332 (mime-entity-media-subtype entity-info)))
338 (defun mime-entity-content (entity)
339 (mime-entity-send entity 'entity-content))
341 (defun mime-write-entity-content (entity filename)
342 "Write content of ENTITY into FILENAME."
343 (mime-entity-send entity 'write-entity-content filename))
345 (defun mime-write-entity (entity filename)
346 "Write ENTITY into FILENAME."
347 (mime-entity-send entity 'write-entity filename))
349 (defun mime-write-entity-body (entity filename)
350 "Write body of ENTITY into FILENAME."
351 (mime-entity-send entity 'write-entity-body filename))
359 ;;; mime.el ends here