Rename `mime-entity-info' -> `mime-entity'.
[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 (defconst rfc822/quoted-pair-regexp "\\\\.")
49 (defconst rfc822/qtext-regexp
50   (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
51 (defconst rfc822/quoted-string-regexp
52   (concat "\""
53           (regexp-*
54            (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
55            )
56           "\""))
57
58 (defconst mime/content-parameter-value-regexp
59   (concat "\\("
60           rfc822/quoted-string-regexp
61           "\\|[^; \t\n]*\\)"))
62
63 (defconst mime::parameter-regexp
64   (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
65           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
66
67 (defun mime-parse-parameter (str)
68   (if (string-match mime::parameter-regexp str)
69       (let ((e (match-end 2)))
70         (cons
71          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
72                (std11-strip-quoted-string
73                 (substring str (match-beginning 2) e))
74                )
75          (substring str e)
76          ))))
77
78 (defun mime-parse-Content-Type (string)
79   "Parse STRING as field-body of Content-Type field.
80 Return value is
81     (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
82 or nil.  PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
83 are string."
84   (setq string (std11-unfold-string string))
85   (if (string-match `,(concat "^\\(" mime-token-regexp
86                               "\\)/\\(" mime-token-regexp "\\)") string)
87       (let* ((type (downcase
88                     (substring string (match-beginning 1) (match-end 1))))
89              (subtype (downcase
90                        (substring string (match-beginning 2) (match-end 2))))
91              ret dest)
92         (setq string (substring string (match-end 0)))
93         (while (setq ret (mime-parse-parameter string))
94           (setq dest (cons (car ret) dest)
95                 string (cdr ret))
96           )
97         (cons (intern type) (cons (intern subtype) (nreverse dest)))
98         )))
99
100
101 (defconst mime-disposition-type-regexp mime-token-regexp)
102
103 (defun mime-parse-Content-Disposition (string)
104   "Parse STRING as field-body of Content-Disposition field."
105   (setq string (std11-unfold-string string))
106   (if (string-match `,(concat "^" mime-disposition-type-regexp) string)
107       (let* ((e (match-end 0))
108              (ctype (downcase (substring string 0 e)))
109              ret dest)
110         (setq string (substring string e))
111         (while (setq ret (mime-parse-parameter string))
112           (setq dest (cons (car ret) dest)
113                 string (cdr ret))
114           )
115         (cons ctype (nreverse dest))
116         )))
117
118
119 ;;; @ field reader
120 ;;;
121
122 (defun mime-read-Content-Type ()
123   "Read field-body of Content-Type field from current-buffer,
124 and return parsed it.  Format of return value is as same as
125 `mime-parse-Content-Type'."
126   (let ((str (std11-field-body "Content-Type")))
127     (if str
128         (mime-parse-Content-Type str)
129       )))
130
131 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
132   "Read field-body of Content-Transfer-Encoding field from
133 current-buffer, and return it.
134 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
135   (let ((str (std11-field-body "Content-Transfer-Encoding")))
136     (if str
137         (progn
138           (if (string-match "[ \t\n\r]+$" str)
139               (setq str (substring str 0 (match-beginning 0)))
140             )
141           (downcase str)
142           )
143       default-encoding)
144     ))
145
146 (defun mime/Content-Disposition ()
147   "Read field-body of Content-Disposition field from current-buffer,
148 and return parsed it. [mime-parse.el]"
149   (let ((str (std11-field-body "Content-Disposition")))
150     (if str
151         (mime-parse-Content-Disposition str)
152       )))
153
154
155 ;;; @ message parser
156 ;;;
157
158 (defsubst make-mime-entity (node-id
159                             point-min point-max
160                             media-type media-subtype parameters
161                             encoding children)
162   (vector node-id point-min point-max
163           media-type media-subtype parameters encoding children))
164
165 (defsubst mime-entity-node-id (entity-info)       (aref entity-info 0))
166 (defsubst mime-entity-point-min (entity-info)     (aref entity-info 1))
167 (defsubst mime-entity-point-max (entity-info)     (aref entity-info 2))
168 (defsubst mime-entity-media-type (entity-info)    (aref entity-info 3))
169 (defsubst mime-entity-media-subtype (entity-info) (aref entity-info 4))
170 (defsubst mime-entity-parameters (entity-info)    (aref entity-info 5))
171 (defsubst mime-entity-encoding (entity-info)      (aref entity-info 6))
172 (defsubst mime-entity-children (entity-info)      (aref entity-info 7))
173
174 (defsubst mime-entity-type/subtype (entity-info)
175   (let ((type (mime-entity-media-type entity-info)))
176     (if type
177         (let ((subtype (mime-entity-media-subtype entity-info)))
178           (if subtype
179               (format "%s/%s" type subtype)
180             (symbol-name type))))))
181
182 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
183   (goto-char (point-min))
184   (let* ((dash-boundary   (concat "--" boundary))
185          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
186          (close-delimiter (concat delimiter "--[ \t]*$"))
187          (beg (point-min))
188          (end (progn
189                 (goto-char (point-max))
190                 (if (re-search-backward close-delimiter nil t)
191                     (match-beginning 0)
192                   (point-max)
193                   )))
194          (rsep (concat delimiter "[ \t]*\n"))
195          (dc-ctl
196           (if (eq subtype 'digest)
197               '(message rfc822)
198             '(text plain)
199             ))
200          cb ce ret ncb children (i 0))
201     (save-restriction
202       (narrow-to-region beg end)
203       (goto-char beg)
204       (re-search-forward rsep nil t)
205       (setq cb (match-end 0))
206       (while (re-search-forward rsep nil t)
207         (setq ce (match-beginning 0))
208         (setq ncb (match-end 0))
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         (goto-char (mime-entity-point-max ret))
215         (goto-char (setq cb ncb))
216         (setq i (1+ i))
217         )
218       (setq ce (point-max))
219       (save-restriction
220         (narrow-to-region cb ce)
221         (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
222         )
223       (setq children (cons ret children))
224       )
225     (make-mime-entity rcnum beg (point-max)
226                       primtype subtype params encoding
227                       (nreverse children))
228     ))
229
230 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
231   "Parse current-buffer as a MIME message.
232 DEFAULT-CTL is used when an entity does not have valid Content-Type
233 field.  Its format must be as same as return value of
234 mime-{parse|read}-Content-Type."
235   (setq default-ctl (or (mime-read-Content-Type) default-ctl))
236   (let ((primtype (car default-ctl))
237         (subtype (car (cdr default-ctl)))
238         (params (cdr (cdr default-ctl)))
239         (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
240         )
241     (let ((boundary (assoc "boundary" params)))
242       (cond (boundary
243              (setq boundary (std11-strip-quoted-string (cdr boundary)))
244              (mime-parse-multipart
245               boundary
246               primtype subtype params encoding rcnum)
247              )
248             ((and (eq primtype 'message)
249                   (memq subtype '(rfc822 news))
250                   )
251              (goto-char (point-min))
252              (make-mime-entity rcnum (point-min) (point-max)
253                                primtype subtype params encoding
254                                (save-restriction
255                                  (narrow-to-region
256                                   (if (re-search-forward "^$" nil t)
257                                       (1+ (match-end 0))
258                                     (point-min)
259                                     )
260                                   (point-max))
261                                  (list (mime-parse-message
262                                         nil nil (cons 0 rcnum)))
263                                  ))
264              )
265             (t 
266              (make-mime-entity rcnum (point-min) (point-max)
267                                primtype subtype params encoding
268                                nil)
269              ))
270       )))
271
272
273 ;;; @ end
274 ;;;
275
276 (provide 'mime-parse)
277
278 ;;; mime-parse.el ends here