(regexp-*): New inline-function; copied from tl-str.el.
[elisp/semi.git] / mime-parse.el
1 ;;; mime-parse.el --- MIME message parser
2
3 ;; Copyright (C) 1994,1995,1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: mime-parse.el,v 0.4 1997-02-27 13:36:22 tmorioka Exp $
7 ;; Keywords: parse, MIME, multimedia, mail, news
8
9 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'std11)
29 (require 'mime-def)
30
31 (defmacro define-structure (name &rest slots)
32   (let ((pred (symbol-concat name '-p)))
33     (cons 'progn
34           (nconc
35            (list
36             (` (defun (, pred) (obj)
37                  (and (vectorp obj)
38                       (eq (elt obj 0) '(, name))
39                       ))
40                )
41             (` (defun (, (symbol-concat name '/create)) (, slots)
42                  (, (cons 'vector (cons (list 'quote name) slots)))
43                  )
44                ))
45            (let ((i 1))
46              (mapcar (function
47                       (lambda (slot)
48                         (prog1
49                             (` (defun (, (symbol-concat name '/ slot)) (obj)
50                                  (if ((, pred) obj)
51                                      (elt obj (, i))
52                                    ))
53                                )
54                           (setq i (+ i 1))
55                           )
56                         )) slots)
57              )
58            (list (list 'quote name))
59            ))))
60
61
62 ;;; @ field parser
63 ;;;
64
65 (defsubst regexp-* (regexp)
66   (concat regexp "*"))
67
68 (defconst rfc822/quoted-pair-regexp "\\\\.")
69 (defconst rfc822/qtext-regexp
70   (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
71 (defconst rfc822/quoted-string-regexp
72   (concat "\""
73           (regexp-*
74            (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
75            )
76           "\""))
77
78 (defconst mime/content-parameter-value-regexp
79   (concat "\\("
80           rfc822/quoted-string-regexp
81           "\\|[^; \t\n]*\\)"))
82
83 (defconst mime::parameter-regexp
84   (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
85           "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
86
87 (defun mime-parse-parameter (str)
88   (if (string-match mime::parameter-regexp str)
89       (let ((e (match-end 2)))
90         (cons
91          (cons (downcase (substring str (match-beginning 1) (match-end 1)))
92                (std11-strip-quoted-string
93                 (substring str (match-beginning 2) e))
94                )
95          (substring str e)
96          ))))
97
98 (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
99
100 (defun mime-parse-Content-Type (string)
101   "Parse STRING as field-body of Content-Type field. [mime-parse.el]"
102   (setq string (std11-unfold-string string))
103   (if (string-match mime::ctype-regexp string)
104       (let* ((e (match-end 0))
105              (ctype (downcase (substring string 0 e)))
106              ret dest)
107         (setq string (substring string e))
108         (while (setq ret (mime-parse-parameter string))
109           (setq dest (cons (car ret) dest)
110                 string (cdr ret))
111           )
112         (cons ctype (nreverse dest))
113         )))
114
115 (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
116
117 (defun mime-parse-Content-Disposition (string)
118   "Parse STRING as field-body of Content-Disposition field. [mime-parse.el]"
119   (setq string (std11-unfold-string string))
120   (if (string-match mime::dtype-regexp string)
121       (let* ((e (match-end 0))
122              (ctype (downcase (substring string 0 e)))
123              ret dest)
124         (setq string (substring string e))
125         (while (setq ret (mime-parse-parameter string))
126           (setq dest (cons (car ret) dest)
127                 string (cdr ret))
128           )
129         (cons ctype (nreverse dest))
130         )))
131
132
133 ;;; @ field reader
134 ;;;
135
136 (defun mime/Content-Type ()
137   "Read field-body of Content-Type field from current-buffer,
138 and return parsed it. [mime-parse.el]"
139   (let ((str (std11-field-body "Content-Type")))
140     (if str
141         (mime-parse-Content-Type str)
142       )))
143
144 (defun mime/Content-Transfer-Encoding (&optional default-encoding)
145   "Read field-body of Content-Transfer-Encoding field from
146 current-buffer, and return it.
147 If is is not found, return DEFAULT-ENCODING. [mime-parse.el]"
148   (let ((str (std11-field-body "Content-Transfer-Encoding")))
149     (if str
150         (progn
151           (if (string-match "[ \t\n\r]+$" str)
152               (setq str (substring str 0 (match-beginning 0)))
153             )
154           (downcase str)
155           )
156       default-encoding)
157     ))
158
159 (defun mime/Content-Disposition ()
160   "Read field-body of Content-Disposition field from current-buffer,
161 and return parsed it. [mime-parse.el]"
162   (let ((str (std11-field-body "Content-Disposition")))
163     (if str
164         (mime-parse-Content-Disposition str)
165       )))
166
167
168 ;;; @ message parser
169 ;;;
170
171 (define-structure mime::content-info
172   rcnum point-min point-max type parameters encoding children)
173
174
175 (defun mime-parse-multipart (boundary ctype params encoding rcnum)
176   (goto-char (point-min))
177   (let* ((dash-boundary   (concat "--" boundary))
178          (delimiter       (concat "\n" (regexp-quote dash-boundary)))
179          (close-delimiter (concat delimiter "--[ \t]*$"))
180          (beg (point-min))
181          (end (progn
182                 (goto-char (point-max))
183                 (if (re-search-backward close-delimiter nil t)
184                     (match-beginning 0)
185                   (point-max)
186                   )))
187          (rsep (concat delimiter "[ \t]*\n"))
188          (dc-ctl
189           (if (string-equal ctype "multipart/digest")
190               '("message/rfc822")
191             '("text/plain")
192             ))
193          cb ce ct ret ncb children (i 0))
194     (save-restriction
195       (narrow-to-region beg end)
196       (goto-char beg)
197       (re-search-forward rsep nil t)
198       (setq cb (match-end 0))
199       (while (re-search-forward rsep nil t)
200         (setq ce (match-beginning 0))
201         (setq ncb (match-end 0))
202         (save-restriction
203           (narrow-to-region cb ce)
204           (setq ret (mime-parse-message dc-ctl "7bit" (cons i rcnum)))
205           )
206         (setq children (cons ret children))
207         (goto-char (mime::content-info/point-max ret))
208         (goto-char (setq cb ncb))
209         (setq i (1+ i))
210         )
211       (setq ce (point-max))
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       )
218     (mime::content-info/create rcnum beg (point-max)
219                                ctype params encoding
220                                (nreverse children))
221     ))
222
223 (defun mime-parse-message (&optional ctl encoding rcnum)
224   "Parse current-buffer as a MIME message. [mime-parse.el]"
225   (setq ctl (or (mime/Content-Type) ctl))
226   (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
227   (let ((ctype (car ctl))
228         (params (cdr ctl))
229         )
230     (let ((boundary (assoc "boundary" params)))
231       (cond (boundary
232              (setq boundary (std11-strip-quoted-string (cdr boundary)))
233              (mime-parse-multipart boundary ctype params encoding rcnum)
234              )
235             ((or (string-equal ctype "message/rfc822")
236                  (string-equal ctype "message/news")
237                  )
238              (goto-char (point-min))
239              (mime::content-info/create rcnum
240                                         (point-min) (point-max)
241                                         ctype params encoding
242                                         (save-restriction
243                                           (narrow-to-region
244                                            (if (re-search-forward "^$" nil t)
245                                                (1+ (match-end 0))
246                                              (point-min)
247                                              )
248                                            (point-max))
249                                           (list (mime-parse-message
250                                                  nil nil (cons 0 rcnum)))
251                                           )
252                                         )
253              )
254             (t 
255              (mime::content-info/create rcnum (point-min) (point-max)
256                                         ctype params encoding nil)
257              ))
258       )))
259
260
261 ;;; @ end
262 ;;;
263
264 (provide 'mime-parse)
265
266 ;;; mime-parse.el ends here