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