1 ;;; mime-parse.el --- MIME message parser
3 ;; Copyright (C) 1994,1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: parse, MIME, multimedia, mail, news
8 ;; This file is part of SEMI (Spadework for Emacs MIME Interfaces).
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.
31 (eval-when-compile (require 'cl))
37 (defconst mime/content-parameter-value-regexp
39 std11-quoted-string-regexp
42 (defconst mime::parameter-regexp
43 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
44 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
46 (defun mime-parse-parameter (str)
47 (if (string-match mime::parameter-regexp str)
48 (let ((e (match-end 2)))
50 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
51 (std11-strip-quoted-string
52 (substring str (match-beginning 2) e))
61 (defun mime-parse-Content-Type (string)
62 "Parse STRING as field-body of Content-Type field.
64 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
65 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
67 (setq string (std11-unfold-string string))
68 (if (string-match `,(concat "^\\(" mime-token-regexp
69 "\\)/\\(" mime-token-regexp "\\)") string)
70 (let* ((type (downcase
71 (substring string (match-beginning 1) (match-end 1))))
73 (substring string (match-beginning 2) (match-end 2))))
75 (setq string (substring string (match-end 0)))
76 (while (setq ret (mime-parse-parameter string))
77 (setq dest (cons (car ret) dest)
80 (make-mime-content-type (intern type)(intern subtype)
84 (defun mime-read-Content-Type ()
85 "Read field-body of Content-Type field from current-buffer,
86 and return parsed it. Format of return value is as same as
87 `mime-parse-Content-Type'."
88 (let ((str (std11-field-body "Content-Type")))
90 (mime-parse-Content-Type str)
94 ;;; @ Content-Disposition
97 (defconst mime-disposition-type-regexp mime-token-regexp)
99 (defun mime-parse-Content-Disposition (string)
100 "Parse STRING as field-body of Content-Disposition field."
101 (setq string (std11-unfold-string string))
102 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
103 (let* ((e (match-end 0))
104 (type (downcase (substring string 0 e)))
106 (setq string (substring string e))
107 (while (setq ret (mime-parse-parameter string))
108 (setq dest (cons (car ret) dest)
111 (cons (cons 'type (intern type))
115 (defun mime-read-Content-Disposition ()
116 "Read field-body of Content-Disposition field from current-buffer,
117 and return parsed it."
118 (let ((str (std11-field-body "Content-Disposition")))
120 (mime-parse-Content-Disposition str)
123 (defsubst mime-content-disposition-type (content-disposition)
124 "Return disposition-type of CONTENT-DISPOSITION."
125 (cdr (car content-disposition)))
127 (defsubst mime-content-disposition-parameters (content-disposition)
128 "Return disposition-parameters of CONTENT-DISPOSITION."
129 (cdr content-disposition))
131 (defsubst mime-content-disposition-parameter (content-disposition parameter)
132 "Return PARAMETER value of CONTENT-DISPOSITION."
133 (cdr (assoc parameter (cdr content-disposition))))
135 (defsubst mime-content-disposition-filename (content-disposition)
136 "Return filename of CONTENT-DISPOSITION."
137 (mime-content-disposition-parameter content-disposition "filename"))
140 ;;; @ Content-Transfer-Encoding
143 (defun mime-parse-Content-Transfer-Encoding (string)
144 "Parse STRING as field-body of Content-Transfer-Encoding field."
145 (if (string-match "[ \t\n\r]+$" string)
146 (setq string (match-string 0 string))
150 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
151 "Read field-body of Content-Transfer-Encoding field from
152 current-buffer, and return it.
153 If is is not found, return DEFAULT-ENCODING."
154 (let ((str (std11-field-body "Content-Transfer-Encoding")))
156 (mime-parse-Content-Transfer-Encoding str)
163 (defalias 'mime-entity-point-min 'mime-entity-header-start)
164 (defalias 'mime-entity-point-max 'mime-entity-body-end)
166 (defsubst mime-entity-media-type (entity)
167 (mime-content-type-primary-type (mime-entity-content-type entity)))
168 (defsubst mime-entity-media-subtype (entity)
169 (mime-content-type-subtype (mime-entity-content-type entity)))
170 (defsubst mime-entity-parameters (entity)
171 (mime-content-type-parameters (mime-entity-content-type entity)))
173 (defsubst mime-entity-type/subtype (entity-info)
174 (mime-type/subtype-string (mime-entity-media-type entity-info)
175 (mime-entity-media-subtype entity-info)))
177 (defun mime-parse-multipart (header-start header-end body-start body-end
178 content-type content-disposition
180 (goto-char (point-min))
181 (let* ((dash-boundary
183 (std11-strip-quoted-string
184 (mime-content-type-parameter content-type "boundary"))))
185 (delimiter (concat "\n" (regexp-quote dash-boundary)))
186 (close-delimiter (concat delimiter "--[ \t]*$"))
187 (rsep (concat delimiter "[ \t]*\n"))
189 (if (eq (mime-content-type-subtype content-type) 'digest)
190 (make-mime-content-type 'message 'rfc822)
191 (make-mime-content-type 'text 'plain)
193 cb ce ret ncb children (i 0))
196 (narrow-to-region header-end
197 (if (re-search-backward close-delimiter nil t)
200 (goto-char header-end)
201 (re-search-forward rsep nil t)
202 (setq cb (match-end 0))
203 (while (re-search-forward rsep nil t)
204 (setq ce (match-beginning 0))
205 (setq ncb (match-end 0))
207 (narrow-to-region cb ce)
208 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
210 (setq children (cons ret children))
211 (goto-char (setq cb ncb))
214 (setq ce (point-max))
216 (narrow-to-region cb ce)
217 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
219 (setq children (cons ret children))
221 (make-mime-entity (current-buffer)
222 header-start header-end body-start body-end
223 node-id content-type content-disposition encoding
227 (defun mime-parse-message (&optional default-ctl default-encoding node-id)
228 "Parse current-buffer as a MIME message.
229 DEFAULT-CTL is used when an entity does not have valid Content-Type
230 field. Its format must be as same as return value of
231 mime-{parse|read}-Content-Type."
232 (let ((header-start (point-min))
235 (body-end (point-max))
236 content-type content-disposition encoding
238 (goto-char header-start)
239 (if (re-search-forward "^$" nil t)
240 (setq header-end (match-end 0)
241 body-start (1+ header-end))
242 (setq header-end (point-min)
243 body-start (point-min))
246 (narrow-to-region header-start header-end)
247 (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
249 (mime-parse-Content-Type str)
252 content-disposition (let ((str (std11-fetch-field
253 "Content-Disposition")))
255 (mime-parse-Content-Disposition str)
257 encoding (let ((str (std11-fetch-field
258 "Content-Transfer-Encoding")))
260 (mime-parse-Content-Transfer-Encoding str)
262 primary-type (mime-content-type-primary-type content-type))
264 (cond ((eq primary-type 'multipart)
265 (mime-parse-multipart header-start header-end
267 content-type content-disposition encoding
270 ((and (eq primary-type 'message)
271 (memq (mime-content-type-subtype content-type)
272 '(rfc822 news external-body)
274 (make-mime-entity (current-buffer)
275 header-start header-end body-start body-end
277 content-type content-disposition encoding
279 (narrow-to-region body-start body-end)
280 (list (mime-parse-message
281 nil nil (cons 0 node-id)))
285 (make-mime-entity (current-buffer)
286 header-start header-end body-start body-end
288 content-type content-disposition encoding nil)
296 (defvar mime-message-structure nil
297 "Information about structure of message.
298 Please use reference function `mime-entity-SLOT' to get value of SLOT.
300 Following is a list of slots of the structure:
302 buffer buffer includes this entity (buffer).
303 node-id node-id (list of integers)
304 header-start minimum point of header in raw-buffer
305 header-end maximum point of header in raw-buffer
306 body-start minimum point of body in raw-buffer
307 body-end maximum point of body in raw-buffer
308 content-type content-type (content-type)
309 content-disposition content-disposition (content-disposition)
310 encoding Content-Transfer-Encoding (string or nil)
311 children entities included in this entity (list of entity)
313 If an entity includes other entities in its body, such as multipart or
314 message/rfc822, `mime-entity' structures of them are included in
315 `children', so the `mime-entity' structure become a tree.")
316 (make-variable-buffer-local 'mime-message-structure)
318 (defun mime-parse-buffer (&optional buffer)
319 "Parse BUFFER as a MIME message.
320 If buffer is omitted, it parses current-buffer."
322 (if buffer (set-buffer buffer))
323 (setq mime-message-structure (mime-parse-message))
330 (defsubst mime-root-entity-p (entity)
331 "Return t if ENTITY is root-entity (message)."
332 (null (mime-entity-node-id entity)))
338 (provide 'mime-parse)
340 ;;; mime-parse.el ends here