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.
35 (defconst mime/content-parameter-value-regexp
37 std11-quoted-string-regexp
40 (defconst mime::parameter-regexp
41 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
42 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
44 (defun mime-parse-parameter (str)
45 (if (string-match mime::parameter-regexp str)
46 (let ((e (match-end 2)))
48 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
49 (std11-strip-quoted-string
50 (substring str (match-beginning 2) e))
59 (defsubst make-mime-content-type (type subtype &optional parameters)
60 (list* (cons 'type type)
61 (cons 'subtype subtype)
62 (nreverse parameters))
65 (defun mime-parse-Content-Type (string)
66 "Parse STRING as field-body of Content-Type field.
68 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
69 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
71 (setq string (std11-unfold-string string))
72 (if (string-match `,(concat "^\\(" mime-token-regexp
73 "\\)/\\(" mime-token-regexp "\\)") string)
74 (let* ((type (downcase
75 (substring string (match-beginning 1) (match-end 1))))
77 (substring string (match-beginning 2) (match-end 2))))
79 (setq string (substring string (match-end 0)))
80 (while (setq ret (mime-parse-parameter string))
81 (setq dest (cons (car ret) dest)
84 (make-mime-content-type (intern type)(intern subtype)
88 (defun mime-read-Content-Type ()
89 "Read field-body of Content-Type field from current-buffer,
90 and return parsed it. Format of return value is as same as
91 `mime-parse-Content-Type'."
92 (let ((str (std11-field-body "Content-Type")))
94 (mime-parse-Content-Type str)
97 (defsubst mime-content-type-primary-type (content-type)
98 "Return primary-type of CONTENT-TYPE."
99 (cdr (car content-type)))
101 (defsubst mime-content-type-subtype (content-type)
102 "Return primary-type of CONTENT-TYPE."
103 (cdr (cadr content-type)))
105 (defsubst mime-content-type-parameters (content-type)
106 "Return primary-type of CONTENT-TYPE."
109 (defsubst mime-content-type-parameter (content-type parameter)
110 "Return PARAMETER value of CONTENT-TYPE."
111 (cdr (assoc parameter (mime-content-type-parameters content-type))))
114 ;;; @ Content-Disposition
117 (defconst mime-disposition-type-regexp mime-token-regexp)
119 (defun mime-parse-Content-Disposition (string)
120 "Parse STRING as field-body of Content-Disposition field."
121 (setq string (std11-unfold-string string))
122 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
123 (let* ((e (match-end 0))
124 (type (downcase (substring string 0 e)))
126 (setq string (substring string e))
127 (while (setq ret (mime-parse-parameter string))
128 (setq dest (cons (car ret) dest)
131 (cons (cons 'type (intern type))
135 (defun mime-read-Content-Disposition ()
136 "Read field-body of Content-Disposition field from current-buffer,
137 and return parsed it."
138 (let ((str (std11-field-body "Content-Disposition")))
140 (mime-parse-Content-Disposition str)
143 (defsubst mime-content-disposition-type (content-disposition)
144 "Return disposition-type of CONTENT-DISPOSITION."
145 (cdr (car content-disposition)))
147 (defsubst mime-content-disposition-parameters (content-disposition)
148 "Return disposition-parameters of CONTENT-DISPOSITION."
149 (cdr content-disposition))
151 (defsubst mime-content-disposition-parameter (content-disposition parameter)
152 "Return PARAMETER value of CONTENT-DISPOSITION."
153 (cdr (assoc parameter (cdr content-disposition))))
155 (defsubst mime-content-disposition-filename (content-disposition)
156 "Return filename of CONTENT-DISPOSITION."
157 (mime-content-disposition-parameter content-disposition "filename"))
160 ;;; @ Content-Transfer-Encoding
163 (defun mime-parse-Content-Transfer-Encoding (string)
164 "Parse STRING as field-body of Content-Transfer-Encoding field."
165 (if (string-match "[ \t\n\r]+$" string)
166 (setq string (match-string 0 string))
170 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
171 "Read field-body of Content-Transfer-Encoding field from
172 current-buffer, and return it.
173 If is is not found, return DEFAULT-ENCODING."
174 (let ((str (std11-field-body "Content-Transfer-Encoding")))
176 (mime-parse-Content-Transfer-Encoding str)
183 (defsubst make-mime-entity (buffer
185 header-start header-end body-start body-end
186 content-type content-disposition
189 node-id header-start header-end body-start body-end
190 content-type content-disposition encoding children))
192 (defsubst mime-entity-buffer (entity) (aref entity 0))
193 (defsubst mime-entity-node-id (entity) (aref entity 1))
194 (defsubst mime-entity-header-start (entity) (aref entity 2))
195 (defsubst mime-entity-header-end (entity) (aref entity 3))
196 (defsubst mime-entity-body-start (entity) (aref entity 4))
197 (defsubst mime-entity-body-end (entity) (aref entity 5))
198 (defsubst mime-entity-content-type (entity) (aref entity 6))
199 (defsubst mime-entity-content-disposition (entity) (aref entity 7))
200 (defsubst mime-entity-encoding (entity) (aref entity 8))
201 (defsubst mime-entity-children (entity) (aref entity 9))
203 (defsubst mime-entity-number (entity)
204 (reverse (mime-entity-node-id entity)))
206 (defalias 'mime-entity-point-min 'mime-entity-header-start)
207 (defalias 'mime-entity-point-max 'mime-entity-body-end)
209 (defsubst mime-entity-media-type (entity)
210 (mime-content-type-primary-type (mime-entity-content-type entity)))
211 (defsubst mime-entity-media-subtype (entity)
212 (mime-content-type-subtype (mime-entity-content-type entity)))
213 (defsubst mime-entity-parameters (entity)
214 (mime-content-type-parameters (mime-entity-content-type entity)))
216 (defsubst mime-entity-type/subtype (entity-info)
217 (mime-type/subtype-string (mime-entity-media-type entity-info)
218 (mime-entity-media-subtype entity-info)))
220 (defun mime-parse-multipart (header-start header-end body-start body-end
221 content-type content-disposition
223 (goto-char (point-min))
224 (let* ((dash-boundary
226 (std11-strip-quoted-string
227 (mime-content-type-parameter content-type "boundary"))))
228 (delimiter (concat "\n" (regexp-quote dash-boundary)))
229 (close-delimiter (concat delimiter "--[ \t]*$"))
230 (rsep (concat delimiter "[ \t]*\n"))
232 (if (eq (mime-content-type-subtype content-type) 'digest)
233 (make-mime-content-type 'message 'rfc822)
234 (make-mime-content-type 'text 'plain)
236 cb ce ret ncb children (i 0))
239 (narrow-to-region header-end
240 (if (re-search-backward close-delimiter nil t)
243 (goto-char header-start)
244 (re-search-forward rsep nil t)
245 (setq cb (match-end 0))
246 (while (re-search-forward rsep nil t)
247 (setq ce (match-beginning 0))
248 (setq ncb (match-end 0))
250 (narrow-to-region cb ce)
251 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
253 (setq children (cons ret children))
254 (goto-char (mime-entity-point-max ret))
255 (goto-char (setq cb ncb))
258 (setq ce (point-max))
260 (narrow-to-region cb ce)
261 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
263 (setq children (cons ret children))
265 (make-mime-entity (current-buffer) node-id
266 header-start header-end body-start body-end
267 content-type content-disposition encoding
271 (defun mime-parse-message (&optional default-ctl default-encoding node-id)
272 "Parse current-buffer as a MIME message.
273 DEFAULT-CTL is used when an entity does not have valid Content-Type
274 field. Its format must be as same as return value of
275 mime-{parse|read}-Content-Type."
276 (let ((header-start (point-min))
279 (body-end (point-max))
280 content-type content-disposition encoding
282 (goto-char header-start)
283 (if (re-search-forward "^$" nil t)
284 (setq header-end (match-end 0)
285 body-start (1+ header-end))
286 (setq header-end (point-min)
287 body-start (point-min))
290 (narrow-to-region header-start header-end)
291 (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
293 (mime-parse-Content-Type str)
296 content-disposition (let ((str (std11-fetch-field
297 "Content-Disposition")))
299 (mime-parse-Content-Disposition str)
301 encoding (let ((str (std11-fetch-field
302 "Content-Transfer-Encoding")))
304 (mime-parse-Content-Transfer-Encoding str)
306 primary-type (mime-content-type-primary-type content-type))
308 (cond ((eq primary-type 'multipart)
309 (mime-parse-multipart header-start header-end
311 content-type content-disposition encoding
314 ((and (eq primary-type 'message)
315 (memq (mime-content-type-subtype content-type)
318 (make-mime-entity (current-buffer) node-id
319 header-start header-end body-start body-end
320 content-type content-disposition encoding
322 (narrow-to-region body-start body-end)
323 (list (mime-parse-message
324 nil nil (cons 0 node-id)))
328 (make-mime-entity (current-buffer) node-id
329 header-start header-end body-start body-end
330 content-type content-disposition encoding nil)
338 (defsubst mime-root-entity-p (entity)
339 "Return t if ENTITY is root-entity (message)."
340 (null (mime-entity-node-id entity)))
346 (provide 'mime-parse)
348 ;;; mime-parse.el ends here