1 ;;; mime.el --- MIME library module
3 ;; Copyright (C) 1998,1999,2000,2001,2003 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 'mime-parse-Content-Type "mime-parse"
40 "Parse STRING as field-body of Content-Type field.")
41 (autoload 'mime-read-Content-Type "mime-parse"
42 "Read field-body of Content-Type field from current-buffer,
43 and return parsed it.")
45 (autoload 'mime-parse-Content-Disposition "mime-parse"
46 "Parse STRING as field-body of Content-Disposition field.")
47 (autoload 'mime-read-Content-Disposition "mime-parse"
48 "Read field-body of Content-Disposition field from current-buffer,
49 and return parsed it.")
51 (autoload 'mime-parse-Content-Transfer-Encoding "mime-parse"
52 "Parse STRING as field-body of Content-Transfer-Encoding field.")
53 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
54 "Read field-body of Content-Transfer-Encoding field from
55 current-buffer, and return it.")
57 (autoload 'mime-parse-msg-id "mime-parse"
58 "Parse TOKENS as msg-id of Content-Id or Message-Id field.")
60 (autoload 'mime-uri-parse-cid "mime-parse"
61 "Parse STRING as cid URI.")
63 (autoload 'mime-parse-buffer "mime-parse"
64 "Parse BUFFER as a MIME message.")
68 (autoload 'mime-encode-field-body "eword-encode"
69 "Encode FIELD-BODY as FIELD-NAME, and return the result.")
72 ;;; @ Entity Representation and Implementation
75 (defmacro mime-entity-send (entity message &rest args)
76 `(luna-send ,entity ',(intern (format "mime-%s" (eval message))) ,@args))
78 (defun mime-open-entity (type location)
79 "Open an entity and return it.
80 TYPE is representation-type.
81 LOCATION is location of entity. Specification of it is depended on
83 (require (intern (format "mm%s" type)))
84 (luna-make-entity (mm-expand-class-name type) :location location))
86 (luna-define-generic mime-entity-cooked-p (entity)
87 "Return non-nil if contents of ENTITY has been already code-converted.")
90 ;;; @ Entity as node of message
93 (defun mime-entity-children (entity)
94 "Return list of entities included in the ENTITY."
95 (or (mime-entity-children-internal entity)
96 (luna-send entity 'mime-entity-children entity)))
98 (defun mime-entity-node-id (entity)
99 "Return node-id of ENTITY."
100 (mime-entity-node-id-internal entity))
102 (defun mime-entity-number (entity)
103 "Return entity-number of ENTITY."
104 (reverse (mime-entity-node-id-internal entity)))
106 (defun mime-find-entity-from-number (entity-number message)
107 "Return entity from ENTITY-NUMBER in MESSAGE."
108 (let ((sn (car entity-number)))
111 (let ((rc (nth sn (mime-entity-children message))))
113 (mime-find-entity-from-number (cdr entity-number) rc)
117 (defun mime-find-entity-from-node-id (entity-node-id message)
118 "Return entity from ENTITY-NODE-ID in MESSAGE."
119 (mime-find-entity-from-number (reverse entity-node-id) message))
121 (defun mime-find-entity-from-content-id (cid message)
122 "Return entity from CID in MESSAGE."
123 (if (equal cid (mime-entity-read-field message "Content-Id"))
125 (let ((children (mime-entity-children message))
128 (null (setq ret (mime-find-entity-from-content-id
129 cid (car children)))))
130 (setq children (cdr children)))
133 (defun mime-entity-parent (entity &optional message)
134 "Return mother entity of ENTITY.
135 If MESSAGE is specified, it is regarded as root entity."
136 (if (equal entity message)
138 (mime-entity-parent-internal entity)))
140 (defun mime-root-entity-p (entity &optional message)
141 "Return t if ENTITY is root-entity (message).
142 If MESSAGE is specified, it is regarded as root entity."
143 (null (mime-entity-parent entity message)))
145 (defun mime-find-root-entity (entity)
146 "Return root entity of ENTITY."
147 (while (not (mime-root-entity-p entity))
148 (setq entity (mime-entity-parent entity)))
152 ;;; @ Header buffer (obsolete)
155 ;; (luna-define-generic mime-entity-header-buffer (entity))
157 ;; (luna-define-generic mime-goto-header-start-point (entity)
158 ;; "Set buffer and point to header-start-position of ENTITY.")
160 ;; (luna-define-generic mime-entity-header-start-point (entity)
161 ;; "Return header-start-position of ENTITY.")
163 ;; (luna-define-generic mime-entity-header-end-point (entity)
164 ;; "Return header-end-position of ENTITY.")
166 ;; (make-obsolete 'mime-entity-header-buffer "don't use it.")
167 ;; (make-obsolete 'mime-goto-header-start-point "don't use it.")
168 ;; (make-obsolete 'mime-entity-header-start-point "don't use it.")
169 ;; (make-obsolete 'mime-entity-header-end-point "don't use it.")
172 ;;; @ Body buffer (obsolete)
175 ;; (luna-define-generic mime-entity-body-buffer (entity))
177 ;; (luna-define-generic mime-goto-body-start-point (entity)
178 ;; "Set buffer and point to body-start-position of ENTITY.")
180 ;; (luna-define-generic mime-goto-body-end-point (entity)
181 ;; "Set buffer and point to body-end-position of ENTITY.")
183 ;; (luna-define-generic mime-entity-body-start-point (entity)
184 ;; "Return body-start-position of ENTITY.")
186 ;; (luna-define-generic mime-entity-body-end-point (entity)
187 ;; "Return body-end-position of ENTITY.")
189 ;; (defalias 'mime-entity-body-start 'mime-entity-body-start-point)
190 ;; (defalias 'mime-entity-body-end 'mime-entity-body-end-point)
192 ;; (make-obsolete 'mime-entity-body-buffer "don't use it.")
193 ;; (make-obsolete 'mime-goto-body-start-point "don't use it.")
194 ;; (make-obsolete 'mime-goto-body-end-point "don't use it.")
195 ;; (make-obsolete 'mime-entity-body-start-point "don't use it.")
196 ;; (make-obsolete 'mime-entity-body-end-point "don't use it.")
197 ;; (make-obsolete 'mime-entity-body-start "don't use it.")
198 ;; (make-obsolete 'mime-entity-body-end "don't use it.")
201 ;;; @ Entity buffer (obsolete)
204 ;; (luna-define-generic mime-entity-buffer (entity))
205 ;; (make-obsolete 'mime-entity-buffer "don't use it.")
207 ;; (luna-define-generic mime-entity-point-min (entity))
208 ;; (make-obsolete 'mime-entity-point-min "don't use it.")
210 ;; (luna-define-generic mime-entity-point-max (entity))
211 ;; (make-obsolete 'mime-entity-point-max "don't use it.")
217 (luna-define-generic mime-insert-entity (entity)
218 "Insert header and body of ENTITY at point.")
220 (luna-define-generic mime-write-entity (entity filename)
221 "Write header and body of ENTITY into FILENAME.")
227 (luna-define-generic mime-entity-body (entity)
228 "Return network representation of ENTITY body.")
230 (luna-define-generic mime-insert-entity-body (entity)
231 "Insert network representation of ENTITY body at point.")
233 (luna-define-generic mime-write-entity-body (entity filename)
234 "Write body of ENTITY into FILENAME.")
240 (luna-define-generic mime-entity-content (entity)
241 "Return content of ENTITY as byte sequence (string).")
243 (luna-define-generic mime-insert-entity-content (entity)
244 "Insert content of ENTITY at point.")
246 (luna-define-generic mime-write-entity-content (entity filename)
247 "Write content of ENTITY into FILENAME.")
249 (luna-define-generic mime-insert-text-content (entity)
250 "Insert decoded text body of ENTITY.")
256 (luna-define-generic mime-entity-fetch-field (entity field-name)
257 "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
259 ;; (defun mime-fetch-field (field-name &optional entity)
260 ;; "Return the value of the ENTITY's header field whose type is FIELD-NAME."
261 ;; (if (symbolp field-name)
262 ;; (setq field-name (symbol-name field-name))
265 ;; (setq entity mime-message-structure))
266 ;; (mime-entity-fetch-field entity field-name)
268 ;; (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
270 (defun mime-entity-content-type (entity)
271 "Return content-type of ENTITY."
272 (or (mime-entity-content-type-internal entity)
273 (let ((ret (mime-entity-fetch-field entity "Content-Type")))
275 (mime-entity-set-content-type-internal
276 entity (mime-parse-Content-Type ret))
279 (defun mime-entity-content-disposition (entity)
280 "Return content-disposition of ENTITY."
281 (or (mime-entity-content-disposition-internal entity)
282 (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
284 (mime-entity-set-content-disposition-internal
285 entity (mime-parse-Content-Disposition ret))
288 (defun mime-entity-encoding (entity &optional default-encoding)
289 "Return content-transfer-encoding of ENTITY.
290 If the ENTITY does not have Content-Transfer-Encoding field, this
291 function returns DEFAULT-ENCODING. If it is nil, \"7bit\" is used as
293 (or (mime-entity-encoding-internal entity)
294 (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
295 (mime-entity-set-encoding-internal
297 (or (and ret (mime-parse-Content-Transfer-Encoding ret))
298 default-encoding "7bit"))
301 (defvar mime-field-parser-alist
302 '((Return-Path . std11-parse-route-addr)
304 (Reply-To . std11-parse-addresses)
306 (Sender . std11-parse-mailbox)
307 (From . std11-parse-addresses)
309 (Resent-Reply-To . std11-parse-addresses)
311 (Resent-Sender . std11-parse-mailbox)
312 (Resent-From . std11-parse-addresses)
314 (To . std11-parse-addresses)
315 (Resent-To . std11-parse-addresses)
316 (Cc . std11-parse-addresses)
317 (Resent-Cc . std11-parse-addresses)
318 (Bcc . std11-parse-addresses)
319 (Resent-Bcc . std11-parse-addresses)
321 (Message-Id . mime-parse-msg-id)
322 (Recent-Message-Id . mime-parse-msg-id)
324 (In-Reply-To . std11-parse-msg-ids)
325 (References . std11-parse-msg-ids)
327 (Content-Id . mime-parse-msg-id)
330 (defun mime-entity-read-field (entity field-name)
331 (let ((sym (if (symbolp field-name)
334 (setq field-name (symbol-name field-name)))
335 (intern (capitalize field-name)))))
336 (cond ((eq sym 'Content-Type)
337 (mime-entity-content-type entity)
339 ((eq sym 'Content-Disposition)
340 (mime-entity-content-disposition entity)
342 ((eq sym 'Content-Transfer-Encoding)
343 (mime-entity-encoding entity)
346 (let* ((header (mime-entity-parsed-header-internal entity))
347 (field (cdr (assq sym header))))
349 (let ((field-body (mime-entity-fetch-field entity field-name))
353 (cdr (assq sym mime-field-parser-alist)))
357 (eword-lexical-analyze field-body))
358 (mime-decode-field-body field-body sym 'plain)
360 (mime-entity-set-parsed-header-internal
361 entity (put-alist sym field header))
364 ;; (defun mime-read-field (field-name &optional entity)
366 ;; (setq entity mime-message-structure))
367 ;; (mime-entity-read-field entity field-name)
369 ;; (make-obsolete 'mime-read-field 'mime-entity-read-field)
371 (luna-define-generic mime-insert-header (entity &optional invisible-fields
373 "Insert before point a decoded header of ENTITY.")
376 ;;; @ Entity Attributes
379 (luna-define-generic mime-entity-name (entity)
380 "Return name of the ENTITY.")
382 (defun mime-entity-uu-filename (entity)
383 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
385 (mime-insert-entity-body entity)
386 (if (re-search-forward "^begin [0-9]+ " nil t)
387 (if (looking-at ".+$")
388 (buffer-substring (match-beginning 0)(match-end 0))
391 (defun mime-entity-filename (entity)
392 "Return filename of ENTITY."
393 (or (mime-entity-uu-filename entity)
394 (let ((ret (mime-content-disposition-filename
395 (mime-entity-content-disposition entity))))
396 (if (and mime-header-accept-quoted-encoded-words
398 (eword-decode-string ret)
400 (cdr (let ((param (mime-content-type-parameters
401 (mime-entity-content-type entity))))
402 (or (assoc "name" param)
403 (assoc "x-name" param))))))
406 (defsubst mime-entity-media-type (entity)
407 "Return primary media-type of ENTITY."
408 (mime-content-type-primary-type (mime-entity-content-type entity)))
410 (defsubst mime-entity-media-subtype (entity)
411 "Return media-subtype of ENTITY."
412 (mime-content-type-subtype (mime-entity-content-type entity)))
414 (defsubst mime-entity-parameters (entity)
415 "Return parameters of Content-Type of ENTITY."
416 (mime-content-type-parameters (mime-entity-content-type entity)))
418 (defsubst mime-entity-type/subtype (entity-info)
419 "Return type/subtype of Content-Type of ENTITY."
420 (mime-type/subtype-string (mime-entity-media-type entity-info)
421 (mime-entity-media-subtype entity-info)))
423 (defun mime-entity-set-content-type (entity content-type)
424 "Set ENTITY's content-type to CONTENT-TYPE."
425 (mime-entity-set-content-type-internal entity content-type))
427 (defun mime-entity-set-encoding (entity encoding)
428 "Set ENTITY's content-transfer-encoding to ENCODING."
429 (mime-entity-set-encoding-internal entity encoding))
437 ;;; mime.el ends here