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.15 1996/10/28 08:44:12 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 GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
36 (defconst mime/content-parameter-value-regexp
38 rfc822/quoted-string-regexp
41 (defconst mime::parameter-regexp
42 (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
43 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
45 (defun mime/parse-parameter (str)
46 (if (string-match mime::parameter-regexp str)
47 (let ((e (match-end 2)))
49 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
50 (std11-strip-quoted-string
51 (substring str (match-beginning 2) e))
56 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
58 (defun mime/parse-Content-Type (string)
59 "Parse STRING as field-body of Content-Type field. [tm-parse.el]"
60 (setq string (std11-unfold-string string))
61 (if (string-match mime::ctype-regexp string)
62 (let* ((e (match-end 0))
63 (ctype (downcase (substring string 0 e)))
65 (setq string (substring string e))
66 (while (setq ret (mime/parse-parameter string))
67 (setq dest (cons (car ret) dest)
70 (cons ctype (nreverse dest))
73 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
75 (defun mime/parse-Content-Disposition (string)
76 "Parse STRING as field-body of Content-Disposition field. [tm-parse.el]"
77 (setq string (std11-unfold-string string))
78 (if (string-match mime::dtype-regexp string)
79 (let* ((e (match-end 0))
80 (ctype (downcase (substring string 0 e)))
82 (setq string (substring string e))
83 (while (setq ret (mime/parse-parameter string))
84 (setq dest (cons (car ret) dest)
87 (cons ctype (nreverse dest))
94 (defun mime/Content-Type ()
95 "Read field-body of Content-Type field from current-buffer,
96 and return parsed it. [tm-parse.el]"
97 (let ((str (std11-field-body "Content-Type")))
99 (mime/parse-Content-Type str)
102 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
103 "Read field-body of Content-Transfer-Encoding field from
104 current-buffer, and return it.
105 If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
106 (let ((str (std11-field-body "Content-Transfer-Encoding")))
109 (if (string-match "[ \t\n\r]+$" str)
110 (setq str (substring str 0 (match-beginning 0)))
117 (defun mime/Content-Disposition ()
118 "Read field-body of Content-Disposition field from current-buffer,
119 and return parsed it. [tm-parse.el]"
120 (let ((str (std11-field-body "Content-Disposition")))
122 (mime/parse-Content-Disposition str)
129 (define-structure mime::content-info
130 rcnum point-min point-max type parameters encoding children)
133 (defun mime/parse-multipart (boundary ctype params encoding rcnum)
134 (goto-char (point-min))
135 (let* ((dash-boundary (concat "--" boundary))
136 (delimiter (concat "\n" (regexp-quote dash-boundary)))
137 (close-delimiter (concat delimiter "--[ \t]*$"))
140 (goto-char (point-max))
141 (if (re-search-backward close-delimiter nil t)
145 (rsep (concat delimiter "[ \t]*\n"))
147 (if (string-equal ctype "multipart/digest")
151 cb ce ct ret ncb children (i 0))
153 (narrow-to-region beg end)
155 (re-search-forward rsep nil t)
156 (setq cb (match-end 0))
157 (while (re-search-forward rsep nil t)
158 (setq ce (match-beginning 0))
159 (setq ncb (match-end 0))
161 (narrow-to-region cb ce)
162 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
164 (setq children (cons ret children))
165 (goto-char (mime::content-info/point-max ret))
166 (goto-char (setq cb ncb))
169 (setq ce (point-max))
171 (narrow-to-region cb ce)
172 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
174 (setq children (cons ret children))
176 (mime::content-info/create rcnum beg (point-max)
177 ctype params encoding
181 (defun mime/parse-message (&optional ctl encoding rcnum)
182 "Parse current-buffer as a MIME message. [tm-parse.el]"
183 (setq ctl (or (mime/Content-Type) ctl))
184 (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
185 (let ((ctype (car ctl))
188 (let ((boundary (assoc "boundary" params)))
190 (setq boundary (std11-strip-quoted-string (cdr boundary)))
191 (mime/parse-multipart boundary ctype params encoding rcnum)
193 ((or (string-equal ctype "message/rfc822")
194 (string-equal ctype "message/news")
196 (goto-char (point-min))
197 (mime::content-info/create rcnum
198 (point-min) (point-max)
199 ctype params encoding
202 (if (re-search-forward "^$" nil t)
207 (list (mime/parse-message
208 nil nil (cons 0 rcnum)))
213 (mime::content-info/create rcnum (point-min) (point-max)
214 ctype params encoding nil)
224 ;;; tm-parse.el ends here