* sasl.texi: Texinfo markup fixes. From Jesper Harder
[elisp/flim.git] / mmexternal.el
1 ;;; mmexternal.el --- MIME entity module for external buffer
2
3 ;; Copyright (C) 1998,1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 'mmgeneric)
28 (require 'mime)
29
30 (eval-and-compile
31   (luna-define-class mime-external-entity (mime-entity)
32                      (body-buffer
33                       body-file))
34   (luna-define-internal-accessors 'mime-external-entity)
35
36   ;; In an external entity, information of media-type or other
37   ;; information which are represented in a header in a non-external
38   ;; entity are in the body of the parent entity.
39   )
40
41 (luna-define-method mime-entity-name ((entity mime-external-entity))
42   (concat "child of "
43           (mime-entity-name
44            (mime-entity-parent-internal entity))))
45
46
47 (defun mmexternal-require-file-name (entity)
48   (condition-case nil
49       (or (mime-external-entity-body-file-internal entity)
50           (let* ((ct (mime-entity-content-type
51                       (mime-entity-parent-internal entity)))
52                  (access-type
53                   (mime-content-type-parameter ct "access-type")))
54             (if (and access-type
55                      (string= access-type "anon-ftp"))
56                 (let ((site (mime-content-type-parameter ct "site"))
57                       (directory
58                        (mime-content-type-parameter ct "directory"))
59                       (name (mime-content-type-parameter ct "name")))
60                   (mime-external-entity-set-body-file-internal
61                    entity
62                    (expand-file-name
63                     name
64                     (concat "/anonymous@" site ":"
65                             (file-name-as-directory directory))))))))
66     (error (message "Can't make file-name of external-body."))))
67
68 (defun mmexternal-require-buffer (entity)
69   (unless (and (mime-external-entity-body-buffer-internal entity)
70                (buffer-live-p
71                 (mime-external-entity-body-buffer-internal entity)))
72     (condition-case nil
73         (progn
74           (mmexternal-require-file-name entity)
75           (mime-external-entity-set-body-buffer-internal
76            entity
77            (with-current-buffer (get-buffer-create
78                                  (concat " *Body of "
79                                          (mime-entity-name entity)
80                                          "*"))
81              (binary-insert-encoded-file
82               (mime-external-entity-body-file-internal entity))
83              (current-buffer))))
84       (error (message "Can't get external-body.")))))
85
86
87 ;;; @ entity
88 ;;;
89
90 (luna-define-method mime-insert-entity ((entity mime-external-entity))
91   (mime-insert-entity-body (mime-entity-parent-internal entity))
92   (insert "\n")
93   (mime-insert-entity-body entity))
94
95 (luna-define-method mime-write-entity ((entity mime-external-entity) filename)
96   (with-temp-buffer
97     (mime-insert-entity entity)
98     (let ((coding-system-for-write 'raw-text-dos))
99       (write-region (point-min) (point-max) filename))))
100
101
102 ;;; @ entity header
103 ;;;
104
105
106 ;;; @ entity body
107 ;;;
108
109 (luna-define-method mime-entity-body ((entity mime-external-entity))
110   (mmexternal-require-buffer entity)
111   (with-current-buffer (mime-external-entity-body-buffer-internal entity)
112     (buffer-string)))
113
114 (luna-define-method mime-insert-entity-body ((entity mime-external-entity))
115   (mmexternal-require-buffer entity)
116   (insert-buffer-substring
117    (mime-external-entity-body-buffer-internal entity)))
118
119 (luna-define-method mime-write-entity-body ((entity mime-external-entity)
120                                             filename)
121   (mmexternal-require-buffer entity)
122   (with-current-buffer (mime-external-entity-body-buffer-internal entity)
123     (binary-write-decoded-region (point-min) (point-max) filename)))
124
125
126 ;;; @ entity content
127 ;;;
128
129 (luna-define-method mime-entity-content ((entity mime-external-entity))
130   (let ((ret (mime-entity-body entity)))
131     (if ret
132         (mime-decode-string ret (mime-entity-encoding entity))
133       (message "Cannot get content")
134       nil)))
135
136 (luna-define-method mime-insert-entity-content ((entity mime-external-entity))
137   (insert (mime-entity-content entity)))
138
139 (luna-define-method mime-write-entity-content ((entity mime-external-entity)
140                                                filename)
141   (mmexternal-require-buffer entity)
142   (with-current-buffer (mime-external-entity-body-buffer-internal entity)
143     (mime-write-decoded-region (point-min) (point-max)
144                                filename
145                                (or (mime-entity-encoding entity) "7bit"))))
146
147
148 ;;; @ header field
149 ;;;
150
151 (luna-define-method mime-entity-fetch-field :around
152   ((entity mime-external-entity) field-name)
153   (or (luna-call-next-method)
154       (with-temp-buffer
155         (mime-insert-entity-body (mime-entity-parent-internal entity))
156         (let ((ret (std11-fetch-field field-name)))
157           (when ret
158             (or (symbolp field-name)
159                 (setq field-name (intern (capitalize field-name))))
160             (mime-entity-set-original-header-internal
161              entity
162              (put-alist field-name ret
163                         (mime-entity-original-header-internal entity)))
164             ret)))))
165
166 (luna-define-method mime-insert-header ((entity mime-external-entity)
167                                         &optional invisible-fields
168                                         visible-fields)
169   (let ((the-buf (current-buffer))
170         buf p-min p-max)
171     (with-temp-buffer
172       (mime-insert-entity-body (mime-entity-parent-internal entity))
173       (setq buf (current-buffer)
174             p-min (point-min)
175             p-max (point-max))
176       (set-buffer the-buf)
177       (mime-insert-header-from-buffer buf p-min p-max
178                                       invisible-fields visible-fields))))
179
180
181 ;;; @ end
182 ;;;
183
184 (provide 'mmexternal)
185
186 ;;; mmexternal.el ends here