2 ;;; tm-parse.el --- MIME message parser
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1994,1995 MORIOKA Tomohiko
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; $Id: tm-parse.el,v 6.0 1995/11/15 11:56:10 morioka Exp $
10 ;;; Keywords: mail, news, MIME, multimedia
12 ;;; This file is part of tm (Tools for MIME).
23 (defconst mime::parameter-regexp
24 (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
25 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
27 (defun mime/parse-parameter (str)
28 (if (string-match mime::parameter-regexp str)
29 (let ((e (match-end 2)))
31 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
32 (rfc822/strip-quoted-string
33 (substring str (match-beginning 2) e))
38 (defconst mime::ctype-regexp
39 (concat "^" mime/content-type-subtype-regexp))
41 (defun mime/parse-Content-Type (str)
42 "Parse STR as field-body of Content-Type field. [tm-parse.el]"
43 (setq str (rfc822/unfolding-string str))
44 (if (string-match mime::ctype-regexp str)
45 (let* ((e (match-end 0))
46 (ctype (downcase (substring str 0 e)))
48 (setq str (substring str e))
49 (while (setq ret (mime/parse-parameter str))
50 (setq dest (cons (car ret) dest))
53 (cons ctype (reverse dest))
60 (defun mime/Content-Type ()
61 "Read field-body of Content-Type field from current-buffer,
62 and return parsed it. [tm-parse.el]"
63 (let ((str (rfc822/get-field-body "Content-Type")))
65 (mime/parse-Content-Type str)
68 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
69 "Read field-body of Content-Transfer-Encoding field from
70 current-buffer, and return it.
71 If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
72 (let ((str (rfc822/get-field-body "Content-Transfer-Encoding")))
82 (define-structure mime::content-info
83 rcnum point-min point-max type parameters encoding children)
86 (defun mime/parse-multipart (boundary ctype params encoding rcnum)
87 (goto-char (point-min))
88 (let* ((dash-boundary (concat "--" boundary))
89 (delimiter (concat "\n" dash-boundary))
90 (close-delimiter (concat delimiter "--"))
92 (end (if (search-forward close-delimiter nil t)
96 (rsep (concat (regexp-quote delimiter) "[ \t]*\n"))
98 (cond ((string= ctype "multipart/digest") '("message/rfc822"))
101 cb ce ct ret ncb children (i 0))
103 (narrow-to-region beg end)
105 (re-search-forward rsep nil t)
106 (setq cb (match-end 0))
107 (while (re-search-forward rsep nil t)
108 (setq ce (match-beginning 0))
109 (setq ncb (match-end 0))
111 (narrow-to-region cb ce)
112 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
114 (setq children (cons ret children))
115 (goto-char (mime::content-info/point-max ret))
116 (goto-char (setq cb ncb))
119 (setq ce (point-max))
121 (narrow-to-region cb ce)
122 (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
124 (setq children (cons ret children))
126 (mime::content-info/create rcnum beg end ctype params encoding
130 (defun mime/parse-message (&optional ctl encoding rcnum)
131 "Parse current-buffer as a MIME message. [tm-parse.el]"
132 (setq ctl (or (mime/Content-Type) ctl))
133 (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
134 (let ((ctype (car ctl))
137 (let ((boundary (assoc "boundary" params)))
139 (setq boundary (rfc822/strip-quoted-string (cdr boundary)))
140 (mime/parse-multipart boundary ctype params encoding rcnum)
142 ((string= ctype "message/rfc822")
143 (goto-char (point-min))
144 (mime::content-info/create rcnum
145 (point-min) (point-max)
146 ctype params encoding
149 (if (re-search-forward "^$" nil t)
154 (list (mime/parse-message
155 nil nil (cons 0 rcnum)))
160 (mime::content-info/create rcnum (point-min) (point-max)
161 ctype params encoding nil)