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