`mime/disposition-type-regexp' -> `mime-disposition-type-regexp'.
[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.11 1997-07-07 17:01:51 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 (apply (function concat)
36                  (mapcar (function
37                           (lambda (s)
38                             (cond ((symbolp s) (symbol-name s))
39                                   ((stringp s) s)
40                                   )))
41                          args))))
42
43 (defmacro define-structure (name &rest slots)
44   (let ((pred (symbol-concat name '-p)))
45     (cons 'progn
46           (nconc
47            (list
48             (` (defun (, pred) (obj)
49                  (and (vectorp obj)
50                       (eq (elt obj 0) '(, name))
51                       ))
52                )
53             (` (defun (, (symbol-concat name '/create)) (, slots)
54                  (, (cons 'vector (cons (list 'quote name) slots)))
55                  )
56                ))
57            (let ((i 1))
58              (mapcar (function
59                       (lambda (slot)
60                         (prog1
61                             (` (defun (, (symbol-concat name '/ slot)) (obj)
62                                  (if ((, pred) obj)
63                                      (elt obj (, i))
64                                    ))
65                                )
66                           (setq i (+ i 1))
67                           )
68                         )) slots)
69              )
70            (list (list 'quote name))
71            ))))
72
73
74 ;;; @ field parser
75 ;;;
76
77 (defsubst regexp-* (regexp)
78   (concat regexp "*"))
79
80 (defconst rfc822/quoted-pair-regexp "\\\\.")
81 (defconst rfc822/qtext-regexp
82   (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
83 (defconst rfc822/quoted-string-regexp
84   (concat "\""
85           (regexp-*
86            (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
87            )
88           "\""))
89
90 (defconst mime/content-parameter-value-regexp
91   (concat "\\("
92           rfc822/quoted-string-regexp
93           "\\|[^; \t\n]*\\)"))
94
95 (defconst mime::parameter-regexp
96   (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
97           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
98
99 (defun mime-parse-parameter (str)
100   (if (string-match mime::parameter-regexp str)
101       (let ((e (match-end 2)))
102         (cons
103          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
104                (std11-strip-quoted-string
105                 (substring str (match-beginning 2) e))
106                )
107          (substring str e)
108          ))))
109
110 (defconst mime::ctype-regexp (concat "^" mime-media-type/subtype-regexp))
111
112 (defun mime-parse-Content-Type (string)
113   "Parse STRING as field-body of Content-Type field."
114   (setq string (std11-unfold-string string))
115   (if (string-match mime::ctype-regexp string)
116       (let* ((e (match-end 0))
117              (ctype (downcase (substring string 0 e)))
118              ret dest)
119         (setq string (substring string e))
120         (while (setq ret (mime-parse-parameter string))
121           (setq dest (cons (car ret) dest)
122                 string (cdr ret))
123           )
124         (cons ctype (nreverse dest))
125         )))
126
127 (defconst mime::dtype-regexp (concat "^" mime-disposition-type-regexp))
128
129 (defun mime-parse-Content-Disposition (string)
130   "Parse STRING as field-body of Content-Disposition field."
131   (setq string (std11-unfold-string string))
132   (if (string-match mime::dtype-regexp string)
133       (let* ((e (match-end 0))
134              (ctype (downcase (substring string 0 e)))
135              ret dest)
136         (setq string (substring string e))
137         (while (setq ret (mime-parse-parameter string))
138           (setq dest (cons (car ret) dest)
139                 string (cdr ret))
140           )
141         (cons ctype (nreverse dest))
142         )))
143
144
145 ;;; @ field reader
146 ;;;
147
148 (defun mime/Content-Type ()
149   "Read field-body of Content-Type field from current-buffer,
150 and return parsed it. [mime-parse.el]"
151   (let ((str (std11-field-body "Content-Type")))
152     (if str
153         (mime-parse-Content-Type str)
154       )))
155
156 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
157   "Read field-body of Content-Transfer-Encoding field from
158 current-buffer, and return it.
159 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
160   (let ((str (std11-field-body "Content-Transfer-Encoding")))
161     (if str
162         (progn
163           (if (string-match "[ \t\n\r]+$" str)
164               (setq str (substring str 0 (match-beginning 0)))
165             )
166           (downcase str)
167           )
168       default-encoding)
169     ))
170
171 (defun mime/Content-Disposition ()
172   "Read field-body of Content-Disposition field from current-buffer,
173 and return parsed it. [mime-parse.el]"
174   (let ((str (std11-field-body "Content-Disposition")))
175     (if str
176         (mime-parse-Content-Disposition str)
177       )))
178
179
180 ;;; @ message parser
181 ;;;
182
183 (define-structure mime::content-info
184   rcnum point-min point-max type parameters encoding children)
185
186
187 (defun mime-parse-multipart (boundary ctype params encoding rcnum)
188   (goto-char (point-min))
189   (let* ((dash-boundary   (concat "--" boundary))
190          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
191          (close-delimiter (concat delimiter "--[ \t]*$"))
192          (beg (point-min))
193          (end (progn
194                 (goto-char (point-max))
195                 (if (re-search-backward close-delimiter nil t)
196                     (match-beginning 0)
197                   (point-max)
198                   )))
199          (rsep (concat delimiter "[ \t]*\n"))
200          (dc-ctl
201           (if (string-equal ctype "multipart/digest")
202               '("message/rfc822")
203             '("text/plain")
204             ))
205          cb ce ret ncb children (i 0))
206     (save-restriction
207       (narrow-to-region beg end)
208       (goto-char beg)
209       (re-search-forward rsep nil t)
210       (setq cb (match-end 0))
211       (while (re-search-forward rsep nil t)
212         (setq ce (match-beginning 0))
213         (setq ncb (match-end 0))
214         (save-restriction
215           (narrow-to-region cb ce)
216           (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
217           )
218         (setq children (cons ret children))
219         (goto-char (mime::content-info/point-max ret))
220         (goto-char (setq cb ncb))
221         (setq i (1+ i))
222         )
223       (setq ce (point-max))
224       (save-restriction
225         (narrow-to-region cb ce)
226         (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
227         )
228       (setq children (cons ret children))
229       )
230     (mime::content-info/create rcnum beg (point-max)
231                                ctype params encoding
232                                (nreverse children))
233     ))
234
235 (defun mime-parse-message (&optional ctl encoding rcnum)
236   "Parse current-buffer as a MIME message. [mime-parse.el]"
237   (setq ctl (or (mime/Content-Type) ctl))
238   (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
239   (let ((ctype (car ctl))
240         (params (cdr ctl))
241         )
242     (let ((boundary (assoc "boundary" params)))
243       (cond (boundary
244              (setq boundary (std11-strip-quoted-string (cdr boundary)))
245              (mime-parse-multipart boundary ctype params encoding rcnum)
246              )
247             ((or (string-equal ctype "message/rfc822")
248                  (string-equal ctype "message/news")
249                  )
250              (goto-char (point-min))
251              (mime::content-info/create rcnum
252                                         (point-min) (point-max)
253                                         ctype params encoding
254                                         (save-restriction
255                                           (narrow-to-region
256                                            (if (re-search-forward "^$" nil t)
257                                                (1+ (match-end 0))
258                                              (point-min)
259                                              )
260                                            (point-max))
261                                           (list (mime-parse-message
262                                                  nil nil (cons 0 rcnum)))
263                                           )
264                                         )
265              )
266             (t 
267              (mime::content-info/create rcnum (point-min) (point-max)
268                                         ctype params encoding nil)
269              ))
270       )))
271
272
273 ;;; @ end
274 ;;;
275
276 (provide 'mime-parse)
277
278 ;;; mime-parse.el ends here