2741a04fbfa899a9c705e75af3761bb0a4ab9274
[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 (defsubst mime-content-type-parameter (content-type parameter)
126   "Return PARAMETER value of CONTENT-TYPE."
127   (cdr (assoc parameter (mime-content-type-parameters content-type))))
128
129
130 ;;; @ Content-Disposition
131 ;;;
132
133 (defconst mime-disposition-type-regexp mime-token-regexp)
134
135 (defun mime-parse-Content-Disposition (string)
136   "Parse STRING as field-body of Content-Disposition field."
137   (setq string (std11-unfold-string string))
138   (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
139       (let* ((e (match-end 0))
140              (type (downcase (substring string 0 e)))
141              ret dest)
142         (setq string (substring string e))
143         (while (setq ret (mime-parse-parameter string))
144           (setq dest (cons (car ret) dest)
145                 string (cdr ret))
146           )
147         (cons (cons 'type (intern type))
148               (nreverse dest))
149         )))
150
151 (defun mime-read-Content-Disposition ()
152   "Read field-body of Content-Disposition field from current-buffer,
153 and return parsed it."
154   (let ((str (std11-field-body "Content-Disposition")))
155     (if str
156         (mime-parse-Content-Disposition str)
157       )))
158
159 (defsubst mime-content-disposition-type (content-disposition)
160   "Return disposition-type of CONTENT-DISPOSITION."
161   (cdr (car content-disposition)))
162
163 (defsubst mime-content-disposition-parameters (content-disposition)
164   "Return disposition-parameters of CONTENT-DISPOSITION."
165   (cdr content-disposition))
166
167 (defsubst mime-content-disposition-parameter (content-disposition parameter)
168   "Return PARAMETER value of CONTENT-DISPOSITION."
169   (cdr (assoc parameter (cdr content-disposition))))
170
171 (defsubst mime-content-disposition-filename (content-disposition)
172   "Return filename of CONTENT-DISPOSITION."
173   (mime-content-disposition-parameter content-disposition "filename"))
174
175
176 ;;; @ Content-Transfer-Encoding
177 ;;;
178
179 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
180   "Read field-body of Content-Transfer-Encoding field from
181 current-buffer, and return it.
182 If is is not found, return DEFAULT-ENCODING."
183   (let ((str (std11-field-body "Content-Transfer-Encoding")))
184     (if str
185         (progn
186           (if (string-match "[ \t\n\r]+$" str)
187               (setq str (substring str 0 (match-beginning 0)))
188             )
189           (downcase str)
190           )
191       default-encoding)))
192
193
194 ;;; @ message parser
195 ;;;
196
197 (defsubst make-mime-entity (node-id
198                             point-min point-max
199                             content-type content-disposition encoding
200                             children)
201   (vector node-id point-min point-max
202           content-type content-disposition encoding children))
203
204 (defsubst mime-entity-node-id (entity)             (aref entity 0))
205 (defsubst mime-entity-point-min (entity)           (aref entity 1))
206 (defsubst mime-entity-point-max (entity)           (aref entity 2))
207 (defsubst mime-entity-content-type (entity)        (aref entity 3))
208 (defsubst mime-entity-content-disposition (entity) (aref entity 4))
209 (defsubst mime-entity-encoding (entity)            (aref entity 5))
210 (defsubst mime-entity-children (entity)            (aref entity 6))
211
212 (defsubst mime-entity-media-type (entity)
213   (mime-content-type-primary-type (mime-entity-content-type entity)))
214 (defsubst mime-entity-media-subtype (entity)
215   (mime-content-type-subtype (mime-entity-content-type entity)))
216 (defsubst mime-entity-parameters (entity)
217   (mime-content-type-parameters (mime-entity-content-type entity)))
218 (defsubst mime-entity-type/subtype (entity-info)
219   (mime-type/subtype-string (mime-entity-media-type entity-info)
220                             (mime-entity-media-subtype entity-info)))
221
222 (defun mime-parse-multipart (content-type content-disposition encoding node-id)
223   (goto-char (point-min))
224   (let* ((dash-boundary
225           (concat "--"
226                   (std11-strip-quoted-string
227                    (cdr (assoc "boundary"
228                                (mime-content-type-parameters content-type))))))
229          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
230          (close-delimiter (concat delimiter "--[ \t]*$"))
231          (beg (point-min))
232          (end (progn
233                 (goto-char (point-max))
234                 (if (re-search-backward close-delimiter nil t)
235                     (match-beginning 0)
236                   (point-max)
237                   )))
238          (rsep (concat delimiter "[ \t]*\n"))
239          (dc-ctl
240           (if (eq (mime-content-type-subtype content-type) 'digest)
241               (make-mime-content-type 'message 'rfc822)
242             (make-mime-content-type 'text 'plain)
243             ))
244          cb ce ret ncb children (i 0))
245     (save-restriction
246       (narrow-to-region beg end)
247       (goto-char beg)
248       (re-search-forward rsep nil t)
249       (setq cb (match-end 0))
250       (while (re-search-forward rsep nil t)
251         (setq ce (match-beginning 0))
252         (setq ncb (match-end 0))
253         (save-restriction
254           (narrow-to-region cb ce)
255           (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
256           )
257         (setq children (cons ret children))
258         (goto-char (mime-entity-point-max ret))
259         (goto-char (setq cb ncb))
260         (setq i (1+ i))
261         )
262       (setq ce (point-max))
263       (save-restriction
264         (narrow-to-region cb ce)
265         (setq ret (mime-parse-message dc-ctl "7bit" (cons i node-id)))
266         )
267       (setq children (cons ret children))
268       )
269     (make-mime-entity node-id beg (point-max)
270                       content-type content-disposition encoding
271                       (nreverse children))
272     ))
273
274 (defun mime-parse-message (&optional default-ctl default-encoding node-id)
275   "Parse current-buffer as a MIME message.
276 DEFAULT-CTL is used when an entity does not have valid Content-Type
277 field.  Its format must be as same as return value of
278 mime-{parse|read}-Content-Type."
279   (let* ((content-type (or (mime-read-Content-Type) default-ctl))
280          (content-disposition (mime-read-Content-Disposition))
281          (primary-type (mime-content-type-primary-type content-type))
282          (encoding (mime-read-Content-Transfer-Encoding default-encoding)))
283     (cond ((eq primary-type 'multipart)
284            (mime-parse-multipart content-type content-disposition encoding
285                                  node-id)
286            )
287           ((and (eq primary-type 'message)
288                 (memq (mime-content-type-subtype content-type)
289                       '(rfc822 news)
290                       ))
291            (goto-char (point-min))
292            (make-mime-entity node-id (point-min) (point-max)
293                              content-type content-disposition encoding
294                              (save-restriction
295                                (narrow-to-region
296                                 (if (re-search-forward "^$" nil t)
297                                     (1+ (match-end 0))
298                                   (point-min)
299                                   )
300                                 (point-max))
301                                (list (mime-parse-message
302                                       nil nil (cons 0 node-id)))
303                                ))
304            )
305           (t 
306            (make-mime-entity node-id (point-min) (point-max)
307                              content-type content-disposition encoding nil)
308            ))
309     ))
310
311
312 ;;; @ utilities
313 ;;;
314
315 (defsubst mime-root-entity-p (entity)
316   "Return t if ENTITY is root-entity (message)."
317   (null (mime-entity-node-id entity)))
318
319
320 ;;; @ end
321 ;;;
322
323 (provide 'mime-parse)
324
325 ;;; mime-parse.el ends here