1 ;;; mime-parse.el --- MIME message parser
3 ;; Copyright (C) 1994,1995,1996,1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: parse, MIME, multimedia, mail, news
8 ;; This file is part of SEMI (Spadework for Emacs MIME Interfaces).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
33 ;;; @ lexical analyzer
36 (defcustom mime-lexical-analyzer
37 '(std11-analyze-quoted-string
38 std11-analyze-domain-literal
43 "*List of functions to return result of lexical analyze.
44 Each function must have two arguments: STRING and START.
45 STRING is the target string to be analyzed.
46 START is start position of STRING to analyze.
48 Previous function is preferred to next function. If a function
49 returns nil, next function is used. Otherwise the return value will
52 :type '(repeat function))
54 (defun mime-analyze-tspecial (string start)
55 (if (and (> (length string) start)
56 (memq (aref string start) mime-tspecial-char-list))
57 (cons (cons 'tpecials (substring string start (1+ start)))
61 (defun mime-analyze-token (string start)
62 (if (and (string-match mime-token-regexp string start)
63 (= (match-beginning 0) start))
64 (let ((end (match-end 0)))
65 (cons (cons 'mime-token (substring string start end))
66 ;;(substring string end)
74 (defconst mime/content-parameter-value-regexp
76 std11-quoted-string-regexp
79 (defconst mime::parameter-regexp
80 (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
81 "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
83 (defun mime-parse-parameter (str)
84 (if (string-match mime::parameter-regexp str)
85 (let ((e (match-end 2)))
87 (cons (downcase (substring str (match-beginning 1) (match-end 1)))
88 (std11-strip-quoted-string
89 (substring str (match-beginning 2) e))
99 (defun mime-parse-Content-Type (string)
100 "Parse STRING as field-body of Content-Type field.
102 (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
103 or nil. PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
105 (setq string (std11-unfold-string string))
106 (if (string-match `,(concat "^\\(" mime-token-regexp
107 "\\)/\\(" mime-token-regexp "\\)") string)
108 (let* ((type (downcase
109 (substring string (match-beginning 1) (match-end 1))))
111 (substring string (match-beginning 2) (match-end 2))))
113 (setq string (substring string (match-end 0)))
114 (while (setq ret (mime-parse-parameter string))
115 (setq dest (cons (car ret) dest)
118 (make-mime-content-type (intern type)(intern subtype)
123 (defun mime-read-Content-Type ()
124 "Read field-body of Content-Type field from current-buffer,
125 and return parsed it. Format of return value is as same as
126 `mime-parse-Content-Type'."
127 (let ((str (std11-field-body "Content-Type")))
129 (mime-parse-Content-Type str)
133 ;;; @ Content-Disposition
137 (defconst mime-disposition-type-regexp mime-token-regexp)
141 (defun mime-parse-Content-Disposition (string)
142 "Parse STRING as field-body of Content-Disposition field."
143 (setq string (std11-unfold-string string))
144 (if (string-match (eval-when-compile
145 (concat "^" mime-disposition-type-regexp)) string)
146 (let* ((e (match-end 0))
147 (type (downcase (substring string 0 e)))
149 (setq string (substring string e))
150 (while (setq ret (mime-parse-parameter string))
151 (setq dest (cons (car ret) dest)
154 (cons (cons 'type (intern type))
159 (defun mime-read-Content-Disposition ()
160 "Read field-body of Content-Disposition field from current-buffer,
161 and return parsed it."
162 (let ((str (std11-field-body "Content-Disposition")))
164 (mime-parse-Content-Disposition str)
168 ;;; @ Content-Transfer-Encoding
172 (defun mime-parse-Content-Transfer-Encoding (string)
173 "Parse STRING as field-body of Content-Transfer-Encoding field."
174 (let ((tokens (std11-lexical-analyze string mime-lexical-analyzer))
177 (setq token (car tokens))
178 (std11-ignored-token-p token))
179 (setq tokens (cdr tokens)))
181 (if (eq (car token) 'mime-token)
182 (downcase (cdr token))
186 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
187 "Read field-body of Content-Transfer-Encoding field from
188 current-buffer, and return it.
189 If is is not found, return DEFAULT-ENCODING."
190 (let ((str (std11-field-body "Content-Transfer-Encoding")))
192 (mime-parse-Content-Transfer-Encoding str)
196 ;;; @ Content-Id / Message-Id
200 (defun mime-parse-msg-id (tokens)
201 "Parse TOKENS as msg-id of Content-Id or Message-Id field."
202 (car (std11-parse-msg-id tokens)))
205 (defun mime-uri-parse-cid (string)
206 "Parse STRING as cid URI."
208 (mime-parse-msg-id (cons '(specials . "<")
210 (cdr (cdr (std11-lexical-analyze string)))
211 '((specials . ">")))))))
217 (defun mime-parse-multipart (entity)
218 (goto-char (point-min))
219 (let* ((representation-type
220 (mime-entity-representation-type-internal entity))
221 (content-type (mime-entity-content-type-internal entity))
223 (concat "--" (mime-content-type-parameter content-type "boundary")))
224 (delimiter (concat "\n" (regexp-quote dash-boundary)))
225 (close-delimiter (concat delimiter "--[ \t]*$"))
226 (rsep (concat delimiter "[ \t]*\n"))
228 (if (eq (mime-content-type-subtype content-type) 'digest)
229 (make-mime-content-type 'message 'rfc822)
230 (make-mime-content-type 'text 'plain)
232 (header-end (mime-entity-header-end-internal entity))
233 (body-end (mime-entity-body-end-internal entity)))
236 (narrow-to-region header-end
237 (if (re-search-backward close-delimiter nil t)
240 (goto-char header-end)
241 (if (re-search-forward rsep nil t)
242 (let ((cb (match-end 0))
244 (node-id (mime-entity-node-id-internal entity))
246 (while (re-search-forward rsep nil t)
247 (setq ce (match-beginning 0))
248 (setq ncb (match-end 0))
250 (narrow-to-region cb ce)
251 (setq ret (mime-parse-message representation-type dc-ctl
252 entity (cons i node-id)))
254 (setq children (cons ret children))
255 (goto-char (setq cb ncb))
258 (setq ce (point-max))
260 (narrow-to-region cb ce)
261 (setq ret (mime-parse-message representation-type dc-ctl
262 entity (cons i node-id)))
264 (setq children (cons ret children))
265 (mime-entity-set-children-internal entity (nreverse children))
267 (mime-entity-set-content-type-internal
268 entity (make-mime-content-type 'message 'x-broken))
272 (defun mime-parse-encapsulated (entity)
273 (mime-entity-set-children-internal
276 (narrow-to-region (mime-entity-body-start-internal entity)
277 (mime-entity-body-end-internal entity))
278 (list (mime-parse-message
279 (mime-entity-representation-type-internal entity) nil
280 entity (cons 0 (mime-entity-node-id-internal entity))))
283 (defun mime-parse-message (representation-type &optional default-ctl
285 (let ((header-start (point-min))
288 (body-end (point-max))
290 (goto-char header-start)
291 (if (re-search-forward "^$" nil t)
292 (setq header-end (match-end 0)
293 body-start (if (= header-end body-end)
296 (setq header-end (point-min)
297 body-start (point-min)))
299 (narrow-to-region header-start header-end)
300 (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
302 (mime-parse-Content-Type str)
306 (make-mime-entity-internal representation-type
308 content-type nil parent node-id
313 header-start header-end
322 (defun mime-parse-buffer (&optional buffer representation-type)
323 "Parse BUFFER as a MIME message.
324 If buffer is omitted, it parses current-buffer."
326 (if buffer (set-buffer buffer))
327 (setq mime-message-structure
328 (mime-parse-message (or representation-type 'buffer) nil))
335 (provide 'mime-parse)
337 ;;; mime-parse.el ends here