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 (cond ((eq field-name 'Date)
194 (or (mime-entity-date-internal entity)
195 (mime-entity-set-date-internal
196 entity (mime-entity-send entity 'fetch-field "Date"))
198 ((eq field-name 'Message-Id)
199 (or (mime-entity-message-id-internal entity)
200 (mime-entity-set-message-id-internal
201 entity (mime-entity-send entity 'fetch-field "Message-Id"))
203 ((eq field-name 'References)
204 (or (mime-entity-references-internal entity)
205 (mime-entity-set-references-internal
206 entity (mime-entity-send entity 'fetch-field "References"))
209 (let* ((header (mime-entity-original-header-internal entity))
210 (field-body (cdr (assq field-name header))))
214 (mime-entity-send entity 'fetch-field
215 (symbol-name field-name)))
216 (mime-entity-set-original-header-internal
217 entity (put-alist field-name field-body header))
222 (defun mime-entity-content-type (entity)
223 (or (mime-entity-content-type-internal entity)
224 (let ((ret (mime-fetch-field 'Content-Type entity)))
226 (mime-entity-set-content-type-internal
227 entity (mime-parse-Content-Type ret))
230 (defun mime-entity-content-disposition (entity)
231 (or (mime-entity-content-disposition-internal entity)
232 (let ((ret (mime-fetch-field 'Content-Disposition entity)))
234 (mime-entity-set-content-disposition-internal
235 entity (mime-parse-Content-Disposition ret))
238 (defun mime-entity-encoding (entity &optional default-encoding)
239 (or (mime-entity-encoding-internal entity)
240 (let ((ret (mime-fetch-field 'Content-Transfer-Encoding entity)))
241 (mime-entity-set-encoding-internal
243 (or (and ret (mime-parse-Content-Transfer-Encoding ret))
244 default-encoding "7bit"))
247 (defun mime-read-field (field-name &optional entity)
248 (or (symbolp field-name)
249 (setq field-name (capitalize (capitalize field-name))))
251 (setq entity mime-message-structure))
252 (cond ((eq field-name 'Content-Type)
253 (mime-entity-content-type entity)
255 ((eq field-name 'Content-Disposition)
256 (mime-entity-content-disposition entity)
258 ((eq field-name 'Content-Transfer-Encoding)
259 (mime-entity-encoding entity)
262 (let* ((header (mime-entity-parsed-header-internal entity))
263 (field (cdr (assq field-name header))))
265 (let ((field-body (mime-fetch-field field-name entity)))
267 (cond ((memq field-name '(From Resent-From
271 Reply-To Resent-Reply-To))
272 (setq field (std11-parse-addresses
273 (eword-lexical-analyze field-body)))
275 ((memq field-name '(Sender Resent-Sender))
276 (setq field (std11-parse-address
277 (eword-lexical-analyze field-body)))
280 (setq field (mime-decode-field-body
281 field-body field-name 'plain))
283 (mime-entity-set-parsed-header-internal
284 entity (put-alist field-name field header))
287 (mm-define-generic insert-header (entity &optional invisible-fields
289 "Insert before point a decoded header of ENTITY.")
291 (define-obsolete-function-alias
292 'mime-insert-decoded-header 'mime-insert-header)
295 ;;; @ Entity Attributes
298 (defun mime-entity-uu-filename (entity)
299 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
301 (set-buffer (mime-entity-buffer entity))
302 (goto-char (mime-entity-body-start entity))
303 (if (re-search-forward "^begin [0-9]+ "
304 (mime-entity-body-end entity) t)
305 (if (looking-at ".+$")
306 (buffer-substring (match-beginning 0)(match-end 0))
309 (defun mime-entity-filename (entity)
310 "Return filename of ENTITY."
311 (or (mime-entity-uu-filename entity)
312 (mime-content-disposition-filename
313 (mime-entity-content-disposition entity))
314 (cdr (let ((param (mime-content-type-parameters
315 (mime-entity-content-type entity))))
316 (or (assoc "name" param)
317 (assoc "x-name" param))
321 (defsubst mime-entity-media-type (entity)
322 (mime-content-type-primary-type (mime-entity-content-type entity)))
323 (defsubst mime-entity-media-subtype (entity)
324 (mime-content-type-subtype (mime-entity-content-type entity)))
325 (defsubst mime-entity-parameters (entity)
326 (mime-content-type-parameters (mime-entity-content-type entity)))
327 (defsubst mime-entity-type/subtype (entity-info)
328 (mime-type/subtype-string (mime-entity-media-type entity-info)
329 (mime-entity-media-subtype entity-info)))
335 (mm-define-generic entity-content (entity)
336 "Return content of ENTITY as byte sequence (string).")
338 (mm-define-generic insert-text-content (entity)
339 "Insert decoded text body of ENTITY.")
341 (mm-define-generic write-entity-content (entity filename)
342 "Write content of ENTITY into FILENAME.")
344 (mm-define-generic write-entity (entity filename)
345 "Write header and body of ENTITY into FILENAME.")
347 (mm-define-generic write-entity-body (entity filename)
348 "Write body of ENTITY into FILENAME.")
356 ;;; mime.el ends here