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))
34 ;;; @ lexical analyzer
37 (defcustom mime-lexical-analyzer
38 '(std11-analyze-quoted-string
39 std11-analyze-domain-literal
44 "*List of functions to return result of lexical analyze.
45 Each function must have two arguments: STRING and START.
46 STRING is the target string to be analyzed.
47 START is start position of STRING to analyze.
49 Previous function is preferred to next function. If a function
50 returns nil, next function is used. Otherwise the return value will
53 :type '(repeat function))
55 (defun mime-analyze-tspecial (string start)
56 (if (and (> (length string) start)
57 (memq (aref string start) mime-tspecial-char-list))
58 (cons (cons 'tpecials (substring string start (1+ start)))
62 (defun mime-analyze-token (string start)
63 (if (and (string-match mime-token-regexp string start)
64 (= (match-beginning 0) start))
65 (let ((end (match-end 0)))
66 (cons (cons 'mime-token (substring string start end))
67 ;;(substring string end)
75 (defconst mime/content-parameter-value-regexp
77 std11-quoted-string-regexp
80 (defconst mime::parameter-regexp
81 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
82 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
84 (defun mime-parse-parameter (str)
85 (if (string-match mime::parameter-regexp str)
86 (let ((e (match-end 2)))
88 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
89 (std11-strip-quoted-string
90 (substring str (match-beginning 2) e))
100 (defun mime-parse-Content-Type (string)
101 "Parse STRING as field-body of Content-Type field.
103 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
104 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
106 (setq string (std11-unfold-string string))
107 (if (string-match `,(concat "^\\(" mime-token-regexp
108 "\\)/\\(" mime-token-regexp "\\)") string)
109 (let* ((type (downcase
110 (substring string (match-beginning 1) (match-end 1))))
112 (substring string (match-beginning 2) (match-end 2))))
114 (setq string (substring string (match-end 0)))
115 (while (setq ret (mime-parse-parameter string))
116 (setq dest (cons (car ret) dest)
119 (make-mime-content-type (intern type)(intern subtype)
124 (defun mime-read-Content-Type ()
125 "Read field-body of Content-Type field from current-buffer,
126 and return parsed it. Format of return value is as same as
127 `mime-parse-Content-Type'."
128 (let ((str (std11-field-body "Content-Type")))
130 (mime-parse-Content-Type str)
134 ;;; @ Content-Disposition
138 (defconst mime-disposition-type-regexp mime-token-regexp)
142 (defun mime-parse-Content-Disposition (string)
143 "Parse STRING as field-body of Content-Disposition field."
144 (setq string (std11-unfold-string string))
145 (if (string-match (eval-when-compile
146 (concat "^" mime-disposition-type-regexp)) string)
147 (let* ((e (match-end 0))
148 (type (downcase (substring string 0 e)))
150 (setq string (substring string e))
151 (while (setq ret (mime-parse-parameter string))
152 (setq dest (cons (car ret) dest)
155 (cons (cons 'type (intern type))
160 (defun mime-read-Content-Disposition ()
161 "Read field-body of Content-Disposition field from current-buffer,
162 and return parsed it."
163 (let ((str (std11-field-body "Content-Disposition")))
165 (mime-parse-Content-Disposition str)
169 ;;; @ Content-Transfer-Encoding
173 (defun mime-parse-Content-Transfer-Encoding (string)
174 "Parse STRING as field-body of Content-Transfer-Encoding field."
175 (let ((tokens (std11-lexical-analyze string mime-lexical-analyzer))
178 (setq token (car tokens))
179 (std11-ignored-token-p token))
180 (setq tokens (cdr tokens)))
182 (if (eq (car token) 'mime-token)
183 (downcase (cdr token))
187 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
188 "Read field-body of Content-Transfer-Encoding field from
189 current-buffer, and return it.
190 If is is not found, return DEFAULT-ENCODING."
191 (let ((str (std11-field-body "Content-Transfer-Encoding")))
193 (mime-parse-Content-Transfer-Encoding str)
200 (defun mime-parse-multipart (entity)
201 (goto-char (point-min))
202 (let* ((representation-type
203 (mime-entity-representation-type-internal entity))
204 (content-type (mime-entity-content-type-internal entity))
206 (concat "--" (mime-content-type-parameter content-type "boundary")))
207 (delimiter (concat "\n" (regexp-quote dash-boundary)))
208 (close-delimiter (concat delimiter "--[ \t]*$"))
209 (rsep (concat delimiter "[ \t]*\n"))
211 (if (eq (mime-content-type-subtype content-type) 'digest)
212 (make-mime-content-type 'message 'rfc822)
213 (make-mime-content-type 'text 'plain)
215 (header-end (mime-entity-header-end-internal entity))
216 (body-end (mime-entity-body-end-internal entity)))
219 (narrow-to-region header-end
220 (if (re-search-backward close-delimiter nil t)
223 (goto-char header-end)
224 (if (re-search-forward rsep nil t)
225 (let ((cb (match-end 0))
227 (node-id (mime-entity-node-id-internal entity))
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 representation-type dc-ctl
235 entity (cons i node-id)))
237 (setq children (cons ret children))
238 (goto-char (setq cb ncb))
241 (setq ce (point-max))
243 (narrow-to-region cb ce)
244 (setq ret (mime-parse-message representation-type dc-ctl
245 entity (cons i node-id)))
247 (setq children (cons ret children))
248 (mime-entity-set-children-internal entity (nreverse children))
250 (mime-entity-set-content-type-internal
251 entity (make-mime-content-type 'message 'x-broken))
255 (defun mime-parse-encapsulated (entity)
256 (mime-entity-set-children-internal
259 (narrow-to-region (mime-entity-body-start-internal entity)
260 (mime-entity-body-end-internal entity))
261 (list (mime-parse-message
262 (mime-entity-representation-type-internal entity) nil
263 entity (cons 0 (mime-entity-node-id-internal entity))))
266 (defun mime-parse-message (representation-type &optional default-ctl
268 (let ((header-start (point-min))
271 (body-end (point-max))
273 (goto-char header-start)
274 (if (re-search-forward "^$" nil t)
275 (setq header-end (match-end 0)
276 body-start (if (= header-end body-end)
279 (setq header-end (point-min)
280 body-start (point-min)))
282 (narrow-to-region header-start header-end)
283 (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
285 (mime-parse-Content-Type str)
289 (make-mime-entity-internal representation-type
291 content-type nil parent node-id
296 header-start header-end
305 (defun mime-parse-buffer (&optional buffer representation-type)
306 "Parse BUFFER as a MIME message.
307 If buffer is omitted, it parses current-buffer."
309 (if buffer (set-buffer buffer))
310 (setq mime-message-structure
311 (mime-parse-message (or representation-type 'buffer) nil))
318 (provide 'mime-parse)
320 ;;; mime-parse.el ends here