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"
65 (number-to-string (1+ node-id)))
68 'mmimap-entity-section
73 (defun-maybe mime-decode-parameters (attrlist)
76 (setq ret-val (append ret-val
77 (list (cons (downcase (car attrlist))
78 (car (cdr attrlist))))))
79 (setq attrlist (cdr (cdr attrlist))))
82 (defun mmimap-make-mime-entity (bodystructure class location node-id number
84 "Analyze parsed IMAP4 BODYSTRUCTURE response and make MIME entity.
85 CLASS, LOCATION, NODE-ID, PARENT are set to the returned entity."
87 ((listp (car bodystructure)) ; multipart
89 curp children content-type entity)
96 :node-id (if (eq number 0)
98 (nconc (list number) node-id))))
99 (while (and (setq curp (car bodystructure))
104 (mmimap-make-mime-entity curp class
108 (nconc (list number) node-id))
112 (setq bodystructure (cdr bodystructure)))
113 (mime-entity-set-children-internal entity children)
114 (mime-entity-set-content-type-internal
116 (make-mime-content-type 'multipart
117 (if (car bodystructure)
119 (car bodystructure))))
120 (mime-decode-parameters
121 (nth 1 bodystructure))))
124 (let (content-type entity)
125 (setq node-id (nconc (list number) node-id))
130 :size (nth 6 bodystructure)
131 :content-type content-type
135 (mime-entity-set-content-type-internal
137 (make-mime-content-type (intern (downcase (car bodystructure)))
138 (if (nth 1 bodystructure)
140 (nth 1 bodystructure))))
141 (mime-decode-parameters
142 (nth 2 bodystructure))))
143 (mime-entity-set-encoding-internal entity
144 (and (nth 5 bodystructure)
146 (nth 5 bodystructure))))
147 (if (and (nth 7 bodystructure)
148 (nth 8 bodystructure)) ; children.
149 (mime-entity-set-children-internal
151 (list (mmimap-make-mime-entity
152 (nth 8 bodystructure) class
157 (luna-define-method initialize-instance :after ((entity mime-imap-entity)
159 ;; To prevent infinite loop...
160 (if (mime-imap-entity-new-internal entity)
162 (mmimap-make-mime-entity
163 (mime-imap-location-bodystructure
164 (mime-entity-location-internal entity))
165 (luna-class-name entity)
166 (mime-entity-location-internal entity)
172 (luna-define-method mime-insert-entity ((entity mime-imap-entity))
173 (if (mime-root-entity-p entity)
175 (insert (mime-imap-entity-header-string entity))
176 (mime-insert-entity-body entity))
177 ;; Insert body if it is not a multipart.
178 (unless (eq (mime-content-type-primary-type
179 (mime-entity-content-type entity))
181 (mime-insert-entity-body entity))))
183 (luna-define-method mime-write-entity ((entity mime-imap-entity) filename)
185 (mime-insert-entity entity)
186 (write-region-as-raw-text-CRLF (point-min) (point-max) filename)))
191 (luna-define-method mime-entity-body ((entity mime-imap-entity))
192 (or (mime-imap-entity-body-string-internal entity)
193 (if (or (mime-imap-entity-requested-internal entity) ; second time.
194 (mime-imap-location-fetch-entity-p
195 (mime-entity-location-internal entity)
197 (mime-imap-entity-set-body-string-internal
199 (mime-imap-location-section-body
200 (mime-entity-location-internal entity)
201 (mmimap-entity-section
202 (mime-entity-node-id-internal entity))))
203 (mime-imap-entity-set-requested-internal entity t)
206 (luna-define-method mime-insert-entity-body ((entity mime-imap-entity))
207 (insert (mime-entity-body entity)))
209 (luna-define-method mime-write-entity-body ((entity mime-imap-entity)
212 (mime-insert-entity-body entity)
213 (write-region-as-binary (point-min) (point-max) filename)))
218 (luna-define-method mime-entity-content ((entity mime-imap-entity))
219 (let ((ret (mime-entity-body entity)))
221 (mime-decode-string ret (mime-entity-encoding entity))
222 (message "Cannot decode content.")
225 (luna-define-method mime-insert-entity-content ((entity mime-imap-entity))
226 (insert (mime-entity-content entity)))
228 (luna-define-method mime-write-entity-content ((entity mime-imap-entity)
231 (mime-insert-entity-body entity)
232 (mime-write-decoded-region (point-min) (point-max)
234 (or (mime-entity-encoding entity) "7bit"))))
239 (defun mime-imap-entity-header-string (entity)
240 (or (mime-imap-entity-header-string-internal entity)
241 (mime-imap-entity-set-header-string-internal
243 (mime-imap-location-section-body
244 (mime-entity-location-internal entity)
245 (if (if (eq (car (mime-entity-node-id-internal entity)) 0)
246 (cdr (mime-entity-node-id-internal entity))
247 (mime-entity-node-id-internal entity))
248 (concat (mmimap-entity-section
249 (if (eq (car (mime-entity-node-id-internal entity)) 0)
250 (cdr (mime-entity-node-id-internal entity))
251 (mime-entity-node-id-internal entity)))
255 (luna-define-method mime-entity-fetch-field :around
256 ((entity mime-imap-entity) field-name)
257 (if (mime-root-entity-p entity)
258 (or (luna-call-next-method)
260 (insert (mime-imap-entity-header-string entity))
261 (let ((ret (std11-fetch-field field-name)))
263 (or (symbolp field-name)
265 (intern (capitalize (capitalize field-name)))))
266 (mime-entity-set-original-header-internal
268 (put-alist field-name ret
269 (mime-entity-original-header-internal entity)))
272 (luna-define-method mime-insert-header ((entity mime-imap-entity)
273 &optional invisible-fields
275 (let ((the-buf (current-buffer))
278 (insert (mime-imap-entity-header-string entity))
279 (setq buf (current-buffer)
283 (mime-insert-header-from-buffer buf p-min p-max
284 invisible-fields visible-fields))))
291 ;;; mmimap.el ends here