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 (defsubst regexp-* (regexp)
38 (defsubst regexp-or (&rest args)
39 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
41 (defconst rfc822/quoted-pair-regexp "\\\\.")
42 (defconst rfc822/qtext-regexp
43 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
44 (defconst rfc822/quoted-string-regexp
47 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
51 (defconst mime/content-parameter-value-regexp
53 rfc822/quoted-string-regexp
56 (defconst mime::parameter-regexp
57 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
58 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
60 (defun mime-parse-parameter (str)
61 (if (string-match mime::parameter-regexp str)
62 (let ((e (match-end 2)))
64 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
65 (std11-strip-quoted-string
66 (substring str (match-beginning 2) e))
75 (defsubst make-mime-content-type (type subtype &optional parameters)
76 (list* (cons 'type type)
77 (cons 'subtype subtype)
78 (nreverse parameters))
81 (defun mime-parse-Content-Type (string)
82 "Parse STRING as field-body of Content-Type field.
84 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
85 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
87 (setq string (std11-unfold-string string))
88 (if (string-match `,(concat "^\\(" mime-token-regexp
89 "\\)/\\(" mime-token-regexp "\\)") string)
90 (let* ((type (downcase
91 (substring string (match-beginning 1) (match-end 1))))
93 (substring string (match-beginning 2) (match-end 2))))
95 (setq string (substring string (match-end 0)))
96 (while (setq ret (mime-parse-parameter string))
97 (setq dest (cons (car ret) dest)
100 (make-mime-content-type (intern type)(intern subtype)
104 (defun mime-read-Content-Type ()
105 "Read field-body of Content-Type field from current-buffer,
106 and return parsed it. Format of return value is as same as
107 `mime-parse-Content-Type'."
108 (let ((str (std11-field-body "Content-Type")))
110 (mime-parse-Content-Type str)
113 (defsubst mime-content-type-primary-type (content-type)
114 "Return primary-type of CONTENT-TYPE."
115 (cdr (car content-type)))
117 (defsubst mime-content-type-subtype (content-type)
118 "Return primary-type of CONTENT-TYPE."
119 (cdr (cadr content-type)))
121 (defsubst mime-content-type-parameters (content-type)
122 "Return primary-type of CONTENT-TYPE."
125 (defsubst mime-content-type-parameter (content-type parameter)
126 "Return PARAMETER value of CONTENT-TYPE."
127 (cdr (assoc parameter (mime-content-type-parameters content-type))))
130 ;;; @ Content-Disposition
133 (defconst mime-disposition-type-regexp mime-token-regexp)
135 (defun mime-parse-Content-Disposition (string)
136 "Parse STRING as field-body of Content-Disposition field."
137 (setq string (std11-unfold-string string))
138 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
139 (let* ((e (match-end 0))
140 (type (downcase (substring string 0 e)))
142 (setq string (substring string e))
143 (while (setq ret (mime-parse-parameter string))
144 (setq dest (cons (car ret) dest)
147 (cons (cons 'type (intern type))
151 (defun mime-read-Content-Disposition ()
152 "Read field-body of Content-Disposition field from current-buffer,
153 and return parsed it."
154 (let ((str (std11-field-body "Content-Disposition")))
156 (mime-parse-Content-Disposition str)
159 (defsubst mime-content-disposition-type (content-disposition)
160 "Return disposition-type of CONTENT-DISPOSITION."
161 (cdr (car content-disposition)))
163 (defsubst mime-content-disposition-parameters (content-disposition)
164 "Return disposition-parameters of CONTENT-DISPOSITION."
165 (cdr content-disposition))
167 (defsubst mime-content-disposition-parameter (content-disposition parameter)
168 "Return PARAMETER value of CONTENT-DISPOSITION."
169 (cdr (assoc parameter (cdr content-disposition))))
171 (defsubst mime-content-disposition-filename (content-disposition)
172 "Return filename of CONTENT-DISPOSITION."
173 (mime-content-disposition-parameter content-disposition "filename"))
176 ;;; @ Content-Transfer-Encoding
179 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
180 "Read field-body of Content-Transfer-Encoding field from
181 current-buffer, and return it.
182 If is is not found, return DEFAULT-ENCODING."
183 (let ((str (std11-field-body "Content-Transfer-Encoding")))
186 (if (string-match "[ \t\n\r]+$" str)
187 (setq str (substring str 0 (match-beginning 0)))
197 (defsubst make-mime-entity (node-id
199 content-type content-disposition encoding
201 (vector node-id point-min point-max
202 content-type content-disposition encoding children))
204 (defsubst mime-entity-node-id (entity) (aref entity 0))
205 (defsubst mime-entity-point-min (entity) (aref entity 1))
206 (defsubst mime-entity-point-max (entity) (aref entity 2))
207 (defsubst mime-entity-content-type (entity) (aref entity 3))
208 (defsubst mime-entity-content-disposition (entity) (aref entity 4))
209 (defsubst mime-entity-encoding (entity) (aref entity 5))
210 (defsubst mime-entity-children (entity) (aref entity 6))
212 (defsubst mime-entity-media-type (entity)
213 (mime-content-type-primary-type (mime-entity-content-type entity)))
214 (defsubst mime-entity-media-subtype (entity)
215 (mime-content-type-subtype (mime-entity-content-type entity)))
216 (defsubst mime-entity-parameters (entity)
217 (mime-content-type-parameters (mime-entity-content-type entity)))
218 (defsubst mime-entity-type/subtype (entity-info)
219 (mime-type/subtype-string (mime-entity-media-type entity-info)
220 (mime-entity-media-subtype entity-info)))
222 (defun mime-parse-multipart (content-type content-disposition encoding node-id)
223 (goto-char (point-min))
224 (let* ((dash-boundary
226 (std11-strip-quoted-string
227 (cdr (assoc "boundary"
228 (mime-content-type-parameters content-type))))))
229 (delimiter (concat "\n" (regexp-quote dash-boundary)))
230 (close-delimiter (concat delimiter "--[ \t]*$"))
233 (goto-char (point-max))
234 (if (re-search-backward close-delimiter nil t)
238 (rsep (concat delimiter "[ \t]*\n"))
240 (if (eq (mime-content-type-subtype content-type) 'digest)
241 (make-mime-content-type 'message 'rfc822)
242 (make-mime-content-type 'text 'plain)
244 cb ce ret ncb children (i 0))
246 (narrow-to-region beg end)
248 (re-search-forward rsep nil t)
249 (setq cb (match-end 0))
250 (while (re-search-forward rsep nil t)
251 (setq ce (match-beginning 0))
252 (setq ncb (match-end 0))
254 (narrow-to-region cb ce)
255 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
257 (setq children (cons ret children))
258 (goto-char (mime-entity-point-max ret))
259 (goto-char (setq cb ncb))
262 (setq ce (point-max))
264 (narrow-to-region cb ce)
265 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
267 (setq children (cons ret children))
269 (make-mime-entity node-id beg (point-max)
270 content-type content-disposition encoding
274 (defun mime-parse-message (&optional default-ctl default-encoding node-id)
275 "Parse current-buffer as a MIME message.
276 DEFAULT-CTL is used when an entity does not have valid Content-Type
277 field. Its format must be as same as return value of
278 mime-{parse|read}-Content-Type."
279 (let* ((content-type (or (mime-read-Content-Type) default-ctl))
280 (content-disposition (mime-read-Content-Disposition))
281 (primary-type (mime-content-type-primary-type content-type))
282 (encoding (mime-read-Content-Transfer-Encoding default-encoding)))
283 (cond ((eq primary-type 'multipart)
284 (mime-parse-multipart content-type content-disposition encoding
287 ((and (eq primary-type 'message)
288 (memq (mime-content-type-subtype content-type)
291 (goto-char (point-min))
292 (make-mime-entity node-id (point-min) (point-max)
293 content-type content-disposition encoding
296 (if (re-search-forward "^$" nil t)
301 (list (mime-parse-message
302 nil nil (cons 0 node-id)))
306 (make-mime-entity node-id (point-min) (point-max)
307 content-type content-disposition encoding nil)
315 (defsubst mime-root-entity-p (entity)
316 "Return t if ENTITY is root-entity (message)."
317 (null (mime-entity-node-id entity)))
323 (provide 'mime-parse)
325 ;;; mime-parse.el ends here