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))
71 (defun mime-parse-Content-Type (string)
72 "Parse STRING as field-body of Content-Type field.
74 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
75 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
77 (setq string (std11-unfold-string string))
78 (if (string-match `,(concat "^\\(" mime-token-regexp
79 "\\)/\\(" mime-token-regexp "\\)") string)
80 (let* ((type (downcase
81 (substring string (match-beginning 1) (match-end 1))))
83 (substring string (match-beginning 2) (match-end 2))))
85 (setq string (substring string (match-end 0)))
86 (while (setq ret (mime-parse-parameter string))
87 (setq dest (cons (car ret) dest)
90 (cons (intern type) (cons (intern subtype) (nreverse dest)))
94 (defconst mime-disposition-type-regexp mime-token-regexp)
96 (defun mime-parse-Content-Disposition (string)
97 "Parse STRING as field-body of Content-Disposition field."
98 (setq string (std11-unfold-string string))
99 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
100 (let* ((e (match-end 0))
101 (ctype (downcase (substring string 0 e)))
103 (setq string (substring string e))
104 (while (setq ret (mime-parse-parameter string))
105 (setq dest (cons (car ret) dest)
108 (cons ctype (nreverse dest))
115 (defun mime-read-Content-Type ()
116 "Read field-body of Content-Type field from current-buffer,
117 and return parsed it. Format of return value is as same as
118 `mime-parse-Content-Type'."
119 (let ((str (std11-field-body "Content-Type")))
121 (mime-parse-Content-Type str)
124 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
125 "Read field-body of Content-Transfer-Encoding field from
126 current-buffer, and return it.
127 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
128 (let ((str (std11-field-body "Content-Transfer-Encoding")))
131 (if (string-match "[ \t\n\r]+$" str)
132 (setq str (substring str 0 (match-beginning 0)))
139 (defun mime/Content-Disposition ()
140 "Read field-body of Content-Disposition field from current-buffer,
141 and return parsed it. [mime-parse.el]"
142 (let ((str (std11-field-body "Content-Disposition")))
144 (mime-parse-Content-Disposition str)
151 (defsubst make-mime-entity (node-id
153 media-type media-subtype parameters
155 (vector node-id point-min point-max
156 media-type media-subtype parameters encoding children))
158 (defsubst mime-entity-node-id (entity-info) (aref entity-info 0))
159 (defsubst mime-entity-point-min (entity-info) (aref entity-info 1))
160 (defsubst mime-entity-point-max (entity-info) (aref entity-info 2))
161 (defsubst mime-entity-media-type (entity-info) (aref entity-info 3))
162 (defsubst mime-entity-media-subtype (entity-info) (aref entity-info 4))
163 (defsubst mime-entity-parameters (entity-info) (aref entity-info 5))
164 (defsubst mime-entity-encoding (entity-info) (aref entity-info 6))
165 (defsubst mime-entity-children (entity-info) (aref entity-info 7))
167 (defsubst mime-entity-type/subtype (entity-info)
168 (mime-type/subtype-string (mime-entity-media-type entity-info)
169 (mime-entity-media-subtype entity-info)))
171 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
172 (goto-char (point-min))
173 (let* ((dash-boundary (concat "--" boundary))
174 (delimiter (concat "\n" (regexp-quote dash-boundary)))
175 (close-delimiter (concat delimiter "--[ \t]*$"))
178 (goto-char (point-max))
179 (if (re-search-backward close-delimiter nil t)
183 (rsep (concat delimiter "[ \t]*\n"))
185 (if (eq subtype 'digest)
189 cb ce ret ncb children (i 0))
191 (narrow-to-region beg end)
193 (re-search-forward rsep nil t)
194 (setq cb (match-end 0))
195 (while (re-search-forward rsep nil t)
196 (setq ce (match-beginning 0))
197 (setq ncb (match-end 0))
199 (narrow-to-region cb ce)
200 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
202 (setq children (cons ret children))
203 (goto-char (mime-entity-point-max ret))
204 (goto-char (setq cb ncb))
207 (setq ce (point-max))
209 (narrow-to-region cb ce)
210 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
212 (setq children (cons ret children))
214 (make-mime-entity rcnum beg (point-max)
215 primtype subtype params encoding
219 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
220 "Parse current-buffer as a MIME message.
221 DEFAULT-CTL is used when an entity does not have valid Content-Type
222 field. Its format must be as same as return value of
223 mime-{parse|read}-Content-Type."
224 (setq default-ctl (or (mime-read-Content-Type) default-ctl))
225 (let ((primtype (car default-ctl))
226 (subtype (car (cdr default-ctl)))
227 (params (cdr (cdr default-ctl)))
228 (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
230 (let ((boundary (assoc "boundary" params)))
232 (setq boundary (std11-strip-quoted-string (cdr boundary)))
233 (mime-parse-multipart
235 primtype subtype params encoding rcnum)
237 ((and (eq primtype 'message)
238 (memq subtype '(rfc822 news))
240 (goto-char (point-min))
241 (make-mime-entity rcnum (point-min) (point-max)
242 primtype subtype params encoding
245 (if (re-search-forward "^$" nil t)
250 (list (mime-parse-message
251 nil nil (cons 0 rcnum)))
255 (make-mime-entity rcnum (point-min) (point-max)
256 primtype subtype params encoding
265 (defsubst mime-root-entity-p (entity)
266 "Return t if ENTITY is root-entity (message)."
267 (null (mime-entity-node-id entity)))
273 (provide 'mime-parse)
275 ;;; mime-parse.el ends here