1 ;;; mime-parse.el --- MIME message parser
3 ;; Copyright (C) 1994,1995,1996,1997,1998 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 (defsubst symbol-concat (&rest args)
32 "Return a symbol whose name is concatenation of arguments ARGS
33 which are string or symbol."
34 (intern (mapconcat (function
36 (cond ((symbolp s) (symbol-name s))
45 (defsubst regexp-* (regexp)
48 (defconst rfc822/quoted-pair-regexp "\\\\.")
49 (defconst rfc822/qtext-regexp
50 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
51 (defconst rfc822/quoted-string-regexp
54 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
58 (defconst mime/content-parameter-value-regexp
60 rfc822/quoted-string-regexp
63 (defconst mime::parameter-regexp
64 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
65 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
67 (defun mime-parse-parameter (str)
68 (if (string-match mime::parameter-regexp str)
69 (let ((e (match-end 2)))
71 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
72 (std11-strip-quoted-string
73 (substring str (match-beginning 2) e))
78 (defun mime-parse-Content-Type (string)
79 "Parse STRING as field-body of Content-Type field.
81 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
82 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
84 (setq string (std11-unfold-string string))
85 (if (string-match `,(concat "^\\(" mime-token-regexp
86 "\\)/\\(" mime-token-regexp "\\)") string)
87 (let* ((type (downcase
88 (substring string (match-beginning 1) (match-end 1))))
90 (substring string (match-beginning 2) (match-end 2))))
92 (setq string (substring string (match-end 0)))
93 (while (setq ret (mime-parse-parameter string))
94 (setq dest (cons (car ret) dest)
97 (cons (intern type) (cons (intern subtype) (nreverse dest)))
101 (defconst mime-disposition-type-regexp mime-token-regexp)
103 (defun mime-parse-Content-Disposition (string)
104 "Parse STRING as field-body of Content-Disposition field."
105 (setq string (std11-unfold-string string))
106 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
107 (let* ((e (match-end 0))
108 (ctype (downcase (substring string 0 e)))
110 (setq string (substring string e))
111 (while (setq ret (mime-parse-parameter string))
112 (setq dest (cons (car ret) dest)
115 (cons ctype (nreverse dest))
122 (defun mime-read-Content-Type ()
123 "Read field-body of Content-Type field from current-buffer,
124 and return parsed it. Format of return value is as same as
125 `mime-parse-Content-Type'."
126 (let ((str (std11-field-body "Content-Type")))
128 (mime-parse-Content-Type str)
131 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
132 "Read field-body of Content-Transfer-Encoding field from
133 current-buffer, and return it.
134 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
135 (let ((str (std11-field-body "Content-Transfer-Encoding")))
138 (if (string-match "[ \t\n\r]+$" str)
139 (setq str (substring str 0 (match-beginning 0)))
146 (defun mime/Content-Disposition ()
147 "Read field-body of Content-Disposition field from current-buffer,
148 and return parsed it. [mime-parse.el]"
149 (let ((str (std11-field-body "Content-Disposition")))
151 (mime-parse-Content-Disposition str)
158 (defsubst make-mime-entity-info (rcnum
160 media-type media-subtype parameters
162 (vector rcnum point-min point-max
163 media-type media-subtype parameters encoding children))
165 (defsubst mime-entity-info-rnum (entity-info) (aref entity-info 0))
166 (defsubst mime-entity-info-point-min (entity-info) (aref entity-info 1))
167 (defsubst mime-entity-info-point-max (entity-info) (aref entity-info 2))
168 (defsubst mime-entity-info-media-type (entity-info) (aref entity-info 3))
169 (defsubst mime-entity-info-media-subtype (entity-info) (aref entity-info 4))
170 (defsubst mime-entity-info-parameters (entity-info) (aref entity-info 5))
171 (defsubst mime-entity-info-encoding (entity-info) (aref entity-info 6))
172 (defsubst mime-entity-info-children (entity-info) (aref entity-info 7))
174 (defsubst mime-entity-info-type/subtype (entity-info)
175 (let ((type (mime-entity-info-media-type entity-info)))
177 (let ((subtype (mime-entity-info-media-subtype entity-info)))
179 (format "%s/%s" type subtype)
180 (symbol-name type))))))
182 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
183 (goto-char (point-min))
184 (let* ((dash-boundary (concat "--" boundary))
185 (delimiter (concat "\n" (regexp-quote dash-boundary)))
186 (close-delimiter (concat delimiter "--[ \t]*$"))
189 (goto-char (point-max))
190 (if (re-search-backward close-delimiter nil t)
194 (rsep (concat delimiter "[ \t]*\n"))
196 (if (eq subtype 'digest)
200 cb ce ret ncb children (i 0))
202 (narrow-to-region beg end)
204 (re-search-forward rsep nil t)
205 (setq cb (match-end 0))
206 (while (re-search-forward rsep nil t)
207 (setq ce (match-beginning 0))
208 (setq ncb (match-end 0))
210 (narrow-to-region cb ce)
211 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
213 (setq children (cons ret children))
214 (goto-char (mime-entity-info-point-max ret))
215 (goto-char (setq cb ncb))
218 (setq ce (point-max))
220 (narrow-to-region cb ce)
221 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
223 (setq children (cons ret children))
225 (make-mime-entity-info rcnum beg (point-max)
226 primtype subtype params encoding
230 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
231 "Parse current-buffer as a MIME message.
232 DEFAULT-CTL is used when an entity does not have valid Content-Type
233 field. Its format must be as same as return value of
234 mime-{parse|read}-Content-Type."
235 (setq default-ctl (or (mime-read-Content-Type) default-ctl))
236 (let ((primtype (car default-ctl))
237 (subtype (car (cdr default-ctl)))
238 (params (cdr (cdr default-ctl)))
239 (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
241 (let ((boundary (assoc "boundary" params)))
243 (setq boundary (std11-strip-quoted-string (cdr boundary)))
244 (mime-parse-multipart
246 primtype subtype params encoding rcnum)
248 ((and (eq primtype 'message)
249 (memq subtype '(rfc822 news))
251 (goto-char (point-min))
252 (make-mime-entity-info rcnum
253 (point-min) (point-max)
254 primtype subtype params encoding
257 (if (re-search-forward "^$" nil t)
262 (list (mime-parse-message
263 nil nil (cons 0 rcnum)))
267 (make-mime-entity-info rcnum (point-min) (point-max)
268 primtype subtype params encoding
277 (provide 'mime-parse)
279 ;;; mime-parse.el ends here