* elmo-dop.el (elmo-folder-status-dop): If spool-folder is absent,
[elisp/wanderlust.git] / elmo / elmo-imap4.el
1 ;;; elmo-imap4.el --- IMAP4 Interface for ELMO.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1999,2000      Kenichi OKADA <okada@opaopa.org>
5 ;; Copyright (C) 2000           OKAZAKI Tetsurou <okazaki@be.to>
6 ;; Copyright (C) 2000           Daiki Ueno <ueno@unixuser.org>
7
8 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
9 ;;      Kenichi OKADA <okada@opaopa.org>
10 ;;      OKAZAKI Tetsurou <okazaki@be.to>
11 ;;      Daiki Ueno <ueno@unixuser.org>
12 ;; Keywords: mail, net news
13
14 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
15
16 ;; This program is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
20 ;;
21 ;; This program is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 ;; GNU General Public License for more details.
25 ;;
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
30 ;;
31
32 ;;; Commentary:
33 ;;
34 ;; Origin of IMAP parser part is imap.el, included in Gnus.
35 ;;
36 ;;    Copyright (C) 1998, 1999, 2000
37 ;;    Free Software Foundation, Inc.
38 ;;    Author: Simon Josefsson <jas@pdc.kth.se>
39 ;;
40
41 (require 'elmo-vars)
42 (require 'elmo-util)
43 (require 'elmo-date)
44 (require 'elmo-msgdb)
45 (require 'elmo-cache)
46 (require 'elmo)
47 (require 'elmo-net)
48 (require 'utf7)
49 (require 'elmo-mime)
50
51 ;;; Code:
52 (eval-when-compile (require 'cl))
53
54 (defvar elmo-imap4-disuse-server-flag-mailbox-regexp "^#mh" ; UW imapd
55   "Regexp to match IMAP4 mailbox names whose message flags on server should be ignored.
56 (Except `\\Deleted' flag).")
57
58 (defvar elmo-imap4-overview-fetch-chop-length 200
59   "*Number of overviews to fetch in one request.")
60
61 ;; c.f. rfc2683 3.2.1.5 Long Command Lines
62 ;;
63 ;; "A client should limit the length of the command lines it generates
64 ;;  to approximately 1000 octets (including all quoted strings but not
65 ;;  including literals). If the client is unable to group things into
66 ;;  ranges so that the command line is within that length, it should
67 ;;  split the request into multiple commands. The client should use
68 ;;  literals instead of long quoted strings, in order to keep the command
69 ;;  length down.
70 ;;  For its part, a server should allow for a command line of at least
71 ;;  8000 octets. This provides plenty of leeway for accepting reasonable
72 ;;  length commands from clients. The server should send a BAD response
73 ;;  to a command that does not end within the server's maximum accepted
74 ;;  command length. "
75
76 ;; To limit command line length, chop number set.
77 (defvar elmo-imap4-number-set-chop-length 1000
78   "*Number of messages to specify as a number-set argument for one request.")
79
80 (defvar elmo-imap4-force-login nil
81   "*Non-nil forces to try 'login' if there is no 'auth' capability in imapd.")
82
83 (defvar elmo-imap4-use-select-to-update-status nil
84   "*Some imapd have to send select command to update status.
85 (ex. UW imapd 4.5-BETA?).  For these imapd, you must set this variable t.")
86
87 (defvar elmo-imap4-use-modified-utf7 nil
88   "*Use mofidied UTF-7 (rfc2060) encoding for IMAP4 folder name.")
89
90 (defvar elmo-imap4-use-cache t
91   "Use cache in imap4 folder.")
92
93 (defvar elmo-imap4-extra-namespace-alist
94   '(("^\\({.*/nntp}\\).*$" . ".")) ; Default is for UW's remote nntp mailbox...
95   "Extra namespace alist. 
96 A list of cons cell like: (REGEXP . DELIMITER).
97 REGEXP should have a grouping for namespace prefix.")
98 ;;
99 ;;; internal variables
100 ;;
101 (defvar elmo-imap4-seq-prefix "elmo-imap4")
102 (defvar elmo-imap4-seqno 0)
103 (defvar elmo-imap4-use-uid t
104   "Use UID as message number.")
105
106 (defvar elmo-imap4-current-response nil)
107 (defvar elmo-imap4-status nil)
108 (defvar elmo-imap4-reached-tag "elmo-imap40")
109
110 ;;; buffer local variables
111 (defvar elmo-imap4-default-hierarchy-delimiter "/")
112
113 (defvar elmo-imap4-server-capability nil)
114 (defvar elmo-imap4-server-namespace nil)
115
116 (defvar elmo-imap4-parsing nil) ; indicates parsing.
117
118 (defvar elmo-imap4-fetch-callback nil)
119 (defvar elmo-imap4-fetch-callback-data nil)
120 (defvar elmo-imap4-status-callback nil)
121 (defvar elmo-imap4-status-callback-data nil)
122
123 (defvar elmo-imap4-server-diff-async-callback nil)
124 (defvar elmo-imap4-server-diff-async-callback-data nil)
125
126 ;;; progress...(no use?)
127 (defvar elmo-imap4-count-progress nil)
128 (defvar elmo-imap4-count-progress-message nil)
129 (defvar elmo-imap4-progress-count nil)
130
131 ;;; XXX Temporal implementation
132 (defvar elmo-imap4-current-msgdb nil)
133 (defvar elmo-imap4-seen-messages nil)
134
135 (defvar elmo-imap4-local-variables
136   '(elmo-imap4-status
137     elmo-imap4-current-response
138     elmo-imap4-seqno
139     elmo-imap4-parsing
140     elmo-imap4-reached-tag
141     elmo-imap4-count-progress
142     elmo-imap4-count-progress-message
143     elmo-imap4-progress-count
144     elmo-imap4-fetch-callback
145     elmo-imap4-fetch-callback-data
146     elmo-imap4-status-callback
147     elmo-imap4-status-callback-data
148     elmo-imap4-current-msgdb
149     elmo-imap4-seen-messages))
150
151 ;;;;
152
153 (defconst elmo-imap4-quoted-specials-list '(?\\ ?\"))
154
155 (defconst elmo-imap4-non-atom-char-regex
156   (eval-when-compile
157     (concat "[^" "]!#$&'+,./0-9:;<=>?@A-Z[^_`a-z|}~-" "]")))
158
159 (defconst elmo-imap4-non-text-char-regex
160   (eval-when-compile
161     (concat "[^"
162             "]\x01-\x09\x0b\x0c\x0e-\x1f\x7f !\"#$%&'()*+,./0-9:;<=>?@A-Z[\\^_`a-z{|}~-"
163             "]")))
164
165 (defconst elmo-imap4-literal-threshold 1024
166  "Limitation of characters that can be used in a quoted string.")
167
168 ;; For debugging.
169 (defvar elmo-imap4-debug nil
170   "Non-nil forces IMAP4 folder as debug mode.
171 Debug information is inserted in the buffer \"*IMAP4 DEBUG*\"")
172
173 (defvar elmo-imap4-debug-inhibit-logging nil)
174
175 ;;; ELMO IMAP4 folder
176 (eval-and-compile
177   (luna-define-class elmo-imap4-folder (elmo-net-folder)
178                      (mailbox))
179   (luna-define-internal-accessors 'elmo-imap4-folder))
180
181 ;;; Session
182 (eval-and-compile
183   (luna-define-class elmo-imap4-session (elmo-network-session)
184                      (capability current-mailbox read-only))
185   (luna-define-internal-accessors 'elmo-imap4-session))
186
187 ;;; MIME-ELMO-IMAP Location
188 (eval-and-compile
189   (luna-define-class mime-elmo-imap-location
190                      (mime-imap-location)
191                      (folder number rawbuf strategy))
192   (luna-define-internal-accessors 'mime-elmo-imap-location))
193
194 ;;; Debug
195 (defmacro elmo-imap4-debug (message &rest args)
196   (` (if elmo-imap4-debug
197          (elmo-imap4-debug-1 (, message) (,@ args)))))
198
199 (defun elmo-imap4-debug-1 (message &rest args)
200   (with-current-buffer (get-buffer-create "*IMAP4 DEBUG*")
201     (goto-char (point-max))
202     (if elmo-imap4-debug-inhibit-logging
203         (insert "NO LOGGING\n")
204       (insert (apply 'format message args) "\n"))))
205
206 (defsubst elmo-imap4-decode-folder-string (string)
207   (if elmo-imap4-use-modified-utf7
208       (utf7-decode-string string 'imap)
209     string))
210
211 (defsubst elmo-imap4-encode-folder-string (string)
212   (if elmo-imap4-use-modified-utf7
213       (utf7-encode-string string 'imap)
214     string))
215
216 ;;; Response
217
218 (defmacro elmo-imap4-response-continue-req-p (response)
219   "Returns non-nil if RESPONSE is '+' response."
220   (` (assq 'continue-req (, response))))
221
222 (defmacro elmo-imap4-response-ok-p (response)
223   "Returns non-nil if RESPONSE is an 'OK' response."
224   (` (assq 'ok (, response))))
225
226 (defmacro elmo-imap4-response-bye-p (response)
227   "Returns non-nil if RESPONSE is an 'BYE' response."
228   (` (assq 'bye (, response))))
229
230 (defmacro elmo-imap4-response-value (response symbol)
231   "Get value of the SYMBOL from RESPONSE."
232   (` (nth 1 (assq (, symbol) (, response)))))
233
234 (defsubst elmo-imap4-response-value-all (response symbol)
235   "Get all value of the SYMBOL from RESPONSE."
236   (let (matched)
237     (while response
238       (if (eq (car (car response)) symbol)
239           (setq matched (nconc matched (nth 1 (car response)))))
240       (setq response (cdr response)))
241     matched))
242
243 (defmacro elmo-imap4-response-error-text (response)
244   "Returns text of NO, BAD, BYE response."
245   (` (nth 1 (or (elmo-imap4-response-value (, response) 'no)
246                 (elmo-imap4-response-value (, response) 'bad)
247                 (elmo-imap4-response-value (, response) 'bye)))))
248
249 (defmacro elmo-imap4-response-bodydetail-text (response)
250   "Returns text of BODY[section]<partial>."
251   (` (nth 3 (assq 'bodydetail (, response)))))
252
253 ;;; Session commands.
254
255 ; (defun elmo-imap4-send-command-wait (session command)
256 ;   "Send COMMAND to the SESSION and wait for response.
257 ; Returns RESPONSE (parsed lisp object) of IMAP session."
258 ;   (elmo-imap4-read-response session
259 ;                           (elmo-imap4-send-command
260 ;                            session
261 ;                            command)))
262
263 (defun elmo-imap4-send-command-wait (session command)
264   "Send COMMAND to the SESSION.
265 Returns RESPONSE (parsed lisp object) of IMAP session.
266 If response is not `OK', causes error with IMAP response text."
267   (elmo-imap4-accept-ok session
268                         (elmo-imap4-send-command
269                          session
270                          command)))
271
272 (defun elmo-imap4-send-command (session command)
273   "Send COMMAND to the SESSION.
274 Returns a TAG string which is assigned to the COMMAND."
275   (let* ((command-args (if (listp command)
276                            command
277                          (list command)))
278          (process (elmo-network-session-process-internal session))
279          cmdstr tag token kind)
280     (with-current-buffer (process-buffer process)
281       (setq tag (concat elmo-imap4-seq-prefix
282                         (number-to-string
283                          (setq elmo-imap4-seqno (+ 1 elmo-imap4-seqno)))))
284       (setq cmdstr (concat tag " "))
285       ;; (erase-buffer) No need.
286       (goto-char (point-min))
287       (when (elmo-imap4-response-bye-p elmo-imap4-current-response)
288         (elmo-imap4-process-bye session))
289       (setq elmo-imap4-current-response nil)
290       (if elmo-imap4-parsing
291           (error "IMAP process is running. Please wait (or plug again)"))
292       (setq elmo-imap4-parsing t)
293       (elmo-imap4-debug "<-(%s)- %s" tag command)
294       (while (setq token (car command-args))
295         (cond ((stringp token)   ; formatted
296                (setq cmdstr (concat cmdstr token)))
297               ((listp token)     ; unformatted
298                (setq kind (car token))
299                (cond ((eq kind 'atom)
300                       (setq cmdstr (concat cmdstr (nth 1 token))))
301                      ((eq kind 'quoted)
302                       (setq cmdstr (concat
303                                     cmdstr
304                                     (elmo-imap4-format-quoted (nth 1 token)))))
305                      ((eq kind 'literal)
306                       (setq cmdstr (concat cmdstr
307                                            (format "{%d}" (nth 2 token))))
308                       (process-send-string process cmdstr)
309                       (process-send-string process "\r\n")
310                       (setq cmdstr nil)
311                       (elmo-imap4-accept-continue-req session)
312                       (cond ((stringp (nth 1 token))
313                              (setq cmdstr (nth 1 token)))
314                             ((bufferp (nth 1 token))
315                              (with-current-buffer (nth 1 token)
316                                (process-send-region
317                                 process
318                                 (point-min)
319                                 (+ (point-min) (nth 2 token)))))
320                             (t
321                              (error "Wrong argument for literal"))))
322                      (t
323                       (error "Unknown token kind %s" kind))))
324               (t
325                (error "Invalid argument")))
326         (setq command-args (cdr command-args)))
327       (if cmdstr
328           (process-send-string process cmdstr))
329       (process-send-string process "\r\n")
330       tag)))
331
332 (defun elmo-imap4-send-string (session string)
333   "Send STRING to the SESSION."
334   (with-current-buffer (process-buffer
335                         (elmo-network-session-process-internal session))
336     (setq elmo-imap4-current-response nil)
337     (goto-char (point-min))
338     (elmo-imap4-debug "<-- %s" string)
339     (process-send-string (elmo-network-session-process-internal session)
340                          string)
341     (process-send-string (elmo-network-session-process-internal session)
342                          "\r\n")))
343
344 (defun elmo-imap4-read-response (session tag)
345   "Read parsed response from SESSION.
346 TAG is the tag of the command"
347   (with-current-buffer (process-buffer
348                         (elmo-network-session-process-internal session))
349     (while (not (or (string= tag elmo-imap4-reached-tag)
350                     (elmo-imap4-response-bye-p elmo-imap4-current-response)))
351       (when (memq (process-status
352                    (elmo-network-session-process-internal session))
353                   '(open run))
354         (accept-process-output (elmo-network-session-process-internal session)
355                                1)))
356     (elmo-imap4-debug "=>%s" (prin1-to-string elmo-imap4-current-response))
357     (setq elmo-imap4-parsing nil)
358     elmo-imap4-current-response))
359
360 (defsubst elmo-imap4-read-untagged (process)
361   (with-current-buffer (process-buffer process)
362     (while (not elmo-imap4-current-response)
363       (accept-process-output process 1))
364     (elmo-imap4-debug "=>%s" (prin1-to-string elmo-imap4-current-response))
365     elmo-imap4-current-response))
366
367 (defun elmo-imap4-read-continue-req (session)
368   "Returns a text following to continue-req in SESSION.
369 If response is not `+' response, returns nil."
370   (elmo-imap4-response-value
371    (elmo-imap4-read-untagged
372     (elmo-network-session-process-internal session))
373    'continue-req))
374
375 (defun elmo-imap4-process-bye (session)
376   (with-current-buffer (elmo-network-session-buffer session)
377     (let ((r elmo-imap4-current-response))
378       (setq elmo-imap4-current-response nil)
379       (elmo-network-close-session session)
380       (signal 'elmo-imap4-bye-error
381               (list (concat (elmo-imap4-response-error-text r))
382                     "Try Again")))))
383
384 (defun elmo-imap4-accept-continue-req (session)
385   "Returns non-nil if `+' (continue-req) response is arrived in SESSION.
386 If response is not `+' response, cause an error."
387   (let (response)
388     (setq response
389           (elmo-imap4-read-untagged
390            (elmo-network-session-process-internal session)))
391     (or (elmo-imap4-response-continue-req-p response)
392         (error "IMAP error: %s"
393                (or (elmo-imap4-response-error-text response)
394                    "No continut-req from server.")))))
395
396 (defun elmo-imap4-read-ok (session tag)
397   "Returns non-nil if `OK' response of the command with TAG is arrived
398 in SESSION. If response is not `OK' response, returns nil."
399   (elmo-imap4-response-ok-p
400    (elmo-imap4-read-response session tag)))
401
402 (defun elmo-imap4-accept-ok (session tag)
403   "Accept only `OK' response from SESSION.
404 If response is not `OK' response, causes error with IMAP response text."
405   (let ((response (elmo-imap4-read-response session tag)))
406     (if (elmo-imap4-response-ok-p response)
407         response
408       (if (elmo-imap4-response-bye-p response)
409           (elmo-imap4-process-bye session)
410         (error "IMAP error: %s"
411                (or (elmo-imap4-response-error-text response)
412                    "No `OK' response from server."))))))
413
414 ;;; MIME-ELMO-IMAP Location
415 (luna-define-method mime-imap-location-section-body ((location
416                                                       mime-elmo-imap-location)
417                                                      section)
418   (if (and (stringp section)
419            (string= section "HEADER"))
420       ;; Even in the section mode, header fields should be saved to the
421       ;; raw buffer .
422       (with-current-buffer (mime-elmo-imap-location-rawbuf-internal location)
423         (erase-buffer)
424         (elmo-message-fetch
425          (mime-elmo-imap-location-folder-internal location)
426          (mime-elmo-imap-location-number-internal location)
427          (mime-elmo-imap-location-strategy-internal location)
428          section
429          (current-buffer)
430          'unseen)
431         (buffer-string))
432     (elmo-message-fetch
433      (mime-elmo-imap-location-folder-internal location)
434      (mime-elmo-imap-location-number-internal location)
435      (mime-elmo-imap-location-strategy-internal location)
436      section
437      nil 'unseen)))
438
439
440 (luna-define-method mime-imap-location-bodystructure
441   ((location mime-elmo-imap-location))
442   (elmo-imap4-fetch-bodystructure
443    (mime-elmo-imap-location-folder-internal location)
444    (mime-elmo-imap-location-number-internal location)
445    (mime-elmo-imap-location-strategy-internal location)))
446
447 (luna-define-method mime-imap-location-fetch-entity-p
448   ((location mime-elmo-imap-location) entity)
449   (or (not elmo-message-displaying) ; Fetching entity to save or force display.
450       ;; cache exists
451       (file-exists-p
452        (expand-file-name
453         (mmimap-entity-section (mime-entity-node-id-internal entity))
454         (elmo-fetch-strategy-cache-path
455          (mime-elmo-imap-location-strategy-internal location))))
456       ;; not too large to fetch.
457       (> elmo-message-fetch-threshold
458          (or (mime-imap-entity-size-internal entity) 0))))
459
460 ;;;
461
462 (defun elmo-imap4-session-check (session)
463   (with-current-buffer (elmo-network-session-buffer session)
464     (setq elmo-imap4-fetch-callback nil)
465     (setq elmo-imap4-fetch-callback-data nil))
466   (elmo-imap4-send-command-wait session "check"))
467
468 (defun elmo-imap4-atom-p (string)
469   "Return t if STRING is an atom defined in rfc2060."
470   (if (string= string "")
471       nil
472     (save-match-data
473       (not (string-match elmo-imap4-non-atom-char-regex string)))))
474
475 (defun elmo-imap4-quotable-p (string)
476   "Return t if STRING can be formatted as a quoted defined in rfc2060."
477   (save-match-data
478     (not (string-match elmo-imap4-non-text-char-regex string))))
479
480 (defun elmo-imap4-nil (string)
481   "Return a list represents the special atom \"NIL\" defined in rfc2060, \
482 if STRING is nil.
483 Otherwise return nil."
484   (if (eq string nil)
485       (list 'atom "NIL")))
486
487 (defun elmo-imap4-atom (string)
488   "Return a list represents STRING as an atom defined in rfc2060.
489 Return nil if STRING is not an atom.  See `elmo-imap4-atom-p'."
490   (if (elmo-imap4-atom-p string)
491       (list 'atom string)))
492
493 (defun elmo-imap4-quoted (string)
494   "Return a list represents STRING as a quoted defined in rfc2060.
495 Return nil if STRING can not be formatted as a quoted.  See `elmo-imap4-quotable-p'."
496   (if (elmo-imap4-quotable-p string)
497       (list 'quoted string)))
498
499 (defun elmo-imap4-literal-1 (string-or-buffer length)
500   "Internal function for `elmo-imap4-literal' and `elmo-imap4-buffer-literal'.
501 Return a list represents STRING-OR-BUFFER as a literal defined in rfc2060.
502 STRING-OR-BUFFER must be an encoded string or a single-byte string or a single-byte buffer.
503 LENGTH must be the number of octets for STRING-OR-BUFFER."
504   (list 'literal string-or-buffer length))
505
506 (defun elmo-imap4-literal (string)
507   "Return a list represents STRING as a literal defined in rfc2060.
508 STRING must be an encoded or a single-byte string."
509   (elmo-imap4-literal-1 string (length string)))
510
511 (defun elmo-imap4-buffer-literal (buffer)
512   "Return a list represents BUFFER as a literal defined in rfc2060.
513 BUFFER must be a single-byte buffer."
514   (elmo-imap4-literal-1 buffer (with-current-buffer buffer
515                                  (buffer-size))))
516
517 (defun elmo-imap4-string-1 (string length)
518   "Internal function for `elmo-imap4-string' and `elmo-imap4-buffer-string'.
519 Return a list represents STRING as a string defined in rfc2060.
520 STRING must be an encoded or a single-byte string.
521 LENGTH must be the number of octets for STRING."
522   (or (elmo-imap4-quoted string)
523       (elmo-imap4-literal-1 string length)))
524
525 (defun elmo-imap4-string (string)
526   "Return a list represents STRING as a string defined in rfc2060.
527 STRING must be an encoded or a single-byte string."
528   (let ((length (length string)))
529     (if (< elmo-imap4-literal-threshold length)
530         (elmo-imap4-literal-1 string length)
531       (elmo-imap4-string-1 string length))))
532
533 (defun elmo-imap4-buffer-string (buffer)
534   "Return a list represents BUFFER as a string defined in rfc2060.
535 BUFFER must be a single-byte buffer."
536   (let ((length (with-current-buffer buffer
537                   (buffer-size))))
538     (if (< elmo-imap4-literal-threshold length)
539         (elmo-imap4-literal-1 buffer length)
540       (elmo-imap4-string-1 (with-current-buffer buffer
541                              (buffer-string))
542                            length))))
543
544 (defun elmo-imap4-astring-1 (string length)
545   "Internal function for `elmo-imap4-astring' and `elmo-imap4-buffer-astring'.
546 Return a list represents STRING as an astring defined in rfc2060.
547 STRING must be an encoded or a single-byte string.
548 LENGTH must be the number of octets for STRING."
549   (or (elmo-imap4-atom string)
550       (elmo-imap4-string-1 string length)))
551
552 (defun elmo-imap4-astring (string)
553   "Return a list represents STRING as an astring defined in rfc2060.
554 STRING must be an encoded or a single-byte string."
555   (let ((length (length string)))
556     (if (< elmo-imap4-literal-threshold length)
557         (elmo-imap4-literal-1 string length)
558       (elmo-imap4-astring-1 string length))))
559
560 (defun elmo-imap4-buffer-astring (buffer)
561   "Return a list represents BUFFER as an astring defined in rfc2060.
562 BUFFER must be a single-byte buffer."
563   (let ((length (with-current-buffer buffer
564                   (buffer-size))))
565     (if (< elmo-imap4-literal-threshold length)
566         (elmo-imap4-literal-1 buffer length)
567       (elmo-imap4-astring-1 (with-current-buffer buffer
568                               (buffer-string))
569                             length))))
570
571 (defun elmo-imap4-nstring (string)
572   "Return a list represents STRING as a nstring defined in rfc2060.
573 STRING must be an encoded or a single-byte string."
574    (or (elmo-imap4-nil string)
575        (elmo-imap4-string string)))
576
577 (defun elmo-imap4-buffer-nstring (buffer)
578   "Return a list represents BUFFER as a nstring defined in rfc2060.
579 BUFFER must be a single-byte buffer."
580    (or (elmo-imap4-nil buffer)
581        (elmo-imap4-buffer-string buffer)))
582
583 (defalias 'elmo-imap4-mailbox 'elmo-imap4-astring)
584 (defalias 'elmo-imap4-field-body 'elmo-imap4-astring)
585 (defalias 'elmo-imap4-userid 'elmo-imap4-astring)
586 (defalias 'elmo-imap4-password 'elmo-imap4-astring)
587
588 (defun elmo-imap4-format-quoted (string)
589   "Return STRING in a form of the quoted-string defined in rfc2060."
590   (concat "\""
591           (std11-wrap-as-quoted-pairs string elmo-imap4-quoted-specials-list)
592           "\""))
593
594 (defsubst elmo-imap4-response-get-selectable-mailbox-list (response)
595   (delq nil
596         (mapcar
597          (lambda (entry)
598            (if (and (eq 'list (car entry))
599                     (not (member "\\NoSelect" (nth 1 (nth 1 entry)))))
600                (car (nth 1 entry))))
601          response)))
602
603 (defun elmo-imap4-fetch-bodystructure (folder number strategy)
604   "Fetch BODYSTRUCTURE for the message in the FOLDER with NUMBER using STRATEGY."
605   (if (elmo-fetch-strategy-use-cache strategy)
606       (elmo-object-load
607        (elmo-file-cache-expand-path
608         (elmo-fetch-strategy-cache-path strategy)
609         "bodystructure"))
610     (let ((session (elmo-imap4-get-session folder))
611           bodystructure)
612       (elmo-imap4-session-select-mailbox
613        session
614        (elmo-imap4-folder-mailbox-internal folder))
615       (with-current-buffer (elmo-network-session-buffer session)
616         (setq elmo-imap4-fetch-callback nil)
617         (setq elmo-imap4-fetch-callback-data nil))
618       (prog1 (setq bodystructure
619                    (elmo-imap4-response-value
620                     (elmo-imap4-response-value
621                      (elmo-imap4-send-command-wait
622                       session
623                       (format
624                        (if elmo-imap4-use-uid
625                            "uid fetch %s bodystructure"
626                          "fetch %s bodystructure")
627                        number))
628                      'fetch)
629                     'bodystructure))
630         (when (elmo-fetch-strategy-save-cache strategy)
631           (elmo-file-cache-delete
632            (elmo-fetch-strategy-cache-path strategy))
633           (elmo-object-save
634            (elmo-file-cache-expand-path
635             (elmo-fetch-strategy-cache-path strategy)
636             "bodystructure")
637            bodystructure))))))
638
639 ;;; Backend methods.
640 (luna-define-method elmo-create-folder-plugged ((folder elmo-imap4-folder))
641   (elmo-imap4-send-command-wait
642    (elmo-imap4-get-session folder)
643    (list "create " (elmo-imap4-mailbox
644                     (elmo-imap4-folder-mailbox-internal folder)))))
645
646 (defun elmo-imap4-get-session (folder &optional if-exists)
647   (elmo-network-get-session 'elmo-imap4-session
648                             (concat
649                              (if (elmo-folder-biff-internal folder)
650                                  "BIFF-")
651                              "IMAP")
652                             folder if-exists))
653
654 (defun elmo-imap4-session-select-mailbox (session mailbox
655                                                   &optional force no-error)
656   "Select MAILBOX in SESSION.
657 If optional argument FORCE is non-nil, select mailbox even if current mailbox
658 is same as MAILBOX.
659 If second optional argument NO-ERROR is non-nil, don't cause an error when
660 selecting folder was failed.
661 If NO-ERROR is 'notify-bye, only BYE response is reported as error.
662 Returns response value if selecting folder succeed. "
663   (when (or force
664             (not (string=
665                   (elmo-imap4-session-current-mailbox-internal session)
666                   mailbox)))
667     (let (response result)
668       (unwind-protect
669           (setq response
670                 (elmo-imap4-read-response
671                  session
672                  (elmo-imap4-send-command
673                   session
674                   (list
675                    "select "
676                    (elmo-imap4-mailbox mailbox)))))
677         (if (setq result (elmo-imap4-response-ok-p response))
678             (progn
679               (elmo-imap4-session-set-current-mailbox-internal session mailbox)
680               (elmo-imap4-session-set-read-only-internal
681                session
682                (nth 1 (assq 'read-only (assq 'ok response)))))
683           (elmo-imap4-session-set-current-mailbox-internal session nil)
684           (if (and (eq no-error 'notify-bye)
685                    (elmo-imap4-response-bye-p response))
686               (elmo-imap4-process-bye session)
687             (unless no-error
688               (error (or
689                       (elmo-imap4-response-error-text response)
690                       (format "Select %s failed" mailbox)))))))
691       (and result response))))
692
693 (defun elmo-imap4-check-validity (spec validity-file)
694 ;;; Not used.
695 ;;;(elmo-imap4-send-command-wait
696 ;;;(elmo-imap4-get-session spec)
697 ;;;(list "status "
698 ;;;      (elmo-imap4-mailbox
699 ;;;       (elmo-imap4-spec-mailbox spec))
700 ;;;      " (uidvalidity)")))
701   )
702
703 (defun elmo-imap4-sync-validity  (spec validity-file)
704   ;; Not used.
705   )
706
707 (defun elmo-imap4-list (folder flag)
708   (let ((session (elmo-imap4-get-session folder)))
709     (elmo-imap4-session-select-mailbox
710      session
711      (elmo-imap4-folder-mailbox-internal folder))
712     (elmo-imap4-response-value
713      (elmo-imap4-send-command-wait
714       session
715       (format (if elmo-imap4-use-uid "uid search %s"
716                 "search %s") flag))
717      'search)))
718
719 (static-cond
720  ((fboundp 'float)
721   ;; Emacs can parse dot symbol.
722   (defvar elmo-imap4-rfc822-size "RFC822\.SIZE")
723   (defvar elmo-imap4-rfc822-text "RFC822\.TEXT")
724   (defvar elmo-imap4-rfc822-header "RFC822\.HEADER")
725   (defvar elmo-imap4-rfc822-size "RFC822\.SIZE")
726   (defvar elmo-imap4-header-fields "HEADER\.FIELDS")
727   (defmacro elmo-imap4-replace-dot-symbols ()) ;; noop
728   (defalias 'elmo-imap4-fetch-read 'read)
729   )
730  (t
731   ;;; For Nemacs.
732   ;; Cannot parse dot symbol.
733   (defvar elmo-imap4-rfc822-size "RFC822_SIZE")
734   (defvar elmo-imap4-header-fields "HEADER_FIELDS")
735   (defvar elmo-imap4-rfc822-size "RFC822_SIZE")
736   (defvar elmo-imap4-rfc822-text "RFC822_TEXT")
737   (defvar elmo-imap4-rfc822-header "RFC822_HEADER")
738   (defvar elmo-imap4-header-fields "HEADER_FIELDS")
739   (defun elmo-imap4-fetch-read (buffer)
740     (with-current-buffer buffer
741       (let ((beg (point))
742             token)
743         (when (re-search-forward "[[ ]" nil t)
744           (goto-char (match-beginning 0))
745           (setq token (buffer-substring beg (point)))
746           (cond ((string= token "RFC822.SIZE")
747                  (intern elmo-imap4-rfc822-size))
748                 ((string= token "RFC822.HEADER")
749                  (intern elmo-imap4-rfc822-header))
750                 ((string= token "RFC822.TEXT")
751                  (intern elmo-imap4-rfc822-text))
752                 ((string= token "HEADER\.FIELDS")
753                  (intern elmo-imap4-header-fields))
754                 (t (goto-char beg)
755                    (elmo-read (current-buffer))))))))))
756
757 (defun elmo-imap4-make-number-set-list (msg-list &optional chop-length)
758   "Make RFC2060's message set specifier from MSG-LIST.
759 Returns a list of (NUMBER . SET-STRING).
760 SET-STRING is the message set specifier described in RFC2060.
761 NUMBER is contained message number in SET-STRING.
762 Every SET-STRING does not contain number of messages longer than CHOP-LENGTH.
763 If CHOP-LENGTH is not specified, message set is not chopped."
764   (let (count cont-list set-list)
765     (setq msg-list (sort (copy-sequence msg-list) '<))
766     (while msg-list
767       (setq cont-list nil)
768       (setq count 0)
769       (unless chop-length
770         (setq chop-length (length msg-list)))
771       (while (and (not (null msg-list))
772                   (< count chop-length))
773         (setq cont-list
774               (elmo-number-set-append
775                cont-list (car msg-list)))
776         (incf count)
777         (setq msg-list (cdr msg-list)))
778       (setq set-list
779             (cons
780              (cons
781               count
782               (mapconcat
783                (lambda (x)
784                  (cond ((consp x)
785                         (format "%s:%s" (car x) (cdr x)))
786                        ((integerp x)
787                         (int-to-string x))))
788                cont-list
789                ","))
790              set-list)))
791     (nreverse set-list)))
792
793 ;;
794 ;; app-data:
795 ;; cons of list
796 ;; 0: new-mark 1: already-mark 2: seen-mark 3: important-mark
797 ;; 4: seen-list
798 ;; and result of use-flag-p.
799 (defsubst elmo-imap4-fetch-callback-1-subr (entity flags app-data)
800   "A msgdb entity callback function."
801   (let* ((use-flag (cdr app-data))
802          (app-data (car app-data))
803          (seen (member (car entity) (nth 4 app-data)))
804          mark)
805     (if (member "\\Flagged" flags)
806         (elmo-msgdb-global-mark-set (car entity) (nth 3 app-data)))
807     (if (setq mark (elmo-msgdb-global-mark-get (car entity)))
808         (unless (member "\\Seen" flags)
809           (setq elmo-imap4-seen-messages
810                 (cons
811                  (elmo-msgdb-overview-entity-get-number entity)
812                  elmo-imap4-seen-messages)))
813       (setq mark (or (if (elmo-file-cache-status
814                           (elmo-file-cache-get (car entity)))
815                          (if (or seen
816                                  (and use-flag
817                                       (member "\\Seen" flags)))
818                              nil
819                            (nth 1 app-data))
820                        (if (or seen
821                                (and use-flag
822                                     (member "\\Seen" flags)))
823                            (if elmo-imap4-use-cache
824                                (nth 2 app-data))
825                          (nth 0 app-data))))))
826     (setq elmo-imap4-current-msgdb
827           (elmo-msgdb-append
828            elmo-imap4-current-msgdb
829            (list (list entity)
830                  (list (cons (elmo-msgdb-overview-entity-get-number entity)
831                              (car entity)))
832                  (if mark
833                      (list
834                       (list (elmo-msgdb-overview-entity-get-number entity)
835                             mark))))))))
836
837 ;; Current buffer is process buffer.
838 (defun elmo-imap4-fetch-callback-1 (element app-data)
839   (elmo-imap4-fetch-callback-1-subr
840    (with-temp-buffer
841      (insert (or (elmo-imap4-response-bodydetail-text element)
842                  ""))
843      ;; Delete CR.
844      (goto-char (point-min))
845      (while (search-forward "\r\n" nil t)
846        (replace-match "\n"))
847      (elmo-msgdb-create-overview-from-buffer
848       (elmo-imap4-response-value element 'uid)
849       (elmo-imap4-response-value element 'rfc822size)))
850    (elmo-imap4-response-value element 'flags)
851    app-data))
852
853 (defun elmo-imap4-parse-capability (string)
854   (if (string-match "^\\*\\(.*\\)$" string)
855       (elmo-read
856        (concat "(" (downcase (elmo-match-string 1 string)) ")"))))
857
858 (defun elmo-imap4-clear-login (session)
859   (let ((elmo-imap4-debug-inhibit-logging t))
860     (or
861      (elmo-imap4-read-ok
862       session
863       (elmo-imap4-send-command
864        session
865        (list "login "
866              (elmo-imap4-userid (elmo-network-session-user-internal session))
867              " "
868              (elmo-imap4-password
869               (elmo-get-passwd (elmo-network-session-password-key session))))))
870      (signal 'elmo-authenticate-error '(elmo-imap4-clear-login)))))
871
872 (defun elmo-imap4-auth-login (session)
873   (let ((tag (elmo-imap4-send-command session "authenticate login"))
874         (elmo-imap4-debug-inhibit-logging t))
875     (or (elmo-imap4-read-continue-req session)
876         (signal 'elmo-authenticate-error '(elmo-imap4-auth-login)))
877     (elmo-imap4-send-string session
878                             (elmo-base64-encode-string
879                              (elmo-network-session-user-internal session)))
880     (or (elmo-imap4-read-continue-req session)
881         (signal 'elmo-authenticate-error '(elmo-imap4-auth-login)))
882     (elmo-imap4-send-string session
883                             (elmo-base64-encode-string
884                              (elmo-get-passwd
885                               (elmo-network-session-password-key session))))
886     (or (elmo-imap4-read-ok session tag)
887         (signal 'elmo-authenticate-error '(elmo-imap4-auth-login)))
888     (setq elmo-imap4-status 'auth)))
889
890 (luna-define-method
891   elmo-network-initialize-session-buffer :after ((session
892                                                   elmo-imap4-session) buffer)
893   (with-current-buffer buffer
894     (mapcar 'make-variable-buffer-local elmo-imap4-local-variables)
895     (setq elmo-imap4-seqno 0)
896     (setq elmo-imap4-status 'initial)))
897
898 (luna-define-method elmo-network-initialize-session ((session
899                                                       elmo-imap4-session))
900   (let ((process (elmo-network-session-process-internal session)))
901     (with-current-buffer (process-buffer process)
902       ;; Skip garbage output from process before greeting.
903       (while (and (memq (process-status process) '(open run))
904                   (goto-char (point-max))
905                   (forward-line -1)
906                   (not (elmo-imap4-parse-greeting)))
907         (accept-process-output process 1))
908       (set-process-filter process 'elmo-imap4-arrival-filter)
909       (set-process-sentinel process 'elmo-imap4-sentinel)
910 ;;;   (while (and (memq (process-status process) '(open run))
911 ;;;               (eq elmo-imap4-status 'initial))
912 ;;;     (message "Waiting for server response...")
913 ;;;     (accept-process-output process 1))
914 ;;;   (message "")
915       (unless (memq elmo-imap4-status '(nonauth auth))
916         (signal 'elmo-open-error
917                 (list 'elmo-network-initialize-session)))
918       (elmo-imap4-session-set-capability-internal
919        session
920        (elmo-imap4-response-value
921         (elmo-imap4-send-command-wait session "capability")
922         'capability))
923       (when (eq (elmo-network-stream-type-symbol
924                  (elmo-network-session-stream-type-internal session))
925                 'starttls)
926         (or (memq 'starttls
927                   (elmo-imap4-session-capability-internal session))
928             (signal 'elmo-open-error
929                     '(elmo-imap4-starttls-error)))
930         (elmo-imap4-send-command-wait session "starttls")
931         (starttls-negotiate process)
932         (elmo-imap4-session-set-capability-internal
933          session
934          (elmo-imap4-response-value
935           (elmo-imap4-send-command-wait session "capability")
936           'capability))))))
937
938 (luna-define-method elmo-network-authenticate-session ((session
939                                                         elmo-imap4-session))
940   (with-current-buffer (process-buffer
941                         (elmo-network-session-process-internal session))
942     (let* ((auth (elmo-network-session-auth-internal session))
943            (auth (if (listp auth) auth (list auth))))
944       (unless (or (eq elmo-imap4-status 'auth)
945                   (null auth))
946         (cond
947          ((eq 'clear (car auth))
948           (elmo-imap4-clear-login session))
949          ((eq 'login (car auth))
950           (elmo-imap4-auth-login session))
951          (t
952           (let* ((elmo-imap4-debug-inhibit-logging t)
953                  (sasl-mechanisms
954                   (delq nil
955                         (mapcar
956                          '(lambda (cap)
957                             (if (string-match "^auth=\\(.*\\)$"
958                                               (symbol-name cap))
959                                 (match-string 1 (upcase (symbol-name cap)))))
960                          (elmo-imap4-session-capability-internal session))))
961                  (mechanism
962                   (sasl-find-mechanism
963                    (delq nil
964                          (mapcar '(lambda (cap) (upcase (symbol-name cap)))
965                                  (if (listp auth)
966                                      auth
967                                    (list auth)))))) ;)
968                  client name step response tag
969                  sasl-read-passphrase)
970             (unless mechanism
971               (if (or elmo-imap4-force-login
972                       (y-or-n-p
973                        (format
974                         "There's no %s capability in server. continue?"
975                         (elmo-list-to-string
976                          (elmo-network-session-auth-internal session)))))
977                   (setq mechanism (sasl-find-mechanism
978                                    sasl-mechanisms))
979                 (signal 'elmo-authenticate-error
980                         '(elmo-imap4-auth-no-mechanisms))))
981             (setq client
982                   (sasl-make-client
983                    mechanism
984                    (elmo-network-session-user-internal session)
985                    "imap"
986                    (elmo-network-session-server-internal session)))
987 ;;;         (if elmo-imap4-auth-user-realm
988 ;;;             (sasl-client-set-property client 'realm elmo-imap4-auth-user-realm))
989             (setq name (sasl-mechanism-name mechanism)
990                   step (sasl-next-step client nil))
991             (elmo-network-session-set-auth-internal
992              session
993              (intern (downcase name)))
994             (setq sasl-read-passphrase
995                   (function
996                    (lambda (prompt)
997                      (elmo-get-passwd
998                       (elmo-network-session-password-key session)))))
999             (setq tag
1000                   (elmo-imap4-send-command
1001                    session
1002                    (concat "AUTHENTICATE " name
1003                            (and (sasl-step-data step)
1004                                 (concat
1005                                  " "
1006                                  (elmo-base64-encode-string
1007                                   (sasl-step-data step)
1008                                   'no-lin-break))))))
1009             (catch 'done
1010               (while t
1011                 (setq response
1012                       (elmo-imap4-read-untagged
1013                        (elmo-network-session-process-internal session)))
1014                 (if (elmo-imap4-response-ok-p response)
1015                     (if (sasl-next-step client step)
1016                         ;; Bogus server?
1017                         (signal 'elmo-authenticate-error
1018                                 (list (intern
1019                                        (concat "elmo-imap4-auth-"
1020                                                (downcase name)))))
1021                       ;; The authentication process is finished.
1022                       (throw 'done nil)))
1023                 (unless (elmo-imap4-response-continue-req-p response)
1024                   ;; response is NO or BAD.
1025                   (signal 'elmo-authenticate-error
1026                           (list (intern
1027                                  (concat "elmo-imap4-auth-"
1028                                          (downcase name))))))
1029                 (sasl-step-set-data
1030                  step
1031                  (elmo-base64-decode-string
1032                   (elmo-imap4-response-value response 'continue-req)))
1033                 (setq step (sasl-next-step client step))
1034                 (setq tag
1035                       (elmo-imap4-send-string
1036                        session
1037                        (if (sasl-step-data step)
1038                            (elmo-base64-encode-string (sasl-step-data step)
1039                                                       'no-line-break)
1040                          ""))))))))))))
1041
1042 (luna-define-method elmo-network-setup-session ((session
1043                                                  elmo-imap4-session))
1044   (with-current-buffer (elmo-network-session-buffer session)
1045     (when (memq 'namespace (elmo-imap4-session-capability-internal session))
1046       (setq elmo-imap4-server-namespace
1047             (elmo-imap4-response-value
1048              (elmo-imap4-send-command-wait session "namespace")
1049              'namespace)))))
1050
1051 (defun elmo-imap4-setup-send-buffer (&optional string)
1052   (let ((send-buf (get-buffer-create " *elmo-imap4-setup-send-buffer*"))
1053         (source-buf (unless string (current-buffer))))
1054     (save-excursion
1055       (save-match-data
1056         (set-buffer send-buf)
1057         (erase-buffer)
1058         (elmo-set-buffer-multibyte nil)
1059         (if string
1060             (insert string)
1061           (with-current-buffer source-buf
1062             (copy-to-buffer send-buf (point-min) (point-max))))
1063         (goto-char (point-min))
1064         (if (eq (re-search-forward "^$" nil t)
1065                 (point-max))
1066             (insert "\n"))
1067         (goto-char (point-min))
1068         (while (search-forward "\n" nil t)
1069           (replace-match "\r\n"))))
1070     send-buf))
1071
1072 (defun elmo-imap4-setup-send-buffer-from-file (file)
1073   (let ((tmp-buf (get-buffer-create
1074                   " *elmo-imap4-setup-send-buffer-from-file*")))
1075     (save-excursion
1076       (save-match-data
1077         (set-buffer tmp-buf)
1078         (erase-buffer)
1079         (as-binary-input-file
1080          (insert-file-contents file))
1081         (goto-char (point-min))
1082         (if (eq (re-search-forward "^$" nil t)
1083                 (point-max))
1084             (insert "\n"))
1085         (goto-char (point-min))
1086         (while (search-forward "\n" nil t)
1087           (replace-match "\r\n"))))
1088     tmp-buf))
1089
1090 (luna-define-method elmo-delete-message-safe ((folder elmo-imap4-folder)
1091                                               number msgid)
1092   (let ((session (elmo-imap4-get-session folder))
1093         candidates)
1094     (elmo-imap4-session-select-mailbox
1095      session
1096      (elmo-imap4-folder-mailbox-internal folder))
1097     (setq candidates
1098           (elmo-imap4-response-value
1099            (elmo-imap4-send-command-wait session
1100                                          (list
1101                                           (if elmo-imap4-use-uid
1102                                               "uid search header message-id "
1103                                             "search header message-id ")
1104                                           (elmo-imap4-field-body msgid)))
1105            'search))
1106     (if (memq number candidates)
1107         (elmo-folder-delete-messages folder (list number)))))
1108
1109 (defun elmo-imap4-server-diff-async-callback-1 (status data)
1110   (funcall elmo-imap4-server-diff-async-callback
1111            (list (elmo-imap4-response-value status 'recent)
1112                  (elmo-imap4-response-value status 'unseen)
1113                  (elmo-imap4-response-value status 'messages))
1114            data))
1115
1116 (defun elmo-imap4-server-diff-async (folder)
1117   (let ((session (elmo-imap4-get-session folder)))
1118     ;; We should `check' folder to obtain newest information here.
1119     ;; But since there's no asynchronous check mechanism in elmo yet,
1120     ;; checking is not done here.
1121     (with-current-buffer (elmo-network-session-buffer session)
1122       (setq elmo-imap4-status-callback
1123             'elmo-imap4-server-diff-async-callback-1)
1124       (setq elmo-imap4-status-callback-data
1125             elmo-imap4-server-diff-async-callback-data))
1126     (elmo-imap4-send-command session
1127                              (list
1128                               "status "
1129                               (elmo-imap4-mailbox
1130                                (elmo-imap4-folder-mailbox-internal folder))
1131                               " (recent unseen messages)"))))
1132
1133 (luna-define-method elmo-server-diff-async ((folder elmo-imap4-folder))
1134   (let ((session (elmo-imap4-get-session folder)))
1135     ;; commit.
1136     ;; (elmo-imap4-commit spec)
1137     (with-current-buffer (elmo-network-session-buffer session)
1138       (setq elmo-imap4-status-callback
1139             'elmo-imap4-server-diff-async-callback-1)
1140       (setq elmo-imap4-status-callback-data
1141             elmo-imap4-server-diff-async-callback-data))
1142     (elmo-imap4-send-command session
1143                              (list
1144                               "status "
1145                               (elmo-imap4-mailbox
1146                                (elmo-imap4-folder-mailbox-internal folder))
1147                               " (recent unseen messages)"))))
1148
1149 ;;; IMAP parser.
1150
1151 (defvar elmo-imap4-server-eol "\r\n"
1152   "The EOL string sent from the server.")
1153
1154 (defvar elmo-imap4-client-eol "\r\n"
1155   "The EOL string we send to the server.")
1156
1157 (defvar elmo-imap4-display-literal-progress nil)
1158
1159 (defun elmo-imap4-find-next-line ()
1160   "Return point at end of current line, taking into account literals.
1161 Return nil if no complete line has arrived."
1162   (when (re-search-forward (concat elmo-imap4-server-eol "\\|{\\([0-9]+\\)}"
1163                                    elmo-imap4-server-eol)
1164                            nil t)
1165     (if (match-string 1)
1166         (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
1167             (progn
1168               (if (and elmo-imap4-display-literal-progress
1169                        (> (string-to-number (match-string 1))
1170                           (min elmo-display-retrieval-progress-threshold 100)))
1171                   (elmo-display-progress
1172                    'elmo-imap4-display-literal-progress
1173                    (format "Retrieving (%d/%d bytes)..."
1174                            (- (point-max) (point))
1175                            (string-to-number (match-string 1)))
1176                    (/ (- (point-max) (point))
1177                       (/ (string-to-number (match-string 1)) 100))))
1178               nil)
1179           (goto-char (+ (point) (string-to-number (match-string 1))))
1180           (elmo-imap4-find-next-line))
1181       (point))))
1182
1183 (defun elmo-imap4-sentinel (process string)
1184   (delete-process process))
1185
1186 (defun elmo-imap4-arrival-filter (proc string)
1187   "IMAP process filter."
1188   (when (buffer-live-p (process-buffer proc))
1189   (with-current-buffer (process-buffer proc)
1190     (elmo-imap4-debug "-> %s" string)
1191     (goto-char (point-max))
1192     (insert string)
1193     (let (end)
1194       (goto-char (point-min))
1195       (while (setq end (elmo-imap4-find-next-line))
1196         (save-restriction
1197           (narrow-to-region (point-min) end)
1198           (delete-backward-char (length elmo-imap4-server-eol))
1199           (goto-char (point-min))
1200           (unwind-protect
1201               (cond ((eq elmo-imap4-status 'initial)
1202                      (setq elmo-imap4-current-response
1203                            (list
1204                             (list 'greeting (elmo-imap4-parse-greeting)))))
1205                     ((or (eq elmo-imap4-status 'auth)
1206                          (eq elmo-imap4-status 'nonauth)
1207                          (eq elmo-imap4-status 'selected)
1208                          (eq elmo-imap4-status 'examine))
1209                      (setq elmo-imap4-current-response
1210                            (cons
1211                             (elmo-imap4-parse-response)
1212                             elmo-imap4-current-response)))
1213                     (t
1214                      (message "Unknown state %s in arrival filter"
1215                               elmo-imap4-status))))
1216           (delete-region (point-min) (point-max))))))))
1217
1218 ;; IMAP parser.
1219
1220 (defsubst elmo-imap4-forward ()
1221   (or (eobp) (forward-char 1)))
1222
1223 (defsubst elmo-imap4-parse-number ()
1224   (when (looking-at "[0-9]+")
1225     (prog1
1226         (string-to-number (match-string 0))
1227       (goto-char (match-end 0)))))
1228
1229 (defsubst elmo-imap4-parse-literal ()
1230   (when (looking-at "{\\([0-9]+\\)}\r\n")
1231     (let ((pos (match-end 0))
1232           (len (string-to-number (match-string 1))))
1233       (if (< (point-max) (+ pos len))
1234           nil
1235         (goto-char (+ pos len))
1236         (buffer-substring pos (+ pos len))))))
1237 ;;;     (list ' pos (+ pos len))))))
1238
1239 (defsubst elmo-imap4-parse-string ()
1240   (cond ((eq (char-after (point)) ?\")
1241          (forward-char 1)
1242          (let ((p (point)) (name ""))
1243            (skip-chars-forward "^\"\\\\")
1244            (setq name (buffer-substring p (point)))
1245            (while (eq (char-after (point)) ?\\)
1246              (setq p (1+ (point)))
1247              (forward-char 2)
1248              (skip-chars-forward "^\"\\\\")
1249              (setq name (concat name (buffer-substring p (point)))))
1250            (forward-char 1)
1251            name))
1252         ((eq (char-after (point)) ?{)
1253          (elmo-imap4-parse-literal))))
1254
1255 (defsubst elmo-imap4-parse-nil ()
1256   (if (looking-at "NIL")
1257       (goto-char (match-end 0))))
1258
1259 (defsubst elmo-imap4-parse-nstring ()
1260   (or (elmo-imap4-parse-string)
1261       (and (elmo-imap4-parse-nil)
1262            nil)))
1263
1264 (defsubst elmo-imap4-parse-astring ()
1265   (or (elmo-imap4-parse-string)
1266       (buffer-substring (point)
1267                         (if (re-search-forward "[(){ \r\n%*\"\\]" nil t)
1268                             (goto-char (1- (match-end 0)))
1269                           (end-of-line)
1270                           (point)))))
1271
1272 (defsubst elmo-imap4-parse-address ()
1273   (let (address)
1274     (when (eq (char-after (point)) ?\()
1275       (elmo-imap4-forward)
1276       (setq address (vector (prog1 (elmo-imap4-parse-nstring)
1277                               (elmo-imap4-forward))
1278                             (prog1 (elmo-imap4-parse-nstring)
1279                               (elmo-imap4-forward))
1280                             (prog1 (elmo-imap4-parse-nstring)
1281                               (elmo-imap4-forward))
1282                             (elmo-imap4-parse-nstring)))
1283       (when (eq (char-after (point)) ?\))
1284         (elmo-imap4-forward)
1285         address))))
1286
1287 (defsubst elmo-imap4-parse-address-list ()
1288   (if (eq (char-after (point)) ?\()
1289       (let (address addresses)
1290         (elmo-imap4-forward)
1291         (while (and (not (eq (char-after (point)) ?\)))
1292                     ;; next line for MS Exchange bug
1293                     (progn (and (eq (char-after (point)) ? ) (elmo-imap4-forward)) t)
1294                     (setq address (elmo-imap4-parse-address)))
1295           (setq addresses (cons address addresses)))
1296         (when (eq (char-after (point)) ?\))
1297           (elmo-imap4-forward)
1298           (nreverse addresses)))
1299     (assert (elmo-imap4-parse-nil))))
1300
1301 (defsubst elmo-imap4-parse-mailbox ()
1302   (let ((mailbox (elmo-imap4-parse-astring)))
1303     (if (string-equal "INBOX" (upcase mailbox))
1304         "INBOX"
1305       mailbox)))
1306
1307 (defun elmo-imap4-parse-greeting ()
1308   "Parse a IMAP greeting."
1309   (cond ((looking-at "\\* OK ")
1310          (setq elmo-imap4-status 'nonauth))
1311         ((looking-at "\\* PREAUTH ")
1312          (setq elmo-imap4-status 'auth))
1313         ((looking-at "\\* BYE ")
1314          (setq elmo-imap4-status 'closed))))
1315
1316 (defun elmo-imap4-parse-response ()
1317   "Parse a IMAP command response."
1318   (let (token)
1319     (case (setq token (elmo-read (current-buffer)))
1320       (+ (progn
1321            (skip-chars-forward " ")
1322            (list 'continue-req (buffer-substring (point) (point-max)))))
1323       (* (case (prog1 (setq token (elmo-read (current-buffer)))
1324                  (elmo-imap4-forward))
1325            (OK         (elmo-imap4-parse-resp-text-code))
1326            (NO         (elmo-imap4-parse-resp-text-code))
1327            (BAD        (elmo-imap4-parse-resp-text-code))
1328            (BYE        (elmo-imap4-parse-bye))
1329            (FLAGS      (list 'flags
1330                              (elmo-imap4-parse-flag-list)))
1331            (LIST       (list 'list (elmo-imap4-parse-data-list)))
1332            (LSUB       (list 'lsub (elmo-imap4-parse-data-list)))
1333            (SEARCH     (list
1334                         'search
1335                         (elmo-read (concat "("
1336                                       (buffer-substring (point) (point-max))
1337                                       ")"))))
1338            (STATUS     (elmo-imap4-parse-status))
1339            ;; Added
1340            (NAMESPACE  (elmo-imap4-parse-namespace))
1341            (CAPABILITY (list 'capability
1342                              (elmo-read
1343                               (concat "(" (downcase (buffer-substring
1344                                                      (point) (point-max)))
1345                                       ")"))))
1346            (ACL (elmo-imap4-parse-acl))
1347            (t       (case (prog1 (elmo-read (current-buffer))
1348                             (elmo-imap4-forward))
1349                       (EXISTS  (list 'exists token))
1350                       (RECENT  (list 'recent token))
1351                       (EXPUNGE (list 'expunge token))
1352                       (FETCH   (elmo-imap4-parse-fetch token))
1353                       (t       (list 'garbage (buffer-string)))))))
1354       (t (if (not (string-match elmo-imap4-seq-prefix (symbol-name token)))
1355              (list 'garbage (buffer-string))
1356            (case (prog1 (elmo-read (current-buffer))
1357                    (elmo-imap4-forward))
1358              (OK  (progn
1359                     (setq elmo-imap4-parsing nil)
1360                     (setq token (symbol-name token))
1361                     (elmo-unintern token)
1362                     (elmo-imap4-debug "*%s* OK arrived" token)
1363                     (setq elmo-imap4-reached-tag token)
1364                     (list 'ok (elmo-imap4-parse-resp-text-code))))
1365              (NO  (progn
1366                     (setq elmo-imap4-parsing nil)
1367                     (setq token (symbol-name token))
1368                     (elmo-unintern token)
1369                     (elmo-imap4-debug "*%s* NO arrived" token)
1370                     (setq elmo-imap4-reached-tag token)
1371                     (let (code text)
1372                       (when (eq (char-after (point)) ?\[)
1373                         (setq code (buffer-substring (point)
1374                                                      (search-forward "]")))
1375                         (elmo-imap4-forward))
1376                       (setq text (buffer-substring (point) (point-max)))
1377                       (list 'no (list code text)))))
1378              (BAD (progn
1379                     (setq elmo-imap4-parsing nil)
1380                     (elmo-imap4-debug "*%s* BAD arrived" token)
1381                     (setq token (symbol-name token))
1382                     (elmo-unintern token)
1383                     (setq elmo-imap4-reached-tag token)
1384                     (let (code text)
1385                       (when (eq (char-after (point)) ?\[)
1386                         (setq code (buffer-substring (point)
1387                                                      (search-forward "]")))
1388                         (elmo-imap4-forward))
1389                       (setq text (buffer-substring (point) (point-max)))
1390                       (list 'bad (list code text)))))
1391              (t   (list 'garbage (buffer-string)))))))))
1392
1393 (defun elmo-imap4-parse-bye ()
1394   (let (code text)
1395     (when (eq (char-after (point)) ?\[)
1396       (setq code (buffer-substring (point)
1397                                    (search-forward "]")))
1398       (elmo-imap4-forward))
1399     (setq text (buffer-substring (point) (point-max)))
1400     (list 'bye (list code text))))
1401
1402 (defun elmo-imap4-parse-text ()
1403   (goto-char (point-min))
1404   (when (search-forward "[" nil t)
1405     (search-forward "]")
1406     (elmo-imap4-forward))
1407   (list 'text (buffer-substring (point) (point-max))))
1408
1409 (defun elmo-imap4-parse-resp-text-code ()
1410   (when (eq (char-after (point)) ?\[)
1411     (elmo-imap4-forward)
1412     (cond ((search-forward "PERMANENTFLAGS " nil t)
1413            (list 'permanentflags (elmo-imap4-parse-flag-list)))
1414           ((search-forward "UIDNEXT " nil t)
1415            (list 'uidnext (elmo-read (current-buffer))))
1416           ((search-forward "UNSEEN " nil t)
1417            (list 'unseen (elmo-read (current-buffer))))
1418           ((looking-at "UIDVALIDITY \\([0-9]+\\)")
1419            (list 'uidvalidity (match-string 1)))
1420           ((search-forward "READ-ONLY" nil t)
1421            (list 'read-only t))
1422           ((search-forward "READ-WRITE" nil t)
1423            (list 'read-write t))
1424           ((search-forward "NEWNAME " nil t)
1425            (let (oldname newname)
1426              (setq oldname (elmo-imap4-parse-string))
1427              (elmo-imap4-forward)
1428              (setq newname (elmo-imap4-parse-string))
1429              (list 'newname newname oldname)))
1430           ((search-forward "TRYCREATE" nil t)
1431            (list 'trycreate t))
1432           ((looking-at "APPENDUID \\([0-9]+\\) \\([0-9]+\\)")
1433            (list 'appenduid
1434                  (list (match-string 1)
1435                        (string-to-number (match-string 2)))))
1436           ((looking-at "COPYUID \\([0-9]+\\) \\([0-9,:]+\\) \\([0-9,:]+\\)")
1437            (list 'copyuid (list (match-string 1)
1438                                 (match-string 2)
1439                                 (match-string 3))))
1440           ((search-forward "ALERT] " nil t)
1441            (message "IMAP server information: %s"
1442                     (buffer-substring (point) (point-max))))
1443           (t (list 'unknown)))))
1444
1445 (defun elmo-imap4-parse-data-list ()
1446   (let (flags delimiter mailbox)
1447     (setq flags (elmo-imap4-parse-flag-list))
1448     (when (looking-at " NIL\\| \"\\\\?\\(.\\)\"")
1449       (setq delimiter (match-string 1))
1450       (goto-char (1+ (match-end 0)))
1451       (when (setq mailbox (elmo-imap4-parse-mailbox))
1452         (list mailbox flags delimiter)))))
1453
1454 (defsubst elmo-imap4-parse-header-list ()
1455   (when (eq (char-after (point)) ?\()
1456     (let (strlist)
1457       (while (not (eq (char-after (point)) ?\)))
1458         (elmo-imap4-forward)
1459         (push (elmo-imap4-parse-astring) strlist))
1460       (elmo-imap4-forward)
1461       (nreverse strlist))))
1462
1463 (defsubst elmo-imap4-parse-fetch-body-section ()
1464   (let ((section
1465          (buffer-substring (point)
1466                            (1-
1467                             (progn (re-search-forward "[] ]" nil t)
1468                                    (point))))))
1469     (if (eq (char-before) ? )
1470         (prog1
1471             (mapconcat 'identity
1472                        (cons section (elmo-imap4-parse-header-list)) " ")
1473           (search-forward "]" nil t))
1474       section)))
1475
1476 (defun elmo-imap4-parse-fetch (response)
1477   (when (eq (char-after (point)) ?\()
1478     (let (element list)
1479       (while (not (eq (char-after (point)) ?\)))
1480         (elmo-imap4-forward)
1481         (let ((token (elmo-imap4-fetch-read (current-buffer))))
1482           (elmo-imap4-forward)
1483           (setq element
1484                 (cond ((eq token 'UID)
1485                        (list 'uid (condition-case nil
1486                                       (elmo-read (current-buffer))
1487                                     (error nil))))
1488                       ((eq token 'FLAGS)
1489                        (list 'flags (elmo-imap4-parse-flag-list)))
1490                       ((eq token 'ENVELOPE)
1491                        (list 'envelope (elmo-imap4-parse-envelope)))
1492                       ((eq token 'INTERNALDATE)
1493                        (list 'internaldate (elmo-imap4-parse-string)))
1494                       ((eq token 'RFC822)
1495                        (list 'rfc822 (elmo-imap4-parse-nstring)))
1496                       ((eq token (intern elmo-imap4-rfc822-header))
1497                        (list 'rfc822header (elmo-imap4-parse-nstring)))
1498                       ((eq token (intern elmo-imap4-rfc822-text))
1499                        (list 'rfc822text (elmo-imap4-parse-nstring)))
1500                       ((eq token (intern elmo-imap4-rfc822-size))
1501                        (list 'rfc822size (elmo-read (current-buffer))))
1502                       ((eq token 'BODY)
1503                        (if (eq (char-before) ?\[)
1504                            (list
1505                             'bodydetail
1506                             (upcase (elmo-imap4-parse-fetch-body-section))
1507                             (and
1508                              (eq (char-after (point)) ?<)
1509                              (buffer-substring (1+ (point))
1510                                                (progn
1511                                                  (search-forward ">" nil t)
1512                                                  (point))))
1513                             (progn (elmo-imap4-forward)
1514                                    (elmo-imap4-parse-nstring)))
1515                          (list 'body (elmo-imap4-parse-body))))
1516                       ((eq token 'BODYSTRUCTURE)
1517                        (list 'bodystructure (elmo-imap4-parse-body)))))
1518           (setq list (cons element list))))
1519       (and elmo-imap4-fetch-callback
1520            (funcall elmo-imap4-fetch-callback
1521                     list elmo-imap4-fetch-callback-data))
1522       (list 'fetch list))))
1523
1524 (defun elmo-imap4-parse-status ()
1525   (let ((mailbox (elmo-imap4-parse-mailbox))
1526         status)
1527     (when (and mailbox (search-forward "(" nil t))
1528       (while (not (eq (char-after (point)) ?\)))
1529         (setq status
1530               (cons
1531                (let ((token (elmo-read (current-buffer))))
1532                  (cond ((eq token 'MESSAGES)
1533                         (list 'messages (elmo-read (current-buffer))))
1534                        ((eq token 'RECENT)
1535                         (list 'recent (elmo-read (current-buffer))))
1536                        ((eq token 'UIDNEXT)
1537                         (list 'uidnext (elmo-read (current-buffer))))
1538                        ((eq token 'UIDVALIDITY)
1539                         (and (looking-at " \\([0-9]+\\)")
1540                              (prog1 (list 'uidvalidity (match-string 1))
1541                                (goto-char (match-end 1)))))
1542                        ((eq token 'UNSEEN)
1543                         (list 'unseen (elmo-read (current-buffer))))
1544                        (t
1545                         (message
1546                          "Unknown status data %s in mailbox %s ignored"
1547                          token mailbox))))
1548                status))
1549         (skip-chars-forward " ")))
1550     (and elmo-imap4-status-callback
1551          (funcall elmo-imap4-status-callback
1552                   status
1553                   elmo-imap4-status-callback-data))
1554     (list 'status status)))
1555
1556
1557 (defmacro elmo-imap4-value (value)
1558   (` (if (eq (, value) 'NIL) nil
1559        (, value))))
1560
1561 (defmacro elmo-imap4-nth (pos list)
1562   (` (let ((value (nth (, pos) (, list))))
1563        (elmo-imap4-value value))))
1564
1565 (defun elmo-imap4-parse-namespace ()
1566   (list 'namespace
1567         (nconc
1568          (copy-sequence elmo-imap4-extra-namespace-alist)
1569          (elmo-imap4-parse-namespace-subr
1570           (elmo-read (concat "(" (buffer-substring
1571                                   (point) (point-max))
1572                              ")"))))))
1573
1574 (defun elmo-imap4-parse-namespace-subr (ns)
1575   (let (prefix delim namespace-alist default-delim)
1576     ;; 0: personal, 1: other, 2: shared
1577     (dotimes (i 3)
1578       (setq namespace-alist
1579             (nconc namespace-alist
1580                    (delq nil
1581                          (mapcar
1582                           (lambda (namespace)
1583                             (setq prefix (elmo-imap4-nth 0 namespace)
1584                                   delim (elmo-imap4-nth 1 namespace))
1585                             (if (and prefix delim
1586                                      (string-match
1587                                       (concat (regexp-quote delim) "\\'")
1588                                       prefix))
1589                                 (setq prefix (substring prefix 0
1590                                                         (match-beginning 0))))
1591                             (if (eq (length prefix) 0)
1592                                 (progn (setq default-delim delim) nil)
1593                               (cons
1594                                (concat "^\\("
1595                                        (if (string= (downcase prefix) "inbox")
1596                                            "[Ii][Nn][Bb][Oo][Xx]"
1597                                          (regexp-quote prefix))
1598                                        "\\).*$")
1599                                delim)))
1600                           (elmo-imap4-nth i ns))))))
1601     (if default-delim
1602         (setq namespace-alist
1603               (nconc namespace-alist
1604                      (list (cons "^.*$" default-delim)))))
1605     namespace-alist))
1606
1607 (defun elmo-imap4-parse-acl ()
1608   (let ((mailbox (elmo-imap4-parse-mailbox))
1609         identifier rights acl)
1610     (while (eq (char-after (point)) ?\ )
1611       (elmo-imap4-forward)
1612       (setq identifier (elmo-imap4-parse-astring))
1613       (elmo-imap4-forward)
1614       (setq rights (elmo-imap4-parse-astring))
1615       (setq acl (append acl (list (cons identifier rights)))))
1616     (list 'acl acl mailbox)))
1617
1618 (defun elmo-imap4-parse-flag-list ()
1619   (let ((str (buffer-substring (+ (point) 1)
1620                                (progn (search-forward ")" nil t)
1621                                       (- (point) 1)))))
1622     (unless (eq (length str) 0)
1623       (split-string str))))
1624
1625 (defun elmo-imap4-parse-envelope ()
1626   (when (eq (char-after (point)) ?\()
1627     (elmo-imap4-forward)
1628     (vector (prog1 (elmo-imap4-parse-nstring);; date
1629               (elmo-imap4-forward))
1630             (prog1 (elmo-imap4-parse-nstring);; subject
1631               (elmo-imap4-forward))
1632             (prog1 (elmo-imap4-parse-address-list);; from
1633               (elmo-imap4-forward))
1634             (prog1 (elmo-imap4-parse-address-list);; sender
1635               (elmo-imap4-forward))
1636             (prog1 (elmo-imap4-parse-address-list);; reply-to
1637               (elmo-imap4-forward))
1638             (prog1 (elmo-imap4-parse-address-list);; to
1639               (elmo-imap4-forward))
1640             (prog1 (elmo-imap4-parse-address-list);; cc
1641               (elmo-imap4-forward))
1642             (prog1 (elmo-imap4-parse-address-list);; bcc
1643               (elmo-imap4-forward))
1644             (prog1 (elmo-imap4-parse-nstring);; in-reply-to
1645               (elmo-imap4-forward))
1646             (prog1 (elmo-imap4-parse-nstring);; message-id
1647               (elmo-imap4-forward)))))
1648
1649 (defsubst elmo-imap4-parse-string-list ()
1650   (cond ((eq (char-after (point)) ?\();; body-fld-param
1651          (let (strlist str)
1652            (elmo-imap4-forward)
1653            (while (setq str (elmo-imap4-parse-string))
1654              (push str strlist)
1655              (elmo-imap4-forward))
1656            (nreverse strlist)))
1657         ((elmo-imap4-parse-nil)
1658          nil)))
1659
1660 (defun elmo-imap4-parse-body-extension ()
1661   (if (eq (char-after (point)) ?\()
1662       (let (b-e)
1663         (elmo-imap4-forward)
1664         (push (elmo-imap4-parse-body-extension) b-e)
1665         (while (eq (char-after (point)) ?\ )
1666           (elmo-imap4-forward)
1667           (push (elmo-imap4-parse-body-extension) b-e))
1668         (assert (eq (char-after (point)) ?\)))
1669         (elmo-imap4-forward)
1670         (nreverse b-e))
1671     (or (elmo-imap4-parse-number)
1672         (elmo-imap4-parse-nstring))))
1673
1674 (defsubst elmo-imap4-parse-body-ext ()
1675   (let (ext)
1676     (when (eq (char-after (point)) ?\ );; body-fld-dsp
1677       (elmo-imap4-forward)
1678       (let (dsp)
1679         (if (eq (char-after (point)) ?\()
1680             (progn
1681               (elmo-imap4-forward)
1682               (push (elmo-imap4-parse-string) dsp)
1683               (elmo-imap4-forward)
1684               (push (elmo-imap4-parse-string-list) dsp)
1685               (elmo-imap4-forward))
1686           (assert (elmo-imap4-parse-nil)))
1687         (push (nreverse dsp) ext))
1688       (when (eq (char-after (point)) ?\ );; body-fld-lang
1689         (elmo-imap4-forward)
1690         (if (eq (char-after (point)) ?\()
1691             (push (elmo-imap4-parse-string-list) ext)
1692           (push (elmo-imap4-parse-nstring) ext))
1693         (while (eq (char-after (point)) ?\ );; body-extension
1694           (elmo-imap4-forward)
1695           (setq ext (append (elmo-imap4-parse-body-extension) ext)))))
1696     ext))
1697
1698 (defun elmo-imap4-parse-body ()
1699   (let (body)
1700     (when (eq (char-after (point)) ?\()
1701       (elmo-imap4-forward)
1702       (if (eq (char-after (point)) ?\()
1703           (let (subbody)
1704             (while (and (eq (char-after (point)) ?\()
1705                         (setq subbody (elmo-imap4-parse-body)))
1706               (push subbody body))
1707             (elmo-imap4-forward)
1708             (push (elmo-imap4-parse-string) body);; media-subtype
1709             (when (eq (char-after (point)) ?\ );; body-ext-mpart:
1710               (elmo-imap4-forward)
1711               (if (eq (char-after (point)) ?\();; body-fld-param
1712                   (push (elmo-imap4-parse-string-list) body)
1713                 (push (and (elmo-imap4-parse-nil) nil) body))
1714               (setq body
1715                     (append (elmo-imap4-parse-body-ext) body)));; body-ext-...
1716             (assert (eq (char-after (point)) ?\)))
1717             (elmo-imap4-forward)
1718             (nreverse body))
1719
1720         (push (elmo-imap4-parse-string) body);; media-type
1721         (elmo-imap4-forward)
1722         (push (elmo-imap4-parse-string) body);; media-subtype
1723         (elmo-imap4-forward)
1724         ;; next line for Sun SIMS bug
1725         (and (eq (char-after (point)) ? ) (elmo-imap4-forward))
1726         (if (eq (char-after (point)) ?\();; body-fld-param
1727             (push (elmo-imap4-parse-string-list) body)
1728           (push (and (elmo-imap4-parse-nil) nil) body))
1729         (elmo-imap4-forward)
1730         (push (elmo-imap4-parse-nstring) body);; body-fld-id
1731         (elmo-imap4-forward)
1732         (push (elmo-imap4-parse-nstring) body);; body-fld-desc
1733         (elmo-imap4-forward)
1734         (push (elmo-imap4-parse-string) body);; body-fld-enc
1735         (elmo-imap4-forward)
1736         (push (elmo-imap4-parse-number) body);; body-fld-octets
1737
1738         ;; ok, we're done parsing the required parts, what comes now is one
1739         ;; of three things:
1740         ;;
1741         ;; envelope       (then we're parsing body-type-msg)
1742         ;; body-fld-lines (then we're parsing body-type-text)
1743         ;; body-ext-1part (then we're parsing body-type-basic)
1744         ;;
1745         ;; the problem is that the two first are in turn optionally followed
1746         ;; by the third.  So we parse the first two here (if there are any)...
1747
1748         (when (eq (char-after (point)) ?\ )
1749           (elmo-imap4-forward)
1750           (let (lines)
1751             (cond ((eq (char-after (point)) ?\();; body-type-msg:
1752                    (push (elmo-imap4-parse-envelope) body);; envelope
1753                    (elmo-imap4-forward)
1754                    (push (elmo-imap4-parse-body) body);; body
1755                    (elmo-imap4-forward)
1756                    (push (elmo-imap4-parse-number) body));; body-fld-lines
1757                   ((setq lines (elmo-imap4-parse-number));; body-type-text:
1758                    (push lines body));; body-fld-lines
1759                   (t
1760                    (backward-char)))));; no match...
1761
1762         ;; ...and then parse the third one here...
1763
1764         (when (eq (char-after (point)) ?\ );; body-ext-1part:
1765           (elmo-imap4-forward)
1766           (push (elmo-imap4-parse-nstring) body);; body-fld-md5
1767           (setq body
1768                 (append (elmo-imap4-parse-body-ext) body)));; body-ext-1part..
1769
1770         (assert (eq (char-after (point)) ?\)))
1771         (elmo-imap4-forward)
1772         (nreverse body)))))
1773
1774 (luna-define-method elmo-folder-initialize :around ((folder
1775                                                      elmo-imap4-folder)
1776                                                     name)
1777   (let ((default-user   elmo-imap4-default-user)
1778         (default-server elmo-imap4-default-server)
1779         (default-port   elmo-imap4-default-port)
1780         (elmo-network-stream-type-alist
1781          (if elmo-imap4-stream-type-alist
1782              (append elmo-imap4-stream-type-alist
1783                      elmo-network-stream-type-alist)
1784            elmo-network-stream-type-alist))
1785         parse)
1786     (when (string-match "\\(.*\\)@\\(.*\\)" default-server)
1787       ;; case: imap4-default-server is specified like
1788       ;; "hoge%imap.server@gateway".
1789       (setq default-user (elmo-match-string 1 default-server))
1790       (setq default-server (elmo-match-string 2 default-server)))
1791     (setq name (luna-call-next-method))
1792     ;; mailbox
1793     (setq parse (elmo-parse-token name ":"))
1794     (elmo-imap4-folder-set-mailbox-internal folder
1795                                             (elmo-imap4-encode-folder-string
1796                                              (car parse)))
1797     ;; user
1798     (setq parse (elmo-parse-prefixed-element ?: (cdr parse) "/"))
1799     (elmo-net-folder-set-user-internal folder
1800                                        (if (eq (length (car parse)) 0)
1801                                            default-user
1802                                          (car parse)))
1803     ;; auth
1804     (setq parse (elmo-parse-prefixed-element ?/ (cdr parse)))
1805     (elmo-net-folder-set-auth-internal
1806      folder
1807      (if (eq (length (car parse)) 0)
1808          (or elmo-imap4-default-authenticate-type 'clear)
1809        (intern (car parse))))
1810     (unless (elmo-net-folder-server-internal folder)
1811       (elmo-net-folder-set-server-internal folder default-server))
1812     (unless (elmo-net-folder-port-internal folder)
1813       (elmo-net-folder-set-port-internal folder default-port))
1814     (unless (elmo-net-folder-stream-type-internal folder)
1815       (elmo-net-folder-set-stream-type-internal
1816        folder
1817        (elmo-get-network-stream-type elmo-imap4-default-stream-type)))
1818     folder))
1819
1820 ;;; ELMO IMAP4 folder
1821 (luna-define-method elmo-folder-expand-msgdb-path ((folder
1822                                                     elmo-imap4-folder))
1823   (convert-standard-filename
1824    (let ((mailbox (elmo-imap4-folder-mailbox-internal folder)))
1825      (if (string= "inbox" (downcase mailbox))
1826          (setq mailbox "inbox"))
1827      (if (eq (string-to-char mailbox) ?/)
1828          (setq mailbox (substring mailbox 1 (length mailbox))))
1829      (expand-file-name
1830       mailbox
1831       (expand-file-name
1832        (or (elmo-net-folder-user-internal folder) "nobody")
1833        (expand-file-name (or (elmo-net-folder-server-internal folder)
1834                              "nowhere")
1835                          (expand-file-name
1836                           "imap"
1837                           elmo-msgdb-directory)))))))
1838
1839 (luna-define-method elmo-folder-status-plugged ((folder
1840                                                  elmo-imap4-folder))
1841   (elmo-imap4-folder-status-plugged folder))
1842
1843 (defun elmo-imap4-folder-status-plugged (folder)
1844   (let ((session (elmo-imap4-get-session folder))
1845         (killed (elmo-msgdb-killed-list-load
1846                  (elmo-folder-msgdb-path folder)))
1847         status)
1848     (with-current-buffer (elmo-network-session-buffer session)
1849       (setq elmo-imap4-status-callback nil)
1850       (setq elmo-imap4-status-callback-data nil))
1851     (setq status (elmo-imap4-response-value
1852                   (elmo-imap4-send-command-wait
1853                    session
1854                    (list "status "
1855                          (elmo-imap4-mailbox
1856                           (elmo-imap4-folder-mailbox-internal folder))
1857                          " (uidnext messages)"))
1858                   'status))
1859     (cons
1860      (- (elmo-imap4-response-value status 'uidnext) 1)
1861      (if killed
1862          (-
1863           (elmo-imap4-response-value status 'messages)
1864           (elmo-msgdb-killed-list-length killed))
1865        (elmo-imap4-response-value status 'messages)))))
1866
1867 (luna-define-method elmo-folder-list-messages-plugged ((folder
1868                                                         elmo-imap4-folder)
1869                                                        &optional nohide)
1870   (elmo-imap4-list folder
1871                    (let ((max (elmo-msgdb-max-of-killed
1872                                (elmo-folder-killed-list-internal folder))))
1873                      (if (or nohide
1874                              (null (eq max 0)))
1875                          (format "uid %d:*" (1+ max))
1876                        "all"))))
1877
1878 (luna-define-method elmo-folder-list-unreads-plugged
1879   ((folder elmo-imap4-folder))
1880   (elmo-imap4-list folder "unseen"))
1881
1882 (luna-define-method elmo-folder-list-importants-plugged
1883   ((folder elmo-imap4-folder))
1884   (elmo-imap4-list folder "flagged"))
1885
1886 (luna-define-method elmo-folder-use-flag-p ((folder elmo-imap4-folder))
1887   (not (string-match elmo-imap4-disuse-server-flag-mailbox-regexp
1888                      (elmo-imap4-folder-mailbox-internal folder))))
1889
1890 (luna-define-method elmo-folder-list-subfolders ((folder elmo-imap4-folder)
1891                                                  &optional one-level)
1892   (let* ((root (elmo-imap4-folder-mailbox-internal folder))
1893          (session (elmo-imap4-get-session folder))
1894          (prefix (elmo-folder-prefix-internal folder))
1895          (delim (or
1896                  (cdr
1897                   (elmo-string-matched-assoc
1898                    root
1899                    (with-current-buffer (elmo-network-session-buffer session)
1900                      elmo-imap4-server-namespace)))
1901                  elmo-imap4-default-hierarchy-delimiter))
1902          ;; Append delimiter when root with namespace.
1903          (root (if (and (match-end 1)
1904                         (string= (substring root (match-end 1))
1905                                  ""))
1906                    (concat root delim)
1907                  root))
1908          result append-serv type)
1909     (setq result (elmo-imap4-response-get-selectable-mailbox-list
1910                   (elmo-imap4-send-command-wait
1911                    session
1912                    (list "list " (elmo-imap4-mailbox root) " *"))))
1913     (when (or (not (string= (elmo-net-folder-user-internal folder)
1914                             elmo-imap4-default-user))
1915               (not (eq (elmo-net-folder-auth-internal folder)
1916                        (or elmo-imap4-default-authenticate-type 'clear))))
1917       (setq append-serv (concat ":" (elmo-net-folder-user-internal folder))))
1918     (unless (eq (elmo-net-folder-auth-internal folder)
1919                 (or elmo-imap4-default-authenticate-type 'clear))
1920       (setq append-serv
1921             (concat append-serv "/"
1922                     (symbol-name (elmo-net-folder-auth-internal folder)))))
1923     (unless (string= (elmo-net-folder-server-internal folder)
1924                      elmo-imap4-default-server)
1925       (setq append-serv (concat append-serv "@"
1926                                 (elmo-net-folder-server-internal folder))))
1927     (unless (eq (elmo-net-folder-port-internal folder) elmo-imap4-default-port)
1928       (setq append-serv (concat append-serv ":"
1929                                 (int-to-string
1930                                  (elmo-net-folder-port-internal folder)))))
1931     (setq type (elmo-net-folder-stream-type-internal folder))
1932     (unless (eq (elmo-network-stream-type-symbol type)
1933                 elmo-imap4-default-stream-type)
1934       (if type
1935           (setq append-serv (concat append-serv
1936                                     (elmo-network-stream-type-spec-string
1937                                      type)))))
1938     (if one-level
1939         (let ((re-delim (regexp-quote delim))
1940               (case-fold-search nil)
1941               folder ret has-child-p)
1942           ;; Append delimiter
1943           (when (and root
1944                      (not (string= root ""))
1945                      (not (string-match
1946                            (concat "\\(.*\\)" re-delim "\\'")
1947                            root)))
1948             (setq root (concat root delim)))
1949           (while (setq folder (car result))
1950             (when (string-match
1951                    (concat "^\\(" (regexp-quote root) "[^" re-delim "]" "+\\)"
1952                            re-delim)
1953                    folder)
1954               (setq folder (match-string 1 folder)))
1955             (setq has-child-p nil
1956                   result (delq
1957                           nil
1958                           (mapcar (lambda (fld)
1959                                     (if (string-match
1960                                          (concat "^" (regexp-quote folder)
1961                                                  "\\(" re-delim "\\|\\'\\)")
1962                                          fld)
1963                                         (progn (setq has-child-p t) nil)
1964                                       fld))
1965                                   (cdr result)))
1966                   folder (concat prefix
1967                                  (elmo-imap4-decode-folder-string folder)
1968                                  (and append-serv
1969                                       (eval append-serv)))
1970                   ret (append ret (if has-child-p
1971                                       (list (list folder))
1972                                     (list folder)))))
1973           ret)
1974       (mapcar (lambda (fld)
1975                 (concat prefix (elmo-imap4-decode-folder-string fld)
1976                         (and append-serv
1977                              (eval append-serv))))
1978               result))))
1979
1980 (luna-define-method elmo-folder-exists-p-plugged ((folder elmo-imap4-folder))
1981   (let ((session (elmo-imap4-get-session folder)))
1982     (if (string=
1983          (elmo-imap4-session-current-mailbox-internal session)
1984          (elmo-imap4-folder-mailbox-internal folder))
1985         t
1986       (elmo-imap4-session-select-mailbox
1987        session
1988        (elmo-imap4-folder-mailbox-internal folder)
1989        'force 'notify-bye))))
1990
1991 (luna-define-method elmo-folder-creatable-p ((folder elmo-imap4-folder))
1992   t)
1993
1994 (luna-define-method elmo-folder-writable-p ((folder elmo-imap4-folder))
1995   t)
1996
1997 (luna-define-method elmo-folder-delete :before ((folder elmo-imap4-folder))
1998   (let ((session (elmo-imap4-get-session folder))
1999         msgs)
2000     (when (elmo-imap4-folder-mailbox-internal folder)
2001       (when (setq msgs (elmo-folder-list-messages folder))
2002         (elmo-folder-delete-messages folder msgs))
2003       (elmo-imap4-send-command-wait session "close")
2004       (elmo-imap4-send-command-wait
2005        session
2006        (list "delete "
2007              (elmo-imap4-mailbox
2008               (elmo-imap4-folder-mailbox-internal folder)))))))
2009
2010 (luna-define-method elmo-folder-rename-internal ((folder elmo-imap4-folder)
2011                                                  new-folder)
2012   (let ((session (elmo-imap4-get-session folder)))
2013     ;; make sure the folder is selected.
2014     (elmo-imap4-session-select-mailbox session
2015                                        (elmo-imap4-folder-mailbox-internal
2016                                         folder))
2017     (elmo-imap4-send-command-wait session "close")
2018     (elmo-imap4-send-command-wait
2019      session
2020      (list "rename "
2021            (elmo-imap4-mailbox
2022             (elmo-imap4-folder-mailbox-internal folder))
2023            " "
2024            (elmo-imap4-mailbox
2025             (elmo-imap4-folder-mailbox-internal new-folder))))
2026     (elmo-imap4-session-set-current-mailbox-internal
2027      session (elmo-imap4-folder-mailbox-internal new-folder))))
2028
2029 (defun elmo-imap4-copy-messages (src-folder dst-folder numbers)
2030   (let ((session (elmo-imap4-get-session src-folder))
2031         (set-list (elmo-imap4-make-number-set-list
2032                    numbers
2033                    elmo-imap4-number-set-chop-length))
2034         succeeds)
2035     (elmo-imap4-session-select-mailbox session
2036                                        (elmo-imap4-folder-mailbox-internal
2037                                         src-folder))
2038     (while set-list
2039       (if (elmo-imap4-send-command-wait session
2040                                         (list
2041                                          (format
2042                                           (if elmo-imap4-use-uid
2043                                               "uid copy %s "
2044                                             "copy %s ")
2045                                           (cdr (car set-list)))
2046                                          (elmo-imap4-mailbox
2047                                           (elmo-imap4-folder-mailbox-internal
2048                                            dst-folder))))
2049           (setq succeeds (append succeeds numbers)))
2050       (setq set-list (cdr set-list)))
2051     succeeds))
2052
2053 (defun elmo-imap4-set-flag (folder numbers flag &optional remove)
2054   "Set flag on messages.
2055 FOLDER is the ELMO folder structure.
2056 NUMBERS is the message numbers to be flagged.
2057 FLAG is the flag name.
2058 If optional argument REMOVE is non-nil, remove FLAG."
2059   (let ((session (elmo-imap4-get-session folder))
2060         response set-list)
2061     (elmo-imap4-session-select-mailbox session
2062                                        (elmo-imap4-folder-mailbox-internal
2063                                         folder))
2064     (setq set-list (elmo-imap4-make-number-set-list
2065                     numbers
2066                     elmo-imap4-number-set-chop-length))
2067     (while set-list
2068       (with-current-buffer (elmo-network-session-buffer session)
2069         (setq elmo-imap4-fetch-callback nil)
2070         (setq elmo-imap4-fetch-callback-data nil))
2071       (unless (elmo-imap4-response-ok-p
2072                (elmo-imap4-send-command-wait
2073                 session
2074                 (format
2075                  (if elmo-imap4-use-uid
2076                      "uid store %s %sflags.silent (%s)"
2077                    "store %s %sflags.silent (%s)")
2078                  (cdr (car set-list))
2079                  (if remove "-" "+")
2080                  flag)))
2081         (setq response 'fail))
2082       (setq set-list (cdr set-list)))
2083     (not (eq response 'fail))))
2084
2085 (luna-define-method elmo-folder-delete-messages-plugged
2086   ((folder elmo-imap4-folder) numbers)
2087   (let ((session (elmo-imap4-get-session folder)))
2088     (elmo-imap4-set-flag folder numbers "\\Deleted")
2089     (elmo-imap4-send-command-wait session "expunge")))
2090
2091 (defmacro elmo-imap4-detect-search-charset (string)
2092   (` (with-temp-buffer
2093        (insert (, string))
2094        (detect-mime-charset-region (point-min) (point-max)))))
2095
2096 (defun elmo-imap4-search-internal-primitive (folder session filter from-msgs)
2097   (let ((search-key (elmo-filter-key filter))
2098         (imap-search-keys '("bcc" "body" "cc" "from" "subject" "to"
2099                             "larger" "smaller"))
2100         (total 0)
2101         (length (length from-msgs))
2102         charset set-list end results)
2103     (message "Searching...")
2104     (cond
2105      ((string= "last" search-key)
2106       (let ((numbers (or from-msgs (elmo-folder-list-messages folder))))
2107         (nthcdr (max (- (length numbers)
2108                         (string-to-int (elmo-filter-value filter)))
2109                      0)
2110                 numbers)))
2111      ((string= "first" search-key)
2112       (let* ((numbers (or from-msgs (elmo-folder-list-messages folder)))
2113              (rest (nthcdr (string-to-int (elmo-filter-value filter) )
2114                            numbers)))
2115         (mapcar '(lambda (x) (delete x numbers)) rest)
2116         numbers))
2117      ((or (string= "since" search-key)
2118           (string= "before" search-key))
2119       (setq search-key (concat "sent" search-key)
2120             set-list (elmo-imap4-make-number-set-list
2121                       from-msgs
2122                       elmo-imap4-number-set-chop-length)
2123             end nil)
2124       (while (not end)
2125         (setq results
2126               (append
2127                results
2128                (elmo-imap4-response-value
2129                 (elmo-imap4-send-command-wait
2130                  session
2131                  (format
2132                   (if elmo-imap4-use-uid
2133                       "uid search %s%s%s %s"
2134                     "search %s%s%s %s")
2135                   (if from-msgs
2136                       (concat
2137                        (if elmo-imap4-use-uid "uid ")
2138                        (cdr (car set-list))
2139                        " ")
2140                     "")
2141                   (if (eq (elmo-filter-type filter)
2142                           'unmatch)
2143                       "not " "")
2144                   search-key
2145                   (elmo-date-get-description
2146                    (elmo-date-get-datevec
2147                     (elmo-filter-value filter)))))
2148                 'search)))
2149         (when (> length elmo-display-progress-threshold)
2150           (setq total (+ total (car (car set-list))))
2151           (elmo-display-progress
2152            'elmo-imap4-search "Searching..."
2153            (/ (* total 100) length)))
2154         (setq set-list (cdr set-list)
2155               end (null set-list)))
2156       results)
2157      (t
2158       (setq charset
2159             (if (eq (length (elmo-filter-value filter)) 0)
2160                 (setq charset 'us-ascii)
2161               (elmo-imap4-detect-search-charset
2162                (elmo-filter-value filter)))
2163             set-list (elmo-imap4-make-number-set-list
2164                       from-msgs
2165                       elmo-imap4-number-set-chop-length)
2166             end nil)
2167       (while (not end)
2168         (setq results
2169               (append
2170                results
2171                (elmo-imap4-response-value
2172                 (elmo-imap4-send-command-wait
2173                  session
2174                  (list
2175                   (if elmo-imap4-use-uid "uid ")
2176                   "search "
2177                   "CHARSET "
2178                   (elmo-imap4-astring
2179                    (symbol-name charset))
2180                   " "
2181                   (if from-msgs
2182                       (concat
2183                        (if elmo-imap4-use-uid "uid ")
2184                        (cdr (car set-list))
2185                        " ")
2186                     "")
2187                   (if (eq (elmo-filter-type filter)
2188                           'unmatch)
2189                       "not " "")
2190                   (format "%s%s "
2191                           (if (member
2192                                (elmo-filter-key filter)
2193                                imap-search-keys)
2194                               ""
2195                             "header ")
2196                           (elmo-filter-key filter))
2197                   (elmo-imap4-astring
2198                    (encode-mime-charset-string
2199                     (elmo-filter-value filter) charset))))
2200                 'search)))
2201         (when (> length elmo-display-progress-threshold)
2202           (setq total (+ total (car (car set-list))))
2203           (elmo-display-progress
2204            'elmo-imap4-search "Searching..."
2205            (/ (* total 100) length)))
2206         (setq set-list (cdr set-list)
2207               end (null set-list)))
2208       results))))
2209
2210 (defun elmo-imap4-search-internal (folder session condition from-msgs)
2211   (let (result)
2212     (cond
2213      ((vectorp condition)
2214       (setq result (elmo-imap4-search-internal-primitive
2215                     folder session condition from-msgs)))
2216      ((eq (car condition) 'and)
2217       (setq result (elmo-imap4-search-internal folder session (nth 1 condition)
2218                                                from-msgs)
2219             result (elmo-list-filter result
2220                                      (elmo-imap4-search-internal
2221                                       folder session (nth 2 condition)
2222                                       from-msgs))))
2223      ((eq (car condition) 'or)
2224       (setq result (elmo-imap4-search-internal
2225                     folder session (nth 1 condition) from-msgs)
2226             result (elmo-uniq-list
2227                     (nconc result
2228                            (elmo-imap4-search-internal
2229                             folder session (nth 2 condition) from-msgs)))
2230             result (sort result '<))))))
2231
2232 (luna-define-method elmo-folder-search :around ((folder elmo-imap4-folder)
2233                                                 condition &optional numbers)
2234   (if (elmo-folder-plugged-p folder)
2235       (save-excursion
2236         (let ((session (elmo-imap4-get-session folder)))
2237           (elmo-imap4-session-select-mailbox
2238            session
2239            (elmo-imap4-folder-mailbox-internal folder))
2240           (elmo-imap4-search-internal folder session condition numbers)))
2241     (luna-call-next-method)))
2242
2243 (luna-define-method elmo-folder-msgdb-create-plugged
2244   ((folder elmo-imap4-folder) numbers &rest args)
2245   (when numbers
2246     (let ((session (elmo-imap4-get-session folder))
2247           (headers
2248            (append
2249             '("Subject" "From" "To" "Cc" "Date"
2250               "Message-Id" "References" "In-Reply-To")
2251             elmo-msgdb-extra-fields))
2252           (total 0)
2253           (length (length numbers))
2254           rfc2060 set-list)
2255       (setq rfc2060 (memq 'imap4rev1
2256                           (elmo-imap4-session-capability-internal
2257                            session)))
2258       (message "Getting overview...")
2259       (elmo-imap4-session-select-mailbox
2260        session (elmo-imap4-folder-mailbox-internal folder))
2261       (setq set-list (elmo-imap4-make-number-set-list
2262                       numbers
2263                       elmo-imap4-overview-fetch-chop-length))
2264       ;; Setup callback.
2265       (with-current-buffer (elmo-network-session-buffer session)
2266         (setq elmo-imap4-current-msgdb nil
2267               elmo-imap4-seen-messages nil
2268               elmo-imap4-fetch-callback 'elmo-imap4-fetch-callback-1
2269               elmo-imap4-fetch-callback-data (cons args
2270                                                    (elmo-folder-use-flag-p
2271                                                     folder)))
2272         (while set-list
2273           (elmo-imap4-send-command-wait
2274            session
2275            ;; get overview entity from IMAP4
2276            (format "%sfetch %s (%s rfc822.size flags)"
2277                    (if elmo-imap4-use-uid "uid " "")
2278                    (cdr (car set-list))
2279                    (if rfc2060
2280                        (format "body.peek[header.fields %s]" headers)
2281                      (format "%s" headers))))
2282           (when (> length elmo-display-progress-threshold)
2283             (setq total (+ total (car (car set-list))))
2284             (elmo-display-progress
2285              'elmo-imap4-msgdb-create "Getting overview..."
2286              (/ (* total 100) length)))
2287           (setq set-list (cdr set-list)))
2288         (message "Getting overview...done")
2289         (when elmo-imap4-seen-messages
2290           (elmo-imap4-set-flag folder elmo-imap4-seen-messages "\\Seen"))
2291         elmo-imap4-current-msgdb))))
2292
2293 (luna-define-method elmo-folder-unmark-important-plugged
2294   ((folder elmo-imap4-folder) numbers)
2295   (elmo-imap4-set-flag folder numbers "\\Flagged" 'remove))
2296
2297 (luna-define-method elmo-folder-mark-as-important-plugged
2298   ((folder elmo-imap4-folder) numbers)
2299   (elmo-imap4-set-flag folder numbers "\\Flagged"))
2300
2301 (luna-define-method elmo-folder-unmark-read-plugged
2302   ((folder elmo-imap4-folder) numbers)
2303   (elmo-imap4-set-flag folder numbers "\\Seen" 'remove))
2304
2305 (luna-define-method elmo-folder-mark-as-read-plugged
2306   ((folder elmo-imap4-folder) numbers)
2307   (elmo-imap4-set-flag folder numbers "\\Seen"))
2308
2309 (luna-define-method elmo-message-use-cache-p ((folder elmo-imap4-folder)
2310                                               number)
2311   elmo-imap4-use-cache)
2312
2313 (luna-define-method elmo-folder-message-appendable-p ((folder elmo-imap4-folder))
2314   (if (elmo-folder-plugged-p folder)
2315       (not (elmo-imap4-session-read-only-internal
2316             (elmo-imap4-get-session folder)))
2317     elmo-enable-disconnected-operation)) ; offline refile.
2318
2319 (luna-define-method elmo-folder-check-plugged ((folder elmo-imap4-folder))
2320   (let ((session (elmo-imap4-get-session folder 'if-exists)))
2321     (when session
2322       (if (string=
2323            (elmo-imap4-session-current-mailbox-internal session)
2324            (elmo-imap4-folder-mailbox-internal folder))
2325           (if elmo-imap4-use-select-to-update-status
2326               (elmo-imap4-session-select-mailbox
2327                session
2328                (elmo-imap4-folder-mailbox-internal folder)
2329                'force)
2330             (elmo-imap4-session-check session))))))
2331
2332 (defsubst elmo-imap4-folder-diff-plugged (folder)
2333   (let ((session (elmo-imap4-get-session folder))
2334         messages new unread response killed)
2335 ;;; (elmo-imap4-commit spec)
2336     (with-current-buffer (elmo-network-session-buffer session)
2337       (setq elmo-imap4-status-callback nil)
2338       (setq elmo-imap4-status-callback-data nil))
2339     (setq response
2340           (elmo-imap4-send-command-wait session
2341                                         (list
2342                                          "status "
2343                                          (elmo-imap4-mailbox
2344                                           (elmo-imap4-folder-mailbox-internal
2345                                            folder))
2346                                          " (recent unseen messages)")))
2347     (setq response (elmo-imap4-response-value response 'status))
2348     (setq messages (elmo-imap4-response-value response 'messages))
2349     (setq killed (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder)))
2350     (if killed
2351         (setq messages (- messages
2352                           (elmo-msgdb-killed-list-length
2353                            killed))))
2354     (setq new (elmo-imap4-response-value response 'recent)
2355           unread (elmo-imap4-response-value response 'unseen))
2356     (if (< unread new) (setq new unread))
2357     (list new unread messages)))
2358
2359 (luna-define-method elmo-folder-diff-plugged ((folder elmo-imap4-folder))
2360   (elmo-imap4-folder-diff-plugged folder))
2361
2362 (luna-define-method elmo-folder-diff-async ((folder elmo-imap4-folder)
2363                                             &optional number-alist)
2364   (setq elmo-imap4-server-diff-async-callback
2365         elmo-folder-diff-async-callback)
2366   (setq elmo-imap4-server-diff-async-callback-data
2367         elmo-folder-diff-async-callback-data)
2368   (elmo-imap4-server-diff-async folder))
2369
2370 (luna-define-method elmo-folder-open :around ((folder elmo-imap4-folder)
2371                                               &optional load-msgdb)
2372   (if (elmo-folder-plugged-p folder)
2373       (let (session mailbox msgdb result response tag)
2374         (condition-case err
2375             (progn
2376               (setq session (elmo-imap4-get-session folder)
2377                     mailbox (elmo-imap4-folder-mailbox-internal folder)
2378                     tag (elmo-imap4-send-command session
2379                                                  (list "select "
2380                                                        (elmo-imap4-mailbox
2381                                                         mailbox))))
2382               (message "Selecting %s..."
2383                        (elmo-folder-name-internal folder))
2384               (if load-msgdb
2385                   (setq msgdb (elmo-msgdb-load folder 'silent)))
2386               (elmo-folder-set-killed-list-internal
2387                folder
2388                (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder)))
2389               (if (setq result (elmo-imap4-response-ok-p
2390                                 (setq response
2391                                       (elmo-imap4-read-response session tag))))
2392                   (progn
2393                     (elmo-imap4-session-set-current-mailbox-internal
2394                      session mailbox)
2395                     (elmo-imap4-session-set-read-only-internal
2396                      session
2397                      (nth 1 (assq 'read-only (assq 'ok response)))))
2398                 (elmo-imap4-session-set-current-mailbox-internal session nil)
2399                 (if (elmo-imap4-response-bye-p response)
2400                     (elmo-imap4-process-bye session)
2401                   (error (or
2402                           (elmo-imap4-response-error-text response)
2403                           (format "Select %s failed" mailbox)))))
2404               (message "Selecting %s...done"
2405                        (elmo-folder-name-internal folder))
2406               (elmo-folder-set-msgdb-internal
2407                folder msgdb))
2408           (quit
2409            (if (elmo-imap4-response-ok-p response)
2410                (elmo-imap4-session-set-current-mailbox-internal
2411                 session mailbox)
2412              (and session
2413                   (elmo-imap4-session-set-current-mailbox-internal
2414                    session nil))))
2415           (error
2416            (if (elmo-imap4-response-ok-p response)
2417                (elmo-imap4-session-set-current-mailbox-internal
2418                 session mailbox)
2419              (and session
2420                   (elmo-imap4-session-set-current-mailbox-internal
2421                    session nil))))))
2422     (luna-call-next-method)))
2423
2424 ;; elmo-folder-open-internal: do nothing.
2425
2426 (luna-define-method elmo-find-fetch-strategy
2427   ((folder elmo-imap4-folder) entity &optional ignore-cache)
2428   (let ((number (elmo-msgdb-overview-entity-get-number entity))
2429         cache-file size message-id)
2430     (setq size (elmo-msgdb-overview-entity-get-size entity))
2431     (setq message-id (elmo-msgdb-overview-entity-get-id entity))
2432     (setq cache-file (elmo-file-cache-get message-id))
2433     (if (or ignore-cache
2434             (null (elmo-file-cache-status cache-file)))
2435         (if (and elmo-message-fetch-threshold
2436                  (integerp size)
2437                  (>= size elmo-message-fetch-threshold)
2438                  (or (not elmo-message-fetch-confirm)
2439                      (not (prog1 (y-or-n-p
2440                                   (format
2441                                    "Fetch entire message at once? (%dbytes)"
2442                                    size))
2443                             (message "")))))
2444             ;; Fetch message as imap message.
2445             (elmo-make-fetch-strategy 'section
2446                                       nil
2447                                       (elmo-message-use-cache-p
2448                                        folder number)
2449                                       (elmo-file-cache-path
2450                                        cache-file))
2451           ;; Don't use existing cache and fetch entire message at once.
2452           (elmo-make-fetch-strategy 'entire nil
2453                                     (elmo-message-use-cache-p
2454                                      folder number)
2455                                     (elmo-file-cache-path cache-file)))
2456       ;; Cache found and use it.
2457       (if (not ignore-cache)
2458           (if (eq (elmo-file-cache-status cache-file) 'section)
2459               ;; Fetch message with imap message.
2460               (elmo-make-fetch-strategy 'section
2461                                         t
2462                                         (elmo-message-use-cache-p
2463                                          folder number)
2464                                         (elmo-file-cache-path
2465                                          cache-file))
2466             (elmo-make-fetch-strategy 'entire
2467                                       t
2468                                       (elmo-message-use-cache-p
2469                                        folder number)
2470                                       (elmo-file-cache-path
2471                                        cache-file)))))))
2472
2473 (luna-define-method elmo-folder-create-plugged ((folder elmo-imap4-folder))
2474   (elmo-imap4-send-command-wait
2475    (elmo-imap4-get-session folder)
2476    (list "create "
2477          (elmo-imap4-mailbox
2478           (elmo-imap4-folder-mailbox-internal folder)))))
2479
2480 (luna-define-method elmo-folder-append-buffer
2481   ((folder elmo-imap4-folder) unread &optional number)
2482   (if (elmo-folder-plugged-p folder)
2483       (let ((session (elmo-imap4-get-session folder))
2484             send-buffer result)
2485         (elmo-imap4-session-select-mailbox session
2486                                            (elmo-imap4-folder-mailbox-internal
2487                                             folder))
2488         (setq send-buffer (elmo-imap4-setup-send-buffer))
2489         (unwind-protect
2490             (setq result
2491                   (elmo-imap4-send-command-wait
2492                    session
2493                    (list
2494                     "append "
2495                     (elmo-imap4-mailbox (elmo-imap4-folder-mailbox-internal
2496                                          folder))
2497                     (if unread " () " " (\\Seen) ")
2498                     (elmo-imap4-buffer-literal send-buffer))))
2499           (kill-buffer send-buffer))
2500         result)
2501     ;; Unplugged
2502     (if elmo-enable-disconnected-operation
2503         (elmo-folder-append-buffer-dop folder unread number)
2504       (error "Unplugged"))))
2505
2506 (eval-when-compile
2507   (defmacro elmo-imap4-identical-system-p (folder1 folder2)
2508     "Return t if FOLDER1 and FOLDER2 are in the same IMAP4 system."
2509     (` (and (string= (elmo-net-folder-server-internal (, folder1))
2510                      (elmo-net-folder-server-internal (, folder2)))
2511             (eq (elmo-net-folder-port-internal (, folder1))
2512                 (elmo-net-folder-port-internal (, folder2)))
2513             (string= (elmo-net-folder-user-internal (, folder1))
2514                      (elmo-net-folder-user-internal (, folder2)))))))
2515
2516 (luna-define-method elmo-folder-append-messages :around
2517   ((folder elmo-imap4-folder) src-folder numbers unread-marks
2518    &optional same-number)
2519   (if (and (eq (elmo-folder-type-internal src-folder) 'imap4)
2520            (elmo-imap4-identical-system-p folder src-folder)
2521            (elmo-folder-plugged-p folder))
2522       ;; Plugged
2523       (prog1
2524           (elmo-imap4-copy-messages src-folder folder numbers)
2525         (elmo-progress-notify 'elmo-folder-move-messages (length numbers)))
2526     (luna-call-next-method)))
2527
2528 (luna-define-method elmo-message-deletable-p ((folder elmo-imap4-folder)
2529                                               number)
2530   (if (elmo-folder-plugged-p folder)
2531       (not (elmo-imap4-session-read-only-internal
2532             (elmo-imap4-get-session folder)))
2533     elmo-enable-disconnected-operation)) ; offline refile.
2534
2535 ;(luna-define-method elmo-message-fetch-unplugged
2536 ;  ((folder elmo-imap4-folder)
2537 ;   number strategy  &optional section outbuf unseen)
2538 ;  (error "%d%s is not cached." number (if section
2539 ;                                         (format "(%s)" section)
2540 ;                                       "")))
2541
2542 (defsubst elmo-imap4-message-fetch (folder number strategy
2543                                            section outbuf unseen)
2544   (let ((session (elmo-imap4-get-session folder))
2545         response)
2546     (elmo-imap4-session-select-mailbox session
2547                                        (elmo-imap4-folder-mailbox-internal
2548                                         folder))
2549     (with-current-buffer (elmo-network-session-buffer session)
2550       (setq elmo-imap4-fetch-callback nil)
2551       (setq elmo-imap4-fetch-callback-data nil))
2552     (unless elmo-inhibit-display-retrieval-progress
2553       (setq elmo-imap4-display-literal-progress t))
2554     (unwind-protect
2555         (setq response
2556               (elmo-imap4-send-command-wait session
2557                                             (format
2558                                              (if elmo-imap4-use-uid
2559                                                  "uid fetch %s body%s[%s]"
2560                                                "fetch %s body%s[%s]")
2561                                              number
2562                                              (if unseen ".peek" "")
2563                                              (or section "")
2564                                              )))
2565       (setq elmo-imap4-display-literal-progress nil))
2566     (unless elmo-inhibit-display-retrieval-progress
2567       (elmo-display-progress 'elmo-imap4-display-literal-progress
2568                              "Retrieving..." 100)  ; remove progress bar.
2569       (message "Retrieving...done."))
2570     (if (setq response (elmo-imap4-response-bodydetail-text
2571                         (elmo-imap4-response-value-all
2572                          response 'fetch)))
2573         (with-current-buffer outbuf
2574           (erase-buffer)
2575           (insert response)
2576           t))))
2577
2578 (luna-define-method elmo-message-fetch-plugged ((folder elmo-imap4-folder)
2579                                                 number strategy
2580                                                 &optional section
2581                                                 outbuf unseen)
2582   (elmo-imap4-message-fetch folder number strategy section outbuf unseen))
2583
2584 (luna-define-method elmo-message-fetch-field ((folder elmo-imap4-folder)
2585                                               number field)
2586   (let ((session (elmo-imap4-get-session folder)))
2587     (elmo-imap4-session-select-mailbox session
2588                                        (elmo-imap4-folder-mailbox-internal
2589                                         folder))
2590     (with-current-buffer (elmo-network-session-buffer session)
2591       (setq elmo-imap4-fetch-callback nil)
2592       (setq elmo-imap4-fetch-callback-data nil))
2593     (with-temp-buffer
2594       (insert
2595        (elmo-imap4-response-bodydetail-text
2596         (elmo-imap4-response-value
2597          (elmo-imap4-send-command-wait session
2598                                        (concat
2599                                         (if elmo-imap4-use-uid
2600                                             "uid ")
2601                                         (format
2602                                          "fetch %s (body.peek[header.fields (%s)])"
2603                                          number field)))
2604          'fetch)))
2605       (elmo-delete-cr-buffer)
2606       (goto-char (point-min))
2607       (std11-field-body (symbol-name field)))))
2608
2609
2610
2611 (require 'product)
2612 (product-provide (provide 'elmo-imap4) (require 'elmo-version))
2613
2614 ;;; elmo-imap4.el ends here