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