tm 7.6.
[elisp/tm.git] / tm-parse.el
1 ;;;
2 ;;; tm-parse.el --- MIME message parser
3 ;;;
4 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;;; Copyright (C) 1994,1995 MORIOKA Tomohiko
6 ;;;
7 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;; Version:
9 ;;;     $Id: tm-parse.el,v 2.4 1995/09/26 13:19:32 morioka Exp $
10 ;;; Keywords: mail, news, MIME, multimedia
11 ;;;
12 ;;; This file is part of tm (Tools for MIME).
13 ;;;
14
15 (require 'tl-header)
16 (require 'tl-misc)
17 (require 'tm-def)
18
19
20 ;;; @ field parser
21 ;;;
22
23 (defconst mime::parameter-regexp
24   (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
25           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
26
27 (defun mime/parse-parameter (str)
28   (if (string-match mime::parameter-regexp str)
29       (let ((e (match-end 2)))
30         (cons
31          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
32                (message/strip-quoted-string
33                 (substring str (match-beginning 2) e))
34                )
35          (substring str e)
36          ))))
37
38 (defconst mime::ctype-regexp
39   (concat "^" mime/content-type-subtype-regexp))
40
41 (defun mime/parse-Content-Type (str)
42   "Parse STR as field-body of Content-Type field. [tm-parse.el]"
43   (setq str (message/unfolding-string str))
44   (if (string-match mime::ctype-regexp str)
45       (let* ((e (match-end 0))
46              (ctype (downcase (substring str 0 e)))
47              ret dest)
48         (setq str (substring str e))
49         (while (setq ret (mime/parse-parameter str))
50           (setq dest (cons (car ret) dest))
51           (setq str (cdr ret))
52           )
53         (cons ctype (reverse dest))
54         )))
55
56
57 ;;; @ field reader
58 ;;;
59
60 (defun mime/Content-Type ()
61   "Read field-body of Content-Type field from current-buffer,
62 and return parsed it. [tm-parse.el]"
63   (let ((str (message/get-field-body "Content-Type")))
64     (if str
65         (mime/parse-Content-Type str)
66       )))
67
68 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
69   "Read field-body of Content-Transfer-Encoding field from
70 current-buffer, and return it.
71 If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
72   (let ((str (message/get-field-body "Content-Transfer-Encoding")))
73     (if str
74         (downcase str)
75       default-encoding)
76     ))
77
78
79 ;;; @ message parser
80 ;;;
81
82 (define-structure mime::content-info
83   point-min point-max type parameters encoding children)
84
85
86 (defun mime/parse-multipart (boundary ctype params encoding)
87   (goto-char (point-min))
88   (let ((beg (point-min))
89         (end (if (re-search-forward
90                   (concat "^--" (regexp-quote boundary) "--$") nil t)
91                  (match-beginning 0)
92                (point-max)
93                ))
94         (rsep (concat "^--" (regexp-quote boundary) "\n"))
95         (dc-ctl
96          (cond ((string= ctype "multipart/digest") '("message/rfc822"))
97                (t '("text/plain"))))
98         cb ce ct ret ncb children)
99     (save-restriction
100       (narrow-to-region beg end)
101       (goto-char beg)
102       (re-search-forward rsep nil t)
103       (setq cb (match-end 0))
104       (while (re-search-forward rsep nil t)
105         (setq ce (match-beginning 0))
106         (setq ncb (match-end 0))
107         (save-restriction
108           (narrow-to-region cb ce)
109           (setq ret (mime/parse-message dc-ctl "7bit"))
110           )
111         (setq children (cons ret children))
112         (goto-char (mime::content-info/point-max ret))
113         (goto-char (setq cb ncb))
114         )
115       (setq ce (point-max))
116       (save-restriction
117         (narrow-to-region cb ce)
118         (setq ret (mime/parse-message dc-ctl "7bit"))
119         )
120       (setq children (cons ret children))
121       )
122     (mime::content-info/create beg end ctype params encoding
123                                (reverse children))
124     ))
125
126 (defun mime/parse-message (&optional ctl encoding)
127   "Parse current-buffer as a MIME message. [tm-parse.el]"
128   (setq ctl (or (mime/Content-Type) ctl))
129   (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
130   (let ((ctype (car ctl))
131         (params (cdr ctl))
132         )
133     (let ((boundary (assoc "boundary" params)))
134       (cond (boundary
135              (setq boundary (message/strip-quoted-string (cdr boundary)))
136              (mime/parse-multipart boundary ctype params encoding)
137              )
138             ((string= ctype "message/rfc822")
139              (goto-char (point-min))
140              (mime::content-info/create (point-min) (point-max)
141                                         ctype params encoding
142                                         (save-restriction
143                                           (narrow-to-region
144                                            (if (re-search-forward "^$" nil t)
145                                                (+ (match-end 0) 1)
146                                              (point-min)
147                                              )
148                                            (point-max))
149                                           (list (mime/parse-message))
150                                           )
151                                         )
152              )
153             (t 
154              (mime::content-info/create (point-min) (point-max)
155                                         ctype params encoding nil)
156              ))
157       )))
158
159
160 ;;; @ end
161 ;;;
162
163 (provide 'tm-parse)