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 '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 (let ((p (mime-entity-parent entity)))
150 (mime-entity-parent p))))
153 ;;; @ Header buffer (obsolete)
156 ;; (luna-define-generic mime-entity-header-buffer (entity))
158 ;; (luna-define-generic mime-goto-header-start-point (entity)
159 ;; "Set buffer and point to header-start-position of ENTITY.")
161 ;; (luna-define-generic mime-entity-header-start-point (entity)
162 ;; "Return header-start-position of ENTITY.")
164 ;; (luna-define-generic mime-entity-header-end-point (entity)
165 ;; "Return header-end-position of ENTITY.")
167 ;; (make-obsolete 'mime-entity-header-buffer "don't use it.")
168 ;; (make-obsolete 'mime-goto-header-start-point "don't use it.")
169 ;; (make-obsolete 'mime-entity-header-start-point "don't use it.")
170 ;; (make-obsolete 'mime-entity-header-end-point "don't use it.")
173 ;;; @ Body buffer (obsolete)
176 ;; (luna-define-generic mime-entity-body-buffer (entity))
178 ;; (luna-define-generic mime-goto-body-start-point (entity)
179 ;; "Set buffer and point to body-start-position of ENTITY.")
181 ;; (luna-define-generic mime-goto-body-end-point (entity)
182 ;; "Set buffer and point to body-end-position of ENTITY.")
184 ;; (luna-define-generic mime-entity-body-start-point (entity)
185 ;; "Return body-start-position of ENTITY.")
187 ;; (luna-define-generic mime-entity-body-end-point (entity)
188 ;; "Return body-end-position of ENTITY.")
190 ;; (defalias 'mime-entity-body-start 'mime-entity-body-start-point)
191 ;; (defalias 'mime-entity-body-end 'mime-entity-body-end-point)
193 ;; (make-obsolete 'mime-entity-body-buffer "don't use it.")
194 ;; (make-obsolete 'mime-goto-body-start-point "don't use it.")
195 ;; (make-obsolete 'mime-goto-body-end-point "don't use it.")
196 ;; (make-obsolete 'mime-entity-body-start-point "don't use it.")
197 ;; (make-obsolete 'mime-entity-body-end-point "don't use it.")
198 ;; (make-obsolete 'mime-entity-body-start "don't use it.")
199 ;; (make-obsolete 'mime-entity-body-end "don't use it.")
202 ;;; @ Entity buffer (obsolete)
205 ;; (luna-define-generic mime-entity-buffer (entity))
206 ;; (make-obsolete 'mime-entity-buffer "don't use it.")
208 ;; (luna-define-generic mime-entity-point-min (entity))
209 ;; (make-obsolete 'mime-entity-point-min "don't use it.")
211 ;; (luna-define-generic mime-entity-point-max (entity))
212 ;; (make-obsolete 'mime-entity-point-max "don't use it.")
218 (luna-define-generic mime-insert-entity (entity)
219 "Insert header and body of ENTITY at point.")
221 (luna-define-generic mime-write-entity (entity filename)
222 "Write header and body of ENTITY into FILENAME.")
228 (luna-define-generic mime-entity-body (entity)
229 "Return network representation of ENTITY body.")
231 (luna-define-generic mime-insert-entity-body (entity)
232 "Insert network representation of ENTITY body at point.")
234 (luna-define-generic mime-write-entity-body (entity filename)
235 "Write body of ENTITY into FILENAME.")
241 (luna-define-generic mime-entity-content (entity)
242 "Return content of ENTITY as byte sequence (string).")
244 (luna-define-generic mime-insert-entity-content (entity)
245 "Insert content of ENTITY at point.")
247 (luna-define-generic mime-write-entity-content (entity filename)
248 "Write content of ENTITY into FILENAME.")
250 (luna-define-generic mime-insert-text-content (entity)
251 "Insert decoded text body of ENTITY.")
257 (luna-define-generic mime-entity-fetch-field (entity field-name)
258 "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
260 ;; (defun mime-fetch-field (field-name &optional entity)
261 ;; "Return the value of the ENTITY's header field whose type is FIELD-NAME."
262 ;; (if (symbolp field-name)
263 ;; (setq field-name (symbol-name field-name))
266 ;; (setq entity mime-message-structure))
267 ;; (mime-entity-fetch-field entity field-name)
269 ;; (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
271 (defun mime-entity-content-type (entity)
272 "Return content-type of ENTITY."
273 (or (mime-entity-content-type-internal entity)
274 (let ((ret (mime-entity-fetch-field entity "Content-Type")))
276 (mime-entity-set-content-type-internal
277 entity (mime-parse-Content-Type ret))
280 (defun mime-entity-content-disposition (entity)
281 "Return content-disposition of ENTITY."
282 (or (mime-entity-content-disposition-internal entity)
283 (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
285 (mime-entity-set-content-disposition-internal
286 entity (mime-parse-Content-Disposition ret))
289 (defun mime-entity-encoding (entity &optional default-encoding)
290 "Return content-transfer-encoding of ENTITY.
291 If the ENTITY does not have Content-Transfer-Encoding field, this
292 function returns DEFAULT-ENCODING. If it is nil, \"7bit\" is used as
294 (or (mime-entity-encoding-internal entity)
295 (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
296 (mime-entity-set-encoding-internal
298 (or (and ret (mime-parse-Content-Transfer-Encoding ret))
299 default-encoding "7bit"))
302 (defvar mime-field-parser-alist
303 '((Return-Path . std11-parse-route-addr)
305 (Reply-To . std11-parse-addresses)
307 (Sender . std11-parse-mailbox)
308 (From . std11-parse-addresses)
310 (Resent-Reply-To . std11-parse-addresses)
312 (Resent-Sender . std11-parse-mailbox)
313 (Resent-From . std11-parse-addresses)
315 (To . std11-parse-addresses)
316 (Resent-To . std11-parse-addresses)
317 (Cc . std11-parse-addresses)
318 (Resent-Cc . std11-parse-addresses)
319 (Bcc . std11-parse-addresses)
320 (Resent-Bcc . std11-parse-addresses)
322 (Message-Id . mime-parse-msg-id)
323 (Recent-Message-Id . mime-parse-msg-id)
325 (In-Reply-To . std11-parse-msg-ids)
326 (References . std11-parse-msg-ids)
328 (Content-Id . mime-parse-msg-id)
331 (defun mime-entity-read-field (entity field-name)
332 (let ((sym (if (symbolp field-name)
335 (setq field-name (symbol-name field-name)))
336 (intern (capitalize (capitalize field-name))))))
337 (cond ((eq sym 'Content-Type)
338 (mime-entity-content-type entity)
340 ((eq sym 'Content-Disposition)
341 (mime-entity-content-disposition entity)
343 ((eq sym 'Content-Transfer-Encoding)
344 (mime-entity-encoding entity)
347 (let* ((header (mime-entity-parsed-header-internal entity))
348 (field (cdr (assq sym header))))
350 (let ((field-body (mime-entity-fetch-field entity field-name))
354 (cdr (assq sym mime-field-parser-alist)))
358 (eword-lexical-analyze field-body))
359 (mime-decode-field-body field-body sym 'plain)
361 (mime-entity-set-parsed-header-internal
362 entity (put-alist sym field header))
365 ;; (defun mime-read-field (field-name &optional entity)
367 ;; (setq entity mime-message-structure))
368 ;; (mime-entity-read-field entity field-name)
370 ;; (make-obsolete 'mime-read-field 'mime-entity-read-field)
372 (luna-define-generic mime-insert-header (entity &optional invisible-fields
374 "Insert before point a decoded header of ENTITY.")
377 ;;; @ Entity Attributes
380 (luna-define-generic mime-entity-name (entity)
381 "Return name of the ENTITY.")
383 (defun mime-entity-uu-filename (entity)
384 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
386 (mime-insert-entity-body entity)
387 (if (re-search-forward "^begin [0-9]+ " nil t)
388 (if (looking-at ".+$")
389 (buffer-substring (match-beginning 0)(match-end 0))
392 (defun mime-entity-filename (entity)
393 "Return filename of ENTITY."
394 (or (mime-entity-uu-filename entity)
395 (mime-content-disposition-filename
396 (mime-entity-content-disposition entity))
397 (cdr (let ((param (mime-content-type-parameters
398 (mime-entity-content-type entity))))
399 (or (assoc "name" param)
400 (assoc "x-name" param))
404 (defsubst mime-entity-media-type (entity)
405 (mime-content-type-primary-type (mime-entity-content-type entity)))
406 (defsubst mime-entity-media-subtype (entity)
407 (mime-content-type-subtype (mime-entity-content-type entity)))
408 (defsubst mime-entity-parameters (entity)
409 (mime-content-type-parameters (mime-entity-content-type entity)))
410 (defsubst mime-entity-type/subtype (entity-info)
411 (mime-type/subtype-string (mime-entity-media-type entity-info)
412 (mime-entity-media-subtype entity-info)))
414 (defun mime-entity-set-content-type (entity content-type)
415 "Set ENTITY's content-type to CONTENT-TYPE."
416 (mime-entity-set-content-type-internal entity content-type))
418 (defun mime-entity-set-encoding (entity encoding)
419 "Set ENTITY's content-transfer-encoding to ENCODING."
420 (mime-entity-set-encoding-internal entity encoding))
428 ;;; mime.el ends here