Merge flim-chao-1_8_0.
[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            (mime-entity-children-internal entity)
88            )
89           ((and (eq primary-type 'message)
90                 (memq (mime-content-type-subtype content-type)
91                       '(rfc822 news external-body)
92                       ))
93            (mime-parse-encapsulated entity)
94            (mime-entity-children-internal entity)
95            )
96           )))
97
98 (mm-define-method entity-content ((entity buffer))
99   (save-excursion
100     (set-buffer (mime-entity-buffer-internal entity))
101     (mime-decode-string
102      (buffer-substring (mime-entity-body-start-internal entity)
103                        (mime-entity-body-end-internal entity))
104      (mime-entity-encoding entity))))
105
106 (mm-define-method write-entity-content ((entity buffer) filename)
107   (save-excursion
108     (set-buffer (mime-entity-buffer-internal entity))
109     (mime-write-decoded-region (mime-entity-body-start-internal entity)
110                                (mime-entity-body-end-internal entity)
111                                filename
112                                (or (mime-entity-encoding entity) "7bit"))
113     ))
114
115 (mm-define-method write-entity ((entity buffer) filename)
116   (save-excursion
117     (set-buffer (mime-entity-buffer-internal entity))
118     (write-region-as-binary (mime-entity-header-start-internal entity)
119                             (mime-entity-body-end-internal entity)
120                             filename)
121     ))
122
123 (mm-define-method write-entity-body ((entity buffer) filename)
124   (save-excursion
125     (set-buffer (mime-entity-buffer-internal entity))
126     (write-region-as-binary (mime-entity-body-start-internal entity)
127                             (mime-entity-body-end-internal entity)
128                             filename)
129     ))
130
131 (defun mime-visible-field-p (field-name visible-fields invisible-fields)
132   (or (catch 'found
133         (while visible-fields
134           (let ((regexp (car visible-fields)))
135             (if (string-match regexp field-name)
136                 (throw 'found t)
137               ))
138           (setq visible-fields (cdr visible-fields))
139           ))
140       (catch 'found
141         (while invisible-fields
142           (let ((regexp (car invisible-fields)))
143             (if (string-match regexp field-name)
144                 (throw 'found nil)
145               ))
146           (setq invisible-fields (cdr invisible-fields))
147           )
148         t)))
149
150 (mm-define-method insert-decoded-header ((entity buffer)
151                                          &optional invisible-fields
152                                          visible-fields)
153   (save-restriction
154     (narrow-to-region (point)(point))
155     (let ((the-buf (current-buffer))
156           (src-buf (mime-entity-buffer-internal entity))
157           (h-end (mime-entity-header-end-internal entity))
158           beg p end field-name len field)
159       (save-excursion
160         (set-buffer src-buf)
161         (goto-char (mime-entity-header-start-internal entity))
162         (save-restriction
163           (narrow-to-region (point) h-end)
164           (while (re-search-forward std11-field-head-regexp nil t)
165             (setq beg (match-beginning 0)
166                   p (match-end 0)
167                   field-name (buffer-substring beg (1- p))
168                   len (string-width field-name)
169                   end (std11-field-end))
170             (when (mime-visible-field-p field-name
171                                         visible-fields invisible-fields)
172               (setq field (intern (capitalize field-name)))
173               (save-excursion
174                 (set-buffer the-buf)
175                 (insert field-name)
176                 (insert ":")
177                 (cond ((memq field eword-decode-ignored-field-list)
178                        ;; Don't decode
179                        (insert-buffer-substring src-buf p end)
180                        )
181                       ((memq field eword-decode-structured-field-list)
182                        ;; Decode as structured field
183                        (let ((body (save-excursion
184                                      (set-buffer src-buf)
185                                      (buffer-substring p end)
186                                      )))
187                          (insert (eword-decode-and-fold-structured-field
188                                   body (1+ len)))
189                          ))
190                       (t
191                        ;; Decode as unstructured field
192                        (let ((body (save-excursion
193                                      (set-buffer src-buf)
194                                      (buffer-substring p end)
195                                      )))
196                          (insert (eword-decode-unstructured-field-body
197                                   body (1+ len)))
198                          )))
199                 (insert "\n")
200                 ))))))))
201
202
203 ;;; @ end
204 ;;;
205
206 (provide 'mmbuffer)
207
208 ;;; mmbuffer.el ends here