Merge flim-1_12_7.
[elisp/flim.git] / mime-parse.el
1 ;;; mime-parse.el --- MIME message parser
2
3 ;; Copyright (C) 1994,1995,1996,1997,1998,1999 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 'mime-def)
28 (require 'std11)
29
30
31 ;;; @ lexical analyzer
32 ;;;
33
34 (defcustom mime-lexical-analyzer
35   '(std11-analyze-quoted-string
36     std11-analyze-domain-literal
37     std11-analyze-comment
38     std11-analyze-spaces
39     mime-analyze-tspecial
40     mime-analyze-token)
41   "*List of functions to return result of lexical analyze.
42 Each function must have two arguments: STRING and START.
43 STRING is the target string to be analyzed.
44 START is start position of STRING to analyze.
45
46 Previous function is preferred to next function.  If a function
47 returns nil, next function is used.  Otherwise the return value will
48 be the result."
49   :group 'mime
50   :type '(repeat function))
51
52 (defun mime-analyze-tspecial (string start)
53   (if (and (> (length string) start)
54            (memq (aref string start) mime-tspecial-char-list))
55       (cons (cons 'tpecials (substring string start (1+ start)))
56             (1+ start))
57     ))
58
59 (defun mime-analyze-token (string start)
60   (if (and (string-match mime-token-regexp string start)
61            (= (match-beginning 0) start))
62       (let ((end (match-end 0)))
63         (cons (cons 'mime-token (substring string start end))
64               ;;(substring string end)
65               end)
66         )))
67
68
69 ;;; @ field parser
70 ;;;
71
72 (defconst mime/content-parameter-value-regexp
73   (concat "\\("
74           std11-quoted-string-regexp
75           "\\|[^; \t\n]*\\)"))
76
77 (defconst mime::parameter-regexp
78   (concat "^[ \t]*\;[ \t]*\\(" mime-token-regexp "\\)"
79           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
80
81 (defun mime-parse-parameter (str)
82   (if (string-match mime::parameter-regexp str)
83       (let ((e (match-end 2)))
84         (cons
85          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
86                (std11-strip-quoted-string
87                 (substring str (match-beginning 2) e))
88                )
89          (substring str e)
90          ))))
91
92
93 ;;; @ Content-Type
94 ;;;
95
96 ;;;###autoload
97 (defun mime-parse-Content-Type (string)
98   "Parse STRING as field-body of Content-Type field.
99 Return value is
100     (PRIMARY-TYPE SUBTYPE (NAME1 . VALUE1)(NAME2 . VALUE2) ...)
101 or nil.  PRIMARY-TYPE and SUBTYPE are symbol and NAME_n and VALUE_n
102 are string."
103   (setq string (std11-unfold-string string))
104   (if (string-match `,(concat "^\\(" mime-token-regexp
105                               "\\)/\\(" mime-token-regexp "\\)") string)
106       (let* ((type (downcase
107                     (substring string (match-beginning 1) (match-end 1))))
108              (subtype (downcase
109                        (substring string (match-beginning 2) (match-end 2))))
110              ret dest)
111         (setq string (substring string (match-end 0)))
112         (while (setq ret (mime-parse-parameter string))
113           (setq dest (cons (car ret) dest)
114                 string (cdr ret))
115           )
116         (make-mime-content-type (intern type)(intern subtype)
117                                 (nreverse dest))
118         )))
119
120 ;;;###autoload
121 (defun mime-read-Content-Type ()
122   "Read field-body of Content-Type field from current-buffer,
123 and return parsed it.  Format of return value is as same as
124 `mime-parse-Content-Type'."
125   (let ((str (std11-field-body "Content-Type")))
126     (if str
127         (mime-parse-Content-Type str)
128       )))
129
130
131 ;;; @ Content-Disposition
132 ;;;
133
134 (eval-and-compile
135   (defconst mime-disposition-type-regexp mime-token-regexp)
136   )
137
138 ;;;###autoload
139 (defun mime-parse-Content-Disposition (string)
140   "Parse STRING as field-body of Content-Disposition field."
141   (setq string (std11-unfold-string string))
142   (if (string-match (eval-when-compile
143                       (concat "^" mime-disposition-type-regexp)) string)
144       (let* ((e (match-end 0))
145              (type (downcase (substring string 0 e)))
146              ret dest)
147         (setq string (substring string e))
148         (while (setq ret (mime-parse-parameter string))
149           (setq dest (cons (car ret) dest)
150                 string (cdr ret))
151           )
152         (cons (cons 'type (intern type))
153               (nreverse dest))
154         )))
155
156 ;;;###autoload
157 (defun mime-read-Content-Disposition ()
158   "Read field-body of Content-Disposition field from current-buffer,
159 and return parsed it."
160   (let ((str (std11-field-body "Content-Disposition")))
161     (if str
162         (mime-parse-Content-Disposition str)
163       )))
164
165
166 ;;; @ Content-Transfer-Encoding
167 ;;;
168
169 ;;;###autoload
170 (defun mime-parse-Content-Transfer-Encoding (string)
171   "Parse STRING as field-body of Content-Transfer-Encoding field."
172   (let ((tokens (std11-lexical-analyze string mime-lexical-analyzer))
173         token)
174     (while (and tokens
175                 (setq token (car tokens))
176                 (std11-ignored-token-p token))
177       (setq tokens (cdr tokens)))
178     (if token
179         (if (eq (car token) 'mime-token)
180             (downcase (cdr token))
181           ))))
182
183 ;;;###autoload
184 (defun mime-read-Content-Transfer-Encoding (&optional default-encoding)
185   "Read field-body of Content-Transfer-Encoding field from
186 current-buffer, and return it.
187 If is is not found, return DEFAULT-ENCODING."
188   (let ((str (std11-field-body "Content-Transfer-Encoding")))
189     (if str
190         (mime-parse-Content-Transfer-Encoding str)
191       default-encoding)))
192
193
194 ;;; @ Content-Id / Message-Id
195 ;;;
196
197 ;;;###autoload
198 (defun mime-parse-msg-id (tokens)
199   "Parse TOKENS as msg-id of Content-Id or Message-Id field."
200   (car (std11-parse-msg-id tokens)))
201
202 ;;;###autoload
203 (defun mime-uri-parse-cid (string)
204   "Parse STRING as cid URI."
205   (inline
206     (mime-parse-msg-id (cons '(specials . "<")
207                              (nconc
208                               (cdr (cdr (std11-lexical-analyze string)))
209                               '((specials . ">")))))))
210
211
212 ;;; @ message parser
213 ;;;
214
215 (defun mime-parse-multipart (entity)
216   (goto-char (point-min))
217   (let* ((representation-type
218           (mime-entity-representation-type-internal entity))
219          (content-type (mime-entity-content-type-internal entity))
220          (dash-boundary
221           (concat "--" (mime-content-type-parameter content-type "boundary")))
222          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
223          (close-delimiter (concat delimiter "--[ \t]*$"))
224          (rsep (concat delimiter "[ \t]*\n"))
225          (dc-ctl
226           (if (eq (mime-content-type-subtype content-type) 'digest)
227               (make-mime-content-type 'message 'rfc822)
228             (make-mime-content-type 'text 'plain)
229             ))
230          (header-end (mime-entity-header-end-internal entity))
231          (body-end (mime-entity-body-end-internal entity)))
232     (save-restriction
233       (goto-char body-end)
234       (narrow-to-region header-end
235                         (if (re-search-backward close-delimiter nil t)
236                             (match-beginning 0)
237                           body-end))
238       (goto-char header-end)
239       (if (re-search-forward rsep nil t)
240           (let ((cb (match-end 0))
241                 ce ncb ret children
242                 (node-id (mime-entity-node-id-internal entity))
243                 (i 0))
244             (while (re-search-forward rsep nil t)
245               (setq ce (match-beginning 0))
246               (setq ncb (match-end 0))
247               (save-restriction
248                 (narrow-to-region cb ce)
249                 (setq ret (mime-parse-message representation-type dc-ctl
250                                               entity (cons i node-id)))
251                 )
252               (setq children (cons ret children))
253               (goto-char (setq cb ncb))
254               (setq i (1+ i))
255               )
256             (setq ce (point-max))
257             (save-restriction
258               (narrow-to-region cb ce)
259               (setq ret (mime-parse-message representation-type dc-ctl
260                                             entity (cons i node-id)))
261               )
262             (setq children (cons ret children))
263             (mime-entity-set-children-internal entity (nreverse children))
264             )
265         (mime-entity-set-content-type-internal
266          entity (make-mime-content-type 'message 'x-broken))
267         nil)
268       )))
269
270 (defun mime-parse-encapsulated (entity)
271   (mime-entity-set-children-internal
272    entity
273    (save-restriction
274      (narrow-to-region (mime-entity-body-start-internal entity)
275                        (mime-entity-body-end-internal entity))
276      (list (mime-parse-message
277             (mime-entity-representation-type-internal entity) nil
278             entity (cons 0 (mime-entity-node-id-internal entity))))
279      )))
280
281 (defun mime-parse-message (representation-type &optional default-ctl 
282                                                parent node-id)
283   (let ((header-start (point-min))
284         header-end
285         body-start
286         (body-end (point-max))
287         content-type)
288     (goto-char header-start)
289     (if (re-search-forward "^$" nil t)
290         (setq header-end (match-end 0)
291               body-start (if (= header-end body-end)
292                              body-end
293                            (1+ header-end)))
294       (setq header-end (point-min)
295             body-start (point-min)))
296     (save-restriction
297       (narrow-to-region header-start header-end)
298       (setq content-type (or (let ((str (std11-fetch-field "Content-Type")))
299                                (if str
300                                    (mime-parse-Content-Type str)
301                                  ))
302                              default-ctl))
303       )
304     (make-mime-entity-internal representation-type
305                                (current-buffer)
306                                content-type nil parent node-id
307                                nil nil nil nil
308                                nil nil nil nil
309                                nil nil
310                                (current-buffer)
311                                header-start header-end
312                                body-start body-end)
313     ))
314
315
316 ;;; @ for buffer
317 ;;;
318
319 ;;;###autoload
320 (defun mime-parse-buffer (&optional buffer representation-type)
321   "Parse BUFFER as a MIME message.
322 If buffer is omitted, it parses current-buffer."
323   (save-excursion
324     (if buffer (set-buffer buffer))
325     (setq mime-message-structure
326           (mime-parse-message (or representation-type 'buffer) nil))
327     ))
328
329
330 ;;; @ end
331 ;;;
332
333 (provide 'mime-parse)
334
335 ;;; mime-parse.el ends here