1 ;;; mime-parse.el --- MIME message parser
3 ;; Copyright (C) 1994,1995,1996,1997,1998,1999 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
100 (defconst mime-disposition-type-regexp mime-token-regexp)
104 (defun mime-parse-Content-Disposition (string)
105 "Parse STRING as field-body of Content-Disposition field."
106 (setq string (std11-unfold-string string))
107 (if (string-match (eval-when-compile
108 (concat "^" mime-disposition-type-regexp)) string)
109 (let* ((e (match-end 0))
110 (type (downcase (substring string 0 e)))
112 (setq string (substring string e))
113 (while (setq ret (mime-parse-parameter string))
114 (setq dest (cons (car ret) dest)
117 (cons (cons 'type (intern type))
122 (defun mime-read-Content-Disposition ()
123 "Read field-body of Content-Disposition field from current-buffer,
124 and return parsed it."
125 (let ((str (std11-field-body "Content-Disposition")))
127 (mime-parse-Content-Disposition str)
131 ;;; @ Content-Transfer-Encoding
135 (defun mime-parse-Content-Transfer-Encoding (string)
136 "Parse STRING as field-body of Content-Transfer-Encoding field."
137 (let ((tokens (std11-lexical-analyze string))
140 (setq token (car tokens))
141 (std11-ignored-token-p token))
142 (setq tokens (cdr tokens)))
144 (if (eq (car token) 'atom)
145 (downcase (cdr token))
149 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
150 "Read field-body of Content-Transfer-Encoding field from
151 current-buffer, and return it.
152 If is is not found, return DEFAULT-ENCODING."
153 (let ((str (std11-field-body "Content-Transfer-Encoding")))
155 (mime-parse-Content-Transfer-Encoding str)
162 (defun mime-parse-multipart (entity)
163 (goto-char (point-min))
164 (let* ((representation-type
165 (mime-entity-representation-type-internal entity))
166 (content-type (mime-entity-content-type-internal entity))
168 (concat "--" (mime-content-type-parameter content-type "boundary")))
169 (delimiter (concat "\n" (regexp-quote dash-boundary)))
170 (close-delimiter (concat delimiter "--[ \t]*$"))
171 (rsep (concat delimiter "[ \t]*\n"))
173 (if (eq (mime-content-type-subtype content-type) 'digest)
174 (make-mime-content-type 'message 'rfc822)
175 (make-mime-content-type 'text 'plain)
177 (header-end (mime-entity-header-end-internal entity))
178 (body-end (mime-entity-body-end-internal entity)))
181 (narrow-to-region header-end
182 (if (re-search-backward close-delimiter nil t)
185 (goto-char header-end)
186 (if (re-search-forward rsep nil t)
187 (let ((cb (match-end 0))
189 (node-id (mime-entity-node-id-internal entity))
191 (while (re-search-forward rsep nil t)
192 (setq ce (match-beginning 0))
193 (setq ncb (match-end 0))
195 (narrow-to-region cb ce)
196 (setq ret (mime-parse-message representation-type dc-ctl
197 entity (cons i node-id)))
199 (setq children (cons ret children))
200 (goto-char (setq cb ncb))
203 (setq ce (point-max))
205 (narrow-to-region cb ce)
206 (setq ret (mime-parse-message representation-type dc-ctl
207 entity (cons i node-id)))
209 (setq children (cons ret children))
210 (mime-entity-set-children-internal entity (nreverse children))
212 (mime-entity-set-content-type-internal
213 entity (make-mime-content-type 'message 'x-broken))
217 (defun mime-parse-encapsulated (entity)
218 (mime-entity-set-children-internal
221 (narrow-to-region (mime-entity-body-start-internal entity)
222 (mime-entity-body-end-internal entity))
223 (list (mime-parse-message
224 (mime-entity-representation-type-internal entity) nil
225 entity (cons 0 (mime-entity-node-id-internal entity))))
228 (defun mime-parse-message (representation-type &optional default-ctl
230 (let ((header-start (point-min))
233 (body-end (point-max))
235 (goto-char header-start)
236 (if (re-search-forward "^$" nil t)
237 (setq header-end (match-end 0)
238 body-start (if (= header-end body-end)
241 (setq header-end (point-min)
242 body-start (point-min)))
244 (narrow-to-region header-start header-end)
245 (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
247 (mime-parse-Content-Type str)
251 (make-mime-entity-internal representation-type
253 content-type nil parent node-id
258 header-start header-end
267 (defun mime-parse-buffer (&optional buffer representation-type)
268 "Parse BUFFER as a MIME message.
269 If buffer is omitted, it parses current-buffer."
271 (if buffer (set-buffer buffer))
272 (setq mime-message-structure
273 (mime-parse-message (or representation-type 'buffer) nil))
280 (provide 'mime-parse)
282 ;;; mime-parse.el ends here