(mime-parse-Content-Type): Change return format.
[elisp/semi.git] / mime-parse.el
1 ;;; mime-parse.el --- MIME message parser
2
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: mime-parse.el,v 0.17 1997-09-05 06:43:14 morioka Exp $
7 ;; Keywords: parse, MIME, multimedia, mail, news
8
9 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'emu)
29 (require 'std11)
30 (require 'mime-def)
31
32 (defsubst symbol-concat (&rest args)
33   "Return a symbol whose name is concatenation of arguments ARGS
34 which are string or symbol."
35   (intern (mapconcat (function
36                       (lambda (s)
37                         (cond ((symbolp s) (symbol-name s))
38                               ((stringp s) s)
39                               )))
40                      args "")))
41
42 (defmacro define-structure (name &rest slots)
43   (let ((pred (symbol-concat name '-p)))
44     (cons 'progn
45           (nconc
46            (list
47             (` (defun (, pred) (obj)
48                  (and (vectorp obj)
49                       (eq (elt obj 0) '(, name))
50                       ))
51                )
52             (` (defun (, (symbol-concat name '/create)) (, slots)
53                  (, (cons 'vector (cons (list 'quote name) slots)))
54                  )
55                ))
56            (let ((i 1))
57              (mapcar (function
58                       (lambda (slot)
59                         (prog1
60                             (` (defun (, (symbol-concat name '/ slot)) (obj)
61                                  (if ((, pred) obj)
62                                      (elt obj (, i))
63                                    ))
64                                )
65                           (setq i (+ i 1))
66                           )
67                         )) slots)
68              )
69            (list (list 'quote name))
70            ))))
71
72
73 ;;; @ field parser
74 ;;;
75
76 (defsubst regexp-* (regexp)
77   (concat regexp "*"))
78
79 (defconst rfc822/quoted-pair-regexp "\\\\.")
80 (defconst rfc822/qtext-regexp
81   (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
82 (defconst rfc822/quoted-string-regexp
83   (concat "\""
84           (regexp-*
85            (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
86            )
87           "\""))
88
89 (defconst mime/content-parameter-value-regexp
90   (concat "\\("
91           rfc822/quoted-string-regexp
92           "\\|[^; \t\n]*\\)"))
93
94 (defconst mime::parameter-regexp
95   (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
96           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
97
98 (defun mime-parse-parameter (str)
99   (if (string-match mime::parameter-regexp str)
100       (let ((e (match-end 2)))
101         (cons
102          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
103                (std11-strip-quoted-string
104                 (substring str (match-beginning 2) e))
105                )
106          (substring str e)
107          ))))
108
109 (defun mime-parse-Content-Type (string)
110   "Parse STRING as field-body of Content-Type field.
111 Return value is
112     (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
113 or nil.  PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
114 are string."
115   (setq string (std11-unfold-string string))
116   (if (string-match `,(concat "^\\(" mime-token-regexp
117                               "\\)/\\(" mime-token-regexp "\\)") string)
118       (let* ((type (downcase
119                     (substring string (match-beginning 1) (match-end 1))))
120              (subtype (downcase
121                        (substring string (match-beginning 2) (match-end 2))))
122              ret dest)
123         (setq string (substring string (match-end 0)))
124         (while (setq ret (mime-parse-parameter string))
125           (setq dest (cons (car ret) dest)
126                 string (cdr ret))
127           )
128         (cons (intern type) (cons (intern subtype) (nreverse dest)))
129         )))
130
131
132 (defconst mime-disposition-type-regexp mime-token-regexp)
133
134 (defun mime-parse-Content-Disposition (string)
135   "Parse STRING as field-body of Content-Disposition field."
136   (setq string (std11-unfold-string string))
137   (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
138       (let* ((e (match-end 0))
139              (ctype (downcase (substring string 0 e)))
140              ret dest)
141         (setq string (substring string e))
142         (while (setq ret (mime-parse-parameter string))
143           (setq dest (cons (car ret) dest)
144                 string (cdr ret))
145           )
146         (cons ctype (nreverse dest))
147         )))
148
149
150 ;;; @ field reader
151 ;;;
152
153 (defun mime-read-Content-Type ()
154   "Read field-body of Content-Type field from current-buffer,
155 and return parsed it.  Format of return value is as same as
156 `mime-parse-Content-Type'."
157   (let ((str (std11-field-body "Content-Type")))
158     (if str
159         (mime-parse-Content-Type str)
160       )))
161
162 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
163   "Read field-body of Content-Transfer-Encoding field from
164 current-buffer, and return it.
165 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
166   (let ((str (std11-field-body "Content-Transfer-Encoding")))
167     (if str
168         (progn
169           (if (string-match "[ \t\n\r]+$" str)
170               (setq str (substring str 0 (match-beginning 0)))
171             )
172           (downcase str)
173           )
174       default-encoding)
175     ))
176
177 (defun mime/Content-Disposition ()
178   "Read field-body of Content-Disposition field from current-buffer,
179 and return parsed it. [mime-parse.el]"
180   (let ((str (std11-field-body "Content-Disposition")))
181     (if str
182         (mime-parse-Content-Disposition str)
183       )))
184
185
186 ;;; @ message parser
187 ;;;
188
189 (define-structure mime::content-info
190   rcnum point-min point-max type parameters encoding children)
191
192 (defsubst make-mime-entity-info (rcnum
193                                  point-min point-max
194                                  media-type media-subtype
195                                  parameters encoding children)
196   (let ((ctype (if media-type
197                    (if media-subtype
198                        (format "%s/%s" media-type media-subtype)
199                      (symbol-name media-type)))))
200     (mime::content-info/create rcnum point-min point-max
201                                ctype params encoding
202                                children)
203     ))
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               '(message rfc822)
221             '(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::content-info/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-info 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 (car default-ctl))
260         (subtype (car (cdr default-ctl)))
261         (params (cdr (cdr default-ctl)))
262         (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
263         )
264     (let ((boundary (assoc "boundary" params)))
265       (cond (boundary
266              (setq boundary (std11-strip-quoted-string (cdr boundary)))
267              (mime-parse-multipart
268               boundary
269               primtype subtype params encoding rcnum)
270              )
271             ((and (eq primtype 'message)
272                   (memq subtype '(rfc822 news))
273                   )
274              (goto-char (point-min))
275              (make-mime-entity-info rcnum
276                                     (point-min) (point-max)
277                                     primtype subtype params encoding
278                                     (save-restriction
279                                       (narrow-to-region
280                                        (if (re-search-forward "^$" nil t)
281                                            (1+ (match-end 0))
282                                          (point-min)
283                                          )
284                                        (point-max))
285                                       (list (mime-parse-message
286                                              nil nil (cons 0 rcnum)))
287                                       ))
288              )
289             (t 
290              (make-mime-entity-info rcnum (point-min) (point-max)
291                                     primtype subtype params encoding
292                                     nil)
293              ))
294       )))
295
296
297 ;;; @ end
298 ;;;
299
300 (provide 'mime-parse)
301
302 ;;; mime-parse.el ends here