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