1 ;;; mime.el --- MIME library module
3 ;; Copyright (C) 1998,1999 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)
34 (autoload 'eword-encode-header "eword-encode"
35 "Encode header fields to network representation, such as MIME encoded-word.")
37 (autoload 'mime-parse-Content-Type "mime-parse"
38 "Parse STRING as field-body of Content-Type field.")
39 (autoload 'mime-read-Content-Type "mime-parse"
40 "Read field-body of Content-Type field from current-buffer,
41 and return parsed it.")
43 (autoload 'mime-parse-Content-Disposition "mime-parse"
44 "Parse STRING as field-body of Content-Disposition field.")
45 (autoload 'mime-read-Content-Disposition "mime-parse"
46 "Read field-body of Content-Disposition field from current-buffer,
47 and return parsed it.")
49 (autoload 'mime-parse-Content-Transfer-Encoding "mime-parse"
50 "Parse STRING as field-body of Content-Transfer-Encoding field.")
51 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
52 "Read field-body of Content-Transfer-Encoding field from
53 current-buffer, and return it.")
55 (autoload 'mime-parse-msg-id "mime-parse"
56 "Parse TOKENS as msg-id of Content-Id or Message-Id field.")
58 (autoload 'mime-uri-parse-cid "mime-parse"
59 "Parse STRING as cid URI.")
61 (autoload 'mime-parse-buffer "mmbuffer"
62 "Parse BUFFER as a MIME message.")
66 ;;; @ Entity Representation and Implementation
69 (defmacro mime-entity-send (entity message &rest args)
70 `(luna-send ,entity ',(intern (format "mime-%s" (eval message))) ,@args))
72 (defun mime-open-entity (type location)
73 "Open an entity and return it.
74 TYPE is representation-type.
75 LOCATION is location of entity. Specification of it is depended on
77 (require (intern (format "mm%s" type)))
78 (luna-make-entity (mm-expand-class-name type) :location location))
80 (luna-define-generic mime-entity-cooked-p (entity)
81 "Return non-nil if contents of ENTITY has been already code-converted.")
84 ;;; @ Entity as node of message
87 (defun mime-entity-children (entity)
88 (or (mime-entity-children-internal entity)
89 (luna-send entity 'mime-entity-children entity)))
91 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
93 (defun mime-entity-number (entity)
94 "Return entity-number of ENTITY."
95 (reverse (mime-entity-node-id-internal entity)))
97 (defun mime-find-entity-from-number (entity-number &optional message)
98 "Return entity from ENTITY-NUMBER in MESSAGE.
99 If MESSAGE is not specified, `mime-message-structure' is used."
101 (setq message mime-message-structure))
102 (let ((sn (car entity-number)))
105 (let ((rc (nth sn (mime-entity-children message))))
107 (mime-find-entity-from-number (cdr entity-number) rc)
111 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
112 "Return entity from ENTITY-NODE-ID in MESSAGE.
113 If MESSAGE is not specified, `mime-message-structure' is used."
114 (mime-find-entity-from-number (reverse entity-node-id) message))
116 (defun mime-find-entity-from-content-id (cid &optional message)
117 "Return entity from CID in MESSAGE.
118 If MESSAGE is not specified, `mime-message-structure' is used."
120 (setq message mime-message-structure))
121 (if (equal cid (mime-entity-read-field message "Content-Id"))
123 (let ((children (mime-entity-children message))
126 (null (setq ret (mime-find-entity-from-content-id
127 cid (car children)))))
128 (setq children (cdr children)))
131 (defun mime-entity-parent (entity &optional message)
132 "Return mother entity of ENTITY.
133 If MESSAGE is specified, it is regarded as root entity."
134 (if (equal entity message)
136 (mime-entity-parent-internal entity)))
138 (defun mime-root-entity-p (entity &optional message)
139 "Return t if ENTITY is root-entity (message).
140 If MESSAGE is specified, it is regarded as root entity."
141 (null (mime-entity-parent entity message)))
147 (luna-define-generic mime-entity-buffer (entity))
151 "use mime-entity-header-buffer or mime-entity-body-buffer instead.")
153 (luna-define-generic mime-entity-header-buffer (entity))
155 (luna-define-generic mime-entity-body-buffer (entity))
157 (luna-define-generic mime-entity-point-min (entity))
159 (luna-define-generic mime-entity-point-max (entity))
161 (luna-define-generic mime-entity-body-end-point (entity)
162 "Set buffer and point to body-start-position of ENTITY.")
164 (define-obsolete-function-alias
165 'mime-entity-body-end 'mime-entity-body-end-point)
167 (luna-define-generic mime-goto-header-start-point (entity)
168 "Set buffer and point to header-start-position of ENTITY.")
170 (luna-define-generic mime-goto-body-start-point (entity)
171 "Set buffer and point to body-start-position of ENTITY.")
177 (luna-define-generic mime-entity-fetch-field (entity field-name)
178 "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
180 (defun mime-fetch-field (field-name &optional entity)
181 "Return the value of the ENTITY's header field whose type is FIELD-NAME."
182 (if (symbolp field-name)
183 (setq field-name (symbol-name field-name))
186 (setq entity mime-message-structure))
187 (mime-entity-fetch-field entity field-name)
189 (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
191 (defun mime-entity-content-type (entity)
192 (or (mime-entity-content-type-internal entity)
193 (let ((ret (mime-entity-fetch-field entity "Content-Type")))
195 (mime-entity-set-content-type-internal
196 entity (mime-parse-Content-Type ret))
199 (defun mime-entity-content-disposition (entity)
200 (or (mime-entity-content-disposition-internal entity)
201 (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
203 (mime-entity-set-content-disposition-internal
204 entity (mime-parse-Content-Disposition ret))
207 (defun mime-entity-encoding (entity &optional default-encoding)
208 (or (mime-entity-encoding-internal entity)
209 (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
210 (mime-entity-set-encoding-internal
212 (or (and ret (mime-parse-Content-Transfer-Encoding ret))
213 default-encoding "7bit"))
216 (defvar mime-field-parser-alist
217 '((Return-Path . std11-parse-route-addr)
219 (Reply-To . std11-parse-addresses)
221 (Sender . std11-parse-mailbox)
222 (From . std11-parse-addresses)
224 (Resent-Reply-To . std11-parse-addresses)
226 (Resent-Sender . std11-parse-mailbox)
227 (Resent-From . std11-parse-addresses)
229 (To . std11-parse-addresses)
230 (Resent-To . std11-parse-addresses)
231 (Cc . std11-parse-addresses)
232 (Resent-Cc . std11-parse-addresses)
233 (Bcc . std11-parse-addresses)
234 (Resent-Bcc . std11-parse-addresses)
236 (Message-Id . mime-parse-msg-id)
237 (Recent-Message-Id . mime-parse-msg-id)
239 (In-Reply-To . std11-parse-msg-ids)
240 (References . std11-parse-msg-ids)
242 (Content-Id . mime-parse-msg-id)
245 (defun mime-entity-read-field (entity field-name)
246 (let ((sym (if (symbolp field-name)
249 (setq field-name (symbol-name field-name)))
250 (capitalize (capitalize field-name)))))
251 (cond ((eq sym 'Content-Type)
252 (mime-entity-content-type entity)
254 ((eq sym 'Content-Disposition)
255 (mime-entity-content-disposition entity)
257 ((eq sym 'Content-Transfer-Encoding)
258 (mime-entity-encoding entity)
261 (let* ((header (mime-entity-parsed-header-internal entity))
262 (field (cdr (assq sym header))))
264 (let ((field-body (mime-entity-fetch-field entity field-name))
268 (cdr (assq sym mime-field-parser-alist)))
272 (eword-lexical-analyze field-body))
273 (mime-decode-field-body field-body sym 'plain)
275 (mime-entity-set-parsed-header-internal
276 entity (put-alist sym field header))
279 (defun mime-read-field (field-name &optional entity)
281 (setq entity mime-message-structure))
282 (mime-entity-read-field entity field-name)
284 (make-obsolete 'mime-read-field 'mime-entity-read-field)
286 (luna-define-generic mime-insert-header (entity &optional invisible-fields
288 "Insert before point a decoded header of ENTITY.")
291 ;;; @ Entity Attributes
294 (luna-define-generic mime-entity-name (entity)
295 "Return name of the ENTITY.")
297 (defun mime-entity-uu-filename (entity)
298 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
300 (mime-goto-body-start-point entity)
301 (if (re-search-forward "^begin [0-9]+ "
302 (mime-entity-body-end-point entity) t)
303 (if (looking-at ".+$")
304 (buffer-substring (match-beginning 0)(match-end 0))
307 (defun mime-entity-filename (entity)
308 "Return filename of ENTITY."
309 (or (mime-entity-uu-filename entity)
310 (mime-content-disposition-filename
311 (mime-entity-content-disposition entity))
312 (cdr (let ((param (mime-content-type-parameters
313 (mime-entity-content-type entity))))
314 (or (assoc "name" param)
315 (assoc "x-name" param))
319 (defsubst mime-entity-media-type (entity)
320 (mime-content-type-primary-type (mime-entity-content-type entity)))
321 (defsubst mime-entity-media-subtype (entity)
322 (mime-content-type-subtype (mime-entity-content-type entity)))
323 (defsubst mime-entity-parameters (entity)
324 (mime-content-type-parameters (mime-entity-content-type entity)))
325 (defsubst mime-entity-type/subtype (entity-info)
326 (mime-type/subtype-string (mime-entity-media-type entity-info)
327 (mime-entity-media-subtype entity-info)))
333 (luna-define-generic mime-entity-content (entity)
334 "Return content of ENTITY as byte sequence (string).")
336 (luna-define-generic mime-insert-entity-content (entity)
337 "Insert content of ENTITY at point.")
339 (luna-define-generic mime-write-entity-content (entity filename)
340 "Write content of ENTITY into FILENAME.")
342 (luna-define-generic mime-insert-text-content (entity)
343 "Insert decoded text body of ENTITY.")
345 (luna-define-generic mime-insert-entity (entity)
346 "Insert header and body of ENTITY at point.")
348 (luna-define-generic mime-write-entity (entity filename)
349 "Write header and body of ENTITY into FILENAME.")
351 (luna-define-generic mime-write-entity-body (entity filename)
352 "Write body of ENTITY into FILENAME.")
360 ;;; mime.el ends here