rearrangement.
[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 (buffer
184                             node-id
185                             header-start header-end body-start body-end
186                             content-type content-disposition
187                             encoding children)
188   (vector buffer
189           node-id header-start header-end body-start body-end
190           content-type content-disposition encoding children))
191
192 (defsubst mime-entity-buffer (entity)              (aref entity 0))
193 (defsubst mime-entity-node-id (entity)             (aref entity 1))
194 (defsubst mime-entity-header-start (entity)        (aref entity 2))
195 (defsubst mime-entity-header-end (entity)          (aref entity 3))
196 (defsubst mime-entity-body-start (entity)          (aref entity 4))
197 (defsubst mime-entity-body-end (entity)            (aref entity 5))
198 (defsubst mime-entity-content-type (entity)        (aref entity 6))
199 (defsubst mime-entity-content-disposition (entity) (aref entity 7))
200 (defsubst mime-entity-encoding (entity)            (aref entity 8))
201 (defsubst mime-entity-children (entity)            (aref entity 9))
202
203 (defsubst mime-entity-number (entity)
204   (reverse (mime-entity-node-id entity)))
205
206 (defalias 'mime-entity-point-min 'mime-entity-header-start)
207 (defalias 'mime-entity-point-max 'mime-entity-body-end)
208
209 (defsubst mime-entity-media-type (entity)
210   (mime-content-type-primary-type (mime-entity-content-type entity)))
211 (defsubst mime-entity-media-subtype (entity)
212   (mime-content-type-subtype (mime-entity-content-type entity)))
213 (defsubst mime-entity-parameters (entity)
214   (mime-content-type-parameters (mime-entity-content-type entity)))
215
216 (defsubst mime-entity-type/subtype (entity-info)
217   (mime-type/subtype-string (mime-entity-media-type entity-info)
218                             (mime-entity-media-subtype entity-info)))
219
220 (defun mime-parse-multipart (header-start header-end body-start body-end
221                                           content-type content-disposition
222                                           encoding node-id)
223   (goto-char (point-min))
224   (let* ((dash-boundary
225           (concat "--"
226                   (std11-strip-quoted-string
227                    (mime-content-type-parameter content-type "boundary"))))
228          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
229          (close-delimiter (concat delimiter "--[ \t]*$"))
230          (rsep (concat delimiter "[ \t]*\n"))
231          (dc-ctl
232           (if (eq (mime-content-type-subtype content-type) 'digest)
233               (make-mime-content-type 'message 'rfc822)
234             (make-mime-content-type 'text 'plain)
235             ))
236          cb ce ret ncb children (i 0))
237     (save-restriction
238       (goto-char body-end)
239       (narrow-to-region header-end
240                         (if (re-search-backward close-delimiter nil t)
241                             (match-beginning 0)
242                           body-end))
243       (goto-char header-start)
244       (re-search-forward rsep nil t)
245       (setq cb (match-end 0))
246       (while (re-search-forward rsep nil t)
247         (setq ce (match-beginning 0))
248         (setq ncb (match-end 0))
249         (save-restriction
250           (narrow-to-region cb ce)
251           (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
252           )
253         (setq children (cons ret children))
254         (goto-char (mime-entity-point-max ret))
255         (goto-char (setq cb ncb))
256         (setq i (1+ i))
257         )
258       (setq ce (point-max))
259       (save-restriction
260         (narrow-to-region cb ce)
261         (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
262         )
263       (setq children (cons ret children))
264       )
265     (make-mime-entity (current-buffer) node-id
266                       header-start header-end body-start body-end
267                       content-type content-disposition encoding
268                       (nreverse children))
269     ))
270
271 (defun mime-parse-message (&optional default-ctl default-encoding node-id)
272   "Parse current-buffer as a MIME message.
273 DEFAULT-CTL is used when an entity does not have valid Content-Type
274 field.  Its format must be as same as return value of
275 mime-{parse|read}-Content-Type."
276   (let ((header-start (point-min))
277         header-end
278         body-start
279         (body-end (point-max))
280         content-type content-disposition encoding
281         primary-type)
282     (goto-char header-start)
283     (if (re-search-forward "^$" nil t)
284         (setq header-end (match-end 0)
285               body-start (1+ header-end))
286       (setq header-end (point-min)
287             body-start (point-min))
288       )
289     (save-restriction
290       (narrow-to-region header-start header-end)
291       (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
292                                (if str
293                                    (mime-parse-Content-Type str)
294                                  ))
295                              default-ctl)
296             content-disposition (let ((str (std11-fetch-field
297                                             "Content-Disposition")))
298                                   (if str
299                                       (mime-parse-Content-Disposition str)
300                                     ))
301             encoding (let ((str (std11-fetch-field
302                                  "Content-Transfer-Encoding")))
303                        (if str
304                            (mime-parse-Content-Transfer-Encoding str)
305                          default-encoding))
306             primary-type (mime-content-type-primary-type content-type))
307       )
308     (cond ((eq primary-type 'multipart)
309            (mime-parse-multipart header-start header-end
310                                  body-start body-end
311                                  content-type content-disposition encoding
312                                  node-id)
313            )
314           ((and (eq primary-type 'message)
315                 (memq (mime-content-type-subtype content-type)
316                       '(rfc822 news)
317                       ))
318            (make-mime-entity (current-buffer) node-id
319                              header-start header-end body-start body-end
320                              content-type content-disposition encoding
321                              (save-restriction
322                                (narrow-to-region body-start body-end)
323                                (list (mime-parse-message
324                                       nil nil (cons 0 node-id)))
325                                ))
326            )
327           (t 
328            (make-mime-entity (current-buffer) node-id
329                              header-start header-end body-start body-end
330                              content-type content-disposition encoding nil)
331            ))
332     ))
333
334
335 ;;; @ utilities
336 ;;;
337
338 (defsubst mime-root-entity-p (entity)
339   "Return t if ENTITY is root-entity (message)."
340   (null (mime-entity-node-id entity)))
341
342
343 ;;; @ end
344 ;;;
345
346 (provide 'mime-parse)
347
348 ;;; mime-parse.el ends here