1 ;;; tm-parse.el --- MIME message parser
3 ;; Copyright (C) 1994,1995,1996 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; $Id: tm-parse.el,v 7.11 1996/08/30 06:20:52 morioka Exp $
8 ;; Keywords: mail, news, MIME, multimedia
10 ;; This file is part of tm (Tools for MIME).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
37 (defconst mime::parameter-regexp
38 (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
39 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
41 (defun mime/parse-parameter (str)
42 (if (string-match mime::parameter-regexp str)
43 (let ((e (match-end 2)))
45 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
46 (rfc822/strip-quoted-string
47 (substring str (match-beginning 2) e))
52 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
54 (defun mime/parse-Content-Type (string)
55 "Parse STRING as field-body of Content-Type field. [tm-parse.el]"
56 (setq string (std11-unfold-string string))
57 (if (string-match mime::ctype-regexp string)
58 (let* ((e (match-end 0))
59 (ctype (downcase (substring string 0 e)))
61 (setq string (substring string e))
62 (while (setq ret (mime/parse-parameter string))
63 (setq dest (cons (car ret) dest)
66 (cons ctype (nreverse dest))
69 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
71 (defun mime/parse-Content-Disposition (string)
72 "Parse STRING as field-body of Content-Disposition field. [tm-parse.el]"
73 (setq string (std11-unfold-string string))
74 (if (string-match mime::dtype-regexp string)
75 (let* ((e (match-end 0))
76 (ctype (downcase (substring string 0 e)))
78 (setq string (substring string e))
79 (while (setq ret (mime/parse-parameter string))
80 (setq dest (cons (car ret) dest)
83 (cons ctype (nreverse dest))
90 (defun mime/Content-Type ()
91 "Read field-body of Content-Type field from current-buffer,
92 and return parsed it. [tm-parse.el]"
93 (let ((str (std11-field-body "Content-Type")))
95 (mime/parse-Content-Type str)
98 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
99 "Read field-body of Content-Transfer-Encoding field from
100 current-buffer, and return it.
101 If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
102 (let ((str (std11-field-body "Content-Transfer-Encoding")))
105 (if (string-match "[ \t\n\r]+$" str)
106 (setq str (substring str 0 (match-beginning 0)))
113 (defun mime/Content-Disposition ()
114 "Read field-body of Content-Disposition field from current-buffer,
115 and return parsed it. [tm-parse.el]"
116 (let ((str (std11-field-body "Content-Disposition")))
118 (mime/parse-Content-Disposition str)
125 (define-structure mime::content-info
126 rcnum point-min point-max type parameters encoding children)
129 (defun mime/parse-multipart (boundary ctype params encoding rcnum)
130 (goto-char (point-min))
131 (let* ((dash-boundary (concat "--" boundary))
132 (delimiter (concat "\n" (regexp-quote dash-boundary)))
133 (close-delimiter (concat delimiter "--[ \t]*$"))
136 (goto-char (point-max))
137 (if (re-search-backward close-delimiter nil t)
141 (rsep (concat delimiter "[ \t]*\n"))
143 (if (string-equal ctype "multipart/digest")
147 cb ce ct ret ncb children (i 0))
149 (narrow-to-region beg end)
151 (re-search-forward rsep nil t)
152 (setq cb (match-end 0))
153 (while (re-search-forward rsep nil t)
154 (setq ce (match-beginning 0))
155 (setq ncb (match-end 0))
157 (narrow-to-region cb ce)
158 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
160 (setq children (cons ret children))
161 (goto-char (mime::content-info/point-max ret))
162 (goto-char (setq cb ncb))
165 (setq ce (point-max))
167 (narrow-to-region cb ce)
168 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
170 (setq children (cons ret children))
172 (mime::content-info/create rcnum beg (point-max)
173 ctype params encoding
177 (defun mime/parse-message (&optional ctl encoding rcnum)
178 "Parse current-buffer as a MIME message. [tm-parse.el]"
179 (setq ctl (or (mime/Content-Type) ctl))
180 (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
181 (let ((ctype (car ctl))
184 (let ((boundary (assoc "boundary" params)))
186 (setq boundary (rfc822/strip-quoted-string (cdr boundary)))
187 (mime/parse-multipart boundary ctype params encoding rcnum)
189 ((string-equal ctype "message/rfc822")
190 (goto-char (point-min))
191 (mime::content-info/create rcnum
192 (point-min) (point-max)
193 ctype params encoding
196 (if (re-search-forward "^$" nil t)
201 (list (mime/parse-message
202 nil nil (cons 0 rcnum)))
207 (mime::content-info/create rcnum (point-min) (point-max)
208 ctype params encoding nil)
218 ;;; tm-parse.el ends here