Sync up with chao-1_6_1.
[elisp/flim.git] / mime-parse.el
1 ;;; mime-parse.el --- MIME message parser
2
3 ;; Copyright (C) 1994,1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: parse, MIME, multimedia, mail, news
7
8 ;; This file is part of SEMI (Spadework for Emacs MIME Interfaces).
9
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.
14
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.
19
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.
24
25 ;;; Code:
26
27 (require 'emu)
28 (require 'std11)
29 (require 'mime-def)
30
31 (eval-when-compile (require 'cl))
32
33
34 ;;; @ field parser
35 ;;;
36
37 (defconst mime/content-parameter-value-regexp
38   (concat "\\("
39           std11-quoted-string-regexp
40           "\\|[^; \t\n]*\\)"))
41
42 (defconst mime::parameter-regexp
43   (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
44           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
45
46 (defun mime-parse-parameter (str)
47   (if (string-match mime::parameter-regexp str)
48       (let ((e (match-end 2)))
49         (cons
50          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
51                (std11-strip-quoted-string
52                 (substring str (match-beginning 2) e))
53                )
54          (substring str e)
55          ))))
56
57
58 ;;; @ Content-Type
59 ;;;
60
61 ;;;###autoload
62 (defun mime-parse-Content-Type (string)
63   "Parse STRING as field-body of Content-Type field.
64 Return value is
65     (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
66 or nil.  PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
67 are string."
68   (setq string (std11-unfold-string string))
69   (if (string-match `,(concat "^\\(" mime-token-regexp
70                               "\\)/\\(" mime-token-regexp "\\)") string)
71       (let* ((type (downcase
72                     (substring string (match-beginning 1) (match-end 1))))
73              (subtype (downcase
74                        (substring string (match-beginning 2) (match-end 2))))
75              ret dest)
76         (setq string (substring string (match-end 0)))
77         (while (setq ret (mime-parse-parameter string))
78           (setq dest (cons (car ret) dest)
79                 string (cdr ret))
80           )
81         (make-mime-content-type (intern type)(intern subtype)
82                                 (nreverse dest))
83         )))
84
85 ;;;###autoload
86 (defun mime-read-Content-Type ()
87   "Read field-body of Content-Type field from current-buffer,
88 and return parsed it.  Format of return value is as same as
89 `mime-parse-Content-Type'."
90   (let ((str (std11-field-body "Content-Type")))
91     (if str
92         (mime-parse-Content-Type str)
93       )))
94
95
96 ;;; @ Content-Disposition
97 ;;;
98
99 (defconst mime-disposition-type-regexp mime-token-regexp)
100
101 ;;;###autoload
102 (defun mime-parse-Content-Disposition (string)
103   "Parse STRING as field-body of Content-Disposition field."
104   (setq string (std11-unfold-string string))
105   (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
106       (let* ((e (match-end 0))
107              (type (downcase (substring string 0 e)))
108              ret dest)
109         (setq string (substring string e))
110         (while (setq ret (mime-parse-parameter string))
111           (setq dest (cons (car ret) dest)
112                 string (cdr ret))
113           )
114         (cons (cons 'type (intern type))
115               (nreverse dest))
116         )))
117
118 ;;;###autoload
119 (defun mime-read-Content-Disposition ()
120   "Read field-body of Content-Disposition field from current-buffer,
121 and return parsed it."
122   (let ((str (std11-field-body "Content-Disposition")))
123     (if str
124         (mime-parse-Content-Disposition str)
125       )))
126
127
128 ;;; @ Content-Transfer-Encoding
129 ;;;
130
131 ;;;###autoload
132 (defun mime-parse-Content-Transfer-Encoding (string)
133   "Parse STRING as field-body of Content-Transfer-Encoding field."
134   (if (string-match "[ \t\n\r]+$" string)
135       (setq string (match-string 0 string))
136     )
137   (downcase string))
138
139 ;;;###autoload
140 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
141   "Read field-body of Content-Transfer-Encoding field from
142 current-buffer, and return it.
143 If is is not found, return DEFAULT-ENCODING."
144   (let ((str (std11-field-body "Content-Transfer-Encoding")))
145     (if str
146         (mime-parse-Content-Transfer-Encoding str)
147       default-encoding)))
148
149
150 ;;; @ message parser
151 ;;;
152
153 (defun mime-parse-multipart (entity)
154   (goto-char (point-min))
155   (let* ((content-type (mime-entity-content-type-internal entity))
156          (dash-boundary
157           (concat "--" (mime-content-type-parameter content-type "boundary")))
158          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
159          (close-delimiter (concat delimiter "--[ \t]*$"))
160          (rsep (concat delimiter "[ \t]*\n"))
161          (dc-ctl
162           (if (eq (mime-content-type-subtype content-type) 'digest)
163               (make-mime-content-type 'message 'rfc822)
164             (make-mime-content-type 'text 'plain)
165             ))
166          (header-end (mime-entity-header-end-internal entity))
167          (body-end (mime-entity-body-end-internal entity))
168          (node-id (mime-entity-node-id-internal entity))
169          cb ce ret ncb children (i 0))
170     (save-restriction
171       (goto-char body-end)
172       (narrow-to-region header-end
173                         (if (re-search-backward close-delimiter nil t)
174                             (match-beginning 0)
175                           body-end))
176       (goto-char header-end)
177       (re-search-forward rsep nil t)
178       (setq cb (match-end 0))
179       (while (re-search-forward rsep nil t)
180         (setq ce (match-beginning 0))
181         (setq ncb (match-end 0))
182         (save-restriction
183           (narrow-to-region cb ce)
184           (setq ret (mime-parse-message dc-ctl (cons i node-id)))
185           )
186         (setq children (cons ret children))
187         (goto-char (setq cb ncb))
188         (setq i (1+ i))
189         )
190       (setq ce (point-max))
191       (save-restriction
192         (narrow-to-region cb ce)
193         (setq ret (mime-parse-message dc-ctl (cons i node-id)))
194         )
195       (setq children (cons ret children))
196       )
197     (mime-entity-set-children-internal entity (nreverse children))
198     entity))
199
200 (defun mime-parse-encapsulated (entity)
201   (mime-entity-set-children-internal
202    entity
203    (save-restriction
204      (narrow-to-region (mime-entity-body-start-internal entity)
205                        (mime-entity-body-end-internal entity))
206      (list (mime-parse-message
207             nil (cons 0 (mime-entity-node-id-internal entity))))
208      ))
209   entity)
210
211 ;;;###autoload
212 (defun mime-parse-message (&optional default-ctl node-id)
213   "Parse current-buffer as a MIME message.
214 DEFAULT-CTL is used when an entity does not have valid Content-Type
215 field.  Its format must be as same as return value of
216 mime-{parse|read}-Content-Type."
217   (let ((header-start (point-min))
218         header-end
219         body-start
220         (body-end (point-max))
221         content-type primary-type entity)
222     (goto-char header-start)
223     (if (re-search-forward "^$" nil t)
224         (setq header-end (match-end 0)
225               body-start (if (= header-end body-end)
226                              body-end
227                            (1+ header-end)))
228       (setq header-end (point-min)
229             body-start (point-min)))
230     (save-restriction
231       (narrow-to-region header-start header-end)
232       (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
233                                (if str
234                                    (mime-parse-Content-Type str)
235                                  ))
236                              default-ctl)
237             primary-type (mime-content-type-primary-type content-type))
238       )
239     (setq entity
240           (make-mime-entity-internal
241            (current-buffer) header-start header-end body-start body-end
242            node-id content-type))
243     (cond ((eq primary-type 'multipart)
244            (mime-parse-multipart entity)
245            )
246           ((and (eq primary-type 'message)
247                 (memq (mime-content-type-subtype content-type)
248                       '(rfc822 news external-body)
249                       ))
250            (mime-parse-encapsulated entity)
251            )
252           (t entity))))
253
254
255 ;;; @ for buffer
256 ;;;
257
258 ;;;###autoload
259 (defun mime-parse-buffer (&optional buffer)
260   "Parse BUFFER as a MIME message.
261 If buffer is omitted, it parses current-buffer."
262   (save-excursion
263     (if buffer (set-buffer buffer))
264     (setq mime-message-structure (mime-parse-message))
265     ))
266
267
268 ;;; @ end
269 ;;;
270
271 (provide 'mime-parse)
272
273 ;;; mime-parse.el ends here