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-buffer "mime-parse"
57 "Parse BUFFER as a MIME message.")
60 ;;; @ Entity Representation and Implementation
63 (defsubst mime-find-function (service type)
64 (let ((imps (cdr (assq type mime-entity-implementation-alist))))
66 (cdr (assq service imps))
67 (require (intern (format "mm%s" type)))
69 (cdr (assq type mime-entity-implementation-alist))))
72 (defsubst mime-entity-function (entity service)
73 (mime-find-function service
74 (mime-entity-representation-type-internal entity)))
76 (defsubst mime-entity-send (entity message &rest args)
77 "Send MESSAGE to ENTITY with ARGS, and return the result."
78 (apply (mime-find-function
79 message (mime-entity-representation-type-internal entity))
83 (defmacro mm-define-generic (name args &optional doc)
85 `(defun ,(intern (format "mime-%s" name)) ,args
87 (mime-entity-send ,(car args) ',name
88 ,@(mm-arglist-to-arguments (cdr args)))
90 `(defun ,(intern (format "mime-%s" name)) ,args
91 (mime-entity-send ,(car args) ',name
92 ,@(mm-arglist-to-arguments (cdr args)))
95 (put 'mm-define-generic 'lisp-indent-function 'defun)
97 (defun mime-open-entity (type location)
98 "Open an entity and return it.
99 TYPE is representation-type.
100 LOCATION is location of entity. Specification of it is depended on
101 representation-type."
102 (let ((entity (make-mime-entity-internal type location)))
103 (mime-entity-send entity 'initialize-instance)
106 (mm-define-generic entity-cooked-p (entity)
107 "Return non-nil if contents of ENTITY has been already code-converted.")
110 ;;; @ Entity as node of message
113 (defun mime-entity-children (entity)
114 (or (mime-entity-children-internal entity)
115 (mime-entity-send entity 'entity-children)))
117 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
119 (defun mime-entity-number (entity)
120 "Return entity-number of ENTITY."
121 (reverse (mime-entity-node-id-internal entity)))
123 (defun mime-find-entity-from-number (entity-number &optional message)
124 "Return entity from ENTITY-NUMBER in MESSAGE.
125 If MESSAGE is not specified, `mime-message-structure' is used."
127 (setq message mime-message-structure))
128 (let ((sn (car entity-number)))
131 (let ((rc (nth sn (mime-entity-children message))))
133 (mime-find-entity-from-number (cdr entity-number) rc)
137 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
138 "Return entity from ENTITY-NODE-ID in MESSAGE.
139 If MESSAGE is not specified, `mime-message-structure' is used."
140 (mime-find-entity-from-number (reverse entity-node-id) message))
142 (defun mime-entity-parent (entity &optional message)
143 "Return mother entity of ENTITY.
144 If MESSAGE is specified, it is regarded as root entity."
145 (if (equal entity message)
147 (mime-entity-parent-internal entity)))
149 (defun mime-root-entity-p (entity &optional message)
150 "Return t if ENTITY is root-entity (message).
151 If MESSAGE is specified, it is regarded as root entity."
152 (null (mime-entity-parent entity message)))
158 (defun mime-entity-buffer (entity)
159 (or (mime-entity-buffer-internal entity)
160 (mime-entity-send entity 'entity-buffer)))
162 (mm-define-generic entity-point-min (entity)
163 "Return the start point of ENTITY in the buffer which contains ENTITY.")
165 (mm-define-generic entity-point-max (entity)
166 "Return the end point of ENTITY in the buffer which contains ENTITY.")
168 (defun mime-entity-header-start (entity)
169 (or (mime-entity-header-start-internal entity)
170 (mime-entity-send entity 'entity-header-start)))
172 (defun mime-entity-header-end (entity)
173 (or (mime-entity-header-end-internal entity)
174 (mime-entity-send entity 'entity-header-end)))
176 (defun mime-entity-body-start (entity)
177 (or (mime-entity-body-start-internal entity)
178 (mime-entity-send entity 'entity-body-start)))
180 (defun mime-entity-body-end (entity)
181 (or (mime-entity-body-end-internal entity)
182 (mime-entity-send entity 'entity-body-end)))
188 (defun mime-fetch-field (field-name &optional entity)
189 (or (symbolp field-name)
190 (setq field-name (intern (capitalize (capitalize field-name)))))
192 (setq entity mime-message-structure))
193 (let* ((header (mime-entity-original-header-internal entity))
194 (field-body (cdr (assq field-name header))))
198 (mime-entity-send entity 'fetch-field
199 (symbol-name field-name)))
200 (mime-entity-set-original-header-internal
201 entity (put-alist field-name field-body header))
205 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
207 (defun mime-entity-content-disposition (entity)
208 (or (mime-entity-content-disposition-internal entity)
209 (let ((ret (mime-fetch-field 'Content-Disposition entity)))
211 (let ((disposition (mime-parse-Content-Disposition ret)))
213 (mime-entity-set-content-disposition-internal
217 (defun mime-entity-encoding (entity &optional default-encoding)
218 (or (mime-entity-encoding-internal entity)
220 (or (let ((ret (mime-fetch-field
221 'Content-Transfer-Encoding entity)))
222 (and ret (mime-parse-Content-Transfer-Encoding ret)))
223 default-encoding "7bit")))
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 (ew-decode-field (symbol-name field-name)
268 (mime-entity-set-parsed-header-internal
269 entity (put-alist field-name field header))
272 (mm-define-generic insert-decoded-header (entity &optional invisible-fields
274 "Insert before point a decoded header of ENTITY.")
277 ;;; @ Entity Attributes
280 (defun mime-entity-uu-filename (entity)
281 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
283 (set-buffer (mime-entity-buffer entity))
284 (goto-char (mime-entity-body-start entity))
285 (if (re-search-forward "^begin [0-9]+ "
286 (mime-entity-body-end entity) t)
287 (if (looking-at ".+$")
288 (buffer-substring (match-beginning 0)(match-end 0))
291 (defun mime-entity-filename (entity)
292 "Return filename of ENTITY."
293 (or (mime-entity-uu-filename entity)
294 (mime-content-disposition-filename
295 (mime-entity-content-disposition entity))
296 (cdr (let ((param (mime-content-type-parameters
297 (mime-entity-content-type entity))))
298 (or (assoc "name" param)
299 (assoc "x-name" param))
303 (defsubst mime-entity-media-type (entity)
304 (mime-content-type-primary-type (mime-entity-content-type entity)))
305 (defsubst mime-entity-media-subtype (entity)
306 (mime-content-type-subtype (mime-entity-content-type entity)))
307 (defsubst mime-entity-parameters (entity)
308 (mime-content-type-parameters (mime-entity-content-type entity)))
309 (defsubst mime-entity-type/subtype (entity-info)
310 (mime-type/subtype-string (mime-entity-media-type entity-info)
311 (mime-entity-media-subtype entity-info)))
317 (mm-define-generic entity-content (entity)
318 "Return content of ENTITY as byte sequence (string).")
320 (mm-define-generic write-entity-content (entity filename)
321 "Write content of ENTITY into FILENAME.")
323 (mm-define-generic write-entity (entity filename)
324 "Write header and body of ENTITY into FILENAME.")
326 (mm-define-generic write-entity-body (entity filename)
327 "Write body of ENTITY into FILENAME.")
335 ;;; mime.el ends here