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 "Return entity-number of ENTITY."
72 (reverse (mime-entity-node-id-internal entity)))
74 (defun mime-find-entity-from-number (entity-number &optional message)
75 "Return entity from ENTITY-NUMBER in MESSAGE.
76 If MESSAGE is not specified, `mime-message-structure' is used."
78 (setq message mime-message-structure))
79 (let ((sn (car entity-number)))
82 (let ((rc (nth sn (mime-entity-children message))))
84 (mime-find-entity-from-number (cdr entity-number) rc)
88 (defsubst mime-find-entity-from-node-id (entity-node-id &optional message)
89 "Return entity from ENTITY-NODE-ID in MESSAGE.
90 If MESSAGE is not specified, `mime-message-structure' is used."
91 (mime-find-entity-from-number (reverse entity-node-id) message))
93 (defsubst mime-entity-parent (entity &optional message)
94 "Return mother entity of ENTITY.
95 If MESSAGE is not specified, `mime-message-structure' in the buffer of
97 (mime-find-entity-from-node-id
98 (cdr (mime-entity-node-id entity))
101 (set-buffer (mime-entity-buffer entity))
102 mime-message-structure))))
104 (defsubst mime-root-entity-p (entity)
105 "Return t if ENTITY is root-entity (message)."
106 (null (mime-entity-node-id entity)))
112 (defalias 'mime-entity-buffer 'mime-entity-buffer-internal)
114 (defalias 'mime-entity-point-min 'mime-entity-header-start-internal)
115 (defalias 'mime-entity-point-max 'mime-entity-body-end-internal)
117 (defalias 'mime-entity-header-start 'mime-entity-header-start-internal)
118 (defalias 'mime-entity-header-end 'mime-entity-header-end-internal)
120 (defalias 'mime-entity-body-start 'mime-entity-body-start-internal)
121 (defalias 'mime-entity-body-end 'mime-entity-body-end-internal)
127 (defun mime-fetch-field (field-name &optional entity)
128 (or (symbolp field-name)
129 (setq field-name (intern (capitalize (capitalize field-name)))))
131 (setq entity mime-message-structure))
132 (let* ((header (mime-entity-original-header-internal entity))
133 (field-body (cdr (assq field-name header))))
137 (set-buffer (mime-entity-buffer entity))
139 (narrow-to-region (mime-entity-header-start entity)
140 (mime-entity-header-end entity))
142 (std11-fetch-field (symbol-name field-name)))
144 (mime-entity-set-original-header-internal
145 entity (put-alist field-name field-body header))
149 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
151 (defun mime-entity-content-disposition (entity)
152 (or (mime-entity-content-disposition-internal entity)
153 (let ((ret (mime-fetch-field 'Content-Disposition entity)))
155 (let ((disposition (mime-parse-Content-Disposition ret)))
157 (mime-entity-set-content-disposition-internal
161 (defun mime-entity-encoding (entity)
162 (or (mime-entity-encoding-internal entity)
163 (let ((ret (mime-fetch-field 'Content-Transfer-Encoding entity)))
165 (let ((encoding (mime-parse-Content-Transfer-Encoding ret)))
167 (mime-entity-set-encoding-internal entity encoding)
170 (defun mime-read-field (field-name &optional entity)
171 (or (symbolp field-name)
172 (setq field-name (capitalize (capitalize field-name))))
174 (setq entity mime-message-structure))
175 (cond ((eq field-name 'Content-Type)
176 (mime-entity-content-type entity)
178 ((eq field-name 'Content-Disposition)
179 (mime-entity-content-disposition entity)
181 ((eq field-name 'Content-Transfer-Encoding)
182 (mime-entity-encoding entity)
185 (let* ((header (mime-entity-parsed-header-internal entity))
186 (field (cdr (assq field-name header))))
188 (let ((field-body (mime-fetch-field field-name entity)))
190 (cond ((memq field-name '(From Resent-From
194 Reply-To Resent-Reply-To))
195 (setq field (std11-parse-addresses
196 (eword-lexical-analyze field-body)))
198 ((memq field-name '(Sender Resent-Sender))
199 (setq field (std11-parse-address
200 (eword-lexical-analyze field-body)))
202 ((memq field-name eword-decode-ignored-field-list)
203 (setq field field-body))
204 ((memq field-name eword-decode-structured-field-list)
205 (setq field (eword-decode-structured-field-body
208 (setq field (eword-decode-unstructured-field-body
211 (mime-entity-set-parsed-header-internal
212 entity (put-alist field-name field header))
215 (defun eword-visible-field-p (field-name visible-fields invisible-fields)
217 (while visible-fields
218 (let ((regexp (car visible-fields)))
219 (if (string-match regexp field-name)
222 (setq visible-fields (cdr visible-fields))
225 (while invisible-fields
226 (let ((regexp (car invisible-fields)))
227 (if (string-match regexp field-name)
230 (setq invisible-fields (cdr invisible-fields))
234 (defun mime-insert-decoded-header (entity
235 &optional invisible-fields visible-fields
237 "Insert before point a decoded header of ENTITY."
238 (let ((default-charset
240 (if (mime-charset-to-coding-system code-conversion)
242 default-mime-charset))))
244 (narrow-to-region (point)(point))
245 (let ((the-buf (current-buffer))
246 (src-buf (mime-entity-buffer entity))
247 (h-end (mime-entity-header-end entity))
248 beg p end field-name len field)
251 (goto-char (mime-entity-header-start entity))
253 (narrow-to-region (point) h-end)
254 (while (re-search-forward std11-field-head-regexp nil t)
255 (setq beg (match-beginning 0)
257 field-name (buffer-substring beg (1- p))
258 len (string-width field-name)
259 end (std11-field-end))
260 (when (eword-visible-field-p field-name
261 visible-fields invisible-fields)
262 (setq field (intern (capitalize field-name)))
267 (cond ((memq field eword-decode-ignored-field-list)
269 (insert-buffer-substring src-buf p end)
271 ((memq field eword-decode-structured-field-list)
272 ;; Decode as structured field
273 (let ((body (save-excursion
275 (buffer-substring p end)))
276 (default-mime-charset default-charset))
277 (insert (eword-decode-and-fold-structured-field
281 ;; Decode as unstructured field
282 (let ((body (save-excursion
284 (buffer-substring p end)))
285 (default-mime-charset default-charset))
286 (insert (eword-decode-unstructured-field-body
293 ;;; @ Entity Attributes
296 (defun mime-entity-uu-filename (entity)
297 (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
299 (set-buffer (mime-entity-buffer entity))
300 (goto-char (mime-entity-body-start entity))
301 (if (re-search-forward "^begin [0-9]+ "
302 (mime-entity-body-end 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 (defun mime-entity-content (entity)
335 (set-buffer (mime-entity-buffer entity))
336 (mime-decode-string (buffer-substring (mime-entity-body-start entity)
337 (mime-entity-body-end entity))
338 (mime-entity-encoding entity))))
346 ;;; mime.el ends here