def __cbManualSearch(self, result, tag, mbox, query, uid, searchResults=None): """ Marcus, i modified so this supports a generator instead of a list """ if searchResults is None: searchResults = [] i = 0 ineedbreak = False for (id, msg) in result : # searchFilter and singleSearchStep will mutate the query. Dang. # Copy it here or else things will go poorly for subsequent # messages. if self._searchFilter(copy.deepcopy(query), id, msg, len(mbox.listuids), mbox.listuids[-1]): if uid: searchResults.append(str(msg.getUID())) else: searchResults.append(str(id)) # the break must be here else we will miss the 5th of all mails i += 1 if i % 5 == 0: ineedbreak = True break if ineedbreak : from twisted.internet import reactor # result is now a generator! reactor.callLater(0, self.__cbManualSearch, result, tag, mbox, query, uid, searchResults) else : if searchResults: self.sendUntaggedResponse('SEARCH ' + ' '.join(searchResults)) self.sendPositiveResponse(tag, 'SEARCH completed')