2 ;;; tm-parse.el --- MIME message parser
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1994 .. 1996 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; $Id: tm-parse.el,v 7.8 1996/04/20 12:17:27 morioka Exp $
10 ;;; Keywords: mail, news, MIME, multimedia
12 ;;; This file is part of tm (Tools for MIME).
14 ;;; This program is free software; you can redistribute it and/or
15 ;;; modify it under the terms of the GNU General Public License as
16 ;;; published by the Free Software Foundation; either version 2, or
17 ;;; (at your option) any later version.
19 ;;; This program is distributed in the hope that it will be useful,
20 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;;; General Public License for more details.
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with This program. If not, write to the Free Software
26 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
38 (defconst mime::parameter-regexp
39 (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
40 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
42 (defun mime/parse-parameter (str)
43 (if (string-match mime::parameter-regexp str)
44 (let ((e (match-end 2)))
46 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
47 (rfc822/strip-quoted-string
48 (substring str (match-beginning 2) e))
53 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
55 (defun mime/parse-Content-Type (str)
56 "Parse STR as field-body of Content-Type field. [tm-parse.el]"
57 (setq str (rfc822/unfolding-string str))
58 (if (string-match mime::ctype-regexp str)
59 (let* ((e (match-end 0))
60 (ctype (downcase (substring str 0 e)))
62 (setq str (substring str e))
63 (while (setq ret (mime/parse-parameter str))
64 (setq dest (cons (car ret) dest)
67 (cons ctype (nreverse dest))
70 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
72 (defun mime/parse-Content-Disposition (str)
73 "Parse STR as field-body of Content-Disposition field. [tm-parse.el]"
74 (setq str (rfc822/unfolding-string str))
75 (if (string-match mime::dtype-regexp str)
76 (let* ((e (match-end 0))
77 (ctype (downcase (substring str 0 e)))
79 (setq str (substring str e))
80 (while (setq ret (mime/parse-parameter str))
81 (setq dest (cons (car ret) dest)
84 (cons ctype (nreverse dest))
91 (defun mime/Content-Type ()
92 "Read field-body of Content-Type field from current-buffer,
93 and return parsed it. [tm-parse.el]"
94 (let ((str (rfc822/get-field-body "Content-Type")))
96 (mime/parse-Content-Type str)
99 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
100 "Read field-body of Content-Transfer-Encoding field from
101 current-buffer, and return it.
102 If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
103 (let ((str (rfc822/get-field-body "Content-Transfer-Encoding")))
109 (defun mime/Content-Disposition ()
110 "Read field-body of Content-Disposition field from current-buffer,
111 and return parsed it. [tm-parse.el]"
112 (let ((str (rfc822/get-field-body "Content-Disposition")))
114 (mime/parse-Content-Disposition str)
121 (define-structure mime::content-info
122 rcnum point-min point-max type parameters encoding children)
125 (defun mime/parse-multipart (boundary ctype params encoding rcnum)
126 (goto-char (point-min))
127 (let* ((dash-boundary (concat "--" boundary))
128 (delimiter (concat "\n" (regexp-quote dash-boundary)))
129 (close-delimiter (concat delimiter "--[ \t]*$"))
132 (goto-char (point-max))
133 (if (re-search-backward close-delimiter nil t)
137 (rsep (concat delimiter "[ \t]*\n"))
139 (if (string-equal ctype "multipart/digest")
143 cb ce ct ret ncb children (i 0))
145 (narrow-to-region beg end)
147 (re-search-forward rsep nil t)
148 (setq cb (match-end 0))
149 (while (re-search-forward rsep nil t)
150 (setq ce (match-beginning 0))
151 (setq ncb (match-end 0))
153 (narrow-to-region cb ce)
154 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
156 (setq children (cons ret children))
157 (goto-char (mime::content-info/point-max ret))
158 (goto-char (setq cb ncb))
161 (setq ce (point-max))
163 (narrow-to-region cb ce)
164 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
166 (setq children (cons ret children))
168 (mime::content-info/create rcnum beg (point-max)
169 ctype params encoding
173 (defun mime/parse-message (&optional ctl encoding rcnum)
174 "Parse current-buffer as a MIME message. [tm-parse.el]"
175 (setq ctl (or (mime/Content-Type) ctl))
176 (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
177 (let ((ctype (car ctl))
180 (let ((boundary (assoc "boundary" params)))
182 (setq boundary (rfc822/strip-quoted-string (cdr boundary)))
183 (mime/parse-multipart boundary ctype params encoding rcnum)
185 ((string-equal ctype "message/rfc822")
186 (goto-char (point-min))
187 (mime::content-info/create rcnum
188 (point-min) (point-max)
189 ctype params encoding
192 (if (re-search-forward "^$" nil t)
197 (list (mime/parse-message
198 nil nil (cons 0 rcnum)))
203 (mime::content-info/create rcnum (point-min) (point-max)
204 ctype params encoding nil)
214 ;;; tm-parse.el ends here