1 ;;; mime.el --- MIME library module
3 ;; Copyright (C) 1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 (eval-when-compile (require 'mmgeneric))
36 (autoload 'mime-encode-header-in-buffer "eword-encode"
37 "Encode header fields to network representation, such as MIME encoded-word.")
39 (autoload 'eword-encode-header "eword-encode"
40 "Encode header fields to network representation, such as MIME encoded-word.")
41 (make-obsolete 'eword-encode-header 'mime-encode-header-in-buffer)
43 (autoload 'mime-parse-Content-Type "mime-parse"
44 "Parse STRING as field-body of Content-Type field.")
45 (autoload 'mime-read-Content-Type "mime-parse"
46 "Read field-body of Content-Type field from current-buffer,
47 and return parsed it.")
49 (autoload 'mime-parse-Content-Disposition "mime-parse"
50 "Parse STRING as field-body of Content-Disposition field.")
51 (autoload 'mime-read-Content-Disposition "mime-parse"
52 "Read field-body of Content-Disposition field from current-buffer,
53 and return parsed it.")
55 (autoload 'mime-parse-Content-Transfer-Encoding "mime-parse"
56 "Parse STRING as field-body of Content-Transfer-Encoding field.")
57 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
58 "Read field-body of Content-Transfer-Encoding field from
59 current-buffer, and return it.")
61 (autoload 'mime-parse-msg-id "mime-parse"
62 "Parse TOKENS as msg-id of Content-Id or Message-Id field.")
64 (autoload 'mime-uri-parse-cid "mime-parse"
65 "Parse STRING as cid URI.")
67 (autoload 'mime-parse-buffer "mime-parse"
68 "Parse BUFFER as a MIME message.")
72 (autoload 'mime-encode-field-body "eword-encode"
73 "Encode FIELD-BODY as FIELD-NAME, and return the result.")
76 ;;; @ Entity Representation and Implementation
79 (defmacro mime-entity-send (entity message &rest args)
80 (` (luna-send (, entity)
81 '(, (intern (format "mime-%s" (eval message)))) (,@ args))))
83 (defun mime-open-entity (type location)
84 "Open an entity and return it.
85 TYPE is representation-type.
86 LOCATION is location of entity. Specification of it is depended on
88 (require (intern (format "mm%s" type)))
89 (luna-make-entity (mm-expand-class-name type) :location location))
91 (luna-define-generic mime-entity-cooked-p (entity)
92 "Return non-nil if contents of ENTITY has been already code-converted.")
95 ;;; @ Entity as node of message
98 (defun mime-entity-children (entity)
99 "Return list of entities included in the ENTITY."
100 (or (mime-entity-children-internal entity)
101 (luna-send entity 'mime-entity-children entity)))
103 (defun mime-entity-node-id (entity)
104 "Return node-id of ENTITY."
105 (mime-entity-node-id-internal entity))
107 (defun mime-entity-number (entity)
108 "Return entity-number of ENTITY."
109 (reverse (mime-entity-node-id-internal entity)))
111 (defun mime-find-entity-from-number (entity-number message)
112 "Return entity from ENTITY-NUMBER in MESSAGE."
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 message)
123 "Return entity from ENTITY-NODE-ID in MESSAGE."
124 (mime-find-entity-from-number (reverse entity-node-id) message))
126 (defun mime-find-entity-from-content-id (cid message)
127 "Return entity from CID in MESSAGE."
128 (if (equal cid (mime-entity-read-field message "Content-Id"))
130 (let ((children (mime-entity-children message))
133 (null (setq ret (mime-find-entity-from-content-id
134 cid (car children)))))
135 (setq children (cdr children)))
138 (defun mime-entity-parent (entity &optional message)
139 "Return mother entity of ENTITY.
140 If MESSAGE is specified, it is regarded as root entity."
141 (if (equal entity message)
143 (mime-entity-parent-internal entity)))
145 (defun mime-root-entity-p (entity &optional message)
146 "Return t if ENTITY is root-entity (message).
147 If MESSAGE is specified, it is regarded as root entity."
148 (null (mime-entity-parent entity message)))
150 (defun mime-find-root-entity (entity)
151 "Return root entity of ENTITY."
152 (let ((p (mime-entity-parent entity)))
155 (mime-entity-parent p))))
158 ;;; @ Header buffer (obsolete)
161 ;; (luna-define-generic mime-entity-header-buffer (entity))
163 ;; (luna-define-generic mime-goto-header-start-point (entity)
164 ;; "Set buffer and point to header-start-position of ENTITY.")
166 ;; (luna-define-generic mime-entity-header-start-point (entity)
167 ;; "Return header-start-position of ENTITY.")
169 ;; (luna-define-generic mime-entity-header-end-point (entity)
170 ;; "Return header-end-position of ENTITY.")
172 ;; (make-obsolete 'mime-entity-header-buffer "don't use it.")
173 ;; (make-obsolete 'mime-goto-header-start-point "don't use it.")
174 ;; (make-obsolete 'mime-entity-header-start-point "don't use it.")
175 ;; (make-obsolete 'mime-entity-header-end-point "don't use it.")
178 ;;; @ Body buffer (obsolete)
181 ;; (luna-define-generic mime-entity-body-buffer (entity))
183 ;; (luna-define-generic mime-goto-body-start-point (entity)
184 ;; "Set buffer and point to body-start-position of ENTITY.")
186 ;; (luna-define-generic mime-goto-body-end-point (entity)
187 ;; "Set buffer and point to body-end-position of ENTITY.")
189 ;; (luna-define-generic mime-entity-body-start-point (entity)
190 ;; "Return body-start-position of ENTITY.")
192 ;; (luna-define-generic mime-entity-body-end-point (entity)
193 ;; "Return body-end-position of ENTITY.")
195 ;; (defalias 'mime-entity-body-start 'mime-entity-body-start-point)
196 ;; (defalias 'mime-entity-body-end 'mime-entity-body-end-point)
198 ;; (make-obsolete 'mime-entity-body-buffer "don't use it.")
199 ;; (make-obsolete 'mime-goto-body-start-point "don't use it.")
200 ;; (make-obsolete 'mime-goto-body-end-point "don't use it.")
201 ;; (make-obsolete 'mime-entity-body-start-point "don't use it.")
202 ;; (make-obsolete 'mime-entity-body-end-point "don't use it.")
203 ;; (make-obsolete 'mime-entity-body-start "don't use it.")
204 ;; (make-obsolete 'mime-entity-body-end "don't use it.")
207 ;;; @ Entity buffer (obsolete)
210 ;; (luna-define-generic mime-entity-buffer (entity))
211 ;; (make-obsolete 'mime-entity-buffer "don't use it.")
213 ;; (luna-define-generic mime-entity-point-min (entity))
214 ;; (make-obsolete 'mime-entity-point-min "don't use it.")
216 ;; (luna-define-generic mime-entity-point-max (entity))
217 ;; (make-obsolete 'mime-entity-point-max "don't use it.")
223 (luna-define-generic mime-insert-entity (entity)
224 "Insert header and body of ENTITY at point.")
226 (luna-define-generic mime-write-entity (entity filename)
227 "Write header and body of ENTITY into FILENAME.")
233 (luna-define-generic mime-entity-body (entity)
234 "Return network representation of ENTITY body.")
236 (luna-define-generic mime-insert-entity-body (entity)
237 "Insert network representation of ENTITY body at point.")
239 (luna-define-generic mime-write-entity-body (entity filename)
240 "Write body of ENTITY into FILENAME.")
246 (luna-define-generic mime-entity-content (entity)
247 "Return content of ENTITY as byte sequence (string).")
249 (luna-define-generic mime-insert-entity-content (entity)
250 "Insert content of ENTITY at point.")
252 (luna-define-generic mime-write-entity-content (entity filename)
253 "Write content of ENTITY into FILENAME.")
255 (luna-define-generic mime-insert-text-content (entity)
256 "Insert decoded text body of ENTITY.")
262 (luna-define-generic mime-entity-fetch-field (entity field-name)
263 "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
265 ;; (defun mime-fetch-field (field-name &optional entity)
266 ;; "Return the value of the ENTITY's header field whose type is FIELD-NAME."
267 ;; (if (symbolp field-name)
268 ;; (setq field-name (symbol-name field-name))
271 ;; (setq entity mime-message-structure))
272 ;; (mime-entity-fetch-field entity field-name)
274 ;; (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
276 (defun mime-entity-content-type (entity)
277 "Return content-type of ENTITY."
278 (or (mime-entity-content-type-internal entity)
279 (let ((ret (mime-entity-fetch-field entity "Content-Type")))
281 (mime-entity-set-content-type-internal
282 entity (mime-parse-Content-Type ret))
285 (defun mime-entity-content-disposition (entity)
286 "Return content-disposition of ENTITY."
287 (or (mime-entity-content-disposition-internal entity)
288 (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
290 (mime-entity-set-content-disposition-internal
291 entity (mime-parse-Content-Disposition ret))
294 (defun mime-entity-encoding (entity &optional default-encoding)
295 "Return content-transfer-encoding of ENTITY.
296 If the ENTITY does not have Content-Transfer-Encoding field, this
297 function returns DEFAULT-ENCODING. If it is nil, \"7bit\" is used as
299 (or (mime-entity-encoding-internal entity)
300 (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
301 (mime-entity-set-encoding-internal
303 (or (and ret (mime-parse-Content-Transfer-Encoding ret))
304 default-encoding "7bit"))
307 (defvar mime-field-parser-alist
308 '((Return-Path . std11-parse-route-addr)
310 (Reply-To . std11-parse-addresses)
312 (Sender . std11-parse-mailbox)
313 (From . std11-parse-addresses)
315 (Resent-Reply-To . std11-parse-addresses)
317 (Resent-Sender . std11-parse-mailbox)
318 (Resent-From . std11-parse-addresses)
320 (To . std11-parse-addresses)
321 (Resent-To . std11-parse-addresses)
322 (Cc . std11-parse-addresses)
323 (Resent-Cc . std11-parse-addresses)
324 (Bcc . std11-parse-addresses)
325 (Resent-Bcc . std11-parse-addresses)
327 (Message-Id . mime-parse-msg-id)
328 (Recent-Message-Id . mime-parse-msg-id)
330 (In-Reply-To . std11-parse-msg-ids)
331 (References . std11-parse-msg-ids)
333 (Content-Id . mime-parse-msg-id)
336 (defun mime-entity-read-field (entity field-name)
337 (let ((sym (if (symbolp field-name)
340 (setq field-name (symbol-name field-name)))
341 (intern (capitalize (capitalize field-name))))))
342 (cond ((eq sym 'Content-Type)
343 (mime-entity-content-type entity)
345 ((eq sym 'Content-Disposition)
346 (mime-entity-content-disposition entity)
348 ((eq sym 'Content-Transfer-Encoding)
349 (mime-entity-encoding entity)
352 (let* ((header (mime-entity-parsed-header-internal entity))
353 (field (cdr (assq sym header))))
355 (let ((field-body (mime-entity-fetch-field entity field-name))
359 (cdr (assq sym mime-field-parser-alist)))
363 (eword-lexical-analyze field-body))
364 (mime-decode-field-body field-body sym 'plain)
366 (mime-entity-set-parsed-header-internal
367 entity (put-alist sym field header))
370 ;; (defun mime-read-field (field-name &optional entity)
372 ;; (setq entity mime-message-structure))
373 ;; (mime-entity-read-field entity field-name)
375 ;; (make-obsolete 'mime-read-field 'mime-entity-read-field)
377 (luna-define-generic mime-insert-header (entity &optional invisible-fields
379 "Insert before point a decoded header of ENTITY.")
382 ;;; @ Entity Attributes
385 (luna-define-generic mime-entity-name (entity)
386 "Return name of the ENTITY.")
388 (defun mime-entity-uu-filename (entity)
389 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
391 (mime-insert-entity-body entity)
392 (if (re-search-forward "^begin [0-9]+ " nil t)
393 (if (looking-at ".+$")
394 (buffer-substring (match-beginning 0)(match-end 0))
397 (defun mime-entity-filename (entity)
398 "Return filename of ENTITY."
399 (or (mime-entity-uu-filename entity)
400 (mime-content-disposition-filename
401 (mime-entity-content-disposition entity))
402 (cdr (let ((param (mime-content-type-parameters
403 (mime-entity-content-type entity))))
404 (or (assoc "name" param)
405 (assoc "x-name" param))
409 (defsubst mime-entity-media-type (entity)
410 "Return primary media-type of ENTITY."
411 (mime-content-type-primary-type (mime-entity-content-type entity)))
413 (defsubst mime-entity-media-subtype (entity)
414 "Return media-subtype of ENTITY."
415 (mime-content-type-subtype (mime-entity-content-type entity)))
417 (defsubst mime-entity-parameters (entity)
418 "Return parameters of Content-Type of ENTITY."
419 (mime-content-type-parameters (mime-entity-content-type entity)))
421 (defsubst mime-entity-type/subtype (entity-info)
422 "Return type/subtype of Content-Type of ENTITY."
423 (mime-type/subtype-string (mime-entity-media-type entity-info)
424 (mime-entity-media-subtype entity-info)))
426 (defun mime-entity-set-content-type (entity content-type)
427 "Set ENTITY's content-type to CONTENT-TYPE."
428 (mime-entity-set-content-type-internal entity content-type))
430 (defun mime-entity-set-encoding (entity encoding)
431 "Set ENTITY's content-transfer-encoding to ENCODING."
432 (mime-entity-set-encoding-internal entity encoding))
440 ;;; mime.el ends here