493bbcb59943f553b04729fcabf870ac4f5e3b0d
[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-type/subtype-string (type &optional subtype)
175   "Return type/subtype string from TYPE and SUBTYPE."
176   (if type
177       (if subtype
178           (format "%s/%s" type subtype)
179         (format "%s" type))))
180
181 (defsubst mime-entity-type/subtype (entity-info)
182   (mime-type/subtype-string (mime-entity-media-type entity-info)
183                             (mime-entity-media-subtype entity-info)))
184
185 (defun mime-parse-multipart (boundary primtype subtype params encoding rcnum)
186   (goto-char (point-min))
187   (let* ((dash-boundary   (concat "--" boundary))
188          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
189          (close-delimiter (concat delimiter "--[ \t]*$"))
190          (beg (point-min))
191          (end (progn
192                 (goto-char (point-max))
193                 (if (re-search-backward close-delimiter nil t)
194                     (match-beginning 0)
195                   (point-max)
196                   )))
197          (rsep (concat delimiter "[ \t]*\n"))
198          (dc-ctl
199           (if (eq subtype 'digest)
200               '(message rfc822)
201             '(text plain)
202             ))
203          cb ce ret ncb children (i 0))
204     (save-restriction
205       (narrow-to-region beg end)
206       (goto-char beg)
207       (re-search-forward rsep nil t)
208       (setq cb (match-end 0))
209       (while (re-search-forward rsep nil t)
210         (setq ce (match-beginning 0))
211         (setq ncb (match-end 0))
212         (save-restriction
213           (narrow-to-region cb ce)
214           (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
215           )
216         (setq children (cons ret children))
217         (goto-char (mime-entity-point-max ret))
218         (goto-char (setq cb ncb))
219         (setq i (1+ i))
220         )
221       (setq ce (point-max))
222       (save-restriction
223         (narrow-to-region cb ce)
224         (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
225         )
226       (setq children (cons ret children))
227       )
228     (make-mime-entity rcnum beg (point-max)
229                       primtype subtype params encoding
230                       (nreverse children))
231     ))
232
233 (defun mime-parse-message (&optional default-ctl default-encoding rcnum)
234   "Parse current-buffer as a MIME message.
235 DEFAULT-CTL is used when an entity does not have valid Content-Type
236 field.  Its format must be as same as return value of
237 mime-{parse|read}-Content-Type."
238   (setq default-ctl (or (mime-read-Content-Type) default-ctl))
239   (let ((primtype (car default-ctl))
240         (subtype (car (cdr default-ctl)))
241         (params (cdr (cdr default-ctl)))
242         (encoding (or (mime/Content-Transfer-Encoding) default-encoding))
243         )
244     (let ((boundary (assoc "boundary" params)))
245       (cond (boundary
246              (setq boundary (std11-strip-quoted-string (cdr boundary)))
247              (mime-parse-multipart
248               boundary
249               primtype subtype params encoding rcnum)
250              )
251             ((and (eq primtype 'message)
252                   (memq subtype '(rfc822 news))
253                   )
254              (goto-char (point-min))
255              (make-mime-entity rcnum (point-min) (point-max)
256                                primtype subtype params encoding
257                                (save-restriction
258                                  (narrow-to-region
259                                   (if (re-search-forward "^$" nil t)
260                                       (1+ (match-end 0))
261                                     (point-min)
262                                     )
263                                   (point-max))
264                                  (list (mime-parse-message
265                                         nil nil (cons 0 rcnum)))
266                                  ))
267              )
268             (t 
269              (make-mime-entity rcnum (point-min) (point-max)
270                                primtype subtype params encoding
271                                nil)
272              ))
273       )))
274
275
276 ;;; @ utilities
277 ;;;
278
279 (defsubst mime-root-entity-p (entity)
280   "Return t if ENTITY is root-entity (message)."
281   (null (mime-entity-node-id entity)))
282
283
284 ;;; @ end
285 ;;;
286
287 (provide 'mime-parse)
288
289 ;;; mime-parse.el ends here