私がプログラミングしているアプリでは、現在IMAPの検索機能を使用してメールIDを取得しています。これはシンプルで簡単なタスクなので便利ですが、サーバーがいっぱいになると検索速度に悪影響を与えるのではないかと思っていました。現在スピーディーです)そしてもしそうなら、IMAPアイドルコマンドとtwisted.internet.mailを扱う価値があります。
私はすでにこれによってIDLEを実装しています
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コマンド
cmd = Command("IDLE", continuation = self.a)
d = self.imap_connection.sendCommand(cmd)
return d
今、私がIDLEに躊躇している理由は、サーバーがIDLEをサポートしていない場合は最初にオフになり、その後は使用できなくなります(これが主な理由ではありませんが)。アイドルコマンドはマークされていない応答であり、IDLEコマンドに対するものであることを知る方法です。