1 ;;; mmbuffer.el --- MIME entity module for binary buffer
3 ;; Copyright (C) 1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: MIME, multimedia, mail, news
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
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.
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.
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.
31 (luna-define-class mime-buffer-entity (mime-entity)
38 (luna-define-internal-accessors 'mime-buffer-entity)
41 (luna-define-method initialize-instance :after ((entity mime-buffer-entity)
43 (or (mime-buffer-entity-buffer-internal entity)
44 (mime-buffer-entity-set-buffer-internal
45 entity (get-buffer (mime-entity-location-internal entity))))
47 (set-buffer (mime-buffer-entity-buffer-internal entity))
49 (or (mime-buffer-entity-header-start-internal entity)
50 (mime-buffer-entity-set-header-start-internal
52 (header-end (mime-buffer-entity-header-end-internal entity))
53 (body-start (mime-buffer-entity-body-start-internal entity))
55 (or (mime-buffer-entity-body-end-internal entity)
56 (mime-buffer-entity-set-body-end-internal entity (point-max)))))
57 (goto-char header-start)
58 (unless (and header-end body-start)
59 (if (re-search-forward "^$" body-end t)
60 (setq header-end (match-end 0)
61 body-start (if (= header-end body-end)
64 (setq header-end (point-min)
65 body-start (point-min)))
66 (mime-buffer-entity-set-header-end-internal entity header-end)
67 (mime-buffer-entity-set-body-start-internal entity body-start)
69 (or (mime-entity-content-type-internal entity)
71 (narrow-to-region header-start header-end)
72 (mime-entity-set-content-type-internal
74 (let ((str (std11-fetch-field "Content-Type")))
76 (mime-parse-Content-Type str)
82 (luna-define-method mime-entity-name ((entity mime-buffer-entity))
83 (buffer-name (mime-buffer-entity-buffer-internal entity))
90 (luna-define-method mime-insert-entity ((entity mime-buffer-entity))
91 (insert-buffer-substring (mime-buffer-entity-buffer-internal entity)
92 (mime-buffer-entity-header-start-internal entity)
93 (mime-buffer-entity-body-end-internal entity))
96 (luna-define-method mime-write-entity ((entity mime-buffer-entity) filename)
98 (set-buffer (mime-buffer-entity-buffer-internal entity))
99 (let ((coding-system-for-write 'raw-text-dos))
100 (write-region (mime-buffer-entity-header-start-internal entity)
101 (mime-buffer-entity-body-end-internal entity)
112 (luna-define-method mime-entity-body ((entity mime-buffer-entity))
114 (set-buffer (mime-buffer-entity-buffer-internal entity))
115 (buffer-substring (mime-buffer-entity-body-start-internal entity)
116 (mime-buffer-entity-body-end-internal entity))))
118 (luna-define-method mime-insert-entity-body ((entity mime-buffer-entity))
119 (insert-buffer-substring (mime-buffer-entity-buffer-internal entity)
120 (mime-buffer-entity-body-start-internal entity)
121 (mime-buffer-entity-body-end-internal entity))
124 (luna-define-method mime-write-entity-body ((entity mime-buffer-entity)
127 (set-buffer (mime-buffer-entity-buffer-internal entity))
128 (binary-write-decoded-region
129 (mime-buffer-entity-body-start-internal entity)
130 (mime-buffer-entity-body-end-internal entity)
137 (luna-define-method mime-entity-content ((entity mime-buffer-entity))
139 (set-buffer (mime-buffer-entity-buffer-internal entity))
141 (buffer-substring (mime-buffer-entity-body-start-internal entity)
142 (mime-buffer-entity-body-end-internal entity))
143 (mime-entity-encoding entity))))
145 (luna-define-method mime-insert-entity-content ((entity mime-buffer-entity))
146 (insert (with-current-buffer (mime-buffer-entity-buffer-internal entity)
148 (buffer-substring (mime-buffer-entity-body-start-internal entity)
149 (mime-buffer-entity-body-end-internal entity))
150 (mime-entity-encoding entity)))))
152 (luna-define-method mime-write-entity-content ((entity mime-buffer-entity)
155 (set-buffer (mime-buffer-entity-buffer-internal entity))
156 (mime-write-decoded-region (mime-buffer-entity-body-start-internal entity)
157 (mime-buffer-entity-body-end-internal entity)
159 (or (mime-entity-encoding entity) "7bit"))
166 (luna-define-method mime-entity-fetch-field :around
167 ((entity mime-buffer-entity) field-name)
168 (or (luna-call-next-method)
170 (set-buffer (mime-buffer-entity-buffer-internal entity))
172 (narrow-to-region (mime-buffer-entity-header-start-internal entity)
173 (mime-buffer-entity-header-end-internal entity))
174 (let ((ret (std11-fetch-field field-name)))
176 (or (symbolp field-name)
177 (setq field-name (intern (capitalize field-name))))
178 (mime-entity-set-original-header-internal
180 (put-alist field-name ret
181 (mime-entity-original-header-internal entity)))
184 (luna-define-method mime-insert-header ((entity mime-buffer-entity)
185 &optional invisible-fields
187 (mime-insert-header-from-buffer
188 (mime-buffer-entity-buffer-internal entity)
189 (mime-buffer-entity-header-start-internal entity)
190 (mime-buffer-entity-header-end-internal entity)
191 invisible-fields visible-fields)
198 ;; (luna-define-method mime-entity-header-buffer ((entity mime-buffer-entity))
199 ;; (mime-buffer-entity-buffer-internal entity)
202 ;; (luna-define-method mime-goto-header-start-point ((entity mime-buffer-entity))
203 ;; (set-buffer (mime-buffer-entity-buffer-internal entity))
204 ;; (goto-char (mime-buffer-entity-header-start-internal entity))
207 ;; (luna-define-method mime-entity-header-start-point ((entity
208 ;; mime-buffer-entity))
209 ;; (mime-buffer-entity-header-start-internal entity)
212 ;; (luna-define-method mime-entity-header-end-point ((entity
213 ;; mime-buffer-entity))
214 ;; (mime-buffer-entity-header-end-internal entity)
221 ;; (luna-define-method mime-entity-body-buffer ((entity mime-buffer-entity))
222 ;; (mime-buffer-entity-buffer-internal entity)
225 ;; (luna-define-method mime-goto-body-start-point ((entity mime-buffer-entity))
226 ;; (set-buffer (mime-buffer-entity-buffer-internal entity))
227 ;; (goto-char (mime-buffer-entity-body-start-internal entity))
230 ;; (luna-define-method mime-goto-body-end-point ((entity mime-buffer-entity))
231 ;; (set-buffer (mime-buffer-entity-buffer-internal entity))
232 ;; (goto-char (mime-buffer-entity-body-end-internal entity))
235 ;; (luna-define-method mime-entity-body-start-point ((entity mime-buffer-entity))
236 ;; (mime-buffer-entity-body-start-internal entity)
239 ;; (luna-define-method mime-entity-body-end-point ((entity mime-buffer-entity))
240 ;; (mime-buffer-entity-body-end-internal entity)
244 ;;; @ buffer (obsolete)
247 ;; (luna-define-method mime-entity-buffer ((entity mime-buffer-entity))
248 ;; (mime-buffer-entity-buffer-internal entity)
251 ;; (luna-define-method mime-entity-point-min ((entity mime-buffer-entity))
252 ;; (mime-buffer-entity-header-start-internal entity)
255 ;; (luna-define-method mime-entity-point-max ((entity mime-buffer-entity))
256 ;; (mime-buffer-entity-body-end-internal entity)
263 (defun mmbuffer-parse-multipart (entity &optional representation-type)
264 (with-current-buffer (mime-buffer-entity-buffer-internal entity)
265 (or representation-type
266 (setq representation-type
267 (mime-entity-representation-type-internal entity)))
268 (let* ((content-type (mime-entity-content-type-internal entity))
271 (mime-content-type-parameter content-type "boundary")))
272 (delimiter (concat "\n" (regexp-quote dash-boundary)))
273 (close-delimiter (concat delimiter "--[ \t]*$"))
274 (rsep (concat delimiter "[ \t]*\n"))
276 (if (eq (mime-content-type-subtype content-type) 'digest)
277 (make-mime-content-type 'message 'rfc822)
278 (make-mime-content-type 'text 'plain)
280 (body-start (mime-buffer-entity-body-start-internal entity))
281 (body-end (mime-buffer-entity-body-end-internal entity)))
284 (narrow-to-region body-start
285 (if (re-search-backward close-delimiter nil t)
288 (goto-char body-start)
289 (if (re-search-forward
290 (concat "^" (regexp-quote dash-boundary) "[ \t]*\n")
292 (let ((cb (match-end 0))
294 (node-id (mime-entity-node-id-internal entity))
296 (while (re-search-forward rsep nil t)
297 (setq ce (match-beginning 0))
298 (setq ncb (match-end 0))
300 (narrow-to-region cb ce)
301 (setq ret (mime-parse-message representation-type dc-ctl
302 entity (cons i node-id)))
304 (setq children (cons ret children))
305 (goto-char (setq cb ncb))
308 (setq ce (point-max))
310 (narrow-to-region cb ce)
311 (setq ret (mime-parse-message representation-type dc-ctl
312 entity (cons i node-id)))
314 (setq children (cons ret children))
315 (mime-entity-set-children-internal entity (nreverse children))
317 (mime-entity-set-content-type-internal
318 entity (make-mime-content-type 'message 'x-broken))
322 (defun mmbuffer-parse-encapsulated (entity &optional external
324 (mime-entity-set-children-internal
326 (with-current-buffer (mime-buffer-entity-buffer-internal entity)
328 (narrow-to-region (mime-buffer-entity-body-start-internal entity)
329 (mime-buffer-entity-body-end-internal entity))
330 (list (mime-parse-message
333 (require 'mmexternal)
334 'mime-external-entity)
335 (or representation-type
336 (mime-entity-representation-type-internal entity)))
338 entity (cons 0 (mime-entity-node-id-internal entity))))))))
340 (luna-define-method mime-entity-children ((entity mime-buffer-entity))
341 (let* ((content-type (mime-entity-content-type entity))
342 (primary-type (mime-content-type-primary-type content-type))
344 (cond ((eq primary-type 'multipart)
345 (mmbuffer-parse-multipart entity))
346 ((eq primary-type 'message)
347 (setq sub-type (mime-content-type-subtype content-type))
348 (cond ((eq sub-type 'external-body)
349 (mmbuffer-parse-encapsulated entity 'external))
350 ((memq sub-type '(rfc822 news))
351 (mmbuffer-parse-encapsulated entity)))))))
359 ;;; mmbuffer.el ends here