8951509b576c4f375ae2af82a0b832fa64b6143e
[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* ((representation-type
156           (mime-entity-representation-type-internal entity))
157          (content-type (mime-entity-content-type-internal entity))
158          (dash-boundary
159           (concat "--" (mime-content-type-parameter content-type "boundary")))
160          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
161          (close-delimiter (concat delimiter "--[ \t]*$"))
162          (rsep (concat delimiter "[ \t]*\n"))
163          (dc-ctl
164           (if (eq (mime-content-type-subtype content-type) 'digest)
165               (make-mime-content-type 'message 'rfc822)
166             (make-mime-content-type 'text 'plain)
167             ))
168          (header-end (mime-entity-header-end-internal entity))
169          (body-end (mime-entity-body-end-internal entity)))
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       (if (re-search-forward rsep nil t)
178           (let ((cb (match-end 0))
179                 ce ncb ret children
180                 (node-id (mime-entity-node-id-internal entity))
181                 (i 0))
182             (while (re-search-forward rsep nil t)
183               (setq ce (match-beginning 0))
184               (setq ncb (match-end 0))
185               (save-restriction
186                 (narrow-to-region cb ce)
187                 (setq ret (mime-parse-message representation-type dc-ctl
188                                               entity (cons i node-id)))
189                 )
190               (setq children (cons ret children))
191               (goto-char (setq cb ncb))
192               (setq i (1+ i))
193               )
194             (setq ce (point-max))
195             (save-restriction
196               (narrow-to-region cb ce)
197               (setq ret (mime-parse-message representation-type dc-ctl
198                                             entity (cons i node-id)))
199               )
200             (setq children (cons ret children))
201             (mime-entity-set-children-internal entity (nreverse children))
202             )
203         (mime-entity-set-content-type-internal
204          entity (make-mime-content-type 'message 'x-broken))
205         nil)
206       )))
207
208 (defun mime-parse-encapsulated (entity)
209   (mime-entity-set-children-internal
210    entity
211    (save-restriction
212      (narrow-to-region (mime-entity-body-start-internal entity)
213                        (mime-entity-body-end-internal entity))
214      (list (mime-parse-message
215             (mime-entity-representation-type-internal entity) nil
216             entity (cons 0 (mime-entity-node-id-internal entity))))
217      )))
218
219 (defun mime-parse-message (representation-type &optional default-ctl 
220                                                parent node-id)
221   (let ((header-start (point-min))
222         header-end
223         body-start
224         (body-end (point-max))
225         content-type)
226     (goto-char header-start)
227     (if (re-search-forward "^$" nil t)
228         (setq header-end (match-end 0)
229               body-start (if (= header-end body-end)
230                              body-end
231                            (1+ header-end)))
232       (setq header-end (point-min)
233             body-start (point-min)))
234     (save-restriction
235       (narrow-to-region header-start header-end)
236       (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
237                                (if str
238                                    (mime-parse-Content-Type str)
239                                  ))
240                              default-ctl))
241       )
242     (make-mime-entity-internal representation-type
243                                (current-buffer)
244                                content-type nil parent node-id
245                                nil nil nil nil
246                                nil nil nil nil
247                                nil nil
248                                (current-buffer)
249                                header-start header-end
250                                body-start body-end)
251     ))
252
253
254 ;;; @ for buffer
255 ;;;
256
257 ;;;###autoload
258 (defun mime-parse-buffer (&optional buffer representation-type)
259   "Parse BUFFER as a MIME message.
260 If buffer is omitted, it parses current-buffer."
261   (save-excursion
262     (if buffer (set-buffer buffer))
263     (setq mime-message-structure
264           (mime-parse-message (or representation-type 'buffer) nil))
265     ))
266
267
268 ;;; @ end
269 ;;;
270
271 (provide 'mime-parse)
272
273 ;;; mime-parse.el ends here