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