#!/bin/python # MyBot: BotManager # # (c) 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch> import BotInstance class BotManager: def __init__(self): self.botInstances = {} self.availableChannels = {} self.observedChannels = {} self.comChannels = {} self.results = {} self.nickCounter = 0 self.channelCounter = 0 self.availableChannelsListener = [] self.observedChannelsListener = [] self.resultsListener = [] def updateAvailableChannels(self): """Request update of available channels list""" for b in self.botInstances.itervalues(): b.requestChannelList() def addServer(self, server, port = 6667): """Add a new server (e.g. create a new bot for the server)""" if server not in self.observedChannels.keys(): channel = self.genComChannelName() nick = self.genNick() botInstance = BotInstance.BotInstance(self, server, port, nick, channel) self.botInstances[server] = botInstance self.comChannels[server] = channel botInstance.requestChannelList() def observeChannel(self, server, channel): botInstance = self.botInstances.get(server, None) if botInstance: botInstance.join(channel) def addSearchQuery(self, query, server=None): self.results[query] = [] if not server: for b in self.botInstances.itervalues(): b.queries.append(query) else: botInstance = self.botInstances.get(server, None) if botInstance: botInstance.append(query) def getSearchQueries(self, server=None): queries = set([]) if not server: for b in self.botInstances.itervalues(): queries.union(set(b.queries)) else: botInstance = self.botInstances.get(server, None) if botInstance: queries = set(botInstance.queries) return list(queries) def getSearchResults(self, server=None): results = set([]) if not server: for b in self.botInstances.itervalues(): results.union(set(b.results)) else: botInstance = self.botInstances.get(server, None) if botInstance: results = set(botInstance.results) return list(results) def genComChannelName(self): """Generate a random and unique channel name""" self.channelCounter += 1 return '#mbcc-%s' % self.channelCounter def genNick(self): """Generate a random and unique nick""" self.nickCounter += 1 return 'mybot-%s' % self.nickCounter # API for listener def addAvailableChannelsListener(self, listener): self.availableChannelsListener.append(listener) def removeAvailalbeChannelsListener(self, listener): self.availableChannelsListener.remove(listener) def addObservedChannelsListener(self, listener): self.observedChannelsListener.append(listener) def removeObservedChannelsListener(self, listener): self.observedChannelsListener.remove(listener) def addResultsListener(self, listener): self.resultsListener.append(listener) def removeResultsListener(self, listener): self.resultsListener.remove(listener) # API for BotInstances def availableChannelsUpdated(self, botInstance): serverChannels = self.availableChannels.get(botInstance.server, []) newChannels = set(serverChannels).union(set(botInstance.availableChannels)) self.availableChannels[botInstance.server] = list(newChannels) for listener in self.availableChannelsListener: listener() def observedChannelsUpdated(self, botInstance): serverChannels = self.observedChannels.get(botInstance.server, []) newChannels = set(serverChannels).union(set(botInstance.observedChannels)) self.observedChannels[botInstance.server] = list(newChannels) print "BotManager:observedChannelsUpdated" for listener in self.observedChannelsListener: listener() def resultsUpdated(self, botInstance): for query in botInstance.queries: for result in botInstance.results.get(query, []): if not self.results.has_key(query): self.results[query] = [] self.results[query].append(result) for listener in self.resultsListener: listener() if __name__ == "__main__": botm = BotManager()