1 ;;; mime.el --- MIME library module
3 ;; Copyright (C) 1998 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-message "mime-parse"
57 "Parse current-buffer as a MIME message.")
59 (autoload 'mime-parse-buffer "mime-parse"
60 "Parse BUFFER as a MIME message.")
63 ;;; @ Entity Representation and Implementation
66 (defvar mime-entity-implementation-alist nil)
68 (defsubst mime-find-function (service type)
69 (let ((imps (cdr (assq type mime-entity-implementation-alist))))
71 (let ((func (cdr (assq service imps))))
73 (setq func (intern (format "mm%s-%s" type service)))
74 (set-alist 'mime-entity-implementation-alist
75 type (put-alist service func imps))
78 (let ((prefix (format "mm%s" type)))
79 (require (intern prefix))
80 (let ((func (intern (format "%s-%s" prefix service))))
81 (set-alist 'mime-entity-implementation-alist
83 (list (cons service func)))
86 (defun mime-open-entity (type location)
87 "Open an entity and return it.
88 TYPE is representation-type.
89 LOCATION is location of entity. Specification of it is depended on
91 (funcall (mime-find-function 'open-entity type) location)
94 (defsubst mime-entity-function (entity service)
95 (mime-find-function service
96 (mime-entity-representation-type-internal entity)))
98 (defun mime-entity-cooked-p (entity)
99 "Return non-nil if contents of ENTITY has been already code-converted."
100 (funcall (mime-entity-function entity 'cooked-p))
104 ;;; @ Entity as node of message
107 (defalias 'mime-entity-children 'mime-entity-children-internal)
109 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
111 (defun mime-entity-number (entity)
112 "Return entity-number of ENTITY."
113 (reverse (mime-entity-node-id-internal entity)))
115 (defun mime-find-entity-from-number (entity-number &optional message)
116 "Return entity from ENTITY-NUMBER in MESSAGE.
117 If MESSAGE is not specified, `mime-message-structure' is used."
119 (setq message mime-message-structure))
120 (let ((sn (car entity-number)))
123 (let ((rc (nth sn (mime-entity-children message))))
125 (mime-find-entity-from-number (cdr entity-number) rc)
129 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
130 "Return entity from ENTITY-NODE-ID in MESSAGE.
131 If MESSAGE is not specified, `mime-message-structure' is used."
132 (mime-find-entity-from-number (reverse entity-node-id) message))
134 (defun mime-entity-parent (entity &optional message)
135 "Return mother entity of ENTITY.
136 If MESSAGE is not specified, `mime-message-structure' in the buffer of
138 (mime-find-entity-from-node-id
139 (cdr (mime-entity-node-id entity))
142 (set-buffer (mime-entity-buffer entity))
143 mime-message-structure))))
145 (defun mime-root-entity-p (entity)
146 "Return t if ENTITY is root-entity (message)."
147 (null (mime-entity-node-id entity)))
153 (defun mime-entity-buffer (entity)
154 (or (mime-entity-buffer-internal entity)
155 (funcall (mime-entity-function entity 'entity-buffer) entity)
158 (defun mime-entity-point-min (entity)
159 (funcall (mime-entity-function entity 'entity-point-min) entity)
161 (defun mime-entity-point-max (entity)
162 (funcall (mime-entity-function entity 'entity-point-max) entity)
165 (defun mime-entity-header-start (entity)
166 (or (mime-entity-header-start-internal entity)
167 (funcall (mime-entity-function entity 'entity-header-start) entity)
169 (defsubst mime-entity-header-end (entity)
170 (or (mime-entity-header-end-internal entity)
171 (funcall (mime-entity-function entity 'entity-header-end) entity)
174 (defsubst mime-entity-body-start (entity)
175 (or (mime-entity-body-start-internal entity)
176 (funcall (mime-entity-function entity 'entity-body-start) entity)
178 (defsubst mime-entity-body-end (entity)
179 (or (mime-entity-body-end-internal entity)
180 (funcall (mime-entity-function entity 'entity-body-end) entity)
187 (defun mime-fetch-field (field-name &optional entity)
188 (or (symbolp field-name)
189 (setq field-name (intern (capitalize (capitalize field-name)))))
191 (setq entity mime-message-structure))
192 (let* ((header (mime-entity-original-header-internal entity))
193 (field-body (cdr (assq field-name header))))
197 (funcall (mime-entity-function entity 'fetch-field)
198 entity (symbol-name field-name)))
199 (mime-entity-set-original-header-internal
200 entity (put-alist field-name field-body header))
204 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
206 (defun mime-entity-content-disposition (entity)
207 (or (mime-entity-content-disposition-internal entity)
208 (let ((ret (mime-fetch-field 'Content-Disposition entity)))
210 (let ((disposition (mime-parse-Content-Disposition ret)))
212 (mime-entity-set-content-disposition-internal
216 (defun mime-entity-encoding (entity)
217 (or (mime-entity-encoding-internal entity)
218 (let ((ret (mime-fetch-field 'Content-Transfer-Encoding entity)))
220 (let ((encoding (mime-parse-Content-Transfer-Encoding ret)))
222 (mime-entity-set-encoding-internal entity encoding)
225 (defun mime-read-field (field-name &optional entity)
226 (or (symbolp field-name)
227 (setq field-name (capitalize (capitalize field-name))))
229 (setq entity mime-message-structure))
230 (cond ((eq field-name 'Content-Type)
231 (mime-entity-content-type entity)
233 ((eq field-name 'Content-Disposition)
234 (mime-entity-content-disposition entity)
236 ((eq field-name 'Content-Transfer-Encoding)
237 (mime-entity-encoding entity)
240 (let* ((header (mime-entity-parsed-header-internal entity))
241 (field (cdr (assq field-name header))))
243 (let ((field-body (mime-fetch-field field-name entity)))
245 (cond ((memq field-name '(From Resent-From
249 Reply-To Resent-Reply-To))
250 (setq field (std11-parse-addresses
251 (eword-lexical-analyze field-body)))
253 ((memq field-name '(Sender Resent-Sender))
254 (setq field (std11-parse-address
255 (eword-lexical-analyze field-body)))
257 ((memq field-name eword-decode-ignored-field-list)
258 (setq field field-body))
259 ((memq field-name eword-decode-structured-field-list)
260 (setq field (eword-decode-structured-field-body
263 (setq field (eword-decode-unstructured-field-body
266 (mime-entity-set-parsed-header-internal
267 entity (put-alist field-name field header))
270 (defun eword-visible-field-p (field-name visible-fields invisible-fields)
272 (while visible-fields
273 (let ((regexp (car visible-fields)))
274 (if (string-match regexp field-name)
277 (setq visible-fields (cdr visible-fields))
280 (while invisible-fields
281 (let ((regexp (car invisible-fields)))
282 (if (string-match regexp field-name)
285 (setq invisible-fields (cdr invisible-fields))
289 (defun mime-insert-decoded-header (entity
290 &optional invisible-fields visible-fields
292 "Insert before point a decoded header of ENTITY."
293 (let ((default-charset
295 (if (mime-charset-to-coding-system code-conversion)
297 default-mime-charset))))
299 (narrow-to-region (point)(point))
300 (let ((the-buf (current-buffer))
301 (src-buf (mime-entity-buffer entity))
302 (h-end (mime-entity-header-end entity))
303 beg p end field-name len field)
306 (goto-char (mime-entity-header-start entity))
308 (narrow-to-region (point) h-end)
309 (while (re-search-forward std11-field-head-regexp nil t)
310 (setq beg (match-beginning 0)
312 field-name (buffer-substring beg (1- p))
313 len (string-width field-name)
314 end (std11-field-end))
315 (when (eword-visible-field-p field-name
316 visible-fields invisible-fields)
317 (setq field (intern (capitalize field-name)))
322 (cond ((memq field eword-decode-ignored-field-list)
324 (insert-buffer-substring src-buf p end)
326 ((memq field eword-decode-structured-field-list)
327 ;; Decode as structured field
328 (let ((body (save-excursion
330 (buffer-substring p end)))
331 (default-mime-charset default-charset))
332 (insert (eword-decode-and-fold-structured-field
336 ;; Decode as unstructured field
337 (let ((body (save-excursion
339 (buffer-substring p end)))
340 (default-mime-charset default-charset))
341 (insert (eword-decode-unstructured-field-body
348 ;;; @ Entity Attributes
351 (defun mime-entity-uu-filename (entity)
352 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
354 (set-buffer (mime-entity-buffer entity))
355 (goto-char (mime-entity-body-start entity))
356 (if (re-search-forward "^begin [0-9]+ "
357 (mime-entity-body-end entity) t)
358 (if (looking-at ".+$")
359 (buffer-substring (match-beginning 0)(match-end 0))
362 (defun mime-entity-filename (entity)
363 "Return filename of ENTITY."
364 (or (mime-entity-uu-filename entity)
365 (mime-content-disposition-filename
366 (mime-entity-content-disposition entity))
367 (cdr (let ((param (mime-content-type-parameters
368 (mime-entity-content-type entity))))
369 (or (assoc "name" param)
370 (assoc "x-name" param))
374 (defsubst mime-entity-media-type (entity)
375 (mime-content-type-primary-type (mime-entity-content-type entity)))
376 (defsubst mime-entity-media-subtype (entity)
377 (mime-content-type-subtype (mime-entity-content-type entity)))
378 (defsubst mime-entity-parameters (entity)
379 (mime-content-type-parameters (mime-entity-content-type entity)))
380 (defsubst mime-entity-type/subtype (entity-info)
381 (mime-type/subtype-string (mime-entity-media-type entity-info)
382 (mime-entity-media-subtype entity-info)))
388 (defun mime-entity-content (entity)
390 (set-buffer (mime-entity-buffer entity))
391 (mime-decode-string (buffer-substring (mime-entity-body-start entity)
392 (mime-entity-body-end entity))
393 (mime-entity-encoding entity))))
395 (defun mime-write-entity-content (entity filename)
396 "Write content of ENTITY into FILENAME."
398 (set-buffer (mime-entity-buffer entity))
399 (let ((encoding (or (mime-entity-encoding entity) "7bit")))
400 (if (and (mime-entity-cooked-p entity)
401 (member encoding '("7bit" "8bit" "binary")))
402 (write-region (mime-entity-body-start entity)
403 (mime-entity-body-end entity) filename)
404 (mime-write-decoded-region (mime-entity-body-start entity)
405 (mime-entity-body-end entity)
409 (defun mime-write-entity (entity filename)
410 "Write ENTITY into FILENAME."
412 (set-buffer (mime-entity-buffer entity))
413 (if (mime-entity-cooked-p entity)
414 (write-region (mime-entity-point-min entity)
415 (mime-entity-point-max entity) filename)
416 (write-region-as-binary (mime-entity-point-min entity)
417 (mime-entity-point-max entity) filename)
420 (defun mime-write-entity-body (entity filename)
421 "Write body of ENTITY into FILENAME."
423 (set-buffer (mime-entity-buffer entity))
424 (if (mime-entity-cooked-p entity)
425 (write-region (mime-entity-body-start entity)
426 (mime-entity-body-end entity) filename)
427 (write-region-as-binary (mime-entity-body-start entity)
428 (mime-entity-body-end entity) filename)
437 ;;; mime.el ends here