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."
126 ;;; @ Content-Disposition
129 (defconst mime-disposition-type-regexp mime-token-regexp)
131 (defun mime-parse-Content-Disposition (string)
132 "Parse STRING as field-body of Content-Disposition field."
133 (setq string (std11-unfold-string string))
134 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
135 (let* ((e (match-end 0))
136 (type (downcase (substring string 0 e)))
138 (setq string (substring string e))
139 (while (setq ret (mime-parse-parameter string))
140 (setq dest (cons (car ret) dest)
143 (cons (cons 'type (intern type))
147 (defun mime-read-Content-Disposition ()
148 "Read field-body of Content-Disposition field from current-buffer,
149 and return parsed it."
150 (let ((str (std11-field-body "Content-Disposition")))
152 (mime-parse-Content-Disposition str)
155 (defsubst mime-content-disposition-type (content-disposition)
156 "Return disposition-type of CONTENT-DISPOSITION."
157 (cdr (car content-disposition)))
159 (defsubst mime-content-disposition-parameters (content-disposition)
160 "Return disposition-parameters of CONTENT-DISPOSITION."
161 (cdr content-disposition))
164 ;;; @ Content-Transfer-Encoding
167 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
168 "Read field-body of Content-Transfer-Encoding field from
169 current-buffer, and return it.
170 If is is not found, return DEFAULT-ENCODING."
171 (let ((str (std11-field-body "Content-Transfer-Encoding")))
174 (if (string-match "[ \t\n\r]+$" str)
175 (setq str (substring str 0 (match-beginning 0)))
185 (defsubst make-mime-entity (node-id
187 media-type media-subtype parameters
189 (vector node-id point-min point-max
190 media-type media-subtype parameters encoding children))
192 (defsubst mime-entity-node-id (entity-info) (aref entity-info 0))
193 (defsubst mime-entity-point-min (entity-info) (aref entity-info 1))
194 (defsubst mime-entity-point-max (entity-info) (aref entity-info 2))
195 (defsubst mime-entity-media-type (entity-info) (aref entity-info 3))
196 (defsubst mime-entity-media-subtype (entity-info) (aref entity-info 4))
197 (defsubst mime-entity-parameters (entity-info) (aref entity-info 5))
198 (defsubst mime-entity-encoding (entity-info) (aref entity-info 6))
199 (defsubst mime-entity-children (entity-info) (aref entity-info 7))
201 (defsubst mime-entity-type/subtype (entity-info)
202 (mime-type/subtype-string (mime-entity-media-type entity-info)
203 (mime-entity-media-subtype entity-info)))
205 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
206 (goto-char (point-min))
207 (let* ((dash-boundary (concat "--" boundary))
208 (delimiter (concat "\n" (regexp-quote dash-boundary)))
209 (close-delimiter (concat delimiter "--[ \t]*$"))
212 (goto-char (point-max))
213 (if (re-search-backward close-delimiter nil t)
217 (rsep (concat delimiter "[ \t]*\n"))
219 (if (eq subtype 'digest)
220 (make-mime-content-type 'message 'rfc822)
221 (make-mime-content-type 'text 'plain)
223 cb ce ret ncb children (i 0))
225 (narrow-to-region beg end)
227 (re-search-forward rsep nil t)
228 (setq cb (match-end 0))
229 (while (re-search-forward rsep nil t)
230 (setq ce (match-beginning 0))
231 (setq ncb (match-end 0))
233 (narrow-to-region cb ce)
234 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
236 (setq children (cons ret children))
237 (goto-char (mime-entity-point-max ret))
238 (goto-char (setq cb ncb))
241 (setq ce (point-max))
243 (narrow-to-region cb ce)
244 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
246 (setq children (cons ret children))
248 (make-mime-entity rcnum beg (point-max)
249 primtype subtype params encoding
253 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
254 "Parse current-buffer as a MIME message.
255 DEFAULT-CTL is used when an entity does not have valid Content-Type
256 field. Its format must be as same as return value of
257 mime-{parse|read}-Content-Type."
258 (setq default-ctl (or (mime-read-Content-Type) default-ctl))
259 (let ((primtype (mime-content-type-primary-type default-ctl))
260 (subtype (mime-content-type-subtype default-ctl))
261 (params (mime-content-type-parameters default-ctl))
262 (encoding (mime-read-Content-Transfer-Encoding default-encoding)))
263 (let ((boundary (assoc "boundary" params)))
265 (setq boundary (std11-strip-quoted-string (cdr boundary)))
266 (mime-parse-multipart
268 primtype subtype params encoding rcnum)
270 ((and (eq primtype 'message)
271 (memq subtype '(rfc822 news))
273 (goto-char (point-min))
274 (make-mime-entity rcnum (point-min) (point-max)
275 primtype subtype params encoding
278 (if (re-search-forward "^$" nil t)
283 (list (mime-parse-message
284 nil nil (cons 0 rcnum)))
288 (make-mime-entity rcnum (point-min) (point-max)
289 primtype subtype params encoding
298 (defsubst mime-root-entity-p (entity)
299 "Return t if ENTITY is root-entity (message)."
300 (null (mime-entity-node-id entity)))
306 (provide 'mime-parse)
308 ;;; mime-parse.el ends here