1 ;;; mime-parse.el --- MIME message parser
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: mime-parse.el,v 0.20 1997-09-05 11:14:44 morioka Exp $
7 ;; Keywords: parse, MIME, multimedia, mail, news
9 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
32 (defsubst symbol-concat (&rest args)
33 "Return a symbol whose name is concatenation of arguments ARGS
34 which are string or symbol."
35 (intern (mapconcat (function
37 (cond ((symbolp s) (symbol-name s))
42 (defmacro define-structure (name &rest slots)
43 (let ((pred (symbol-concat name '-p)))
47 (` (defun (, pred) (obj)
49 (eq (elt obj 0) '(, name))
52 (` (defun (, (symbol-concat name '/create)) (, slots)
53 (, (cons 'vector (cons (list 'quote name) slots)))
60 (` (defun (, (symbol-concat name '/ slot)) (obj)
69 (list (list 'quote name))
76 (defsubst regexp-* (regexp)
79 (defconst rfc822/quoted-pair-regexp "\\\\.")
80 (defconst rfc822/qtext-regexp
81 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
82 (defconst rfc822/quoted-string-regexp
85 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
89 (defconst mime/content-parameter-value-regexp
91 rfc822/quoted-string-regexp
94 (defconst mime::parameter-regexp
95 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
96 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
98 (defun mime-parse-parameter (str)
99 (if (string-match mime::parameter-regexp str)
100 (let ((e (match-end 2)))
102 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
103 (std11-strip-quoted-string
104 (substring str (match-beginning 2) e))
109 (defun mime-parse-Content-Type (string)
110 "Parse STRING as field-body of Content-Type field.
112 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
113 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
115 (setq string (std11-unfold-string string))
116 (if (string-match `,(concat "^\\(" mime-token-regexp
117 "\\)/\\(" mime-token-regexp "\\)") string)
118 (let* ((type (downcase
119 (substring string (match-beginning 1) (match-end 1))))
121 (substring string (match-beginning 2) (match-end 2))))
123 (setq string (substring string (match-end 0)))
124 (while (setq ret (mime-parse-parameter string))
125 (setq dest (cons (car ret) dest)
128 (cons (intern type) (cons (intern subtype) (nreverse dest)))
132 (defconst mime-disposition-type-regexp mime-token-regexp)
134 (defun mime-parse-Content-Disposition (string)
135 "Parse STRING as field-body of Content-Disposition field."
136 (setq string (std11-unfold-string string))
137 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
138 (let* ((e (match-end 0))
139 (ctype (downcase (substring string 0 e)))
141 (setq string (substring string e))
142 (while (setq ret (mime-parse-parameter string))
143 (setq dest (cons (car ret) dest)
146 (cons ctype (nreverse dest))
153 (defun mime-read-Content-Type ()
154 "Read field-body of Content-Type field from current-buffer,
155 and return parsed it. Format of return value is as same as
156 `mime-parse-Content-Type'."
157 (let ((str (std11-field-body "Content-Type")))
159 (mime-parse-Content-Type str)
162 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
163 "Read field-body of Content-Transfer-Encoding field from
164 current-buffer, and return it.
165 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
166 (let ((str (std11-field-body "Content-Transfer-Encoding")))
169 (if (string-match "[ \t\n\r]+$" str)
170 (setq str (substring str 0 (match-beginning 0)))
177 (defun mime/Content-Disposition ()
178 "Read field-body of Content-Disposition field from current-buffer,
179 and return parsed it. [mime-parse.el]"
180 (let ((str (std11-field-body "Content-Disposition")))
182 (mime-parse-Content-Disposition str)
189 (define-structure mime::content-info
190 rcnum point-min point-max type parameters encoding children)
192 (defsubst make-mime-entity-info (rcnum
194 media-type media-subtype
195 parameters encoding children)
196 (let ((ctype (if media-type
198 (format "%s/%s" media-type media-subtype)
199 (symbol-name media-type)))))
200 (mime::content-info/create rcnum point-min point-max
201 ctype params encoding
205 (defsubst mime-entity-info-rnum (entity-info)
206 (mime::content-info/rcnum entity-info))
208 (defsubst mime-entity-info-point-min (entity-info)
209 (mime::content-info/point-min entity-info))
211 (defsubst mime-entity-info-point-max (entity-info)
212 (mime::content-info/point-max entity-info))
214 (defsubst mime-entity-info-type/subtype (entity-info)
215 (mime::content-info/type entity-info))
217 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
218 (goto-char (point-min))
219 (let* ((dash-boundary (concat "--" boundary))
220 (delimiter (concat "\n" (regexp-quote dash-boundary)))
221 (close-delimiter (concat delimiter "--[ \t]*$"))
224 (goto-char (point-max))
225 (if (re-search-backward close-delimiter nil t)
229 (rsep (concat delimiter "[ \t]*\n"))
231 (if (eq subtype 'digest)
235 cb ce ret ncb children (i 0))
237 (narrow-to-region beg end)
239 (re-search-forward rsep nil t)
240 (setq cb (match-end 0))
241 (while (re-search-forward rsep nil t)
242 (setq ce (match-beginning 0))
243 (setq ncb (match-end 0))
245 (narrow-to-region cb ce)
246 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
248 (setq children (cons ret children))
249 (goto-char (mime-entity-info-point-max ret))
250 (goto-char (setq cb ncb))
253 (setq ce (point-max))
255 (narrow-to-region cb ce)
256 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
258 (setq children (cons ret children))
260 (make-mime-entity-info rcnum beg (point-max)
261 primtype subtype params encoding
265 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
266 "Parse current-buffer as a MIME message.
267 DEFAULT-CTL is used when an entity does not have valid Content-Type
268 field. Its format must be as same as return value of
269 mime-{parse|read}-Content-Type."
270 (setq default-ctl (or (mime-read-Content-Type) default-ctl))
271 (let ((primtype (car default-ctl))
272 (subtype (car (cdr default-ctl)))
273 (params (cdr (cdr default-ctl)))
274 (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
276 (let ((boundary (assoc "boundary" params)))
278 (setq boundary (std11-strip-quoted-string (cdr boundary)))
279 (mime-parse-multipart
281 primtype subtype params encoding rcnum)
283 ((and (eq primtype 'message)
284 (memq subtype '(rfc822 news))
286 (goto-char (point-min))
287 (make-mime-entity-info rcnum
288 (point-min) (point-max)
289 primtype subtype params encoding
292 (if (re-search-forward "^$" nil t)
297 (list (mime-parse-message
298 nil nil (cons 0 rcnum)))
302 (make-mime-entity-info rcnum (point-min) (point-max)
303 primtype subtype params encoding
312 (provide 'mime-parse)
314 ;;; mime-parse.el ends here