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.24 1997-09-05 12:10:48 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))
46 (defsubst regexp-* (regexp)
49 (defconst rfc822/quoted-pair-regexp "\\\\.")
50 (defconst rfc822/qtext-regexp
51 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
52 (defconst rfc822/quoted-string-regexp
55 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
59 (defconst mime/content-parameter-value-regexp
61 rfc822/quoted-string-regexp
64 (defconst mime::parameter-regexp
65 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
66 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
68 (defun mime-parse-parameter (str)
69 (if (string-match mime::parameter-regexp str)
70 (let ((e (match-end 2)))
72 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
73 (std11-strip-quoted-string
74 (substring str (match-beginning 2) e))
79 (defun mime-parse-Content-Type (string)
80 "Parse STRING as field-body of Content-Type field.
82 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
83 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
85 (setq string (std11-unfold-string string))
86 (if (string-match `,(concat "^\\(" mime-token-regexp
87 "\\)/\\(" mime-token-regexp "\\)") string)
88 (let* ((type (downcase
89 (substring string (match-beginning 1) (match-end 1))))
91 (substring string (match-beginning 2) (match-end 2))))
93 (setq string (substring string (match-end 0)))
94 (while (setq ret (mime-parse-parameter string))
95 (setq dest (cons (car ret) dest)
98 (cons (intern type) (cons (intern subtype) (nreverse dest)))
102 (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 `,(concat "^" mime-disposition-type-regexp) string)
108 (let* ((e (match-end 0))
109 (ctype (downcase (substring string 0 e)))
111 (setq string (substring string e))
112 (while (setq ret (mime-parse-parameter string))
113 (setq dest (cons (car ret) dest)
116 (cons ctype (nreverse dest))
123 (defun mime-read-Content-Type ()
124 "Read field-body of Content-Type field from current-buffer,
125 and return parsed it. Format of return value is as same as
126 `mime-parse-Content-Type'."
127 (let ((str (std11-field-body "Content-Type")))
129 (mime-parse-Content-Type str)
132 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
133 "Read field-body of Content-Transfer-Encoding field from
134 current-buffer, and return it.
135 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
136 (let ((str (std11-field-body "Content-Transfer-Encoding")))
139 (if (string-match "[ \t\n\r]+$" str)
140 (setq str (substring str 0 (match-beginning 0)))
147 (defun mime/Content-Disposition ()
148 "Read field-body of Content-Disposition field from current-buffer,
149 and return parsed it. [mime-parse.el]"
150 (let ((str (std11-field-body "Content-Disposition")))
152 (mime-parse-Content-Disposition str)
159 (defsubst make-mime-entity-info (rcnum
161 media-type media-subtype parameters
163 (vector rcnum point-min point-max
164 media-type media-subtype parameters encoding children))
166 (defsubst mime-entity-info-rnum (entity-info) (aref entity-info 0))
167 (defsubst mime-entity-info-point-min (entity-info) (aref entity-info 1))
168 (defsubst mime-entity-info-point-max (entity-info) (aref entity-info 2))
169 (defsubst mime-entity-info-media-type (entity-info) (aref entity-info 3))
170 (defsubst mime-entity-info-media-subtype (entity-info) (aref entity-info 4))
171 (defsubst mime-entity-info-parameters (entity-info) (aref entity-info 5))
172 (defsubst mime-entity-info-encoding (entity-info) (aref entity-info 6))
173 (defsubst mime-entity-info-children (entity-info) (aref entity-info 7))
175 (defsubst mime-entity-info-type/subtype (entity-info)
176 (let ((type (mime-entity-info-media-type entity-info)))
178 (let ((subtype (mime-entity-info-media-subtype entity-info)))
180 (format "%s/%s" type subtype)
181 (symbol-name type))))))
183 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
184 (goto-char (point-min))
185 (let* ((dash-boundary (concat "--" boundary))
186 (delimiter (concat "\n" (regexp-quote dash-boundary)))
187 (close-delimiter (concat delimiter "--[ \t]*$"))
190 (goto-char (point-max))
191 (if (re-search-backward close-delimiter nil t)
195 (rsep (concat delimiter "[ \t]*\n"))
197 (if (eq subtype 'digest)
201 cb ce ret ncb children (i 0))
203 (narrow-to-region beg end)
205 (re-search-forward rsep nil t)
206 (setq cb (match-end 0))
207 (while (re-search-forward rsep nil t)
208 (setq ce (match-beginning 0))
209 (setq ncb (match-end 0))
211 (narrow-to-region cb ce)
212 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
214 (setq children (cons ret children))
215 (goto-char (mime-entity-info-point-max ret))
216 (goto-char (setq cb ncb))
219 (setq ce (point-max))
221 (narrow-to-region cb ce)
222 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
224 (setq children (cons ret children))
226 (make-mime-entity-info rcnum beg (point-max)
227 primtype subtype params encoding
231 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
232 "Parse current-buffer as a MIME message.
233 DEFAULT-CTL is used when an entity does not have valid Content-Type
234 field. Its format must be as same as return value of
235 mime-{parse|read}-Content-Type."
236 (setq default-ctl (or (mime-read-Content-Type) default-ctl))
237 (let ((primtype (car default-ctl))
238 (subtype (car (cdr default-ctl)))
239 (params (cdr (cdr default-ctl)))
240 (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
242 (let ((boundary (assoc "boundary" params)))
244 (setq boundary (std11-strip-quoted-string (cdr boundary)))
245 (mime-parse-multipart
247 primtype subtype params encoding rcnum)
249 ((and (eq primtype 'message)
250 (memq subtype '(rfc822 news))
252 (goto-char (point-min))
253 (make-mime-entity-info rcnum
254 (point-min) (point-max)
255 primtype subtype params encoding
258 (if (re-search-forward "^$" nil t)
263 (list (mime-parse-message
264 nil nil (cons 0 rcnum)))
268 (make-mime-entity-info rcnum (point-min) (point-max)
269 primtype subtype params encoding
278 (provide 'mime-parse)
280 ;;; mime-parse.el ends here