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