c380312b66c296eb067e888c3d812f99fa70c892
[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.5 1997-02-27 13:43:38 tmorioka 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 'std11)
29 (require 'mime-def)
30
31 (defsubst symbol-concat (&rest args)
32   "Return a symbol whose name is concatenation of arguments ARGS
33 which are string or symbol."
34   (intern (apply (function concat)
35                  (mapcar (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 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
110
111 (defun mime-parse-Content-Type (string)
112   "Parse STRING as field-body of Content-Type field. [mime-parse.el]"
113   (setq string (std11-unfold-string string))
114   (if (string-match mime::ctype-regexp string)
115       (let* ((e (match-end 0))
116              (ctype (downcase (substring string 0 e)))
117              ret dest)
118         (setq string (substring string e))
119         (while (setq ret (mime-parse-parameter string))
120           (setq dest (cons (car ret) dest)
121                 string (cdr ret))
122           )
123         (cons ctype (nreverse dest))
124         )))
125
126 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
127
128 (defun mime-parse-Content-Disposition (string)
129   "Parse STRING as field-body of Content-Disposition field. [mime-parse.el]"
130   (setq string (std11-unfold-string string))
131   (if (string-match mime::dtype-regexp string)
132       (let* ((e (match-end 0))
133              (ctype (downcase (substring string 0 e)))
134              ret dest)
135         (setq string (substring string e))
136         (while (setq ret (mime-parse-parameter string))
137           (setq dest (cons (car ret) dest)
138                 string (cdr ret))
139           )
140         (cons ctype (nreverse dest))
141         )))
142
143
144 ;;; @ field reader
145 ;;;
146
147 (defun mime/Content-Type ()
148   "Read field-body of Content-Type field from current-buffer,
149 and return parsed it. [mime-parse.el]"
150   (let ((str (std11-field-body "Content-Type")))
151     (if str
152         (mime-parse-Content-Type str)
153       )))
154
155 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
156   "Read field-body of Content-Transfer-Encoding field from
157 current-buffer, and return it.
158 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
159   (let ((str (std11-field-body "Content-Transfer-Encoding")))
160     (if str
161         (progn
162           (if (string-match "[ \t\n\r]+$" str)
163               (setq str (substring str 0 (match-beginning 0)))
164             )
165           (downcase str)
166           )
167       default-encoding)
168     ))
169
170 (defun mime/Content-Disposition ()
171   "Read field-body of Content-Disposition field from current-buffer,
172 and return parsed it. [mime-parse.el]"
173   (let ((str (std11-field-body "Content-Disposition")))
174     (if str
175         (mime-parse-Content-Disposition str)
176       )))
177
178
179 ;;; @ message parser
180 ;;;
181
182 (define-structure mime::content-info
183   rcnum point-min point-max type parameters encoding children)
184
185
186 (defun mime-parse-multipart (boundary ctype params encoding rcnum)
187   (goto-char (point-min))
188   (let* ((dash-boundary   (concat "--" boundary))
189          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
190          (close-delimiter (concat delimiter "--[ \t]*$"))
191          (beg (point-min))
192          (end (progn
193                 (goto-char (point-max))
194                 (if (re-search-backward close-delimiter nil t)
195                     (match-beginning 0)
196                   (point-max)
197                   )))
198          (rsep (concat delimiter "[ \t]*\n"))
199          (dc-ctl
200           (if (string-equal ctype "multipart/digest")
201               '("message/rfc822")
202             '("text/plain")
203             ))
204          cb ce ct ret ncb children (i 0))
205     (save-restriction
206       (narrow-to-region beg end)
207       (goto-char beg)
208       (re-search-forward rsep nil t)
209       (setq cb (match-end 0))
210       (while (re-search-forward rsep nil t)
211         (setq ce (match-beginning 0))
212         (setq ncb (match-end 0))
213         (save-restriction
214           (narrow-to-region cb ce)
215           (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
216           )
217         (setq children (cons ret children))
218         (goto-char (mime::content-info/point-max ret))
219         (goto-char (setq cb ncb))
220         (setq i (1+ i))
221         )
222       (setq ce (point-max))
223       (save-restriction
224         (narrow-to-region cb ce)
225         (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
226         )
227       (setq children (cons ret children))
228       )
229     (mime::content-info/create rcnum beg (point-max)
230                                ctype params encoding
231                                (nreverse children))
232     ))
233
234 (defun mime-parse-message (&optional ctl encoding rcnum)
235   "Parse current-buffer as a MIME message. [mime-parse.el]"
236   (setq ctl (or (mime/Content-Type) ctl))
237   (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
238   (let ((ctype (car ctl))
239         (params (cdr ctl))
240         )
241     (let ((boundary (assoc "boundary" params)))
242       (cond (boundary
243              (setq boundary (std11-strip-quoted-string (cdr boundary)))
244              (mime-parse-multipart boundary ctype params encoding rcnum)
245              )
246             ((or (string-equal ctype "message/rfc822")
247                  (string-equal ctype "message/news")
248                  )
249              (goto-char (point-min))
250              (mime::content-info/create rcnum
251                                         (point-min) (point-max)
252                                         ctype params encoding
253                                         (save-restriction
254                                           (narrow-to-region
255                                            (if (re-search-forward "^$" nil t)
256                                                (1+ (match-end 0))
257                                              (point-min)
258                                              )
259                                            (point-max))
260                                           (list (mime-parse-message
261                                                  nil nil (cons 0 rcnum)))
262                                           )
263                                         )
264              )
265             (t 
266              (mime::content-info/create rcnum (point-min) (point-max)
267                                         ctype params encoding nil)
268              ))
269       )))
270
271
272 ;;; @ end
273 ;;;
274
275 (provide 'mime-parse)
276
277 ;;; mime-parse.el ends here