1 ;;; mime.el --- MIME library module
3 ;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
4 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
5 ;; Licensed to the Free Software Foundation.
7 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
8 ;; Keywords: MIME, multimedia, mail, news
10 ;; This file is part of FLIM (Faithful Library about Internet Message).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
32 (require 'eword-decode)
36 (autoload 'eword-encode-header "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 "mmbuffer"
64 "Parse BUFFER as a MIME message.")
68 ;;; @ Entity Representation and Implementation
71 (defmacro mime-entity-send (entity message &rest args)
72 `(luna-send ,entity ',(intern (format "mime-%s" (eval message))) ,@args))
74 (defun mime-open-entity (type location)
75 "Open an entity and return it.
76 TYPE is representation-type.
77 LOCATION is location of entity. Specification of it is depended on
79 (require (intern (format "mm%s" type)))
80 (luna-make-entity (mm-expand-class-name type) :location location))
82 (luna-define-generic mime-entity-cooked-p (entity)
83 "Return non-nil if contents of ENTITY has been already code-converted.")
86 ;;; @ Entity as node of message
89 (defun mime-entity-children (entity)
90 (or (mime-entity-children-internal entity)
91 (luna-send entity 'mime-entity-children entity)))
93 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
95 (defun mime-entity-number (entity)
96 "Return entity-number of ENTITY."
97 (reverse (mime-entity-node-id-internal entity)))
99 (defun mime-find-entity-from-number (entity-number &optional message)
100 "Return entity from ENTITY-NUMBER in MESSAGE.
101 If MESSAGE is not specified, `mime-message-structure' is used."
103 (setq message mime-message-structure))
104 (let ((sn (car entity-number)))
107 (let ((rc (nth sn (mime-entity-children message))))
109 (mime-find-entity-from-number (cdr entity-number) rc)
113 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
114 "Return entity from ENTITY-NODE-ID in MESSAGE.
115 If MESSAGE is not specified, `mime-message-structure' is used."
116 (mime-find-entity-from-number (reverse entity-node-id) message))
118 (defun mime-find-entity-from-content-id (cid &optional message)
119 "Return entity from CID in MESSAGE.
120 If MESSAGE is not specified, `mime-message-structure' is used."
122 (setq message mime-message-structure))
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)))
149 (luna-define-generic mime-entity-buffer (entity))
153 "use mime-entity-header-buffer or mime-entity-body-buffer instead.")
155 (luna-define-generic mime-entity-header-buffer (entity))
157 (luna-define-generic mime-entity-body-buffer (entity))
159 (luna-define-generic mime-entity-point-min (entity))
161 (luna-define-generic mime-entity-point-max (entity))
163 (luna-define-generic mime-entity-body-end-point (entity)
164 "Set buffer and point to body-start-position of ENTITY.")
166 (define-obsolete-function-alias
167 'mime-entity-body-end 'mime-entity-body-end-point)
169 (luna-define-generic mime-goto-header-start-point (entity)
170 "Set buffer and point to header-start-position of ENTITY.")
172 (luna-define-generic mime-goto-body-start-point (entity)
173 "Set buffer and point to body-start-position of ENTITY.")
179 (luna-define-generic mime-entity-fetch-field (entity field-name)
180 "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
182 (defun mime-fetch-field (field-name &optional entity)
183 "Return the value of the ENTITY's header field whose type is FIELD-NAME."
184 (if (symbolp field-name)
185 (setq field-name (symbol-name field-name))
188 (setq entity mime-message-structure))
189 (mime-entity-fetch-field entity field-name)
191 (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
193 (defun mime-entity-content-type (entity)
194 (or (mime-entity-content-type-internal entity)
195 (let ((ret (mime-entity-fetch-field entity "Content-Type")))
197 (mime-entity-set-content-type-internal
198 entity (mime-parse-Content-Type ret))
201 (defun mime-entity-content-disposition (entity)
202 (or (mime-entity-content-disposition-internal entity)
203 (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
205 (mime-entity-set-content-disposition-internal
206 entity (mime-parse-Content-Disposition ret))
209 (defun mime-entity-encoding (entity &optional default-encoding)
210 (or (mime-entity-encoding-internal entity)
211 (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
212 (mime-entity-set-encoding-internal
214 (or (and ret (mime-parse-Content-Transfer-Encoding ret))
215 default-encoding "7bit"))
218 (defvar mime-field-parser-alist
219 '((Return-Path . std11-parse-route-addr)
221 (Reply-To . std11-parse-addresses)
223 (Sender . std11-parse-mailbox)
224 (From . std11-parse-addresses)
226 (Resent-Reply-To . std11-parse-addresses)
228 (Resent-Sender . std11-parse-mailbox)
229 (Resent-From . std11-parse-addresses)
231 (To . std11-parse-addresses)
232 (Resent-To . std11-parse-addresses)
233 (Cc . std11-parse-addresses)
234 (Resent-Cc . std11-parse-addresses)
235 (Bcc . std11-parse-addresses)
236 (Resent-Bcc . std11-parse-addresses)
238 (Message-Id . mime-parse-msg-id)
239 (Recent-Message-Id . mime-parse-msg-id)
241 (In-Reply-To . std11-parse-msg-ids)
242 (References . std11-parse-msg-ids)
244 (Content-Id . mime-parse-msg-id)
247 (defun mime-entity-read-field (entity field-name)
248 (let ((sym (if (symbolp field-name)
251 (setq field-name (symbol-name field-name)))
252 (capitalize (capitalize field-name)))))
253 (cond ((eq sym 'Content-Type)
254 (mime-entity-content-type entity)
256 ((eq sym 'Content-Disposition)
257 (mime-entity-content-disposition entity)
259 ((eq sym 'Content-Transfer-Encoding)
260 (mime-entity-encoding entity)
263 (let* ((header (mime-entity-parsed-header-internal entity))
264 (field (cdr (assq sym header))))
266 (let ((field-body (mime-entity-fetch-field entity field-name))
270 (cdr (assq sym mime-field-parser-alist)))
274 (eword-lexical-analyze field-body))
275 (mime-decode-field-body field-body sym 'plain)
277 (mime-entity-set-parsed-header-internal
278 entity (put-alist sym field header))
281 (defun mime-read-field (field-name &optional entity)
283 (setq entity mime-message-structure))
284 (mime-entity-read-field entity field-name)
286 (make-obsolete 'mime-read-field 'mime-entity-read-field)
288 (luna-define-generic mime-insert-header (entity &optional invisible-fields
290 "Insert before point a decoded header of ENTITY.")
293 ;;; @ Entity Attributes
296 (luna-define-generic mime-entity-name (entity)
297 "Return name of the ENTITY.")
299 (defun mime-entity-uu-filename (entity)
300 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
302 (mime-goto-body-start-point entity)
303 (if (re-search-forward "^begin [0-9]+ "
304 (mime-entity-body-end-point 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 (luna-define-generic mime-entity-content (entity)
336 "Return content of ENTITY as byte sequence (string).")
338 (luna-define-generic mime-insert-entity-content (entity)
339 "Insert content of ENTITY at point.")
341 (luna-define-generic mime-write-entity-content (entity filename)
342 "Write content of ENTITY into FILENAME.")
344 (luna-define-generic mime-insert-text-content (entity)
345 "Insert decoded text body of ENTITY.")
347 (luna-define-generic mime-insert-entity (entity)
348 "Insert header and body of ENTITY at point.")
350 (luna-define-generic mime-write-entity (entity filename)
351 "Write header and body of ENTITY into FILENAME.")
353 (luna-define-generic mime-write-entity-body (entity filename)
354 "Write body of ENTITY into FILENAME.")
362 ;;; mime.el ends here