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.
31 (eval-when-compile (require 'cl))
37 (defconst mime/content-parameter-value-regexp
39 std11-quoted-string-regexp
42 (defconst mime::parameter-regexp
43 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
44 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
46 (defun mime-parse-parameter (str)
47 (if (string-match mime::parameter-regexp str)
48 (let ((e (match-end 2)))
50 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
51 (std11-strip-quoted-string
52 (substring str (match-beginning 2) e))
62 (defun mime-parse-Content-Type (string)
63 "Parse STRING as field-body of Content-Type field.
65 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
66 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
68 (setq string (std11-unfold-string string))
69 (if (string-match `,(concat "^\\(" mime-token-regexp
70 "\\)/\\(" mime-token-regexp "\\)") string)
71 (let* ((type (downcase
72 (substring string (match-beginning 1) (match-end 1))))
74 (substring string (match-beginning 2) (match-end 2))))
76 (setq string (substring string (match-end 0)))
77 (while (setq ret (mime-parse-parameter string))
78 (setq dest (cons (car ret) dest)
81 (make-mime-content-type (intern type)(intern subtype)
86 (defun mime-read-Content-Type ()
87 "Read field-body of Content-Type field from current-buffer,
88 and return parsed it. Format of return value is as same as
89 `mime-parse-Content-Type'."
90 (let ((str (std11-field-body "Content-Type")))
92 (mime-parse-Content-Type str)
96 ;;; @ Content-Disposition
99 (defconst mime-disposition-type-regexp mime-token-regexp)
102 (defun mime-parse-Content-Disposition (string)
103 "Parse STRING as field-body of Content-Disposition field."
104 (setq string (std11-unfold-string string))
105 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
106 (let* ((e (match-end 0))
107 (type (downcase (substring string 0 e)))
109 (setq string (substring string e))
110 (while (setq ret (mime-parse-parameter string))
111 (setq dest (cons (car ret) dest)
114 (cons (cons 'type (intern type))
119 (defun mime-read-Content-Disposition ()
120 "Read field-body of Content-Disposition field from current-buffer,
121 and return parsed it."
122 (let ((str (std11-field-body "Content-Disposition")))
124 (mime-parse-Content-Disposition str)
128 ;;; @ Content-Transfer-Encoding
132 (defun mime-parse-Content-Transfer-Encoding (string)
133 "Parse STRING as field-body of Content-Transfer-Encoding field."
134 (if (string-match "[ \t\n\r]+$" string)
135 (setq string (match-string 0 string))
140 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
141 "Read field-body of Content-Transfer-Encoding field from
142 current-buffer, and return it.
143 If is is not found, return DEFAULT-ENCODING."
144 (let ((str (std11-field-body "Content-Transfer-Encoding")))
146 (mime-parse-Content-Transfer-Encoding str)
153 (defun mime-parse-multipart (header-start header-end body-start body-end
154 content-type content-disposition
156 (goto-char (point-min))
157 (let* ((dash-boundary
159 (std11-strip-quoted-string
160 (mime-content-type-parameter content-type "boundary"))))
161 (delimiter (concat "\n" (regexp-quote dash-boundary)))
162 (close-delimiter (concat delimiter "--[ \t]*$"))
163 (rsep (concat delimiter "[ \t]*\n"))
165 (if (eq (mime-content-type-subtype content-type) 'digest)
166 (make-mime-content-type 'message 'rfc822)
167 (make-mime-content-type 'text 'plain)
169 cb ce ret ncb children (i 0))
172 (narrow-to-region header-end
173 (if (re-search-backward close-delimiter nil t)
176 (goto-char header-end)
177 (re-search-forward rsep nil t)
178 (setq cb (match-end 0))
179 (while (re-search-forward rsep nil t)
180 (setq ce (match-beginning 0))
181 (setq ncb (match-end 0))
183 (narrow-to-region cb ce)
184 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
186 (setq children (cons ret children))
187 (goto-char (setq cb ncb))
190 (setq ce (point-max))
192 (narrow-to-region cb ce)
193 (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
195 (setq children (cons ret children))
197 (make-mime-entity (current-buffer)
198 header-start header-end body-start body-end
199 node-id content-type content-disposition encoding
204 (defun mime-parse-message (&optional default-ctl default-encoding node-id)
205 "Parse current-buffer as a MIME message.
206 DEFAULT-CTL is used when an entity does not have valid Content-Type
207 field. Its format must be as same as return value of
208 mime-{parse|read}-Content-Type."
209 (let ((header-start (point-min))
212 (body-end (point-max))
213 content-type content-disposition encoding
215 (goto-char header-start)
216 (if (re-search-forward "^$" nil t)
217 (setq header-end (match-end 0)
218 body-start (1+ header-end))
219 (setq header-end (point-min)
220 body-start (point-min))
223 (narrow-to-region header-start header-end)
224 (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
226 (mime-parse-Content-Type str)
229 content-disposition (let ((str (std11-fetch-field
230 "Content-Disposition")))
232 (mime-parse-Content-Disposition str)
234 encoding (let ((str (std11-fetch-field
235 "Content-Transfer-Encoding")))
237 (mime-parse-Content-Transfer-Encoding str)
239 primary-type (mime-content-type-primary-type content-type))
241 (cond ((eq primary-type 'multipart)
242 (mime-parse-multipart header-start header-end
244 content-type content-disposition encoding
247 ((and (eq primary-type 'message)
248 (memq (mime-content-type-subtype content-type)
249 '(rfc822 news external-body)
251 (make-mime-entity (current-buffer)
252 header-start header-end body-start body-end
254 content-type content-disposition encoding
256 (narrow-to-region body-start body-end)
257 (list (mime-parse-message
258 nil nil (cons 0 node-id)))
262 (make-mime-entity (current-buffer)
263 header-start header-end body-start body-end
265 content-type content-disposition encoding nil)
274 (defun mime-parse-buffer (&optional buffer)
275 "Parse BUFFER as a MIME message.
276 If buffer is omitted, it parses current-buffer."
278 (if buffer (set-buffer buffer))
279 (setq mime-message-structure (mime-parse-message))
286 (provide 'mime-parse)
288 ;;; mime-parse.el ends here