1 ;;; mmimap.el --- MIME entity module for IMAP4rev1 (RFC2060).
2 ;; **** This is EXPERIMENTAL *****
4 ;; Copyright (C) 2000 Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;; Keywords: IMAP, MIME, multimedia, mail, news
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
38 (luna-define-class mime-imap-entity (mime-entity)
39 (size header-string body-string new requested))
40 (luna-define-internal-accessors 'mime-imap-entity))
42 ;;; @ MIME IMAP location
43 ;; It should contain server, mailbox and uid (sequence number).
45 (luna-define-class mime-imap-location () ()))
47 (luna-define-generic mime-imap-location-section-body (location section)
48 "Return a body string from LOCATION which corresponds to SECTION.
49 SECTION is a section string which is defined in RFC2060.")
51 (luna-define-generic mime-imap-location-bodystructure (location)
52 "Return a parsed bodystructure of LOCATION.
53 `NIL' should be converted to nil, `astring' should be converted to a string.")
55 (luna-define-generic mime-imap-location-fetch-entity-p (location entity)
56 "Return non-nil when LOCATION may fetch the ENTITY.")
61 (defun mmimap-entity-section (node-id)
62 "Return a section string from NODE-ID"
67 (number-to-string (1+ node-id)))
70 'mmimap-entity-section
75 (defun-maybe mime-decode-parameters (attrlist)
78 (setq ret-val (append ret-val
79 (list (cons (downcase (car attrlist))
80 (car (cdr attrlist))))))
81 (setq attrlist (cdr (cdr attrlist))))
84 (defun mmimap-make-mime-entity (bodystructure class location node-id number
86 "Analyze parsed IMAP4 BODYSTRUCTURE response and make MIME entity.
87 CLASS, LOCATION, NODE-ID, PARENT are set to the returned entity."
88 (setq node-id (if number (cons number node-id) node-id))
90 ((listp (car bodystructure)) ; multipart
92 curp children content-type entity)
100 (while (and (setq curp (car bodystructure))
105 (mmimap-make-mime-entity curp class
111 (setq bodystructure (cdr bodystructure)))
112 (mime-entity-set-children-internal entity children)
113 (mime-entity-set-content-type-internal
115 (make-mime-content-type 'multipart
116 (if (car bodystructure)
118 (car bodystructure))))
119 (mime-decode-parameters
120 (nth 1 bodystructure))))
123 (let (content-type entity)
128 :size (nth 6 bodystructure)
129 :content-type content-type
133 (mime-entity-set-content-type-internal
135 (make-mime-content-type (intern (downcase (car bodystructure)))
136 (if (nth 1 bodystructure)
138 (nth 1 bodystructure))))
139 (mime-decode-parameters
140 (nth 2 bodystructure))))
141 (mime-entity-set-encoding-internal entity
142 (and (nth 5 bodystructure)
144 (nth 5 bodystructure))))
145 (if (and (nth 7 bodystructure)
146 (nth 8 bodystructure)) ; children.
147 (mime-entity-set-children-internal
149 (list (mmimap-make-mime-entity
150 (nth 8 bodystructure) class
155 (luna-define-method initialize-instance :after ((entity mime-imap-entity)
157 ;; To prevent infinite loop...
158 (if (mime-imap-entity-new-internal entity)
160 (mmimap-make-mime-entity
161 (mime-imap-location-bodystructure
162 (mime-entity-location-internal entity))
163 (luna-class-name entity)
164 (mime-entity-location-internal entity)
170 (luna-define-method mime-insert-entity ((entity mime-imap-entity))
171 (if (mime-root-entity-p entity)
173 (insert (mime-imap-entity-header-string entity))
174 (mime-insert-entity-body entity))
175 ;; Insert body if it is not a multipart.
176 (unless (eq (mime-content-type-primary-type
177 (mime-entity-content-type entity))
179 (mime-insert-entity-body entity))))
181 (luna-define-method mime-write-entity ((entity mime-imap-entity) filename)
183 (mime-insert-entity entity)
184 (write-region-as-raw-text-CRLF (point-min) (point-max) filename)))
189 (luna-define-method mime-entity-body ((entity mime-imap-entity))
190 (or (mime-imap-entity-body-string-internal entity)
191 (if (or (mime-imap-entity-requested-internal entity) ; second time.
192 (mime-imap-location-fetch-entity-p
193 (mime-entity-location-internal entity)
195 (mime-imap-entity-set-body-string-internal
197 (mime-imap-location-section-body
198 (mime-entity-location-internal entity)
199 (mmimap-entity-section
200 (mime-entity-node-id-internal entity))))
201 (mime-imap-entity-set-requested-internal entity t)
204 (luna-define-method mime-insert-entity-body ((entity mime-imap-entity))
205 (insert (mime-entity-body entity)))
207 (luna-define-method mime-write-entity-body ((entity mime-imap-entity)
210 (mime-insert-entity-body entity)
211 (write-region-as-binary (point-min) (point-max) filename)))
216 (luna-define-method mime-entity-content ((entity mime-imap-entity))
217 (let ((ret (mime-entity-body entity)))
219 (mime-decode-string ret (mime-entity-encoding entity))
220 (message "Cannot decode content.")
223 (luna-define-method mime-insert-entity-content ((entity mime-imap-entity))
224 (insert (mime-entity-content entity)))
226 (luna-define-method mime-write-entity-content ((entity mime-imap-entity)
229 (mime-insert-entity-body entity)
230 (mime-write-decoded-region (point-min) (point-max)
232 (or (mime-entity-encoding entity) "7bit"))))
237 (defun mime-imap-entity-header-string (entity)
238 (or (mime-imap-entity-header-string-internal entity)
239 (mime-imap-entity-set-header-string-internal
241 (mime-imap-location-section-body
242 (mime-entity-location-internal entity)
243 (if (mime-entity-node-id-internal entity)
244 (concat (mmimap-entity-section
245 (mime-entity-node-id-internal entity))
249 (luna-define-method mime-entity-fetch-field :around
250 ((entity mime-imap-entity) field-name)
251 (if (mime-root-entity-p entity)
252 (or (luna-call-next-method)
254 (insert (mime-imap-entity-header-string entity))
255 (let ((ret (std11-fetch-field field-name)))
257 (or (symbolp field-name)
259 (intern (capitalize (capitalize field-name)))))
260 (mime-entity-set-original-header-internal
262 (put-alist field-name ret
263 (mime-entity-original-header-internal entity)))
266 (luna-define-method mime-insert-header ((entity mime-imap-entity)
267 &optional invisible-fields
269 (let ((the-buf (current-buffer))
272 (insert (mime-imap-entity-header-string entity))
273 (setq buf (current-buffer)
277 (mime-insert-header-from-buffer buf p-min p-max
278 invisible-fields visible-fields))))
285 ;;; mmimap.el ends here