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>
6 ;; Version: $Id: tm-parse.el,v 7.13 1996/09/20 07:27:41 morioka Exp $
7 ;; Keywords: mail, news, MIME, multimedia
9 ;; This file is part of tm (Tools for MIME).
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 this program; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
36 (defconst mime::parameter-regexp
37 (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
38 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
40 (defun mime/parse-parameter (str)
41 (if (string-match mime::parameter-regexp str)
42 (let ((e (match-end 2)))
44 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
45 (std11-strip-quoted-string
46 (substring str (match-beginning 2) e))
51 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
53 (defun mime/parse-Content-Type (string)
54 "Parse STRING as field-body of Content-Type field. [tm-parse.el]"
55 (setq string (std11-unfold-string string))
56 (if (string-match mime::ctype-regexp string)
57 (let* ((e (match-end 0))
58 (ctype (downcase (substring string 0 e)))
60 (setq string (substring string e))
61 (while (setq ret (mime/parse-parameter string))
62 (setq dest (cons (car ret) dest)
65 (cons ctype (nreverse dest))
68 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
70 (defun mime/parse-Content-Disposition (string)
71 "Parse STRING as field-body of Content-Disposition field. [tm-parse.el]"
72 (setq string (std11-unfold-string string))
73 (if (string-match mime::dtype-regexp string)
74 (let* ((e (match-end 0))
75 (ctype (downcase (substring string 0 e)))
77 (setq string (substring string e))
78 (while (setq ret (mime/parse-parameter string))
79 (setq dest (cons (car ret) dest)
82 (cons ctype (nreverse dest))
89 (defun mime/Content-Type ()
90 "Read field-body of Content-Type field from current-buffer,
91 and return parsed it. [tm-parse.el]"
92 (let ((str (std11-field-body "Content-Type")))
94 (mime/parse-Content-Type str)
97 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
98 "Read field-body of Content-Transfer-Encoding field from
99 current-buffer, and return it.
100 If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
101 (let ((str (std11-field-body "Content-Transfer-Encoding")))
104 (if (string-match "[ \t\n\r]+$" str)
105 (setq str (substring str 0 (match-beginning 0)))
112 (defun mime/Content-Disposition ()
113 "Read field-body of Content-Disposition field from current-buffer,
114 and return parsed it. [tm-parse.el]"
115 (let ((str (std11-field-body "Content-Disposition")))
117 (mime/parse-Content-Disposition str)
124 (define-structure mime::content-info
125 rcnum point-min point-max type parameters encoding children)
128 (defun mime/parse-multipart (boundary ctype params encoding rcnum)
129 (goto-char (point-min))
130 (let* ((dash-boundary (concat "--" boundary))
131 (delimiter (concat "\n" (regexp-quote dash-boundary)))
132 (close-delimiter (concat delimiter "--[ \t]*$"))
135 (goto-char (point-max))
136 (if (re-search-backward close-delimiter nil t)
140 (rsep (concat delimiter "[ \t]*\n"))
142 (if (string-equal ctype "multipart/digest")
146 cb ce ct ret ncb children (i 0))
148 (narrow-to-region beg end)
150 (re-search-forward rsep nil t)
151 (setq cb (match-end 0))
152 (while (re-search-forward rsep nil t)
153 (setq ce (match-beginning 0))
154 (setq ncb (match-end 0))
156 (narrow-to-region cb ce)
157 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
159 (setq children (cons ret children))
160 (goto-char (mime::content-info/point-max ret))
161 (goto-char (setq cb ncb))
164 (setq ce (point-max))
166 (narrow-to-region cb ce)
167 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
169 (setq children (cons ret children))
171 (mime::content-info/create rcnum beg (point-max)
172 ctype params encoding
176 (defun mime/parse-message (&optional ctl encoding rcnum)
177 "Parse current-buffer as a MIME message. [tm-parse.el]"
178 (setq ctl (or (mime/Content-Type) ctl))
179 (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
180 (let ((ctype (car ctl))
183 (let ((boundary (assoc "boundary" params)))
185 (setq boundary (std11-strip-quoted-string (cdr boundary)))
186 (mime/parse-multipart boundary ctype params encoding rcnum)
188 ((or (string-equal ctype "message/rfc822")
189 (string-equal ctype "message/news")
191 (goto-char (point-min))
192 (mime::content-info/create rcnum
193 (point-min) (point-max)
194 ctype params encoding
197 (if (re-search-forward "^$" nil t)
202 (list (mime/parse-message
203 nil nil (cons 0 rcnum)))
208 (mime::content-info/create rcnum (point-min) (point-max)
209 ctype params encoding nil)
219 ;;; tm-parse.el ends here