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.12 1997-07-07 17:06:36 morioka Exp $
7 ;; Keywords: parse, 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.
32 (defsubst symbol-concat (&rest args)
33 "Return a symbol whose name is concatenation of arguments ARGS
34 which are string or symbol."
35 (intern (apply (function concat)
38 (cond ((symbolp s) (symbol-name s))
43 (defmacro define-structure (name &rest slots)
44 (let ((pred (symbol-concat name '-p)))
48 (` (defun (, pred) (obj)
50 (eq (elt obj 0) '(, name))
53 (` (defun (, (symbol-concat name '/create)) (, slots)
54 (, (cons 'vector (cons (list 'quote name) slots)))
61 (` (defun (, (symbol-concat name '/ slot)) (obj)
70 (list (list 'quote name))
77 (defsubst regexp-* (regexp)
80 (defconst rfc822/quoted-pair-regexp "\\\\.")
81 (defconst rfc822/qtext-regexp
82 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
83 (defconst rfc822/quoted-string-regexp
86 (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
90 (defconst mime/content-parameter-value-regexp
92 rfc822/quoted-string-regexp
95 (defconst mime::parameter-regexp
96 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
97 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
99 (defun mime-parse-parameter (str)
100 (if (string-match mime::parameter-regexp str)
101 (let ((e (match-end 2)))
103 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
104 (std11-strip-quoted-string
105 (substring str (match-beginning 2) e))
110 (defconst mime::ctype-regexp (concat "^" mime-media-type/subtype-regexp))
112 (defun mime-parse-Content-Type (string)
113 "Parse STRING as field-body of Content-Type field."
114 (setq string (std11-unfold-string string))
115 (if (string-match mime::ctype-regexp string)
116 (let* ((e (match-end 0))
117 (ctype (downcase (substring string 0 e)))
119 (setq string (substring string e))
120 (while (setq ret (mime-parse-parameter string))
121 (setq dest (cons (car ret) dest)
124 (cons ctype (nreverse dest))
127 (defun mime-parse-Content-Disposition (string)
128 "Parse STRING as field-body of Content-Disposition field."
129 (setq string (std11-unfold-string string))
130 (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
131 (let* ((e (match-end 0))
132 (ctype (downcase (substring string 0 e)))
134 (setq string (substring string e))
135 (while (setq ret (mime-parse-parameter string))
136 (setq dest (cons (car ret) dest)
139 (cons ctype (nreverse dest))
146 (defun mime/Content-Type ()
147 "Read field-body of Content-Type field from current-buffer,
148 and return parsed it. [mime-parse.el]"
149 (let ((str (std11-field-body "Content-Type")))
151 (mime-parse-Content-Type str)
154 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
155 "Read field-body of Content-Transfer-Encoding field from
156 current-buffer, and return it.
157 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
158 (let ((str (std11-field-body "Content-Transfer-Encoding")))
161 (if (string-match "[ \t\n\r]+$" str)
162 (setq str (substring str 0 (match-beginning 0)))
169 (defun mime/Content-Disposition ()
170 "Read field-body of Content-Disposition field from current-buffer,
171 and return parsed it. [mime-parse.el]"
172 (let ((str (std11-field-body "Content-Disposition")))
174 (mime-parse-Content-Disposition str)
181 (define-structure mime::content-info
182 rcnum point-min point-max type parameters encoding children)
185 (defun mime-parse-multipart (boundary ctype params encoding rcnum)
186 (goto-char (point-min))
187 (let* ((dash-boundary (concat "--" boundary))
188 (delimiter (concat "\n" (regexp-quote dash-boundary)))
189 (close-delimiter (concat delimiter "--[ \t]*$"))
192 (goto-char (point-max))
193 (if (re-search-backward close-delimiter nil t)
197 (rsep (concat delimiter "[ \t]*\n"))
199 (if (string-equal ctype "multipart/digest")
203 cb ce ret ncb children (i 0))
205 (narrow-to-region beg end)
207 (re-search-forward rsep nil t)
208 (setq cb (match-end 0))
209 (while (re-search-forward rsep nil t)
210 (setq ce (match-beginning 0))
211 (setq ncb (match-end 0))
213 (narrow-to-region cb ce)
214 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
216 (setq children (cons ret children))
217 (goto-char (mime::content-info/point-max ret))
218 (goto-char (setq cb ncb))
221 (setq ce (point-max))
223 (narrow-to-region cb ce)
224 (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
226 (setq children (cons ret children))
228 (mime::content-info/create rcnum beg (point-max)
229 ctype params encoding
233 (defun mime-parse-message (&optional ctl encoding rcnum)
234 "Parse current-buffer as a MIME message. [mime-parse.el]"
235 (setq ctl (or (mime/Content-Type) ctl))
236 (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
237 (let ((ctype (car ctl))
240 (let ((boundary (assoc "boundary" params)))
242 (setq boundary (std11-strip-quoted-string (cdr boundary)))
243 (mime-parse-multipart boundary ctype params encoding rcnum)
245 ((or (string-equal ctype "message/rfc822")
246 (string-equal ctype "message/news")
248 (goto-char (point-min))
249 (mime::content-info/create rcnum
250 (point-min) (point-max)
251 ctype params encoding
254 (if (re-search-forward "^$" nil t)
259 (list (mime-parse-message
260 nil nil (cons 0 rcnum)))
265 (mime::content-info/create rcnum (point-min) (point-max)
266 ctype params encoding nil)
274 (provide 'mime-parse)
276 ;;; mime-parse.el ends here