* elmo-imap4.el (elmo-imap4-send-command):
[elisp/wanderlust.git] / elmo / mmelmo-imap4-2.el
1 ;;; mmelmo-imap4-1.el -- MM backend of IMAP4 for ELMO (for FLIM 1.13.x).
2
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
9
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)
13 ;; any later version.
14 ;;
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.
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
26 ;;; Commentary:
27 ;; 
28
29 ;;; Code:
30 ;; 
31 (require 'mmbuffer)
32 (require 'mmelmo)
33 (defvar mmelmo-imap4-threshold nil)
34 (defvar mmelmo-imap4-skipped-parts nil)
35 (defvar mmelmo-imap4-current-message-structure nil)
36
37 ;; Buffer local variable.
38 (defvar mmelmo-imap4-fetched nil)
39 (make-variable-buffer-local 'mmelmo-imap4-fetched)
40
41 (defun mmelmo-imap4-node-id-to-string (node-id)
42   (let ((i (length node-id))
43         result)
44     (while (> i 0)
45       (setq result
46             (concat result
47                     (if result
48                         (concat "." (int-to-string
49                                      (+ 1 (nth (- i 1) node-id))))
50                       (int-to-string (or
51                                       (+ 1 (nth (- i 1) node-id))
52                                       0)))))
53       (setq i (- i 1)))
54     (or result "0")))
55
56 ;; parse IMAP4 body structure entity recursively.
57 (defun mmelmo-imap4-parse-bodystructure-object (folder
58                                                 number msgdb
59                                                 node-id object parent)
60   (cond
61    ((listp (car object));; multipart
62     (let (cur-obj children content-type ret-val (num 0))
63       (setq ret-val
64             (luna-make-entity
65              (mm-expand-class-name 'elmo-imap4)
66              :folder   folder
67              :number   number
68              :msgdb    msgdb
69              :parent   parent
70              :node-id  node-id))
71       (while (and (setq cur-obj (car object))
72                   (listp cur-obj))
73         (setq children
74               (append children
75                       (list
76                        (mmelmo-imap4-parse-bodystructure-object
77                         folder number msgdb
78                         (append (list num) node-id)
79                         cur-obj
80                         ret-val ; myself as parent
81                         ))))
82         (setq num (+ num 1))
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
88                                      (list (cons 'subtype
89                                                  (intern
90                                                   (downcase
91                                                    (elmo-imap4-nth
92                                                     0
93                                                     object))))))))
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)
98       ret-val))
99    (t ;; singlepart
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
105                                      (list
106                                       (cons 'subtype
107                                             (intern
108                                              (downcase
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)))))
114       (setq ret-val
115             (luna-make-entity
116              (mm-expand-class-name 'elmo-imap4)
117              :folder folder
118              :number number
119              :size (nth 6 object)
120              :content-type content-type
121              :parent parent
122              :node-id node-id))
123       (mime-entity-set-encoding-internal ret-val
124                                          (and (elmo-imap4-nth 5 object)
125                                               (downcase
126                                                (elmo-imap4-nth 5 object))))
127       ret-val))))
128
129 (defun mmelmo-imap4-multipart-p (entity)
130   (eq (cdr (assq 'type (mime-entity-content-type entity))) 'multipart))
131
132 (defun mmelmo-imap4-rfc822part-p (entity)
133   (eq (cdr (assq 'type (mime-entity-content-type entity))) 'rfc822))
134
135 (defun mmelmo-imap4-textpart-p (entity)
136   (eq (cdr (assq 'type (mime-entity-content-type entity))) 'text))
137       
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
143      folder
144      number
145      msgdb
146      nil ; node-id
147      (elmo-imap4-response-value
148       (elmo-imap4-response-value
149        (elmo-imap4-send-command-wait
150         session
151         (format
152          (if elmo-imap4-use-uid
153              "uid fetch %s bodystructure"
154            "fetch %s bodystructure")
155          number)) 'fetch) 'bodystructure)
156      nil ; parent
157      )))
158
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)))
166       (progn
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
180           (append
181            mmelmo-imap4-skipped-parts
182            (list (mmelmo-imap4-node-id-to-string
183                   (mime-entity-node-id-internal entity)))))))
184
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
197           (append
198            mmelmo-imap4-skipped-parts
199            (list (mmelmo-imap4-node-id-to-string
200                   (mime-entity-node-id-internal entity)))))))
201
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)
206
207 (luna-define-method initialize-instance ((entity mime-elmo-imap4-entity)
208                                          &rest init-args)
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))
216          entity init-args))
217
218 (defun mmelmo-imap4-mime-entity-buffer (entity)
219   (if (mime-buffer-entity-buffer-internal entity)
220       (save-excursion
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))
233     ;; No buffer exist.
234     (save-excursion
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))
242         (erase-buffer)
243         (mime-entity-node-id entity)
244         (if (mime-root-entity-p entity)
245             (progn
246               ;; root entity
247               (setq mmelmo-imap4-current-message-structure entity)
248               (setq mime-message-structure entity)
249               (setq mmelmo-imap4-skipped-parts nil)
250               ;; insert header
251               (insert (elmo-imap4-read-part
252                        (mime-elmo-entity-folder-internal entity)
253                        (mime-elmo-entity-number-internal entity)
254                        "header"))
255               (mime-buffer-entity-set-header-start-internal
256                entity (point-min))
257               (mime-buffer-entity-set-header-end-internal
258                entity (max (- (point) 1) 1))
259               (if (null (mime-entity-children-internal entity))
260                   (progn
261                     (mime-buffer-entity-set-body-start-internal
262                      entity (point))
263                     ;; insert body if size is OK.
264                     (mmelmo-imap4-insert-body entity)
265                     (mime-buffer-entity-set-body-end-internal
266                      entity (point)))))
267           (setq mime-message-structure
268                 mmelmo-imap4-current-message-structure)
269           (mmelmo-imap4-read-part entity)))
270       (current-buffer))))
271
272 ; mime-entity-children
273
274 ;; override generic function for dynamic body fetching.
275 (luna-define-method mime-entity-body ((entity
276                                        mime-elmo-imap4-entity))
277   (save-excursion
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))))
281
282 (luna-define-method mime-entity-content ((entity
283                                           mime-elmo-imap4-entity))
284   (save-excursion
285     (set-buffer (mmelmo-imap4-mime-entity-buffer entity))
286     (mime-decode-string
287      (buffer-substring (mime-buffer-entity-body-start-internal entity)
288                        (mime-buffer-entity-body-end-internal entity))
289      (mime-entity-encoding entity))))
290
291 (luna-define-method mime-entity-fetch-field ((entity mime-elmo-imap4-entity)
292                                              field-name)
293   (save-excursion
294     (save-restriction
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))
299             (progn
300               (narrow-to-region
301                (mime-buffer-entity-header-start-internal entity)
302                (mime-buffer-entity-header-end-internal entity))
303               (std11-fetch-field field-name))
304           nil)))))
305
306 (luna-define-method mime-insert-header ((entity mime-elmo-imap4-entity)
307                                         &optional invisible-fields
308                                         visible-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))
314
315 (luna-define-method mime-entity-header-buffer ((entity mime-elmo-imap4-entity))
316   (mime-buffer-entity-buffer-internal entity))
317
318 (luna-define-method mime-entity-body-buffer ((entity mime-elmo-imap4-entity))
319   (mime-buffer-entity-buffer-internal entity))
320
321 (luna-define-method mime-write-entity-content ((entity mime-elmo-imap4-entity)
322                                                filename)
323   (let ((mmelmo-imap4-threshold (mime-elmo-entity-size-internal entity)))
324     (if (mime-buffer-entity-buffer-internal entity)
325         (save-excursion
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.
330             ))
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
337            entity
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)
344          filename
345          (or (mime-entity-encoding entity) "7bit"))))))
346
347 (provide 'mmelmo-imap4-2)
348
349 ;;; mmelmo-imap4-2.el ends here