e5a390fc7274c7657d76a4fa4e8cc70e4f4ee2e7
[elisp/tm.git] / tm-parse.el
1 ;;; tm-parse.el --- MIME message parser
2
3 ;; Copyright (C) 1994,1995,1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: tm-parse.el,v 7.13 1996/09/20 07:27:41 morioka Exp $
7 ;; Keywords: mail, news, MIME, multimedia
8
9 ;; This file is part of tm (Tools for MIME).
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 this program; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'std11)
29 (require 'tl-misc)
30 (require 'tm-def)
31
32
33 ;;; @ field parser
34 ;;;
35
36 (defconst mime::parameter-regexp
37   (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
38           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
39
40 (defun mime/parse-parameter (str)
41   (if (string-match mime::parameter-regexp str)
42       (let ((e (match-end 2)))
43         (cons
44          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
45                (std11-strip-quoted-string
46                 (substring str (match-beginning 2) e))
47                )
48          (substring str e)
49          ))))
50
51 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
52
53 (defun mime/parse-Content-Type (string)
54   "Parse STRING as field-body of Content-Type field. [tm-parse.el]"
55   (setq string (std11-unfold-string string))
56   (if (string-match mime::ctype-regexp string)
57       (let* ((e (match-end 0))
58              (ctype (downcase (substring string 0 e)))
59              ret dest)
60         (setq string (substring string e))
61         (while (setq ret (mime/parse-parameter string))
62           (setq dest (cons (car ret) dest)
63                 string (cdr ret))
64           )
65         (cons ctype (nreverse dest))
66         )))
67
68 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
69
70 (defun mime/parse-Content-Disposition (string)
71   "Parse STRING as field-body of Content-Disposition field. [tm-parse.el]"
72   (setq string (std11-unfold-string string))
73   (if (string-match mime::dtype-regexp string)
74       (let* ((e (match-end 0))
75              (ctype (downcase (substring string 0 e)))
76              ret dest)
77         (setq string (substring string e))
78         (while (setq ret (mime/parse-parameter string))
79           (setq dest (cons (car ret) dest)
80                 string (cdr ret))
81           )
82         (cons ctype (nreverse dest))
83         )))
84
85
86 ;;; @ field reader
87 ;;;
88
89 (defun mime/Content-Type ()
90   "Read field-body of Content-Type field from current-buffer,
91 and return parsed it. [tm-parse.el]"
92   (let ((str (std11-field-body "Content-Type")))
93     (if str
94         (mime/parse-Content-Type str)
95       )))
96
97 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
98   "Read field-body of Content-Transfer-Encoding field from
99 current-buffer, and return it.
100 If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
101   (let ((str (std11-field-body "Content-Transfer-Encoding")))
102     (if str
103         (progn
104           (if (string-match "[ \t\n\r]+$" str)
105               (setq str (substring str 0 (match-beginning 0)))
106             )
107           (downcase str)
108           )
109       default-encoding)
110     ))
111
112 (defun mime/Content-Disposition ()
113   "Read field-body of Content-Disposition field from current-buffer,
114 and return parsed it. [tm-parse.el]"
115   (let ((str (std11-field-body "Content-Disposition")))
116     (if str
117         (mime/parse-Content-Disposition str)
118       )))
119
120
121 ;;; @ message parser
122 ;;;
123
124 (define-structure mime::content-info
125   rcnum point-min point-max type parameters encoding children)
126
127
128 (defun mime/parse-multipart (boundary ctype params encoding rcnum)
129   (goto-char (point-min))
130   (let* ((dash-boundary   (concat "--" boundary))
131          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
132          (close-delimiter (concat delimiter "--[ \t]*$"))
133          (beg (point-min))
134          (end (progn
135                 (goto-char (point-max))
136                 (if (re-search-backward close-delimiter nil t)
137                     (match-beginning 0)
138                   (point-max)
139                   )))
140          (rsep (concat delimiter "[ \t]*\n"))
141          (dc-ctl
142           (if (string-equal ctype "multipart/digest")
143               '("message/rfc822")
144             '("text/plain")
145             ))
146          cb ce ct ret ncb children (i 0))
147     (save-restriction
148       (narrow-to-region beg end)
149       (goto-char beg)
150       (re-search-forward rsep nil t)
151       (setq cb (match-end 0))
152       (while (re-search-forward rsep nil t)
153         (setq ce (match-beginning 0))
154         (setq ncb (match-end 0))
155         (save-restriction
156           (narrow-to-region cb ce)
157           (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
158           )
159         (setq children (cons ret children))
160         (goto-char (mime::content-info/point-max ret))
161         (goto-char (setq cb ncb))
162         (setq i (1+ i))
163         )
164       (setq ce (point-max))
165       (save-restriction
166         (narrow-to-region cb ce)
167         (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
168         )
169       (setq children (cons ret children))
170       )
171     (mime::content-info/create rcnum beg (point-max)
172                                ctype params encoding
173                                (nreverse children))
174     ))
175
176 (defun mime/parse-message (&optional ctl encoding rcnum)
177   "Parse current-buffer as a MIME message. [tm-parse.el]"
178   (setq ctl (or (mime/Content-Type) ctl))
179   (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
180   (let ((ctype (car ctl))
181         (params (cdr ctl))
182         )
183     (let ((boundary (assoc "boundary" params)))
184       (cond (boundary
185              (setq boundary (std11-strip-quoted-string (cdr boundary)))
186              (mime/parse-multipart boundary ctype params encoding rcnum)
187              )
188             ((or (string-equal ctype "message/rfc822")
189                  (string-equal ctype "message/news")
190                  )
191              (goto-char (point-min))
192              (mime::content-info/create rcnum
193                                         (point-min) (point-max)
194                                         ctype params encoding
195                                         (save-restriction
196                                           (narrow-to-region
197                                            (if (re-search-forward "^$" nil t)
198                                                (1+ (match-end 0))
199                                              (point-min)
200                                              )
201                                            (point-max))
202                                           (list (mime/parse-message
203                                                  nil nil (cons 0 rcnum)))
204                                           )
205                                         )
206              )
207             (t 
208              (mime::content-info/create rcnum (point-min) (point-max)
209                                         ctype params encoding nil)
210              ))
211       )))
212
213
214 ;;; @ end
215 ;;;
216
217 (provide 'tm-parse)
218
219 ;;; tm-parse.el ends here