update.
[elisp/semi.git] / mime-parse.el
1 ;;; mime-parse.el --- MIME message parser
2
3 ;; Copyright (C) 1994,1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: parse, MIME, multimedia, mail, news
7
8 ;; This file is part of SEMI (Spadework for Emacs MIME Interfaces).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'emu)
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 (mapconcat (function
35                       (lambda (s)
36                         (cond ((symbolp s) (symbol-name s))
37                               ((stringp s) s)
38                               )))
39                      args "")))
40
41
42 ;;; @ field parser
43 ;;;
44
45 (defsubst regexp-* (regexp)
46   (concat regexp "*"))
47
48 (defsubst regexp-or (&rest args)
49   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
50
51 (defconst rfc822/quoted-pair-regexp "\\\\.")
52 (defconst rfc822/qtext-regexp
53   (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
54 (defconst rfc822/quoted-string-regexp
55   (concat "\""
56           (regexp-*
57            (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
58            )
59           "\""))
60
61 (defconst mime/content-parameter-value-regexp
62   (concat "\\("
63           rfc822/quoted-string-regexp
64           "\\|[^; \t\n]*\\)"))
65
66 (defconst mime::parameter-regexp
67   (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
68           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
69
70 (defun mime-parse-parameter (str)
71   (if (string-match mime::parameter-regexp str)
72       (let ((e (match-end 2)))
73         (cons
74          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
75                (std11-strip-quoted-string
76                 (substring str (match-beginning 2) e))
77                )
78          (substring str e)
79          ))))
80
81 (defun mime-parse-Content-Type (string)
82   "Parse STRING as field-body of Content-Type field.
83 Return value is
84     (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
85 or nil.  PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
86 are string."
87   (setq string (std11-unfold-string string))
88   (if (string-match `,(concat "^\\(" mime-token-regexp
89                               "\\)/\\(" mime-token-regexp "\\)") string)
90       (let* ((type (downcase
91                     (substring string (match-beginning 1) (match-end 1))))
92              (subtype (downcase
93                        (substring string (match-beginning 2) (match-end 2))))
94              ret dest)
95         (setq string (substring string (match-end 0)))
96         (while (setq ret (mime-parse-parameter string))
97           (setq dest (cons (car ret) dest)
98                 string (cdr ret))
99           )
100         (cons (intern type) (cons (intern subtype) (nreverse dest)))
101         )))
102
103
104 (defconst mime-disposition-type-regexp mime-token-regexp)
105
106 (defun mime-parse-Content-Disposition (string)
107   "Parse STRING as field-body of Content-Disposition field."
108   (setq string (std11-unfold-string string))
109   (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
110       (let* ((e (match-end 0))
111              (ctype (downcase (substring string 0 e)))
112              ret dest)
113         (setq string (substring string e))
114         (while (setq ret (mime-parse-parameter string))
115           (setq dest (cons (car ret) dest)
116                 string (cdr ret))
117           )
118         (cons ctype (nreverse dest))
119         )))
120
121
122 ;;; @ field reader
123 ;;;
124
125 (defun mime-read-Content-Type ()
126   "Read field-body of Content-Type field from current-buffer,
127 and return parsed it.  Format of return value is as same as
128 `mime-parse-Content-Type'."
129   (let ((str (std11-field-body "Content-Type")))
130     (if str
131         (mime-parse-Content-Type str)
132       )))
133
134 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
135   "Read field-body of Content-Transfer-Encoding field from
136 current-buffer, and return it.
137 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
138   (let ((str (std11-field-body "Content-Transfer-Encoding")))
139     (if str
140         (progn
141           (if (string-match "[ \t\n\r]+$" str)
142               (setq str (substring str 0 (match-beginning 0)))
143             )
144           (downcase str)
145           )
146       default-encoding)
147     ))
148
149 (defun mime/Content-Disposition ()
150   "Read field-body of Content-Disposition field from current-buffer,
151 and return parsed it. [mime-parse.el]"
152   (let ((str (std11-field-body "Content-Disposition")))
153     (if str
154         (mime-parse-Content-Disposition str)
155       )))
156
157
158 ;;; @ message parser
159 ;;;
160
161 (defsubst make-mime-entity (node-id
162                             point-min point-max
163                             media-type media-subtype parameters
164                             encoding children)
165   (vector node-id point-min point-max
166           media-type media-subtype parameters encoding children))
167
168 (defsubst mime-entity-node-id (entity-info)       (aref entity-info 0))
169 (defsubst mime-entity-point-min (entity-info)     (aref entity-info 1))
170 (defsubst mime-entity-point-max (entity-info)     (aref entity-info 2))
171 (defsubst mime-entity-media-type (entity-info)    (aref entity-info 3))
172 (defsubst mime-entity-media-subtype (entity-info) (aref entity-info 4))
173 (defsubst mime-entity-parameters (entity-info)    (aref entity-info 5))
174 (defsubst mime-entity-encoding (entity-info)      (aref entity-info 6))
175 (defsubst mime-entity-children (entity-info)      (aref entity-info 7))
176
177 (defsubst mime-entity-type/subtype (entity-info)
178   (mime-type/subtype-string (mime-entity-media-type entity-info)
179                             (mime-entity-media-subtype entity-info)))
180
181 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
182   (goto-char (point-min))
183   (let* ((dash-boundary   (concat "--" boundary))
184          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
185          (close-delimiter (concat delimiter "--[ \t]*$"))
186          (beg (point-min))
187          (end (progn
188                 (goto-char (point-max))
189                 (if (re-search-backward close-delimiter nil t)
190                     (match-beginning 0)
191                   (point-max)
192                   )))
193          (rsep (concat delimiter "[ \t]*\n"))
194          (dc-ctl
195           (if (eq subtype 'digest)
196               '(message rfc822)
197             '(text plain)
198             ))
199          cb ce ret ncb children (i 0))
200     (save-restriction
201       (narrow-to-region beg end)
202       (goto-char beg)
203       (re-search-forward rsep nil t)
204       (setq cb (match-end 0))
205       (while (re-search-forward rsep nil t)
206         (setq ce (match-beginning 0))
207         (setq ncb (match-end 0))
208         (save-restriction
209           (narrow-to-region cb ce)
210           (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
211           )
212         (setq children (cons ret children))
213         (goto-char (mime-entity-point-max ret))
214         (goto-char (setq cb ncb))
215         (setq i (1+ i))
216         )
217       (setq ce (point-max))
218       (save-restriction
219         (narrow-to-region cb ce)
220         (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
221         )
222       (setq children (cons ret children))
223       )
224     (make-mime-entity rcnum beg (point-max)
225                       primtype subtype params encoding
226                       (nreverse children))
227     ))
228
229 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
230   "Parse current-buffer as a MIME message.
231 DEFAULT-CTL is used when an entity does not have valid Content-Type
232 field.  Its format must be as same as return value of
233 mime-{parse|read}-Content-Type."
234   (setq default-ctl (or (mime-read-Content-Type) default-ctl))
235   (let ((primtype (car default-ctl))
236         (subtype (car (cdr default-ctl)))
237         (params (cdr (cdr default-ctl)))
238         (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
239         )
240     (let ((boundary (assoc "boundary" params)))
241       (cond (boundary
242              (setq boundary (std11-strip-quoted-string (cdr boundary)))
243              (mime-parse-multipart
244               boundary
245               primtype subtype params encoding rcnum)
246              )
247             ((and (eq primtype 'message)
248                   (memq subtype '(rfc822 news))
249                   )
250              (goto-char (point-min))
251              (make-mime-entity rcnum (point-min) (point-max)
252                                primtype subtype 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             (t 
265              (make-mime-entity rcnum (point-min) (point-max)
266                                primtype subtype params encoding
267                                nil)
268              ))
269       )))
270
271
272 ;;; @ utilities
273 ;;;
274
275 (defsubst mime-root-entity-p (entity)
276   "Return t if ENTITY is root-entity (message)."
277   (null (mime-entity-node-id entity)))
278
279
280 ;;; @ end
281 ;;;
282
283 (provide 'mime-parse)
284
285 ;;; mime-parse.el ends here