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 (node-id header-start header-end
185 content-type content-disposition
188 header-start header-end body-start body-end
189 content-type content-disposition encoding children))
191 (defsubst mime-entity-node-id (entity) (aref entity 0))
192 (defsubst mime-entity-header-start (entity) (aref entity 1))
193 (defsubst mime-entity-header-end (entity) (aref entity 2))
194 (defsubst mime-entity-body-start (entity) (aref entity 3))
195 (defsubst mime-entity-body-end (entity) (aref entity 4))
196 (defsubst mime-entity-content-type (entity) (aref entity 5))
197 (defsubst mime-entity-content-disposition (entity) (aref entity 6))
198 (defsubst mime-entity-encoding (entity) (aref entity 7))
199 (defsubst mime-entity-children (entity) (aref entity 8))
201 (defalias 'mime-entity-point-min 'mime-entity-header-start)
202 (defalias 'mime-entity-point-max 'mime-entity-body-end)
203 (defsubst mime-entity-media-type (entity)
204 (mime-content-type-primary-type (mime-entity-content-type entity)))
205 (defsubst mime-entity-media-subtype (entity)
206 (mime-content-type-subtype (mime-entity-content-type entity)))
207 (defsubst mime-entity-parameters (entity)
208 (mime-content-type-parameters (mime-entity-content-type entity)))
209 (defsubst mime-entity-type/subtype (entity-info)
210 (mime-type/subtype-string (mime-entity-media-type entity-info)
211 (mime-entity-media-subtype entity-info)))
213 (defun mime-parse-multipart (header-start header-end body-start body-end
214 content-type content-disposition
216 (goto-char (point-min))
217 (let* ((dash-boundary
219 (std11-strip-quoted-string
220 (mime-content-type-parameter content-type "boundary"))))
221 (delimiter (concat "\n" (regexp-quote dash-boundary)))
222 (close-delimiter (concat delimiter "--[ \t]*$"))
223 (rsep (concat delimiter "[ \t]*\n"))
225 (if (eq (mime-content-type-subtype content-type) 'digest)
226 (make-mime-content-type 'message 'rfc822)
227 (make-mime-content-type 'text 'plain)
229 cb ce ret ncb children (i 0))
231 (if (re-search-backward close-delimiter nil t)
232 (setq body-end (match-beginning 0))
235 (narrow-to-region header-end body-end)
236 (goto-char header-start)
237 (re-search-forward rsep nil t)
238 (setq cb (match-end 0))
239 (while (re-search-forward rsep nil t)
240 (setq ce (match-beginning 0))
241 (setq ncb (match-end 0))
243 (narrow-to-region cb ce)
244 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
246 (setq children (cons ret children))
247 (goto-char (mime-entity-point-max ret))
248 (goto-char (setq cb ncb))
251 (setq ce (point-max))
253 (narrow-to-region cb ce)
254 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
256 (setq children (cons ret children))
258 (make-mime-entity node-id
259 header-start header-end
261 content-type content-disposition encoding
265 (defun mime-parse-message (&optional default-ctl default-encoding node-id)
266 "Parse current-buffer as a MIME message.
267 DEFAULT-CTL is used when an entity does not have valid Content-Type
268 field. Its format must be as same as return value of
269 mime-{parse|read}-Content-Type."
270 (let ((header-start (point-min))
273 (body-end (point-max))
274 content-type content-disposition encoding
276 (goto-char header-start)
277 (if (re-search-forward "^$" nil t)
278 (setq header-end (match-end 0)
279 body-start (1+ header-end))
280 (setq header-end (point-min)
281 body-start (point-min))
284 (narrow-to-region header-start header-end)
285 (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
287 (mime-parse-Content-Type str)
290 content-disposition (let ((str (std11-fetch-field
291 "Content-Disposition")))
293 (mime-parse-Content-Disposition str)
295 encoding (let ((str (std11-fetch-field
296 "Content-Transfer-Encoding")))
298 (mime-parse-Content-Transfer-Encoding str)
300 primary-type (mime-content-type-primary-type content-type))
302 (cond ((eq primary-type 'multipart)
303 (mime-parse-multipart header-start header-end
305 content-type content-disposition encoding
308 ((and (eq primary-type 'message)
309 (memq (mime-content-type-subtype content-type)
312 (make-mime-entity node-id
313 header-start header-end
315 content-type content-disposition encoding
317 (narrow-to-region body-start body-end)
318 (list (mime-parse-message
319 nil nil (cons 0 node-id)))
323 (make-mime-entity node-id
324 header-start header-end
326 content-type content-disposition encoding nil)
334 (defsubst mime-root-entity-p (entity)
335 "Return t if ENTITY is root-entity (message)."
336 (null (mime-entity-node-id entity)))
342 (provide 'mime-parse)
344 ;;; mime-parse.el ends here