diff -r 26200f535296 Lib/imaplib.py --- a/Lib/imaplib.py Wed Oct 03 13:53:28 2012 +0530 +++ b/Lib/imaplib.py Wed Oct 03 15:55:26 2012 +0200 @@ -22,7 +22,7 @@ __version__ = "2.58" -import binascii, errno, random, re, socket, subprocess, sys, time, calendar +import binascii, errno, random, re, socket, subprocess, sys, time, calendar, select from datetime import datetime, timezone, timedelta try: import ssl @@ -61,6 +61,7 @@ 'GETANNOTATION':('AUTH', 'SELECTED'), 'GETQUOTA': ('AUTH', 'SELECTED'), 'GETQUOTAROOT': ('AUTH', 'SELECTED'), + 'IDLE': ('AUTH', 'SELECTED'), 'MYRIGHTS': ('AUTH', 'SELECTED'), 'LIST': ('AUTH', 'SELECTED'), 'LOGIN': ('NONAUTH',), @@ -813,6 +814,30 @@ """ return self._simple_command('UNSUBSCRIBE', mailbox) + def idle(self, timeout = 29 * 60): + """Implements IMAP IDLE extension as described in RFC 2177. + + Waits until state of current mailbox changes. + + Default timeout is the maximum possible idle time according to RFC 2177. + + (typ, {data}) = .idle(timeout) + + Returns dictionary of all untagged responses received while idling. + """ + if not "IDLE" in self.capabilities: + raise self.error("server does not support IDLE command.") + + self.untagged_responses = {} + tag = self._command("IDLE") + self._get_response() # read continuation response + + select.select([self.sock],[],[],timeout) + + self.send(b"DONE" + CRLF) + typ,data = self._command_complete("IDLE", tag) + return typ, self.untagged_responses + def xatom(self, name, *args): """Allow simple extension commands