Sync up with remi-1_4_0_91.
[elisp/semi.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
32 ;;; @ field parser
33 ;;;
34
35 (defconst mime/content-parameter-value-regexp
36   (concat "\\("
37           std11-quoted-string-regexp
38           "\\|[^; \t\n]*\\)"))
39
40 (defconst mime::parameter-regexp
41   (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
42           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
43
44 (defun mime-parse-parameter (str)
45   (if (string-match mime::parameter-regexp str)
46       (let ((e (match-end 2)))
47         (cons
48          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
49                (std11-strip-quoted-string
50                 (substring str (match-beginning 2) e))
51                )
52          (substring str e)
53          ))))
54
55
56 ;;; @ Content-Type
57 ;;;
58
59 (defsubst make-mime-content-type (type subtype &optional parameters)
60   (list* (cons 'type type)
61          (cons 'subtype subtype)
62          (nreverse parameters))
63   )
64
65 (defun mime-parse-Content-Type (string)
66   "Parse STRING as field-body of Content-Type field.
67 Return value is
68     (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
69 or nil.  PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
70 are string."
71   (setq string (std11-unfold-string string))
72   (if (string-match `,(concat "^\\(" mime-token-regexp
73                               "\\)/\\(" mime-token-regexp "\\)") string)
74       (let* ((type (downcase
75                     (substring string (match-beginning 1) (match-end 1))))
76              (subtype (downcase
77                        (substring string (match-beginning 2) (match-end 2))))
78              ret dest)
79         (setq string (substring string (match-end 0)))
80         (while (setq ret (mime-parse-parameter string))
81           (setq dest (cons (car ret) dest)
82                 string (cdr ret))
83           )
84         (make-mime-content-type (intern type)(intern subtype)
85                                 (nreverse dest))
86         )))
87
88 (defun mime-read-Content-Type ()
89   "Read field-body of Content-Type field from current-buffer,
90 and return parsed it.  Format of return value is as same as
91 `mime-parse-Content-Type'."
92   (let ((str (std11-field-body "Content-Type")))
93     (if str
94         (mime-parse-Content-Type str)
95       )))
96
97 (defsubst mime-content-type-primary-type (content-type)
98   "Return primary-type of CONTENT-TYPE."
99   (cdr (car content-type)))
100
101 (defsubst mime-content-type-subtype (content-type)
102   "Return primary-type of CONTENT-TYPE."
103   (cdr (cadr content-type)))
104
105 (defsubst mime-content-type-parameters (content-type)
106   "Return primary-type of CONTENT-TYPE."
107   (cddr content-type))
108
109 (defsubst mime-content-type-parameter (content-type parameter)
110   "Return PARAMETER value of CONTENT-TYPE."
111   (cdr (assoc parameter (mime-content-type-parameters content-type))))
112
113
114 ;;; @ Content-Disposition
115 ;;;
116
117 (defconst mime-disposition-type-regexp mime-token-regexp)
118
119 (defun mime-parse-Content-Disposition (string)
120   "Parse STRING as field-body of Content-Disposition field."
121   (setq string (std11-unfold-string string))
122   (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
123       (let* ((e (match-end 0))
124              (type (downcase (substring string 0 e)))
125              ret dest)
126         (setq string (substring string e))
127         (while (setq ret (mime-parse-parameter string))
128           (setq dest (cons (car ret) dest)
129                 string (cdr ret))
130           )
131         (cons (cons 'type (intern type))
132               (nreverse dest))
133         )))
134
135 (defun mime-read-Content-Disposition ()
136   "Read field-body of Content-Disposition field from current-buffer,
137 and return parsed it."
138   (let ((str (std11-field-body "Content-Disposition")))
139     (if str
140         (mime-parse-Content-Disposition str)
141       )))
142
143 (defsubst mime-content-disposition-type (content-disposition)
144   "Return disposition-type of CONTENT-DISPOSITION."
145   (cdr (car content-disposition)))
146
147 (defsubst mime-content-disposition-parameters (content-disposition)
148   "Return disposition-parameters of CONTENT-DISPOSITION."
149   (cdr content-disposition))
150
151 (defsubst mime-content-disposition-parameter (content-disposition parameter)
152   "Return PARAMETER value of CONTENT-DISPOSITION."
153   (cdr (assoc parameter (cdr content-disposition))))
154
155 (defsubst mime-content-disposition-filename (content-disposition)
156   "Return filename of CONTENT-DISPOSITION."
157   (mime-content-disposition-parameter content-disposition "filename"))
158
159
160 ;;; @ Content-Transfer-Encoding
161 ;;;
162
163 (defun mime-parse-Content-Transfer-Encoding (string)
164   "Parse STRING as field-body of Content-Transfer-Encoding field."
165   (if (string-match "[ \t\n\r]+$" string)
166       (setq string (match-string 0 string))
167     )
168   (downcase string))
169
170 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
171   "Read field-body of Content-Transfer-Encoding field from
172 current-buffer, and return it.
173 If is is not found, return DEFAULT-ENCODING."
174   (let ((str (std11-field-body "Content-Transfer-Encoding")))
175     (if str
176         (mime-parse-Content-Transfer-Encoding str)
177       default-encoding)))
178
179
180 ;;; @ message parser
181 ;;;
182
183 (defsubst make-mime-entity (node-id header-start header-end
184                                     body-start body-end
185                                     content-type content-disposition
186                                     encoding children)
187   (vector node-id
188           header-start header-end body-start body-end
189           content-type content-disposition encoding children))
190
191 (defsubst mime-entity-node-id (entity)             (aref entity 0))
192 (defsubst mime-entity-header-start (entity)        (aref entity 1))
193 (defsubst mime-entity-header-end (entity)          (aref entity 2))
194 (defsubst mime-entity-body-start (entity)          (aref entity 3))
195 (defsubst mime-entity-body-end (entity)            (aref entity 4))
196 (defsubst mime-entity-content-type (entity)        (aref entity 5))
197 (defsubst mime-entity-content-disposition (entity) (aref entity 6))
198 (defsubst mime-entity-encoding (entity)            (aref entity 7))
199 (defsubst mime-entity-children (entity)            (aref entity 8))
200
201 (defalias 'mime-entity-point-min 'mime-entity-header-start)
202 (defalias 'mime-entity-point-max 'mime-entity-body-end)
203 (defsubst mime-entity-media-type (entity)
204   (mime-content-type-primary-type (mime-entity-content-type entity)))
205 (defsubst mime-entity-media-subtype (entity)
206   (mime-content-type-subtype (mime-entity-content-type entity)))
207 (defsubst mime-entity-parameters (entity)
208   (mime-content-type-parameters (mime-entity-content-type entity)))
209 (defsubst mime-entity-type/subtype (entity-info)
210   (mime-type/subtype-string (mime-entity-media-type entity-info)
211                             (mime-entity-media-subtype entity-info)))
212
213 (defun mime-parse-multipart (header-start header-end body-start body-end
214                                           content-type content-disposition
215                                           encoding node-id)
216   (goto-char (point-min))
217   (let* ((dash-boundary
218           (concat "--"
219                   (std11-strip-quoted-string
220                    (mime-content-type-parameter content-type "boundary"))))
221          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
222          (close-delimiter (concat delimiter "--[ \t]*$"))
223          (rsep (concat delimiter "[ \t]*\n"))
224          (dc-ctl
225           (if (eq (mime-content-type-subtype content-type) 'digest)
226               (make-mime-content-type 'message 'rfc822)
227             (make-mime-content-type 'text 'plain)
228             ))
229          cb ce ret ncb children (i 0))
230     (goto-char body-end)
231     (if (re-search-backward close-delimiter nil t)
232         (setq body-end (match-beginning 0))
233       )
234     (save-restriction
235       (narrow-to-region header-end body-end)
236       (goto-char header-start)
237       (re-search-forward rsep nil t)
238       (setq cb (match-end 0))
239       (while (re-search-forward rsep nil t)
240         (setq ce (match-beginning 0))
241         (setq ncb (match-end 0))
242         (save-restriction
243           (narrow-to-region cb ce)
244           (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
245           )
246         (setq children (cons ret children))
247         (goto-char (mime-entity-point-max ret))
248         (goto-char (setq cb ncb))
249         (setq i (1+ i))
250         )
251       (setq ce (point-max))
252       (save-restriction
253         (narrow-to-region cb ce)
254         (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
255         )
256       (setq children (cons ret children))
257       )
258     (make-mime-entity node-id
259                       header-start header-end
260                       body-start body-end
261                       content-type content-disposition encoding
262                       (nreverse children))
263     ))
264
265 (defun mime-parse-message (&optional default-ctl default-encoding node-id)
266   "Parse current-buffer as a MIME message.
267 DEFAULT-CTL is used when an entity does not have valid Content-Type
268 field.  Its format must be as same as return value of
269 mime-{parse|read}-Content-Type."
270   (let ((header-start (point-min))
271         header-end
272         body-start
273         (body-end (point-max))
274         content-type content-disposition encoding
275         primary-type)
276     (goto-char header-start)
277     (if (re-search-forward "^$" nil t)
278         (setq header-end (match-end 0)
279               body-start (1+ header-end))
280       (setq header-end (point-min)
281             body-start (point-min))
282       )
283     (save-restriction
284       (narrow-to-region header-start header-end)
285       (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
286                                (if str
287                                    (mime-parse-Content-Type str)
288                                  ))
289                              default-ctl)
290             content-disposition (let ((str (std11-fetch-field
291                                             "Content-Disposition")))
292                                   (if str
293                                       (mime-parse-Content-Disposition str)
294                                     ))
295             encoding (let ((str (std11-fetch-field
296                                  "Content-Transfer-Encoding")))
297                        (if str
298                            (mime-parse-Content-Transfer-Encoding str)
299                          default-encoding))
300             primary-type (mime-content-type-primary-type content-type))
301       )
302     (cond ((eq primary-type 'multipart)
303            (mime-parse-multipart header-start header-end
304                                  body-start body-end
305                                  content-type content-disposition encoding
306                                  node-id)
307            )
308           ((and (eq primary-type 'message)
309                 (memq (mime-content-type-subtype content-type)
310                       '(rfc822 news)
311                       ))
312            (make-mime-entity node-id
313                              header-start header-end
314                              body-start body-end
315                              content-type content-disposition encoding
316                              (save-restriction
317                                (narrow-to-region body-start body-end)
318                                (list (mime-parse-message
319                                       nil nil (cons 0 node-id)))
320                                ))
321            )
322           (t 
323            (make-mime-entity node-id
324                              header-start header-end
325                              body-start body-end
326                              content-type content-disposition encoding nil)
327            ))
328     ))
329
330
331 ;;; @ utilities
332 ;;;
333
334 (defsubst mime-root-entity-p (entity)
335   "Return t if ENTITY is root-entity (message)."
336   (null (mime-entity-node-id entity)))
337
338
339 ;;; @ end
340 ;;;
341
342 (provide 'mime-parse)
343
344 ;;; mime-parse.el ends here