In an app I am programming I am currently using IMAP’s search function to get the email ID, this is nice as it is a simple and easy task however I was wondering as the server gets fuller will this be detrimental to the search speed (which is currently speedy) and if so is worth dealing with the IMAP idle command and twisted.internet.mail.
I already have IDLE implemented by this
class Command(object):
_1_RESPONSES = ('CAPABILITY', 'FLAGS', 'LIST', 'LSUB', 'STATUS', 'SEARCH', 'NAMESPACE')
_2_RESPONSES = ('EXISTS', 'EXPUNGE', 'FETCH', 'RECENT')
_OK_RESPONSES = ('UIDVALIDITY', 'UNSEEN', 'READ-WRITE', 'READ-ONLY', 'UIDNEXT', 'PERMANENTFLAGS')
defer = None
def __init__(self, command, args=None, wantResponse=(),
continuation=None, *contArgs, **contKw):
self.command = command
self.args = args
self.wantResponse = wantResponse
self.continuation = lambda x: continuation(x, *contArgs, **contKw)
self.lines = []
def format(self, tag):
if self.args is None:
return ' '.join((tag, self.command))
return ' '.join((tag, self.command, self.args))
def finish(self, lastLine, unusedCallback):
send = []
unuse = []
for L in self.lines:
names = parseNestedParens(L)
N = len(names)
if (N >= 1 and names[0] in self._1_RESPONSES or
N >= 2 and names[1] in self._2_RESPONSES or
N >= 1 and names[0] in self.wantResponse or # allows for getting the responses you want, twisted doesn't seem to do that at least with the idle command
N >= 2 and names[1] in self.wantResponse or # same as above line just with 2_RESPONSES check
N >= 2 and names[0] == 'OK' and isinstance(names[1], types.ListType) and names[1][0] in self._OK_RESPONSES):
send.append(names)
else:
unuse.append(names)
d, self.defer = self.defer, None
d.callback((send, lastLine))
if unuse:
unusedCallback(unuse)
IDLE Command being sent
cmd = Command("IDLE", continuation = self.a)
d = self.imap_connection.sendCommand(cmd)
return d
Now the reason I am hesitant with IDLE is first off if the server doesn’t support it I then can’t use it (though this isn’t the primary reason), I also don’t wish to is because the response of the idle command are unmarked response and how to know they are for the IDLE command.
From the RFC:
The IDLE command is sent from the client to the server when the
client is ready to accept unsolicited mailbox update messages.
So, if there are other commands in progress – the results of which you might confuse with an untagged response to an IDLE command – do not send the IDLE command.
Only send IDLE when you’re prepared to interpret its untagged responses correctly. 🙂
Or put more simply, don’t use IDLE concurrently with any other commands. Then, when you are using IDLE, you know all untagged responses are for the IDLE command.
At least, that’s probably right. As with any IMAP4 topic… who really knows. You probably need to examine the servers you want to interoperate with and see if their behavior actually matches the vision laid out in the RFCs.