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)
32 (autoload 'eword-encode-field "eword-encode"
33 "Encode header field STRING, and return the result.")
34 (autoload 'eword-encode-header "eword-encode"
35 "Encode header fields to network representation, such as MIME encoded-word.")
38 (autoload 'mime-parse-Content-Type "mime-parse"
39 "Parse STRING as field-body of Content-Type field.")
40 (autoload 'mime-read-Content-Type "mime-parse"
41 "Read field-body of Content-Type field from current-buffer,
42 and return parsed it.")
44 (autoload 'mime-parse-Content-Disposition "mime-parse"
45 "Parse STRING as field-body of Content-Disposition field.")
46 (autoload 'mime-read-Content-Disposition "mime-parse"
47 "Read field-body of Content-Disposition field from current-buffer,
48 and return parsed it.")
50 (autoload 'mime-parse-Content-Transfer-Encoding "mime-parse"
51 "Parse STRING as field-body of Content-Transfer-Encoding field.")
52 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
53 "Read field-body of Content-Transfer-Encoding field from
54 current-buffer, and return it.")
56 (autoload 'mime-parse-msg-id "mime-parse"
57 "Parse TOKENS as msg-id of Content-Id or Message-Id field.")
59 (autoload 'mime-uri-parse-cid "mime-parse"
60 "Parse STRING as cid URI.")
62 (autoload 'mime-parse-buffer "mime-parse"
63 "Parse BUFFER as a MIME message.")
66 ;;; @ Entity Representation and Implementation
69 (defsubst mime-find-function (service type)
70 (let ((imps (cdr (assq type mime-entity-implementation-alist))))
72 (cdr (assq service imps))
73 (require (intern (format "mm%s" type)))
75 (cdr (assq type mime-entity-implementation-alist))))
78 (defsubst mime-entity-function (entity service)
79 (mime-find-function service
80 (mime-entity-representation-type-internal entity)))
82 (defsubst mime-entity-send (entity message &rest args)
83 "Send MESSAGE to ENTITY with ARGS, and return the result."
84 (apply (mime-find-function
85 message (mime-entity-representation-type-internal entity))
89 (defmacro mm-define-generic (name args &optional doc)
91 `(defun ,(intern (format "mime-%s" name)) ,args
93 (mime-entity-send ,(car args) ',name
94 ,@(mm-arglist-to-arguments (cdr args)))
96 `(defun ,(intern (format "mime-%s" name)) ,args
97 (mime-entity-send ,(car args) ',name
98 ,@(mm-arglist-to-arguments (cdr args)))
101 (put 'mm-define-generic 'lisp-indent-function 'defun)
103 (defun mime-open-entity (type location)
104 "Open an entity and return it.
105 TYPE is representation-type.
106 LOCATION is location of entity. Specification of it is depended on
107 representation-type."
108 (let ((entity (make-mime-entity-internal type location)))
109 (mime-entity-send entity 'initialize-instance)
112 (mm-define-generic entity-cooked-p (entity)
113 "Return non-nil if contents of ENTITY has been already code-converted.")
116 ;;; @ Entity as node of message
119 (defun mime-entity-children (entity)
120 (or (mime-entity-children-internal entity)
121 (mime-entity-send entity 'entity-children)))
123 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
125 (defun mime-entity-number (entity)
126 "Return entity-number of ENTITY."
127 (reverse (mime-entity-node-id-internal entity)))
129 (defun mime-find-entity-from-number (entity-number &optional message)
130 "Return entity from ENTITY-NUMBER in MESSAGE.
131 If MESSAGE is not specified, `mime-message-structure' is used."
133 (setq message mime-message-structure))
134 (let ((sn (car entity-number)))
137 (let ((rc (nth sn (mime-entity-children message))))
139 (mime-find-entity-from-number (cdr entity-number) rc)
143 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
144 "Return entity from ENTITY-NODE-ID in MESSAGE.
145 If MESSAGE is not specified, `mime-message-structure' is used."
146 (mime-find-entity-from-number (reverse entity-node-id) message))
148 (defun mime-find-entity-from-content-id (cid &optional message)
149 "Return entity from CID in MESSAGE.
150 If MESSAGE is not specified, `mime-message-structure' is used."
152 (setq message mime-message-structure))
153 (if (equal cid (mime-read-field 'Content-Id message))
155 (let ((children (mime-entity-children message))
158 (null (setq ret (mime-find-entity-from-content-id
159 cid (car children)))))
160 (setq children (cdr children)))
163 (defun mime-entity-parent (entity &optional message)
164 "Return mother entity of ENTITY.
165 If MESSAGE is specified, it is regarded as root entity."
166 (if (equal entity message)
168 (mime-entity-parent-internal entity)))
170 (defun mime-root-entity-p (entity &optional message)
171 "Return t if ENTITY is root-entity (message).
172 If MESSAGE is specified, it is regarded as root entity."
173 (null (mime-entity-parent entity message)))
179 (defun mime-entity-buffer (entity)
180 (or (mime-entity-buffer-internal entity)
181 (mime-entity-send entity 'entity-buffer)))
183 (mm-define-generic entity-point-min (entity)
184 "Return the start point of ENTITY in the buffer which contains ENTITY.")
186 (mm-define-generic entity-point-max (entity)
187 "Return the end point of ENTITY in the buffer which contains ENTITY.")
189 (defun mime-entity-header-start (entity)
190 (or (mime-entity-header-start-internal entity)
191 (mime-entity-send entity 'entity-header-start)))
193 (defun mime-entity-header-end (entity)
194 (or (mime-entity-header-end-internal entity)
195 (mime-entity-send entity 'entity-header-end)))
197 (defun mime-entity-body-start (entity)
198 (or (mime-entity-body-start-internal entity)
199 (mime-entity-send entity 'entity-body-start)))
201 (defun mime-entity-body-end (entity)
202 (or (mime-entity-body-end-internal entity)
203 (mime-entity-send entity 'entity-body-end)))
209 (defun mime-fetch-field (field-name &optional entity)
210 (or (symbolp field-name)
211 (setq field-name (intern (capitalize (capitalize field-name)))))
213 (setq entity mime-message-structure))
214 (cond ((eq field-name 'Date)
215 (or (mime-entity-date-internal entity)
216 (mime-entity-set-date-internal
217 entity (mime-entity-send entity 'fetch-field "Date"))
219 ((eq field-name 'Message-Id)
220 (or (mime-entity-message-id-internal entity)
221 (mime-entity-set-message-id-internal
222 entity (mime-entity-send entity 'fetch-field "Message-Id"))
224 ((eq field-name 'References)
225 (or (mime-entity-references-internal entity)
226 (mime-entity-set-references-internal
227 entity (mime-entity-send entity 'fetch-field "References"))
230 (let* ((header (mime-entity-original-header-internal entity))
231 (field-body (cdr (assq field-name header))))
235 (mime-entity-send entity 'fetch-field
236 (symbol-name field-name)))
237 (mime-entity-set-original-header-internal
238 entity (put-alist field-name field-body header))
243 (defun mime-entity-content-type (entity)
244 (or (mime-entity-content-type-internal entity)
245 (let ((ret (mime-fetch-field 'Content-Type entity)))
247 (mime-entity-set-content-type-internal
248 entity (mime-parse-Content-Type ret))
251 (defun mime-entity-content-disposition (entity)
252 (or (mime-entity-content-disposition-internal entity)
253 (let ((ret (mime-fetch-field 'Content-Disposition entity)))
255 (mime-entity-set-content-disposition-internal
256 entity (mime-parse-Content-Disposition ret))
259 (defun mime-entity-encoding (entity &optional default-encoding)
260 (or (mime-entity-encoding-internal entity)
261 (let ((ret (mime-fetch-field 'Content-Transfer-Encoding entity)))
262 (mime-entity-set-encoding-internal
264 (or (and ret (mime-parse-Content-Transfer-Encoding ret))
265 default-encoding "7bit"))
268 (defvar mime-field-parser-alist
269 '((Return-Path . std11-parse-route-addr)
271 (Reply-To . std11-parse-addresses)
273 (Sender . std11-parse-mailbox)
274 (From . std11-parse-addresses)
276 (Resent-Reply-To . std11-parse-addresses)
278 (Resent-Sender . std11-parse-mailbox)
279 (Resent-From . std11-parse-addresses)
281 (To . std11-parse-addresses)
282 (Resent-To . std11-parse-addresses)
283 (Cc . std11-parse-addresses)
284 (Resent-Cc . std11-parse-addresses)
285 (Bcc . std11-parse-addresses)
286 (Resent-Bcc . std11-parse-addresses)
288 (Message-Id . mime-parse-msg-id)
289 (Recent-Message-Id . mime-parse-msg-id)
291 (In-Reply-To . std11-parse-msg-ids)
292 (References . std11-parse-msg-ids)
294 (Content-Id . mime-parse-msg-id)
297 (defun mime-read-field (field-name &optional entity)
298 (or (symbolp field-name)
299 (setq field-name (capitalize (capitalize field-name))))
301 (setq entity mime-message-structure))
302 (cond ((eq field-name 'Content-Type)
303 (mime-entity-content-type entity)
305 ((eq field-name 'Content-Disposition)
306 (mime-entity-content-disposition entity)
308 ((eq field-name 'Content-Transfer-Encoding)
309 (mime-entity-encoding entity)
312 (let* ((header (mime-entity-parsed-header-internal entity))
313 (field (cdr (assq field-name header))))
315 (let ((field-body (mime-fetch-field field-name entity))
319 (cdr (assq field-name mime-field-parser-alist)))
323 (eword-lexical-analyze field-body))
324 (mime-decode-field-body
325 field-body field-name 'native)
327 (mime-entity-set-parsed-header-internal
328 entity (put-alist field-name field header))
331 (mm-define-generic insert-header (entity &optional invisible-fields
333 "Insert before point a decoded header of ENTITY.")
335 (define-obsolete-function-alias
336 'mime-insert-decoded-header 'mime-insert-header)
339 ;;; @ Entity Attributes
342 (defun mime-entity-uu-filename (entity)
343 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
345 (set-buffer (mime-entity-buffer entity))
346 (goto-char (mime-entity-body-start entity))
347 (if (re-search-forward "^begin [0-9]+ "
348 (mime-entity-body-end entity) t)
349 (if (looking-at ".+$")
350 (buffer-substring (match-beginning 0)(match-end 0))
353 (defun mime-entity-filename (entity)
354 "Return filename of ENTITY."
355 (or (mime-entity-uu-filename entity)
356 (mime-content-disposition-filename
357 (mime-entity-content-disposition entity))
358 (cdr (let ((param (mime-content-type-parameters
359 (mime-entity-content-type entity))))
360 (or (assoc "name" param)
361 (assoc "x-name" param))
365 (defsubst mime-entity-media-type (entity)
366 (mime-content-type-primary-type (mime-entity-content-type entity)))
367 (defsubst mime-entity-media-subtype (entity)
368 (mime-content-type-subtype (mime-entity-content-type entity)))
369 (defsubst mime-entity-parameters (entity)
370 (mime-content-type-parameters (mime-entity-content-type entity)))
371 (defsubst mime-entity-type/subtype (entity-info)
372 (mime-type/subtype-string (mime-entity-media-type entity-info)
373 (mime-entity-media-subtype entity-info)))
379 (mm-define-generic entity-content (entity)
380 "Return content of ENTITY as byte sequence (string).")
382 (mm-define-generic insert-entity-content (entity)
383 "Insert content of ENTITY at point.")
385 (mm-define-generic write-entity-content (entity filename)
386 "Write content of ENTITY into FILENAME.")
388 (mm-define-generic insert-text-content (entity)
389 "Insert decoded text body of ENTITY.")
391 (mm-define-generic insert-entity (entity)
392 "Insert header and body of ENTITY at point.")
394 (mm-define-generic write-entity (entity filename)
395 "Write header and body of ENTITY into FILENAME.")
397 (mm-define-generic write-entity-body (entity filename)
398 "Write body of ENTITY into FILENAME.")
406 ;;; mime.el ends here