b389222e9abd3209cfbd971a7317435d1b394fab
[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                             media-type media-subtype parameters
188                             encoding children)
189   (vector node-id point-min point-max
190           media-type media-subtype parameters encoding children))
191
192 (defsubst mime-entity-node-id (entity-info)       (aref entity-info 0))
193 (defsubst mime-entity-point-min (entity-info)     (aref entity-info 1))
194 (defsubst mime-entity-point-max (entity-info)     (aref entity-info 2))
195 (defsubst mime-entity-media-type (entity-info)    (aref entity-info 3))
196 (defsubst mime-entity-media-subtype (entity-info) (aref entity-info 4))
197 (defsubst mime-entity-parameters (entity-info)    (aref entity-info 5))
198 (defsubst mime-entity-encoding (entity-info)      (aref entity-info 6))
199 (defsubst mime-entity-children (entity-info)      (aref entity-info 7))
200
201 (defsubst mime-entity-type/subtype (entity-info)
202   (mime-type/subtype-string (mime-entity-media-type entity-info)
203                             (mime-entity-media-subtype entity-info)))
204
205 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
206   (goto-char (point-min))
207   (let* ((dash-boundary   (concat "--" boundary))
208          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
209          (close-delimiter (concat delimiter "--[ \t]*$"))
210          (beg (point-min))
211          (end (progn
212                 (goto-char (point-max))
213                 (if (re-search-backward close-delimiter nil t)
214                     (match-beginning 0)
215                   (point-max)
216                   )))
217          (rsep (concat delimiter "[ \t]*\n"))
218          (dc-ctl
219           (if (eq subtype 'digest)
220               (make-mime-content-type 'message 'rfc822)
221             (make-mime-content-type 'text 'plain)
222             ))
223          cb ce ret ncb children (i 0))
224     (save-restriction
225       (narrow-to-region beg end)
226       (goto-char beg)
227       (re-search-forward rsep nil t)
228       (setq cb (match-end 0))
229       (while (re-search-forward rsep nil t)
230         (setq ce (match-beginning 0))
231         (setq ncb (match-end 0))
232         (save-restriction
233           (narrow-to-region cb ce)
234           (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
235           )
236         (setq children (cons ret children))
237         (goto-char (mime-entity-point-max ret))
238         (goto-char (setq cb ncb))
239         (setq i (1+ i))
240         )
241       (setq ce (point-max))
242       (save-restriction
243         (narrow-to-region cb ce)
244         (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
245         )
246       (setq children (cons ret children))
247       )
248     (make-mime-entity rcnum beg (point-max)
249                       primtype subtype params encoding
250                       (nreverse children))
251     ))
252
253 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
254   "Parse current-buffer as a MIME message.
255 DEFAULT-CTL is used when an entity does not have valid Content-Type
256 field.  Its format must be as same as return value of
257 mime-{parse|read}-Content-Type."
258   (setq default-ctl (or (mime-read-Content-Type) default-ctl))
259   (let ((primtype (mime-content-type-primary-type default-ctl))
260         (subtype (mime-content-type-subtype default-ctl))
261         (params (mime-content-type-parameters default-ctl))
262         (encoding (mime-read-Content-Transfer-Encoding default-encoding)))
263     (let ((boundary (assoc "boundary" params)))
264       (cond (boundary
265              (setq boundary (std11-strip-quoted-string (cdr boundary)))
266              (mime-parse-multipart
267               boundary
268               primtype subtype params encoding rcnum)
269              )
270             ((and (eq primtype 'message)
271                   (memq subtype '(rfc822 news))
272                   )
273              (goto-char (point-min))
274              (make-mime-entity rcnum (point-min) (point-max)
275                                primtype subtype params 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 rcnum)))
285                                  ))
286              )
287             (t 
288              (make-mime-entity rcnum (point-min) (point-max)
289                                primtype subtype params encoding
290                                nil)
291              ))
292       )))
293
294
295 ;;; @ utilities
296 ;;;
297
298 (defsubst mime-root-entity-p (entity)
299   "Return t if ENTITY is root-entity (message)."
300   (null (mime-entity-node-id entity)))
301
302
303 ;;; @ end
304 ;;;
305
306 (provide 'mime-parse)
307
308 ;;; mime-parse.el ends here