1 ;;; mime-parse.el --- MIME message parser
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: mime-parse.el,v 0.3 1997-02-24 09:12:12 tmorioka Exp $
7 ;; Keywords: MIME, multimedia, mail, news
9 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
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.
31 (defmacro define-structure (name &rest slots)
32 (let ((pred (symbol-concat name '-p)))
36 (` (defun (, pred) (obj)
38 (eq (elt obj 0) '(, name))
41 (` (defun (, (symbol-concat name '/create)) (, slots)
42 (, (cons 'vector (cons (list 'quote name) slots)))
49 (` (defun (, (symbol-concat name '/ slot)) (obj)
58 (list (list 'quote name))
65 (defconst rfc822/quoted-pair-regexp "\\\\.")
66 (defconst rfc822/qtext-regexp
67 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
68 (defconst rfc822/quoted-string-regexp
71 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
75 (defconst mime/content-parameter-value-regexp
77 rfc822/quoted-string-regexp
80 (defconst mime::parameter-regexp
81 (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
82 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
84 (defun mime-parse-parameter (str)
85 (if (string-match mime::parameter-regexp str)
86 (let ((e (match-end 2)))
88 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
89 (std11-strip-quoted-string
90 (substring str (match-beginning 2) e))
95 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
97 (defun mime-parse-Content-Type (string)
98 "Parse STRING as field-body of Content-Type field. [mime-parse.el]"
99 (setq string (std11-unfold-string string))
100 (if (string-match mime::ctype-regexp string)
101 (let* ((e (match-end 0))
102 (ctype (downcase (substring string 0 e)))
104 (setq string (substring string e))
105 (while (setq ret (mime-parse-parameter string))
106 (setq dest (cons (car ret) dest)
109 (cons ctype (nreverse dest))
112 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
114 (defun mime-parse-Content-Disposition (string)
115 "Parse STRING as field-body of Content-Disposition field. [mime-parse.el]"
116 (setq string (std11-unfold-string string))
117 (if (string-match mime::dtype-regexp string)
118 (let* ((e (match-end 0))
119 (ctype (downcase (substring string 0 e)))
121 (setq string (substring string e))
122 (while (setq ret (mime-parse-parameter string))
123 (setq dest (cons (car ret) dest)
126 (cons ctype (nreverse dest))
133 (defun mime/Content-Type ()
134 "Read field-body of Content-Type field from current-buffer,
135 and return parsed it. [mime-parse.el]"
136 (let ((str (std11-field-body "Content-Type")))
138 (mime-parse-Content-Type str)
141 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
142 "Read field-body of Content-Transfer-Encoding field from
143 current-buffer, and return it.
144 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
145 (let ((str (std11-field-body "Content-Transfer-Encoding")))
148 (if (string-match "[ \t\n\r]+$" str)
149 (setq str (substring str 0 (match-beginning 0)))
156 (defun mime/Content-Disposition ()
157 "Read field-body of Content-Disposition field from current-buffer,
158 and return parsed it. [mime-parse.el]"
159 (let ((str (std11-field-body "Content-Disposition")))
161 (mime-parse-Content-Disposition str)
168 (define-structure mime::content-info
169 rcnum point-min point-max type parameters encoding children)
172 (defun mime-parse-multipart (boundary ctype params encoding rcnum)
173 (goto-char (point-min))
174 (let* ((dash-boundary (concat "--" boundary))
175 (delimiter (concat "\n" (regexp-quote dash-boundary)))
176 (close-delimiter (concat delimiter "--[ \t]*$"))
179 (goto-char (point-max))
180 (if (re-search-backward close-delimiter nil t)
184 (rsep (concat delimiter "[ \t]*\n"))
186 (if (string-equal ctype "multipart/digest")
190 cb ce ct ret ncb children (i 0))
192 (narrow-to-region beg end)
194 (re-search-forward rsep nil t)
195 (setq cb (match-end 0))
196 (while (re-search-forward rsep nil t)
197 (setq ce (match-beginning 0))
198 (setq ncb (match-end 0))
200 (narrow-to-region cb ce)
201 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
203 (setq children (cons ret children))
204 (goto-char (mime::content-info/point-max ret))
205 (goto-char (setq cb ncb))
208 (setq ce (point-max))
210 (narrow-to-region cb ce)
211 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
213 (setq children (cons ret children))
215 (mime::content-info/create rcnum beg (point-max)
216 ctype params encoding
220 (defun mime-parse-message (&optional ctl encoding rcnum)
221 "Parse current-buffer as a MIME message. [mime-parse.el]"
222 (setq ctl (or (mime/Content-Type) ctl))
223 (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
224 (let ((ctype (car ctl))
227 (let ((boundary (assoc "boundary" params)))
229 (setq boundary (std11-strip-quoted-string (cdr boundary)))
230 (mime-parse-multipart boundary ctype params encoding rcnum)
232 ((or (string-equal ctype "message/rfc822")
233 (string-equal ctype "message/news")
235 (goto-char (point-min))
236 (mime::content-info/create rcnum
237 (point-min) (point-max)
238 ctype params encoding
241 (if (re-search-forward "^$" nil t)
246 (list (mime-parse-message
247 nil nil (cons 0 rcnum)))
252 (mime::content-info/create rcnum (point-min) (point-max)
253 ctype params encoding nil)
261 (provide 'mime-parse)
263 ;;; mime-parse.el ends here