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.17 1997-09-05 06:43:14 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 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
206 (goto-char (point-min))
207 (let* ((dash-boundary (concat "--" boundary))
208 (delimiter (concat "\n" (regexp-quote dash-boundary)))
209 (close-delimiter (concat delimiter "--[ \t]*$"))
212 (goto-char (point-max))
213 (if (re-search-backward close-delimiter nil t)
217 (rsep (concat delimiter "[ \t]*\n"))
219 (if (eq subtype 'digest)
223 cb ce ret ncb children (i 0))
225 (narrow-to-region beg end)
227 (re-search-forward rsep nil t)
228 (setq cb (match-end 0))
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 dc-ctl "7bit" (cons i rcnum)))
236 (setq children (cons ret children))
237 (goto-char (mime::content-info/point-max ret))
238 (goto-char (setq cb ncb))
241 (setq ce (point-max))
243 (narrow-to-region cb ce)
244 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
246 (setq children (cons ret children))
248 (make-mime-entity-info rcnum beg (point-max)
249 primtype subtype params encoding
253 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
254 "Parse current-buffer as a MIME message.
255 DEFAULT-CTL is used when an entity does not have valid Content-Type
256 field. Its format must be as same as return value of
257 mime-{parse|read}-Content-Type."
258 (setq default-ctl (or (mime-read-Content-Type) default-ctl))
259 (let ((primtype (car default-ctl))
260 (subtype (car (cdr default-ctl)))
261 (params (cdr (cdr default-ctl)))
262 (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
264 (let ((boundary (assoc "boundary" params)))
266 (setq boundary (std11-strip-quoted-string (cdr boundary)))
267 (mime-parse-multipart
269 primtype subtype params encoding rcnum)
271 ((and (eq primtype 'message)
272 (memq subtype '(rfc822 news))
274 (goto-char (point-min))
275 (make-mime-entity-info rcnum
276 (point-min) (point-max)
277 primtype subtype params encoding
280 (if (re-search-forward "^$" nil t)
285 (list (mime-parse-message
286 nil nil (cons 0 rcnum)))
290 (make-mime-entity-info rcnum (point-min) (point-max)
291 primtype subtype params encoding
300 (provide 'mime-parse)
302 ;;; mime-parse.el ends here