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 as node of message
66 (defalias 'mime-entity-children 'mime-entity-children-internal)
68 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
70 (defsubst mime-entity-number (entity)
71 (reverse (mime-entity-node-id-internal entity)))
73 (defun mime-find-entity-from-number (entity-number &optional message)
74 "Return entity from ENTITY-NUMBER in MESSAGE.
75 If MESSAGE is not specified, `mime-message-structure' is used."
77 (setq message mime-message-structure))
78 (let ((sn (car entity-number)))
81 (let ((rc (nth sn (mime-entity-children message))))
83 (mime-find-entity-from-number (cdr entity-number) rc)
87 (defsubst mime-find-entity-from-node-id (entity-node-id &optional message)
88 "Return entity from ENTITY-NODE-ID in MESSAGE.
89 If MESSAGE is not specified, `mime-message-structure' is used."
90 (mime-find-entity-from-number (reverse entity-node-id) message))
92 (defsubst mime-entity-parent (entity &optional message)
93 "Return mother entity of ENTITY.
94 If MESSAGE is not specified, `mime-message-structure' in the buffer of
96 (mime-find-entity-from-node-id
97 (cdr (mime-entity-node-id entity))
100 (set-buffer (mime-entity-buffer entity))
101 mime-message-structure))))
103 (defsubst mime-root-entity-p (entity)
104 "Return t if ENTITY is root-entity (message)."
105 (null (mime-entity-node-id entity)))
111 (defalias 'mime-entity-buffer 'mime-entity-buffer-internal)
112 (defalias 'mime-entity-point-min 'mime-entity-header-start-internal)
113 (defalias 'mime-entity-point-max 'mime-entity-body-end-internal)
119 (defalias 'mime-entity-header-start 'mime-entity-header-start-internal)
120 (defalias 'mime-entity-header-end 'mime-entity-header-end-internal)
122 (defun mime-fetch-field (field-name &optional entity)
123 (or (symbolp field-name)
124 (setq field-name (intern (capitalize (capitalize field-name)))))
126 (setq entity mime-message-structure))
127 (let* ((header (mime-entity-original-header-internal entity))
128 (field-body (cdr (assq field-name header))))
132 (set-buffer (mime-entity-buffer entity))
134 (narrow-to-region (mime-entity-header-start entity)
135 (mime-entity-header-end entity))
137 (std11-fetch-field (symbol-name field-name)))
139 (mime-entity-set-original-header-internal
140 entity (put-alist field-name field-body header))
144 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
146 (defun mime-entity-content-disposition (entity)
147 (or (mime-entity-content-disposition-internal entity)
148 (let ((ret (mime-fetch-field 'Content-Disposition entity)))
150 (let ((disposition (mime-parse-Content-Disposition ret)))
152 (mime-entity-set-content-disposition-internal
156 (defun mime-entity-encoding (entity)
157 (or (mime-entity-encoding-internal entity)
158 (let ((ret (mime-fetch-field 'Content-Transfer-Encoding entity)))
160 (let ((encoding (mime-parse-Content-Transfer-Encoding ret)))
162 (mime-entity-set-encoding-internal entity encoding)
165 (defun mime-read-field (field-name &optional entity)
166 (or (symbolp field-name)
167 (setq field-name (capitalize (capitalize field-name))))
169 (setq entity mime-message-structure))
170 (cond ((eq field-name 'Content-Type)
171 (mime-entity-content-type entity)
173 ((eq field-name 'Content-Disposition)
174 (mime-entity-content-disposition entity)
176 ((eq field-name 'Content-Transfer-Encoding)
177 (mime-entity-encoding entity)
180 (let* ((header (mime-entity-parsed-header-internal entity))
181 (field (cdr (assq field-name header))))
183 (let ((field-body (mime-fetch-field field-name entity)))
185 (cond ((memq field-name '(From Resent-From
189 Reply-To Resent-Reply-To))
190 (setq field (std11-parse-addresses
191 (eword-lexical-analyze field-body)))
193 ((memq field-name '(Sender Resent-Sender))
194 (setq field (std11-parse-address
195 (eword-lexical-analyze field-body)))
197 ((memq field-name eword-decode-ignored-field-list)
198 (setq field field-body))
199 ((memq field-name eword-decode-structured-field-list)
200 (setq field (eword-decode-structured-field-body
203 (setq field (eword-decode-unstructured-field-body
206 (mime-entity-set-parsed-header-internal
207 entity (put-alist field-name field header))
210 (defun eword-visible-field-p (field-name visible-fields invisible-fields)
212 (while visible-fields
213 (let ((regexp (car visible-fields)))
214 (if (string-match regexp field-name)
217 (setq visible-fields (cdr visible-fields))
220 (while invisible-fields
221 (let ((regexp (car invisible-fields)))
222 (if (string-match regexp field-name)
225 (setq invisible-fields (cdr invisible-fields))
229 (defun mime-insert-decoded-header (entity
230 &optional invisible-fields visible-fields
232 "Insert before point a decoded header of ENTITY."
233 (let ((default-charset
235 (if (mime-charset-to-coding-system code-conversion)
237 default-mime-charset))))
239 (narrow-to-region (point)(point))
240 (let ((the-buf (current-buffer))
241 (src-buf (mime-entity-buffer entity))
242 (h-end (mime-entity-header-end entity))
243 beg p end field-name len field)
246 (goto-char (mime-entity-header-start entity))
248 (narrow-to-region (point) h-end)
249 (while (re-search-forward std11-field-head-regexp nil t)
250 (setq beg (match-beginning 0)
252 field-name (buffer-substring beg (1- p))
253 len (string-width field-name)
254 end (std11-field-end))
255 (when (eword-visible-field-p field-name
256 visible-fields invisible-fields)
257 (setq field (intern (capitalize field-name)))
262 (cond ((memq field eword-decode-ignored-field-list)
264 (insert-buffer-substring src-buf p end)
266 ((memq field eword-decode-structured-field-list)
267 ;; Decode as structured field
268 (let ((body (save-excursion
270 (buffer-substring p end)))
271 (default-mime-charset default-charset))
272 (insert (eword-decode-and-fold-structured-field
276 ;; Decode as unstructured field
277 (let ((body (save-excursion
279 (buffer-substring p end)))
280 (default-mime-charset default-charset))
281 (insert (eword-decode-unstructured-field-body
291 (defalias 'mime-entity-body-start 'mime-entity-body-start-internal)
292 (defalias 'mime-entity-body-end 'mime-entity-body-end-internal)
294 (defun mime-entity-content (entity)
296 (set-buffer (mime-entity-buffer entity))
297 (mime-decode-string (buffer-substring (mime-entity-body-start entity)
298 (mime-entity-body-end entity))
299 (mime-entity-encoding entity))))
302 ;;; @ Another Entity Information
305 (defun mime-entity-uu-filename (entity)
306 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
308 (set-buffer (mime-entity-buffer entity))
309 (goto-char (mime-entity-body-start entity))
310 (if (re-search-forward "^begin [0-9]+ "
311 (mime-entity-body-end entity) t)
312 (if (looking-at ".+$")
313 (buffer-substring (match-beginning 0)(match-end 0))
316 (defun mime-entity-filename (entity)
317 "Return filename of ENTITY."
318 (or (mime-entity-uu-filename entity)
319 (mime-content-disposition-filename
320 (mime-entity-content-disposition entity))
321 (cdr (let ((param (mime-content-type-parameters
322 (mime-entity-content-type entity))))
323 (or (assoc "name" param)
324 (assoc "x-name" param))
328 (defsubst mime-entity-media-type (entity)
329 (mime-content-type-primary-type (mime-entity-content-type entity)))
330 (defsubst mime-entity-media-subtype (entity)
331 (mime-content-type-subtype (mime-entity-content-type entity)))
332 (defsubst mime-entity-parameters (entity)
333 (mime-content-type-parameters (mime-entity-content-type entity)))
334 (defsubst mime-entity-type/subtype (entity-info)
335 (mime-type/subtype-string (mime-entity-media-type entity-info)
336 (mime-entity-media-subtype entity-info)))
344 ;;; mime.el ends here