1 ;;; mmelmo-imap4-1.el -- MM backend of IMAP4 for ELMO (for FLIM 1.13.x).
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
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.
33 (defvar mmelmo-imap4-threshold nil)
34 (defvar mmelmo-imap4-skipped-parts nil)
35 (defvar mmelmo-imap4-current-message-structure nil)
37 ;; Buffer local variable.
38 (defvar mmelmo-imap4-fetched nil)
39 (make-variable-buffer-local 'mmelmo-imap4-fetched)
41 (defun mmelmo-imap4-node-id-to-string (node-id)
42 (let ((i (length node-id))
48 (concat "." (int-to-string
49 (+ 1 (nth (- i 1) node-id))))
51 (+ 1 (nth (- i 1) node-id))
56 ;; parse IMAP4 body structure entity recursively.
57 (defun mmelmo-imap4-parse-bodystructure-object (folder
59 node-id object parent)
61 ((listp (car object));; multipart
62 (let (cur-obj children content-type ret-val (num 0))
65 (mm-expand-class-name 'elmo-imap4)
71 (while (and (setq cur-obj (car object))
76 (mmelmo-imap4-parse-bodystructure-object
78 (append (list num) node-id)
80 ret-val ; myself as parent
83 (setq object (cdr object)))
84 (mime-entity-set-children-internal ret-val children)
85 (setq content-type (list (cons 'type 'multipart)))
86 (if (elmo-imap4-nth 0 object)
87 (setq content-type (append content-type
94 (setq content-type (append content-type
95 (mime-parse-parameters-from-list
96 (elmo-imap4-nth 1 object))))
97 (mime-entity-set-content-type-internal ret-val content-type)
100 (let (content-type ret-val)
101 ;; append size information into location
102 (setq content-type (list (cons 'type (intern (downcase (car object))))))
103 (if (elmo-imap4-nth 1 object)
104 (setq content-type (append content-type
109 (elmo-imap4-nth 1 object))))))))
110 (if (elmo-imap4-nth 2 object)
111 (setq content-type (append content-type
112 (mime-parse-parameters-from-list
113 (elmo-imap4-nth 2 object)))))
116 (mm-expand-class-name 'elmo-imap4)
120 :content-type content-type
123 (mime-entity-set-encoding-internal ret-val
124 (and (elmo-imap4-nth 5 object)
126 (elmo-imap4-nth 5 object))))
129 (defun mmelmo-imap4-multipart-p (entity)
130 (eq (cdr (assq 'type (mime-entity-content-type entity))) 'multipart))
132 (defun mmelmo-imap4-rfc822part-p (entity)
133 (eq (cdr (assq 'type (mime-entity-content-type entity))) 'rfc822))
135 (defun mmelmo-imap4-textpart-p (entity)
136 (eq (cdr (assq 'type (mime-entity-content-type entity))) 'text))
138 (defun mmelmo-imap4-get-mime-entity (folder number msgdb)
139 (let* ((spec (elmo-folder-get-spec folder))
140 (session (elmo-imap4-get-session spec)))
141 (elmo-imap4-session-select-mailbox session (elmo-imap4-spec-mailbox spec))
142 (mmelmo-imap4-parse-bodystructure-object
147 (elmo-imap4-response-value
148 (elmo-imap4-response-value
149 (elmo-imap4-send-command-wait
152 (if elmo-imap4-use-uid
153 "uid fetch %s bodystructure"
154 "fetch %s bodystructure")
155 number)) 'fetch) 'bodystructure)
159 (defun mmelmo-imap4-read-part (entity)
160 (if (or (not mmelmo-imap4-threshold)
161 (not (mime-elmo-entity-size-internal entity))
162 (and (mime-elmo-entity-size-internal entity)
163 mmelmo-imap4-threshold
164 (<= (mime-elmo-entity-size-internal entity)
165 mmelmo-imap4-threshold)))
167 (cond ((mmelmo-imap4-multipart-p entity)) ; noop
168 (t (insert (elmo-imap4-read-part
169 (mime-elmo-entity-folder-internal entity)
170 (mime-elmo-entity-number-internal entity)
171 (mmelmo-imap4-node-id-to-string
172 (mime-entity-node-id-internal entity))))))
173 (setq mmelmo-imap4-fetched t)
174 (mime-buffer-entity-set-body-start-internal entity (point-min))
175 (mime-buffer-entity-set-body-end-internal entity (point-max)))
176 (setq mmelmo-imap4-fetched nil)
177 (mime-buffer-entity-set-body-start-internal entity (point-min))
178 (mime-buffer-entity-set-body-end-internal entity (point-min))
179 (setq mmelmo-imap4-skipped-parts
181 mmelmo-imap4-skipped-parts
182 (list (mmelmo-imap4-node-id-to-string
183 (mime-entity-node-id-internal entity)))))))
185 (defun mmelmo-imap4-insert-body (entity)
186 (mime-buffer-entity-set-body-start-internal entity (- (point) 1))
187 (if (or (not mmelmo-imap4-threshold)
188 (not (mime-elmo-entity-size-internal entity))
189 (and (mime-elmo-entity-size-internal entity)
190 mmelmo-imap4-threshold
191 (<= (mime-elmo-entity-size-internal entity)
192 mmelmo-imap4-threshold)))
193 (insert (elmo-imap4-read-part
194 (mime-elmo-entity-folder-internal entity)
195 (mime-elmo-entity-number-internal entity) "1"))
196 (setq mmelmo-imap4-skipped-parts
198 mmelmo-imap4-skipped-parts
199 (list (mmelmo-imap4-node-id-to-string
200 (mime-entity-node-id-internal entity)))))))
202 ;;; mime-elmo-imap4-entity class definitions.
203 (luna-define-class mime-elmo-imap4-entity (mime-buffer-entity)
204 (imap folder number msgdb size))
205 (luna-define-internal-accessors 'mime-elmo-imap4-entity)
207 (luna-define-method initialize-instance ((entity mime-elmo-imap4-entity)
209 "The initialization method for elmo-imap4.
210 mime-elmo-entity has its own instance variable
211 `imap', `folder', `msgdb', and `size'.
212 These value must be specified as argument for `luna-make-entity'."
213 (apply (car (luna-class-find-functions
214 (luna-find-class 'standard-object)
215 'initialize-instance))
218 (defun mmelmo-imap4-mime-entity-buffer (entity)
219 (if (mime-buffer-entity-buffer-internal entity)
221 (set-buffer (mime-buffer-entity-buffer-internal entity))
222 (unless (mime-root-entity-p entity)
223 (unless mmelmo-imap4-fetched
224 (setq mmelmo-imap4-skipped-parts nil) ; No need?
225 (let ((mmelmo-imap4-threshold
226 (mime-elmo-entity-size-internal entity)))
227 (mime-buffer-entity-set-buffer-internal entity nil)
228 (message "Fetching skipped part...")
229 (mmelmo-imap4-mime-entity-buffer entity)
230 (message "Fetching skipped part...done."))
231 (setq mmelmo-imap4-fetched t)))
232 (mime-buffer-entity-buffer-internal entity))
235 (set-buffer (get-buffer-create
236 (concat mmelmo-entity-buffer-name
237 (mmelmo-imap4-node-id-to-string
238 (mime-entity-node-id-internal entity)))))
239 (mmelmo-original-mode)
240 (mime-buffer-entity-set-buffer-internal entity (current-buffer))
241 (let ((buffer-read-only nil))
243 (mime-entity-node-id entity)
244 (if (mime-root-entity-p entity)
247 (setq mmelmo-imap4-current-message-structure entity)
248 (setq mime-message-structure entity)
249 (setq mmelmo-imap4-skipped-parts nil)
251 (insert (elmo-imap4-read-part
252 (mime-elmo-entity-folder-internal entity)
253 (mime-elmo-entity-number-internal entity)
255 (mime-buffer-entity-set-header-start-internal
257 (mime-buffer-entity-set-header-end-internal
258 entity (max (- (point) 1) 1))
259 (if (null (mime-entity-children-internal entity))
261 (mime-buffer-entity-set-body-start-internal
263 ;; insert body if size is OK.
264 (mmelmo-imap4-insert-body entity)
265 (mime-buffer-entity-set-body-end-internal
267 (setq mime-message-structure
268 mmelmo-imap4-current-message-structure)
269 (mmelmo-imap4-read-part entity)))
272 ; mime-entity-children
274 ;; override generic function for dynamic body fetching.
275 (luna-define-method mime-entity-body ((entity
276 mime-elmo-imap4-entity))
278 (set-buffer (mmelmo-imap4-mime-entity-buffer entity))
279 (buffer-substring (mime-buffer-entity-body-start-internal entity)
280 (mime-buffer-entity-body-end-internal entity))))
282 (luna-define-method mime-entity-content ((entity
283 mime-elmo-imap4-entity))
285 (set-buffer (mmelmo-imap4-mime-entity-buffer entity))
287 (buffer-substring (mime-buffer-entity-body-start-internal entity)
288 (mime-buffer-entity-body-end-internal entity))
289 (mime-entity-encoding entity))))
291 (luna-define-method mime-entity-fetch-field ((entity mime-elmo-imap4-entity)
295 (when (mime-buffer-entity-buffer-internal entity)
296 (set-buffer (mime-buffer-entity-buffer-internal entity))
297 (if (and (mime-buffer-entity-header-start-internal entity)
298 (mime-buffer-entity-header-end-internal entity))
301 (mime-buffer-entity-header-start-internal entity)
302 (mime-buffer-entity-header-end-internal entity))
303 (std11-fetch-field field-name))
306 (luna-define-method mime-insert-header ((entity mime-elmo-imap4-entity)
307 &optional invisible-fields
309 (mmelmo-insert-sorted-header-from-buffer
310 (mmelmo-imap4-mime-entity-buffer entity)
311 (mime-buffer-entity-header-start-internal entity)
312 (mime-buffer-entity-header-end-internal entity)
313 invisible-fields visible-fields))
315 (luna-define-method mime-entity-header-buffer ((entity mime-elmo-imap4-entity))
316 (mime-buffer-entity-buffer-internal entity))
318 (luna-define-method mime-entity-body-buffer ((entity mime-elmo-imap4-entity))
319 (mime-buffer-entity-buffer-internal entity))
321 (luna-define-method mime-write-entity-content ((entity mime-elmo-imap4-entity)
323 (let ((mmelmo-imap4-threshold (mime-elmo-entity-size-internal entity)))
324 (if (mime-buffer-entity-buffer-internal entity)
326 (set-buffer (mime-buffer-entity-buffer-internal entity))
327 (unless mmelmo-imap4-fetched
328 (setq mmelmo-imap4-skipped-parts nil) ; No need?
329 (mime-buffer-entity-set-buffer-internal entity nil) ; To re-fetch.
331 (unless mmelmo-imap4-fetched
332 (setq mmelmo-imap4-skipped-parts nil) ; No need?
333 (let ((mmelmo-imap4-threshold (mime-elmo-entity-size-internal entity)))
334 (mime-buffer-entity-set-buffer-internal entity nil) ; To re-fetch.
335 (message "Fetching skipped part...")
336 (mime-buffer-entity-set-buffer-internal
338 (mmelmo-imap4-mime-entity-buffer entity))
339 (message "Fetching skipped part...done.")))
340 (with-current-buffer (mime-buffer-entity-buffer-internal entity)
341 (mime-write-decoded-region
342 (mime-buffer-entity-body-start-internal entity)
343 (mime-buffer-entity-body-end-internal entity)
345 (or (mime-entity-encoding entity) "7bit"))))))
347 (provide 'mmelmo-imap4-2)
349 ;;; mmelmo-imap4-2.el ends here