(entity-children): Don't use `mime-entity-children-internal'.
[elisp/flim.git] / mmbuffer.el
1 ;;; mmbuffer.el --- MIME entity module for binary buffer
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: MIME, multimedia, mail, news
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
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 'mime)
28 (require 'mime-parse)
29
30 (mm-define-backend buffer)
31
32 (mm-define-method initialize-instance ((entity buffer))
33   (mime-entity-set-buffer-internal
34    entity (mime-entity-location-internal entity))
35   (save-excursion
36     (set-buffer (mime-entity-buffer-internal entity))
37     (setq mime-message-structure entity)
38     (let ((header-start (point-min))
39           header-end
40           body-start
41           (body-end (point-max)))
42       (goto-char header-start)
43       (if (re-search-forward "^$" nil t)
44           (setq header-end (match-end 0)
45                 body-start (if (= header-end body-end)
46                                body-end
47                              (1+ header-end)))
48         (setq header-end (point-min)
49               body-start (point-min)))
50       (save-restriction
51         (narrow-to-region header-start header-end)
52         (mime-entity-set-content-type-internal
53          entity
54          (let ((str (std11-fetch-field "Content-Type")))
55            (if str
56                (mime-parse-Content-Type str)
57              )))
58         )
59       (mime-entity-set-header-start-internal entity header-start)
60       (mime-entity-set-header-end-internal entity header-end)
61       (mime-entity-set-body-start-internal entity body-start)
62       (mime-entity-set-body-end-internal entity body-end)
63       )))
64
65 (mm-define-method entity-point-min ((entity buffer))
66   (mime-entity-header-start-internal entity))
67
68 (mm-define-method entity-point-max ((entity buffer))
69   (mime-entity-body-end-internal entity))
70
71 (mm-define-method fetch-field ((entity buffer) field-name)
72   (save-excursion
73     (set-buffer (mime-entity-buffer-internal entity))
74     (save-restriction
75       (narrow-to-region (mime-entity-header-start-internal entity)
76                         (mime-entity-header-end-internal entity))
77       (std11-fetch-field field-name)
78       )))
79
80 (mm-define-method entity-cooked-p ((entity buffer)) nil)
81
82 (mm-define-method entity-children ((entity buffer))
83   (let* ((content-type (mime-entity-content-type entity))
84          (primary-type (mime-content-type-primary-type content-type)))
85     (cond ((eq primary-type 'multipart)
86            (mime-parse-multipart entity)
87            )
88           ((and (eq primary-type 'message)
89                 (memq (mime-content-type-subtype content-type)
90                       '(rfc822 news external-body)
91                       ))
92            (mime-parse-encapsulated entity)
93            ))
94     ))
95
96 (mm-define-method entity-content ((entity buffer))
97   (save-excursion
98     (set-buffer (mime-entity-buffer-internal entity))
99     (mime-decode-string
100      (buffer-substring (mime-entity-body-start-internal entity)
101                        (mime-entity-body-end-internal entity))
102      (mime-entity-encoding entity))))
103
104 (mm-define-method write-entity-content ((entity buffer) filename)
105   (save-excursion
106     (set-buffer (mime-entity-buffer-internal entity))
107     (mime-write-decoded-region (mime-entity-body-start-internal entity)
108                                (mime-entity-body-end-internal entity)
109                                filename
110                                (or (mime-entity-encoding entity) "7bit"))
111     ))
112
113 (mm-define-method write-entity ((entity buffer) filename)
114   (save-excursion
115     (set-buffer (mime-entity-buffer-internal entity))
116     (write-region-as-binary (mime-entity-header-start-internal entity)
117                             (mime-entity-body-end-internal entity)
118                             filename)
119     ))
120
121 (mm-define-method write-entity-body ((entity buffer) filename)
122   (save-excursion
123     (set-buffer (mime-entity-buffer-internal entity))
124     (write-region-as-binary (mime-entity-body-start-internal entity)
125                             (mime-entity-body-end-internal entity)
126                             filename)
127     ))
128
129 (defun mime-visible-field-p (field-name visible-fields invisible-fields)
130   (or (catch 'found
131         (while visible-fields
132           (let ((regexp (car visible-fields)))
133             (if (string-match regexp field-name)
134                 (throw 'found t)
135               ))
136           (setq visible-fields (cdr visible-fields))
137           ))
138       (catch 'found
139         (while invisible-fields
140           (let ((regexp (car invisible-fields)))
141             (if (string-match regexp field-name)
142                 (throw 'found nil)
143               ))
144           (setq invisible-fields (cdr invisible-fields))
145           )
146         t)))
147
148 (mm-define-method insert-decoded-header ((entity buffer)
149                                          &optional invisible-fields
150                                          visible-fields)
151   (save-restriction
152     (narrow-to-region (point)(point))
153     (let ((the-buf (current-buffer))
154           (src-buf (mime-entity-buffer-internal entity))
155           (h-end (mime-entity-header-end-internal entity))
156           beg p end field-name len field)
157       (save-excursion
158         (set-buffer src-buf)
159         (goto-char (mime-entity-header-start-internal entity))
160         (save-restriction
161           (narrow-to-region (point) h-end)
162           (while (re-search-forward std11-field-head-regexp nil t)
163             (setq beg (match-beginning 0)
164                   p (match-end 0)
165                   field-name (buffer-substring beg (1- p))
166                   len (string-width field-name)
167                   end (std11-field-end))
168             (when (mime-visible-field-p field-name
169                                         visible-fields invisible-fields)
170               (setq field (intern (capitalize field-name)))
171               (save-excursion
172                 (set-buffer the-buf)
173                 (insert field-name)
174                 (insert ":")
175                 (cond ((memq field eword-decode-ignored-field-list)
176                        ;; Don't decode
177                        (insert-buffer-substring src-buf p end)
178                        )
179                       ((memq field eword-decode-structured-field-list)
180                        ;; Decode as structured field
181                        (let ((body (save-excursion
182                                      (set-buffer src-buf)
183                                      (buffer-substring p end)
184                                      )))
185                          (insert (eword-decode-and-fold-structured-field
186                                   body (1+ len)))
187                          ))
188                       (t
189                        ;; Decode as unstructured field
190                        (let ((body (save-excursion
191                                      (set-buffer src-buf)
192                                      (buffer-substring p end)
193                                      )))
194                          (insert (eword-decode-unstructured-field-body
195                                   body (1+ len)))
196                          )))
197                 (insert "\n")
198                 ))))))))
199
200
201 ;;; @ end
202 ;;;
203
204 (provide 'mmbuffer)
205
206 ;;; mmbuffer.el ends here