+# This example is a lot more interesting if you have an active client\r
+# connected to the same IMAP account!\r
+\r
from imapclient import IMAPClient\r
-import time\r
\r
HOST = 'imap.host.com'\r
USERNAME = 'someuser'\r
ssl = True\r
\r
server = IMAPClient(HOST, use_uid=True, ssl=ssl)\r
-\r
server.login(USERNAME, PASSWORD)\r
+server.select_folder('INBOX')\r
\r
-select_info = server.select_folder('INBOX')\r
-print select_info\r
-print\r
-\r
-idling = True\r
-\r
-def callback(resp, arg):\r
- global idling\r
- \r
- print "Something happened, or timeout was reached"\r
- print\r
- print "Callback response:"\r
- print resp\r
- print\r
- print "The callback function received arg: ", arg\r
- print\r
- idling = False\r
+# Start IDLE mode\r
+server.idle() \r
\r
-server.idle(timeout=5, callback=callback, cb_arg="Hello future self!")\r
-print "Began idling"\r
+# Wait for up to 30 seconds for an IDLE response\r
+responses = server.idle_check(timeout=30)\r
+print responses\r
\r
-while idling:\r
- print "..still idling.."\r
- time.sleep(1)\r
+# Come out of IDLE mode\r
+text, responses = server.idle_done()\r
+print 'IDLE done. Server said %r' % text\r
+print 'Final responses: ', responses\r
\r
-print server.logout()
\ No newline at end of file
+print server.logout()\r
def idle(self):
"""Put server into IDLE mode.
- IDLE mode will end when the server notifies of a change, the server
- reaches the timeout, or another IMAP4 command is scheduled.
-
- @param timeout: timeout for the IDLE command in seconds (default: 29 mins)
- @param callback: Function to be called when IDLE mode ends. If a callback
- is supplied the idle function will be asynchronous, returning immediately.
- @param cb_arg: An optional argument that will be passed to the callback function.
- @return: Server response. (None if callback is specified).
+ In this mode the server will return unsolicited responses
+ about changes to the selected mailbox.
+
+ This method returns immediately. Use idle_check() to check for
+ IDLE responses and idle_done() to stop IDLE mode.
- #XXX update documentation
- #XXX what about finding out what the IDLE data was?
+ Note: Any other commmands issued while the server is in IDLE
+ mode will fail.
+
+ See RFC 2177 for more information about the IDLE extension.
+ http://tools.ietf.org/html/rfc2177
"""
self._idle_tag = self._imap._command('IDLE')
resp = self._imap._get_response()
raise self.Error('Unexpected IDLE response: %s' % resp)
def idle_check(self, timeout=None):
- """XXX
+ """Check for any IDLE responses sent by the server.
+
+ This method should only be called if the server is in IDLE
+ mode (see idle()).
+
+ By default, this method will block until an IDLE response is
+ received. If timeout is provided, the call will block for at
+ most this number of seconds (approximately) while waiting for
+ an IDLE response.
+
+ The return value is a list of received IDLE responses. These
+ will be parsed with values converted to appropriate types. For
+ example:
+
+ [(1, 'EXISTS'),
+ ('OK', 'Still here'),
+ (1, 'FETCH', ('FLAGS', ('\NotJunk',))),
+ (0, 'EXPUNGE')]
+
+ An attempt is made to group responses that were sent together
+ by the server by waiting for a little longer after the first
+ IDLE response is received.
"""
timeouts = 0
resps = []
start_t = time.time()
- def done():
- if timeout is None:
- if resps and timeouts > 1:
- return True
- else:
- if time.time() - start_t >= timeout:
- return True
- return False
+
+ def blocking_done():
+ # Wait for one timed out read to try group IDLE responses
+ # that were sent together
+ return resps and timeouts > 1
+
+ if timeout is None:
+ done = blocking_done
+ else:
+ def done():
+ return blocking_done() or (time.time() - start_t >= timeout)
# make the socket non-blocking so the timeout can be
# implemented for this call
self._imap.sock.settimeout(None)
def idle_done(self):
- """XXX
+ """Take the server out of IDLE mode.
+
+ This method should only be called if the server is in IDLE mode.
+
+ The return value is (command_text, idle_responses) where
+ command_text is the text sent by the server when the IDLE
+ command finished (eg. 'Idle terminated') and idle_responses is
+ a list of parsed idle responses received since the last call
+ to idle_check() (if any). These are returned in parsed form as
+ per idle_check().
"""
self._imap.send('DONE\r\n')
- # Slurp up any remaining IDLE data until the IDLE is done
+ # Slurp up any remaining IDLE responses until the IDLE is done
tag = self._idle_tag
tagged_commands = self._imap.tagged_commands
resps = []