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.16 1996/12/17 11:44:55 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 rfc822/quoted-pair-regexp "\\\\.")
37 (defconst rfc822/qtext-regexp
38 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
39 (defconst rfc822/quoted-string-regexp
42 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
46 (defconst mime/content-parameter-value-regexp
48 rfc822/quoted-string-regexp
51 (defconst mime::parameter-regexp
52 (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
53 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
55 (defun mime/parse-parameter (str)
56 (if (string-match mime::parameter-regexp str)
57 (let ((e (match-end 2)))
59 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
60 (std11-strip-quoted-string
61 (substring str (match-beginning 2) e))
66 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
68 (defun mime/parse-Content-Type (string)
69 "Parse STRING as field-body of Content-Type field. [tm-parse.el]"
70 (setq string (std11-unfold-string string))
71 (if (string-match mime::ctype-regexp string)
72 (let* ((e (match-end 0))
73 (ctype (downcase (substring string 0 e)))
75 (setq string (substring string e))
76 (while (setq ret (mime/parse-parameter string))
77 (setq dest (cons (car ret) dest)
80 (cons ctype (nreverse dest))
83 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
85 (defun mime/parse-Content-Disposition (string)
86 "Parse STRING as field-body of Content-Disposition field. [tm-parse.el]"
87 (setq string (std11-unfold-string string))
88 (if (string-match mime::dtype-regexp string)
89 (let* ((e (match-end 0))
90 (ctype (downcase (substring string 0 e)))
92 (setq string (substring string e))
93 (while (setq ret (mime/parse-parameter string))
94 (setq dest (cons (car ret) dest)
97 (cons ctype (nreverse dest))
104 (defun mime/Content-Type ()
105 "Read field-body of Content-Type field from current-buffer,
106 and return parsed it. [tm-parse.el]"
107 (let ((str (std11-field-body "Content-Type")))
109 (mime/parse-Content-Type str)
112 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
113 "Read field-body of Content-Transfer-Encoding field from
114 current-buffer, and return it.
115 If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
116 (let ((str (std11-field-body "Content-Transfer-Encoding")))
119 (if (string-match "[ \t\n\r]+$" str)
120 (setq str (substring str 0 (match-beginning 0)))
127 (defun mime/Content-Disposition ()
128 "Read field-body of Content-Disposition field from current-buffer,
129 and return parsed it. [tm-parse.el]"
130 (let ((str (std11-field-body "Content-Disposition")))
132 (mime/parse-Content-Disposition str)
139 (define-structure mime::content-info
140 rcnum point-min point-max type parameters encoding children)
143 (defun mime/parse-multipart (boundary ctype params encoding rcnum)
144 (goto-char (point-min))
145 (let* ((dash-boundary (concat "--" boundary))
146 (delimiter (concat "\n" (regexp-quote dash-boundary)))
147 (close-delimiter (concat delimiter "--[ \t]*$"))
150 (goto-char (point-max))
151 (if (re-search-backward close-delimiter nil t)
155 (rsep (concat delimiter "[ \t]*\n"))
157 (if (string-equal ctype "multipart/digest")
161 cb ce ct ret ncb children (i 0))
163 (narrow-to-region beg end)
165 (re-search-forward rsep nil t)
166 (setq cb (match-end 0))
167 (while (re-search-forward rsep nil t)
168 (setq ce (match-beginning 0))
169 (setq ncb (match-end 0))
171 (narrow-to-region cb ce)
172 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
174 (setq children (cons ret children))
175 (goto-char (mime::content-info/point-max ret))
176 (goto-char (setq cb ncb))
179 (setq ce (point-max))
181 (narrow-to-region cb ce)
182 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
184 (setq children (cons ret children))
186 (mime::content-info/create rcnum beg (point-max)
187 ctype params encoding
191 (defun mime/parse-message (&optional ctl encoding rcnum)
192 "Parse current-buffer as a MIME message. [tm-parse.el]"
193 (setq ctl (or (mime/Content-Type) ctl))
194 (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
195 (let ((ctype (car ctl))
198 (let ((boundary (assoc "boundary" params)))
200 (setq boundary (std11-strip-quoted-string (cdr boundary)))
201 (mime/parse-multipart boundary ctype params encoding rcnum)
203 ((or (string-equal ctype "message/rfc822")
204 (string-equal ctype "message/news")
206 (goto-char (point-min))
207 (mime::content-info/create rcnum
208 (point-min) (point-max)
209 ctype params encoding
212 (if (re-search-forward "^$" nil t)
217 (list (mime/parse-message
218 nil nil (cons 0 rcnum)))
223 (mime::content-info/create rcnum (point-min) (point-max)
224 ctype params encoding nil)
234 ;;; tm-parse.el ends here