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