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 (defsubst regexp-or (&rest args)
49 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
51 (defconst rfc822/quoted-pair-regexp "\\\\.")
52 (defconst rfc822/qtext-regexp
53 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
54 (defconst rfc822/quoted-string-regexp
57 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
61 (defconst mime/content-parameter-value-regexp
63 rfc822/quoted-string-regexp
66 (defconst mime::parameter-regexp
67 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
68 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
70 (defun mime-parse-parameter (str)
71 (if (string-match mime::parameter-regexp str)
72 (let ((e (match-end 2)))
74 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
75 (std11-strip-quoted-string
76 (substring str (match-beginning 2) e))
81 (defun mime-parse-Content-Type (string)
82 "Parse STRING as field-body of Content-Type field.
84 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
85 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
87 (setq string (std11-unfold-string string))
88 (if (string-match `,(concat "^\\(" mime-token-regexp
89 "\\)/\\(" mime-token-regexp "\\)") string)
90 (let* ((type (downcase
91 (substring string (match-beginning 1) (match-end 1))))
93 (substring string (match-beginning 2) (match-end 2))))
95 (setq string (substring string (match-end 0)))
96 (while (setq ret (mime-parse-parameter string))
97 (setq dest (cons (car ret) dest)
100 (cons (intern type) (cons (intern subtype) (nreverse dest)))
104 (defconst mime-disposition-type-regexp mime-token-regexp)
106 (defun mime-parse-Content-Disposition (string)
107 "Parse STRING as field-body of Content-Disposition field."
108 (setq string (std11-unfold-string string))
109 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
110 (let* ((e (match-end 0))
111 (ctype (downcase (substring string 0 e)))
113 (setq string (substring string e))
114 (while (setq ret (mime-parse-parameter string))
115 (setq dest (cons (car ret) dest)
118 (cons ctype (nreverse dest))
125 (defun mime-read-Content-Type ()
126 "Read field-body of Content-Type field from current-buffer,
127 and return parsed it. Format of return value is as same as
128 `mime-parse-Content-Type'."
129 (let ((str (std11-field-body "Content-Type")))
131 (mime-parse-Content-Type str)
134 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
135 "Read field-body of Content-Transfer-Encoding field from
136 current-buffer, and return it.
137 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
138 (let ((str (std11-field-body "Content-Transfer-Encoding")))
141 (if (string-match "[ \t\n\r]+$" str)
142 (setq str (substring str 0 (match-beginning 0)))
149 (defun mime/Content-Disposition ()
150 "Read field-body of Content-Disposition field from current-buffer,
151 and return parsed it. [mime-parse.el]"
152 (let ((str (std11-field-body "Content-Disposition")))
154 (mime-parse-Content-Disposition str)
161 (defsubst make-mime-entity (node-id
163 media-type media-subtype parameters
165 (vector node-id point-min point-max
166 media-type media-subtype parameters encoding children))
168 (defsubst mime-entity-node-id (entity-info) (aref entity-info 0))
169 (defsubst mime-entity-point-min (entity-info) (aref entity-info 1))
170 (defsubst mime-entity-point-max (entity-info) (aref entity-info 2))
171 (defsubst mime-entity-media-type (entity-info) (aref entity-info 3))
172 (defsubst mime-entity-media-subtype (entity-info) (aref entity-info 4))
173 (defsubst mime-entity-parameters (entity-info) (aref entity-info 5))
174 (defsubst mime-entity-encoding (entity-info) (aref entity-info 6))
175 (defsubst mime-entity-children (entity-info) (aref entity-info 7))
177 (defsubst mime-type/subtype-string (type &optional subtype)
178 "Return type/subtype string from TYPE and SUBTYPE."
181 (format "%s/%s" type subtype)
182 (format "%s" type))))
184 (defsubst mime-entity-type/subtype (entity-info)
185 (mime-type/subtype-string (mime-entity-media-type entity-info)
186 (mime-entity-media-subtype entity-info)))
188 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
189 (goto-char (point-min))
190 (let* ((dash-boundary (concat "--" boundary))
191 (delimiter (concat "\n" (regexp-quote dash-boundary)))
192 (close-delimiter (concat delimiter "--[ \t]*$"))
195 (goto-char (point-max))
196 (if (re-search-backward close-delimiter nil t)
200 (rsep (concat delimiter "[ \t]*\n"))
202 (if (eq subtype 'digest)
206 cb ce ret ncb children (i 0))
208 (narrow-to-region beg end)
210 (re-search-forward rsep nil t)
211 (setq cb (match-end 0))
212 (while (re-search-forward rsep nil t)
213 (setq ce (match-beginning 0))
214 (setq ncb (match-end 0))
216 (narrow-to-region cb ce)
217 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
219 (setq children (cons ret children))
220 (goto-char (mime-entity-point-max ret))
221 (goto-char (setq cb ncb))
224 (setq ce (point-max))
226 (narrow-to-region cb ce)
227 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
229 (setq children (cons ret children))
231 (make-mime-entity rcnum beg (point-max)
232 primtype subtype params encoding
236 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
237 "Parse current-buffer as a MIME message.
238 DEFAULT-CTL is used when an entity does not have valid Content-Type
239 field. Its format must be as same as return value of
240 mime-{parse|read}-Content-Type."
241 (setq default-ctl (or (mime-read-Content-Type) default-ctl))
242 (let ((primtype (car default-ctl))
243 (subtype (car (cdr default-ctl)))
244 (params (cdr (cdr default-ctl)))
245 (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
247 (let ((boundary (assoc "boundary" params)))
249 (setq boundary (std11-strip-quoted-string (cdr boundary)))
250 (mime-parse-multipart
252 primtype subtype params encoding rcnum)
254 ((and (eq primtype 'message)
255 (memq subtype '(rfc822 news))
257 (goto-char (point-min))
258 (make-mime-entity rcnum (point-min) (point-max)
259 primtype subtype params encoding
262 (if (re-search-forward "^$" nil t)
267 (list (mime-parse-message
268 nil nil (cons 0 rcnum)))
272 (make-mime-entity rcnum (point-min) (point-max)
273 primtype subtype params encoding
282 (defsubst mime-root-entity-p (entity)
283 "Return t if ENTITY is root-entity (message)."
284 (null (mime-entity-node-id entity)))
290 (provide 'mime-parse)
292 ;;; mime-parse.el ends here