1 ;;; elmo-imap4.el --- IMAP4 Interface for ELMO.
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>
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
14 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
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)
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.
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.
34 ;; Origin of IMAP parser part is imap.el, included in Gnus.
36 ;; Copyright (C) 1998, 1999, 2000
37 ;; Free Software Foundation, Inc.
38 ;; Author: Simon Josefsson <jas@pdc.kth.se>
52 (eval-when-compile (require 'cl))
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).")
58 (defvar elmo-imap4-overview-fetch-chop-length 200
59 "*Number of overviews to fetch in one request.")
61 ;; c.f. rfc2683 3.2.1.5 Long Command Lines
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
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
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.")
80 (defvar elmo-imap4-force-login nil
81 "*Non-nil forces to try 'login' if there is no 'auth' capability in imapd.")
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.")
87 (defvar elmo-imap4-use-modified-utf7 nil
88 "*Use mofidied UTF-7 (rfc2060) encoding for IMAP4 folder name.")
90 (defvar elmo-imap4-use-cache t
91 "Use cache in imap4 folder.")
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.")
99 ;;; internal variables
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.")
106 (defvar elmo-imap4-current-response nil)
107 (defvar elmo-imap4-status nil)
108 (defvar elmo-imap4-reached-tag "elmo-imap40")
110 ;;; buffer local variables
111 (defvar elmo-imap4-default-hierarchy-delimiter "/")
113 (defvar elmo-imap4-server-capability nil)
114 (defvar elmo-imap4-server-namespace nil)
116 (defvar elmo-imap4-parsing nil) ; indicates parsing.
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)
123 (defvar elmo-imap4-server-diff-async-callback nil)
124 (defvar elmo-imap4-server-diff-async-callback-data nil)
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)
131 ;;; XXX Temporal implementation
132 (defvar elmo-imap4-current-msgdb nil)
133 (defvar elmo-imap4-seen-messages nil)
135 (defvar elmo-imap4-local-variables
137 elmo-imap4-current-response
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))
153 (defconst elmo-imap4-quoted-specials-list '(?\\ ?\"))
155 (defconst elmo-imap4-non-atom-char-regex
157 (concat "[^" "]!#$&'+,./0-9:;<=>?@A-Z[^_`a-z|}~-" "]")))
159 (defconst elmo-imap4-non-text-char-regex
162 "]\x01-\x09\x0b\x0c\x0e-\x1f\x7f !\"#$%&'()*+,./0-9:;<=>?@A-Z[\\^_`a-z{|}~-"
165 (defconst elmo-imap4-literal-threshold 1024
166 "Limitation of characters that can be used in a quoted string.")
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*\"")
173 (defvar elmo-imap4-debug-inhibit-logging nil)
175 ;;; ELMO IMAP4 folder
177 (luna-define-class elmo-imap4-folder (elmo-net-folder)
179 (luna-define-internal-accessors 'elmo-imap4-folder))
183 (luna-define-class elmo-imap4-session (elmo-network-session)
184 (capability current-mailbox read-only))
185 (luna-define-internal-accessors 'elmo-imap4-session))
187 ;;; MIME-ELMO-IMAP Location
189 (luna-define-class mime-elmo-imap-location
191 (folder number rawbuf strategy))
192 (luna-define-internal-accessors 'mime-elmo-imap-location))
195 (defmacro elmo-imap4-debug (message &rest args)
196 (` (if elmo-imap4-debug
197 (elmo-imap4-debug-1 (, message) (,@ args)))))
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"))))
206 (defsubst elmo-imap4-decode-folder-string (string)
207 (if elmo-imap4-use-modified-utf7
208 (utf7-decode-string string 'imap)
211 (defsubst elmo-imap4-encode-folder-string (string)
212 (if elmo-imap4-use-modified-utf7
213 (utf7-encode-string string 'imap)
218 (defmacro elmo-imap4-response-continue-req-p (response)
219 "Returns non-nil if RESPONSE is '+' response."
220 (` (assq 'continue-req (, response))))
222 (defmacro elmo-imap4-response-ok-p (response)
223 "Returns non-nil if RESPONSE is an 'OK' response."
224 (` (assq 'ok (, response))))
226 (defmacro elmo-imap4-response-bye-p (response)
227 "Returns non-nil if RESPONSE is an 'BYE' response."
228 (` (assq 'bye (, response))))
230 (defmacro elmo-imap4-response-value (response symbol)
231 "Get value of the SYMBOL from RESPONSE."
232 (` (nth 1 (assq (, symbol) (, response)))))
234 (defsubst elmo-imap4-response-value-all (response symbol)
235 "Get all value of the SYMBOL from RESPONSE."
238 (if (eq (car (car response)) symbol)
239 (setq matched (nconc matched (nth 1 (car response)))))
240 (setq response (cdr response)))
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)))))
249 (defmacro elmo-imap4-response-bodydetail-text (response)
250 "Returns text of BODY[section]<partial>."
251 (` (nth 3 (assq 'bodydetail (, response)))))
253 ;;; Session commands.
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
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
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)
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
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))))
304 (elmo-imap4-format-quoted (nth 1 token)))))
306 (setq cmdstr (concat cmdstr
307 (format "{%d}" (nth 2 token))))
308 (process-send-string process cmdstr)
309 (process-send-string process "\r\n")
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)
319 (+ (point-min) (nth 2 token)))))
321 (error "Wrong argument for literal"))))
323 (error "Unknown token kind %s" kind))))
325 (error "Invalid argument")))
326 (setq command-args (cdr command-args)))
328 (process-send-string process cmdstr))
329 (process-send-string process "\r\n")
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)
341 (process-send-string (elmo-network-session-process-internal session)
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))
354 (accept-process-output (elmo-network-session-process-internal session)
356 (elmo-imap4-debug "=>%s" (prin1-to-string elmo-imap4-current-response))
357 (setq elmo-imap4-parsing nil)
358 elmo-imap4-current-response))
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))
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))
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))
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."
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.")))))
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)))
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)
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."))))))
414 ;;; MIME-ELMO-IMAP Location
415 (luna-define-method mime-imap-location-section-body ((location
416 mime-elmo-imap-location)
418 (if (and (stringp section)
419 (string= section "HEADER"))
420 ;; Even in the section mode, header fields should be saved to the
422 (with-current-buffer (mime-elmo-imap-location-rawbuf-internal location)
425 (mime-elmo-imap-location-folder-internal location)
426 (mime-elmo-imap-location-number-internal location)
427 (mime-elmo-imap-location-strategy-internal location)
433 (mime-elmo-imap-location-folder-internal location)
434 (mime-elmo-imap-location-number-internal location)
435 (mime-elmo-imap-location-strategy-internal location)
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)))
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.
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))))
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"))
468 (defun elmo-imap4-atom-p (string)
469 "Return t if STRING is an atom defined in rfc2060."
470 (if (string= string "")
473 (not (string-match elmo-imap4-non-atom-char-regex string)))))
475 (defun elmo-imap4-quotable-p (string)
476 "Return t if STRING can be formatted as a quoted defined in rfc2060."
478 (not (string-match elmo-imap4-non-text-char-regex string))))
480 (defun elmo-imap4-nil (string)
481 "Return a list represents the special atom \"NIL\" defined in rfc2060, \
483 Otherwise return nil."
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)))
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)))
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))
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)))
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
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)))
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))))
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
538 (if (< elmo-imap4-literal-threshold length)
539 (elmo-imap4-literal-1 buffer length)
540 (elmo-imap4-string-1 (with-current-buffer buffer
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)))
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))))
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
565 (if (< elmo-imap4-literal-threshold length)
566 (elmo-imap4-literal-1 buffer length)
567 (elmo-imap4-astring-1 (with-current-buffer buffer
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)))
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)))
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)
588 (defun elmo-imap4-format-quoted (string)
589 "Return STRING in a form of the quoted-string defined in rfc2060."
591 (std11-wrap-as-quoted-pairs string elmo-imap4-quoted-specials-list)
594 (defsubst elmo-imap4-response-get-selectable-mailbox-list (response)
598 (if (and (eq 'list (car entry))
599 (not (member "\\NoSelect" (nth 1 (nth 1 entry)))))
600 (car (nth 1 entry))))
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)
607 (elmo-file-cache-expand-path
608 (elmo-fetch-strategy-cache-path strategy)
610 (let ((session (elmo-imap4-get-session folder))
612 (elmo-imap4-session-select-mailbox
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
624 (if elmo-imap4-use-uid
625 "uid fetch %s bodystructure"
626 "fetch %s bodystructure")
630 (when (elmo-fetch-strategy-save-cache strategy)
631 (elmo-file-cache-delete
632 (elmo-fetch-strategy-cache-path strategy))
634 (elmo-file-cache-expand-path
635 (elmo-fetch-strategy-cache-path strategy)
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)))))
646 (defun elmo-imap4-get-session (folder &optional if-exists)
647 (elmo-network-get-session 'elmo-imap4-session
649 (if (elmo-folder-biff-internal folder)
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
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. "
665 (elmo-imap4-session-current-mailbox-internal session)
667 (let (response result)
670 (elmo-imap4-read-response
672 (elmo-imap4-send-command
676 (elmo-imap4-mailbox mailbox)))))
677 (if (setq result (elmo-imap4-response-ok-p response))
679 (elmo-imap4-session-set-current-mailbox-internal session mailbox)
680 (elmo-imap4-session-set-read-only-internal
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)
689 (elmo-imap4-response-error-text response)
690 (format "Select %s failed" mailbox)))))))
691 (and result response))))
693 (defun elmo-imap4-check-validity (spec validity-file)
695 ;;;(elmo-imap4-send-command-wait
696 ;;;(elmo-imap4-get-session spec)
698 ;;; (elmo-imap4-mailbox
699 ;;; (elmo-imap4-spec-mailbox spec))
700 ;;; " (uidvalidity)")))
703 (defun elmo-imap4-sync-validity (spec validity-file)
707 (defun elmo-imap4-list (folder flag)
708 (let ((session (elmo-imap4-get-session folder)))
709 (elmo-imap4-session-select-mailbox
711 (elmo-imap4-folder-mailbox-internal folder))
712 (elmo-imap4-response-value
713 (elmo-imap4-send-command-wait
715 (format (if elmo-imap4-use-uid "uid search %s"
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)
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
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))
755 (elmo-read (current-buffer))))))))))
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) '<))
770 (setq chop-length (length msg-list)))
771 (while (and (not (null msg-list))
772 (< count chop-length))
774 (elmo-number-set-append
775 cont-list (car msg-list)))
777 (setq msg-list (cdr msg-list)))
785 (format "%s:%s" (car x) (cdr x)))
791 (nreverse set-list)))
796 ;; 0: new-mark 1: already-mark 2: seen-mark 3: important-mark
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)))
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
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)))
817 (member "\\Seen" flags)))
822 (member "\\Seen" flags)))
823 (if elmo-imap4-use-cache
825 (nth 0 app-data))))))
826 (setq elmo-imap4-current-msgdb
828 elmo-imap4-current-msgdb
830 (list (cons (elmo-msgdb-overview-entity-get-number entity)
834 (list (elmo-msgdb-overview-entity-get-number entity)
837 ;; Current buffer is process buffer.
838 (defun elmo-imap4-fetch-callback-1 (element app-data)
839 (elmo-imap4-fetch-callback-1-subr
841 (insert (or (elmo-imap4-response-bodydetail-text element)
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)
853 (defun elmo-imap4-parse-capability (string)
854 (if (string-match "^\\*\\(.*\\)$" string)
856 (concat "(" (downcase (elmo-match-string 1 string)) ")"))))
858 (defun elmo-imap4-clear-login (session)
859 (let ((elmo-imap4-debug-inhibit-logging t))
863 (elmo-imap4-send-command
866 (elmo-imap4-userid (elmo-network-session-user-internal session))
869 (elmo-get-passwd (elmo-network-session-password-key session))))))
870 (signal 'elmo-authenticate-error '(elmo-imap4-clear-login)))))
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
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)))
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)))
898 (luna-define-method elmo-network-initialize-session ((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))
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))
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
920 (elmo-imap4-response-value
921 (elmo-imap4-send-command-wait session "capability")
923 (when (eq (elmo-network-stream-type-symbol
924 (elmo-network-session-stream-type-internal session))
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
934 (elmo-imap4-response-value
935 (elmo-imap4-send-command-wait session "capability")
938 (luna-define-method elmo-network-authenticate-session ((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)
947 ((eq 'clear (car auth))
948 (elmo-imap4-clear-login session))
949 ((eq 'login (car auth))
950 (elmo-imap4-auth-login session))
952 (let* ((elmo-imap4-debug-inhibit-logging t)
957 (if (string-match "^auth=\\(.*\\)$"
959 (match-string 1 (upcase (symbol-name cap)))))
960 (elmo-imap4-session-capability-internal session))))
964 (mapcar '(lambda (cap) (upcase (symbol-name cap)))
968 client name step response tag
969 sasl-read-passphrase)
971 (if (or elmo-imap4-force-login
974 "There's no %s capability in server. continue?"
976 (elmo-network-session-auth-internal session)))))
977 (setq mechanism (sasl-find-mechanism
979 (signal 'elmo-authenticate-error
980 '(elmo-imap4-auth-no-mechanisms))))
984 (elmo-network-session-user-internal session)
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
993 (intern (downcase name)))
994 (setq sasl-read-passphrase
998 (elmo-network-session-password-key session)))))
1000 (elmo-imap4-send-command
1002 (concat "AUTHENTICATE " name
1003 (and (sasl-step-data step)
1006 (elmo-base64-encode-string
1007 (sasl-step-data step)
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)
1017 (signal 'elmo-authenticate-error
1019 (concat "elmo-imap4-auth-"
1021 ;; The authentication process is finished.
1023 (unless (elmo-imap4-response-continue-req-p response)
1024 ;; response is NO or BAD.
1025 (signal 'elmo-authenticate-error
1027 (concat "elmo-imap4-auth-"
1028 (downcase name))))))
1031 (elmo-base64-decode-string
1032 (elmo-imap4-response-value response 'continue-req)))
1033 (setq step (sasl-next-step client step))
1035 (elmo-imap4-send-string
1037 (if (sasl-step-data step)
1038 (elmo-base64-encode-string (sasl-step-data step)
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")
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))))
1056 (set-buffer send-buf)
1058 (elmo-set-buffer-multibyte nil)
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)
1067 (goto-char (point-min))
1068 (while (search-forward "\n" nil t)
1069 (replace-match "\r\n"))))
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*")))
1077 (set-buffer tmp-buf)
1079 (as-binary-input-file
1080 (insert-file-contents file))
1081 (goto-char (point-min))
1082 (if (eq (re-search-forward "^$" nil t)
1085 (goto-char (point-min))
1086 (while (search-forward "\n" nil t)
1087 (replace-match "\r\n"))))
1090 (luna-define-method elmo-delete-message-safe ((folder elmo-imap4-folder)
1092 (let ((session (elmo-imap4-get-session folder))
1094 (elmo-imap4-session-select-mailbox
1096 (elmo-imap4-folder-mailbox-internal folder))
1098 (elmo-imap4-response-value
1099 (elmo-imap4-send-command-wait session
1101 (if elmo-imap4-use-uid
1102 "uid search header message-id "
1103 "search header message-id ")
1104 (elmo-imap4-field-body msgid)))
1106 (if (memq number candidates)
1107 (elmo-folder-delete-messages folder (list number)))))
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))
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
1130 (elmo-imap4-folder-mailbox-internal folder))
1131 " (recent unseen messages)"))))
1133 (luna-define-method elmo-server-diff-async ((folder elmo-imap4-folder))
1134 (let ((session (elmo-imap4-get-session folder)))
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
1146 (elmo-imap4-folder-mailbox-internal folder))
1147 " (recent unseen messages)"))))
1151 (defvar elmo-imap4-server-eol "\r\n"
1152 "The EOL string sent from the server.")
1154 (defvar elmo-imap4-client-eol "\r\n"
1155 "The EOL string we send to the server.")
1157 (defvar elmo-imap4-display-literal-progress nil)
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)
1165 (if (match-string 1)
1166 (if (< (point-max) (+ (point) (string-to-number (match-string 1))))
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))))
1179 (goto-char (+ (point) (string-to-number (match-string 1))))
1180 (elmo-imap4-find-next-line))
1183 (defun elmo-imap4-sentinel (process string)
1184 (delete-process process))
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))
1194 (goto-char (point-min))
1195 (while (setq end (elmo-imap4-find-next-line))
1197 (narrow-to-region (point-min) end)
1198 (delete-backward-char (length elmo-imap4-server-eol))
1199 (goto-char (point-min))
1201 (cond ((eq elmo-imap4-status 'initial)
1202 (setq elmo-imap4-current-response
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
1211 (elmo-imap4-parse-response)
1212 elmo-imap4-current-response)))
1214 (message "Unknown state %s in arrival filter"
1215 elmo-imap4-status))))
1216 (delete-region (point-min) (point-max))))))))
1220 (defsubst elmo-imap4-forward ()
1221 (or (eobp) (forward-char 1)))
1223 (defsubst elmo-imap4-parse-number ()
1224 (when (looking-at "[0-9]+")
1226 (string-to-number (match-string 0))
1227 (goto-char (match-end 0)))))
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))
1235 (goto-char (+ pos len))
1236 (buffer-substring pos (+ pos len))))))
1237 ;;; (list ' pos (+ pos len))))))
1239 (defsubst elmo-imap4-parse-string ()
1240 (cond ((eq (char-after (point)) ?\")
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)))
1248 (skip-chars-forward "^\"\\\\")
1249 (setq name (concat name (buffer-substring p (point)))))
1252 ((eq (char-after (point)) ?{)
1253 (elmo-imap4-parse-literal))))
1255 (defsubst elmo-imap4-parse-nil ()
1256 (if (looking-at "NIL")
1257 (goto-char (match-end 0))))
1259 (defsubst elmo-imap4-parse-nstring ()
1260 (or (elmo-imap4-parse-string)
1261 (and (elmo-imap4-parse-nil)
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)))
1272 (defsubst elmo-imap4-parse-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)
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))))
1301 (defsubst elmo-imap4-parse-mailbox ()
1302 (let ((mailbox (elmo-imap4-parse-astring)))
1303 (if (string-equal "INBOX" (upcase mailbox))
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))))
1316 (defun elmo-imap4-parse-response ()
1317 "Parse a IMAP command response."
1319 (case (setq token (elmo-read (current-buffer)))
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))
1330 (elmo-imap4-parse-flag-list)))
1331 (LIST (list 'list (elmo-imap4-parse-data-list)))
1332 (LSUB (list 'lsub (elmo-imap4-parse-data-list)))
1335 (elmo-read (concat "("
1336 (buffer-substring (point) (point-max))
1338 (STATUS (elmo-imap4-parse-status))
1340 (NAMESPACE (elmo-imap4-parse-namespace))
1341 (CAPABILITY (list 'capability
1343 (concat "(" (downcase (buffer-substring
1344 (point) (point-max)))
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))
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))))
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)
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)))))
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)
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)))))))))
1393 (defun elmo-imap4-parse-bye ()
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))))
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))))
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]+\\)")
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)
1440 ((search-forward "ALERT] " nil t)
1441 (message "IMAP server information: %s"
1442 (buffer-substring (point) (point-max))))
1443 (t (list 'unknown)))))
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)))))
1454 (defsubst elmo-imap4-parse-header-list ()
1455 (when (eq (char-after (point)) ?\()
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))))
1463 (defsubst elmo-imap4-parse-fetch-body-section ()
1465 (buffer-substring (point)
1467 (progn (re-search-forward "[] ]" nil t)
1469 (if (eq (char-before) ? )
1471 (mapconcat 'identity
1472 (cons section (elmo-imap4-parse-header-list)) " ")
1473 (search-forward "]" nil t))
1476 (defun elmo-imap4-parse-fetch (response)
1477 (when (eq (char-after (point)) ?\()
1479 (while (not (eq (char-after (point)) ?\)))
1480 (elmo-imap4-forward)
1481 (let ((token (elmo-imap4-fetch-read (current-buffer))))
1482 (elmo-imap4-forward)
1484 (cond ((eq token 'UID)
1485 (list 'uid (condition-case nil
1486 (elmo-read (current-buffer))
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)))
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))))
1503 (if (eq (char-before) ?\[)
1506 (upcase (elmo-imap4-parse-fetch-body-section))
1508 (eq (char-after (point)) ?<)
1509 (buffer-substring (1+ (point))
1511 (search-forward ">" nil t)
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))))
1524 (defun elmo-imap4-parse-status ()
1525 (let ((mailbox (elmo-imap4-parse-mailbox))
1527 (when (and mailbox (search-forward "(" nil t))
1528 (while (not (eq (char-after (point)) ?\)))
1531 (let ((token (elmo-read (current-buffer))))
1532 (cond ((eq token 'MESSAGES)
1533 (list 'messages (elmo-read (current-buffer))))
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)))))
1543 (list 'unseen (elmo-read (current-buffer))))
1546 "Unknown status data %s in mailbox %s ignored"
1549 (skip-chars-forward " ")))
1550 (and elmo-imap4-status-callback
1551 (funcall elmo-imap4-status-callback
1553 elmo-imap4-status-callback-data))
1554 (list 'status status)))
1557 (defmacro elmo-imap4-value (value)
1558 (` (if (eq (, value) 'NIL) nil
1561 (defmacro elmo-imap4-nth (pos list)
1562 (` (let ((value (nth (, pos) (, list))))
1563 (elmo-imap4-value value))))
1565 (defun elmo-imap4-parse-namespace ()
1568 (copy-sequence elmo-imap4-extra-namespace-alist)
1569 (elmo-imap4-parse-namespace-subr
1570 (elmo-read (concat "(" (buffer-substring
1571 (point) (point-max))
1574 (defun elmo-imap4-parse-namespace-subr (ns)
1575 (let (prefix delim namespace-alist default-delim)
1576 ;; 0: personal, 1: other, 2: shared
1578 (setq namespace-alist
1579 (nconc namespace-alist
1583 (setq prefix (elmo-imap4-nth 0 namespace)
1584 delim (elmo-imap4-nth 1 namespace))
1585 (if (and prefix delim
1587 (concat (regexp-quote delim) "\\'")
1589 (setq prefix (substring prefix 0
1590 (match-beginning 0))))
1591 (if (eq (length prefix) 0)
1592 (progn (setq default-delim delim) nil)
1595 (if (string= (downcase prefix) "inbox")
1596 "[Ii][Nn][Bb][Oo][Xx]"
1597 (regexp-quote prefix))
1600 (elmo-imap4-nth i ns))))))
1602 (setq namespace-alist
1603 (nconc namespace-alist
1604 (list (cons "^.*$" default-delim)))))
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)))
1618 (defun elmo-imap4-parse-flag-list ()
1619 (let ((str (buffer-substring (+ (point) 1)
1620 (progn (search-forward ")" nil t)
1622 (unless (eq (length str) 0)
1623 (split-string str))))
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)))))
1649 (defsubst elmo-imap4-parse-string-list ()
1650 (cond ((eq (char-after (point)) ?\();; body-fld-param
1652 (elmo-imap4-forward)
1653 (while (setq str (elmo-imap4-parse-string))
1655 (elmo-imap4-forward))
1656 (nreverse strlist)))
1657 ((elmo-imap4-parse-nil)
1660 (defun elmo-imap4-parse-body-extension ()
1661 (if (eq (char-after (point)) ?\()
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)
1671 (or (elmo-imap4-parse-number)
1672 (elmo-imap4-parse-nstring))))
1674 (defsubst elmo-imap4-parse-body-ext ()
1676 (when (eq (char-after (point)) ?\ );; body-fld-dsp
1677 (elmo-imap4-forward)
1679 (if (eq (char-after (point)) ?\()
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)))))
1698 (defun elmo-imap4-parse-body ()
1700 (when (eq (char-after (point)) ?\()
1701 (elmo-imap4-forward)
1702 (if (eq (char-after (point)) ?\()
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))
1715 (append (elmo-imap4-parse-body-ext) body)));; body-ext-...
1716 (assert (eq (char-after (point)) ?\)))
1717 (elmo-imap4-forward)
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
1738 ;; ok, we're done parsing the required parts, what comes now is one
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)
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)...
1748 (when (eq (char-after (point)) ?\ )
1749 (elmo-imap4-forward)
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
1760 (backward-char)))));; no match...
1762 ;; ...and then parse the third one here...
1764 (when (eq (char-after (point)) ?\ );; body-ext-1part:
1765 (elmo-imap4-forward)
1766 (push (elmo-imap4-parse-nstring) body);; body-fld-md5
1768 (append (elmo-imap4-parse-body-ext) body)));; body-ext-1part..
1770 (assert (eq (char-after (point)) ?\)))
1771 (elmo-imap4-forward)
1774 (luna-define-method elmo-folder-initialize :around ((folder
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))
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))
1793 (setq parse (elmo-parse-token name ":"))
1794 (elmo-imap4-folder-set-mailbox-internal folder
1795 (elmo-imap4-encode-folder-string
1798 (setq parse (elmo-parse-prefixed-element ?: (cdr parse) "/"))
1799 (elmo-net-folder-set-user-internal folder
1800 (if (eq (length (car parse)) 0)
1804 (setq parse (elmo-parse-prefixed-element ?/ (cdr parse)))
1805 (elmo-net-folder-set-auth-internal
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
1817 (elmo-get-network-stream-type elmo-imap4-default-stream-type)))
1820 ;;; ELMO IMAP4 folder
1821 (luna-define-method elmo-folder-expand-msgdb-path ((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))))
1832 (or (elmo-net-folder-user-internal folder) "nobody")
1833 (expand-file-name (or (elmo-net-folder-server-internal folder)
1837 elmo-msgdb-directory)))))))
1839 (luna-define-method elmo-folder-status-plugged ((folder
1841 (elmo-imap4-folder-status-plugged folder))
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)))
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
1856 (elmo-imap4-folder-mailbox-internal folder))
1857 " (uidnext messages)"))
1860 (- (elmo-imap4-response-value status 'uidnext) 1)
1863 (elmo-imap4-response-value status 'messages)
1864 (elmo-msgdb-killed-list-length killed))
1865 (elmo-imap4-response-value status 'messages)))))
1867 (luna-define-method elmo-folder-list-messages-plugged ((folder
1870 (elmo-imap4-list folder
1871 (let ((max (elmo-msgdb-max-of-killed
1872 (elmo-folder-killed-list-internal folder))))
1875 (format "uid %d:*" (1+ max))
1878 (luna-define-method elmo-folder-list-unreads-plugged
1879 ((folder elmo-imap4-folder))
1880 (elmo-imap4-list folder "unseen"))
1882 (luna-define-method elmo-folder-list-importants-plugged
1883 ((folder elmo-imap4-folder))
1884 (elmo-imap4-list folder "flagged"))
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))))
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))
1897 (elmo-string-matched-assoc
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))
1908 result append-serv type)
1909 (setq result (elmo-imap4-response-get-selectable-mailbox-list
1910 (elmo-imap4-send-command-wait
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))
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 ":"
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)
1935 (setq append-serv (concat append-serv
1936 (elmo-network-stream-type-spec-string
1939 (let ((re-delim (regexp-quote delim))
1940 (case-fold-search nil)
1941 folder ret has-child-p)
1944 (not (string= root ""))
1946 (concat "\\(.*\\)" re-delim "\\'")
1948 (setq root (concat root delim)))
1949 (while (setq folder (car result))
1951 (concat "^\\(" (regexp-quote root) "[^" re-delim "]" "+\\)"
1954 (setq folder (match-string 1 folder)))
1955 (setq has-child-p nil
1958 (mapcar (lambda (fld)
1960 (concat "^" (regexp-quote folder)
1961 "\\(" re-delim "\\|\\'\\)")
1963 (progn (setq has-child-p t) nil)
1966 folder (concat prefix
1967 (elmo-imap4-decode-folder-string folder)
1969 (eval append-serv)))
1970 ret (append ret (if has-child-p
1971 (list (list folder))
1974 (mapcar (lambda (fld)
1975 (concat prefix (elmo-imap4-decode-folder-string fld)
1977 (eval append-serv))))
1980 (luna-define-method elmo-folder-exists-p-plugged ((folder elmo-imap4-folder))
1981 (let ((session (elmo-imap4-get-session folder)))
1983 (elmo-imap4-session-current-mailbox-internal session)
1984 (elmo-imap4-folder-mailbox-internal folder))
1986 (elmo-imap4-session-select-mailbox
1988 (elmo-imap4-folder-mailbox-internal folder)
1989 'force 'notify-bye))))
1991 (luna-define-method elmo-folder-creatable-p ((folder elmo-imap4-folder))
1994 (luna-define-method elmo-folder-writable-p ((folder elmo-imap4-folder))
1997 (luna-define-method elmo-folder-delete :before ((folder elmo-imap4-folder))
1998 (let ((session (elmo-imap4-get-session folder))
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
2008 (elmo-imap4-folder-mailbox-internal folder)))))))
2010 (luna-define-method elmo-folder-rename-internal ((folder elmo-imap4-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
2017 (elmo-imap4-send-command-wait session "close")
2018 (elmo-imap4-send-command-wait
2022 (elmo-imap4-folder-mailbox-internal folder))
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))))
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
2033 elmo-imap4-number-set-chop-length))
2035 (elmo-imap4-session-select-mailbox session
2036 (elmo-imap4-folder-mailbox-internal
2039 (if (elmo-imap4-send-command-wait session
2042 (if elmo-imap4-use-uid
2045 (cdr (car set-list)))
2047 (elmo-imap4-folder-mailbox-internal
2049 (setq succeeds (append succeeds numbers)))
2050 (setq set-list (cdr set-list)))
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))
2061 (elmo-imap4-session-select-mailbox session
2062 (elmo-imap4-folder-mailbox-internal
2064 (setq set-list (elmo-imap4-make-number-set-list
2066 elmo-imap4-number-set-chop-length))
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
2075 (if elmo-imap4-use-uid
2076 "uid store %s %sflags.silent (%s)"
2077 "store %s %sflags.silent (%s)")
2078 (cdr (car set-list))
2081 (setq response 'fail))
2082 (setq set-list (cdr set-list)))
2083 (not (eq response 'fail))))
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")))
2091 (defmacro elmo-imap4-detect-search-charset (string)
2092 (` (with-temp-buffer
2094 (detect-mime-charset-region (point-min) (point-max)))))
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"))
2101 (length (length from-msgs))
2102 charset set-list end results)
2103 (message "Searching...")
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)))
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) )
2115 (mapcar '(lambda (x) (delete x numbers)) rest)
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
2122 elmo-imap4-number-set-chop-length)
2128 (elmo-imap4-response-value
2129 (elmo-imap4-send-command-wait
2132 (if elmo-imap4-use-uid
2133 "uid search %s%s%s %s"
2137 (if elmo-imap4-use-uid "uid ")
2138 (cdr (car set-list))
2141 (if (eq (elmo-filter-type filter)
2145 (elmo-date-get-description
2146 (elmo-date-get-datevec
2147 (elmo-filter-value filter)))))
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)))
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
2165 elmo-imap4-number-set-chop-length)
2171 (elmo-imap4-response-value
2172 (elmo-imap4-send-command-wait
2175 (if elmo-imap4-use-uid "uid ")
2179 (symbol-name charset))
2183 (if elmo-imap4-use-uid "uid ")
2184 (cdr (car set-list))
2187 (if (eq (elmo-filter-type filter)
2192 (elmo-filter-key filter)
2196 (elmo-filter-key filter))
2198 (encode-mime-charset-string
2199 (elmo-filter-value filter) charset))))
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)))
2210 (defun elmo-imap4-search-internal (folder session condition from-msgs)
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)
2219 result (elmo-list-filter result
2220 (elmo-imap4-search-internal
2221 folder session (nth 2 condition)
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
2228 (elmo-imap4-search-internal
2229 folder session (nth 2 condition) from-msgs)))
2230 result (sort result '<))))))
2232 (luna-define-method elmo-folder-search :around ((folder elmo-imap4-folder)
2233 condition &optional numbers)
2234 (if (elmo-folder-plugged-p folder)
2236 (let ((session (elmo-imap4-get-session folder)))
2237 (elmo-imap4-session-select-mailbox
2239 (elmo-imap4-folder-mailbox-internal folder))
2240 (elmo-imap4-search-internal folder session condition numbers)))
2241 (luna-call-next-method)))
2243 (luna-define-method elmo-folder-msgdb-create-plugged
2244 ((folder elmo-imap4-folder) numbers &rest args)
2246 (let ((session (elmo-imap4-get-session folder))
2249 '("Subject" "From" "To" "Cc" "Date"
2250 "Message-Id" "References" "In-Reply-To")
2251 elmo-msgdb-extra-fields))
2253 (length (length numbers))
2255 (setq rfc2060 (memq 'imap4rev1
2256 (elmo-imap4-session-capability-internal
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
2263 elmo-imap4-overview-fetch-chop-length))
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
2273 (elmo-imap4-send-command-wait
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))
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))))
2293 (luna-define-method elmo-folder-unmark-important-plugged
2294 ((folder elmo-imap4-folder) numbers)
2295 (elmo-imap4-set-flag folder numbers "\\Flagged" 'remove))
2297 (luna-define-method elmo-folder-mark-as-important-plugged
2298 ((folder elmo-imap4-folder) numbers)
2299 (elmo-imap4-set-flag folder numbers "\\Flagged"))
2301 (luna-define-method elmo-folder-unmark-read-plugged
2302 ((folder elmo-imap4-folder) numbers)
2303 (elmo-imap4-set-flag folder numbers "\\Seen" 'remove))
2305 (luna-define-method elmo-folder-mark-as-read-plugged
2306 ((folder elmo-imap4-folder) numbers)
2307 (elmo-imap4-set-flag folder numbers "\\Seen"))
2309 (luna-define-method elmo-message-use-cache-p ((folder elmo-imap4-folder)
2311 elmo-imap4-use-cache)
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.
2319 (luna-define-method elmo-folder-check-plugged ((folder elmo-imap4-folder))
2320 (let ((session (elmo-imap4-get-session folder 'if-exists)))
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
2328 (elmo-imap4-folder-mailbox-internal folder)
2330 (elmo-imap4-session-check session))))))
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))
2340 (elmo-imap4-send-command-wait session
2344 (elmo-imap4-folder-mailbox-internal
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)))
2351 (setq messages (- messages
2352 (elmo-msgdb-killed-list-length
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)))
2359 (luna-define-method elmo-folder-diff-plugged ((folder elmo-imap4-folder))
2360 (elmo-imap4-folder-diff-plugged folder))
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))
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)
2376 (setq session (elmo-imap4-get-session folder)
2377 mailbox (elmo-imap4-folder-mailbox-internal folder)
2378 tag (elmo-imap4-send-command session
2382 (message "Selecting %s..."
2383 (elmo-folder-name-internal folder))
2385 (setq msgdb (elmo-msgdb-load folder 'silent)))
2386 (elmo-folder-set-killed-list-internal
2388 (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder)))
2389 (if (setq result (elmo-imap4-response-ok-p
2391 (elmo-imap4-read-response session tag))))
2393 (elmo-imap4-session-set-current-mailbox-internal
2395 (elmo-imap4-session-set-read-only-internal
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)
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
2409 (if (elmo-imap4-response-ok-p response)
2410 (elmo-imap4-session-set-current-mailbox-internal
2413 (elmo-imap4-session-set-current-mailbox-internal
2416 (if (elmo-imap4-response-ok-p response)
2417 (elmo-imap4-session-set-current-mailbox-internal
2420 (elmo-imap4-session-set-current-mailbox-internal
2422 (luna-call-next-method)))
2424 ;; elmo-folder-open-internal: do nothing.
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
2437 (>= size elmo-message-fetch-threshold)
2438 (or (not elmo-message-fetch-confirm)
2439 (not (prog1 (y-or-n-p
2441 "Fetch entire message at once? (%dbytes)"
2444 ;; Fetch message as imap message.
2445 (elmo-make-fetch-strategy 'section
2447 (elmo-message-use-cache-p
2449 (elmo-file-cache-path
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
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
2462 (elmo-message-use-cache-p
2464 (elmo-file-cache-path
2466 (elmo-make-fetch-strategy 'entire
2468 (elmo-message-use-cache-p
2470 (elmo-file-cache-path
2473 (luna-define-method elmo-folder-create-plugged ((folder elmo-imap4-folder))
2474 (elmo-imap4-send-command-wait
2475 (elmo-imap4-get-session folder)
2478 (elmo-imap4-folder-mailbox-internal folder)))))
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))
2485 (elmo-imap4-session-select-mailbox session
2486 (elmo-imap4-folder-mailbox-internal
2488 (setq send-buffer (elmo-imap4-setup-send-buffer))
2491 (elmo-imap4-send-command-wait
2495 (elmo-imap4-mailbox (elmo-imap4-folder-mailbox-internal
2497 (if unread " () " " (\\Seen) ")
2498 (elmo-imap4-buffer-literal send-buffer))))
2499 (kill-buffer send-buffer))
2502 (if elmo-enable-disconnected-operation
2503 (elmo-folder-append-buffer-dop folder unread number)
2504 (error "Unplugged"))))
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)))))))
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))
2524 (elmo-imap4-copy-messages src-folder folder numbers)
2525 (elmo-progress-notify 'elmo-folder-move-messages (length numbers)))
2526 (luna-call-next-method)))
2528 (luna-define-method elmo-message-deletable-p ((folder elmo-imap4-folder)
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.
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)
2542 (defsubst elmo-imap4-message-fetch (folder number strategy
2543 section outbuf unseen)
2544 (let ((session (elmo-imap4-get-session folder))
2546 (elmo-imap4-session-select-mailbox session
2547 (elmo-imap4-folder-mailbox-internal
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))
2556 (elmo-imap4-send-command-wait session
2558 (if elmo-imap4-use-uid
2559 "uid fetch %s body%s[%s]"
2560 "fetch %s body%s[%s]")
2562 (if unseen ".peek" "")
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
2573 (with-current-buffer outbuf
2578 (luna-define-method elmo-message-fetch-plugged ((folder elmo-imap4-folder)
2582 (elmo-imap4-message-fetch folder number strategy section outbuf unseen))
2584 (luna-define-method elmo-message-fetch-field ((folder elmo-imap4-folder)
2586 (let ((session (elmo-imap4-get-session folder)))
2587 (elmo-imap4-session-select-mailbox session
2588 (elmo-imap4-folder-mailbox-internal
2590 (with-current-buffer (elmo-network-session-buffer session)
2591 (setq elmo-imap4-fetch-callback nil)
2592 (setq elmo-imap4-fetch-callback-data nil))
2595 (elmo-imap4-response-bodydetail-text
2596 (elmo-imap4-response-value
2597 (elmo-imap4-send-command-wait session
2599 (if elmo-imap4-use-uid
2602 "fetch %s (body.peek[header.fields (%s)])"
2605 (elmo-delete-cr-buffer)
2606 (goto-char (point-min))
2607 (std11-field-body (symbol-name field)))))
2612 (product-provide (provide 'elmo-imap4) (require 'elmo-version))
2614 ;;; elmo-imap4.el ends here