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