def build_sweetiebot(config=None): if config is None: import config resource = config.nickname + randomstr() if config.fake_redis: log.debug('faking out redis') redis_conn = FakeRedis() else: redis_conn = redis.from_url(config.redis_url) jid = config.username + '/' + resource nick = config.nickname room = config.chatroom password = config.password if config.hostname is not None: address = (config.hostname, config.port) else: address = () bot = MUCJabberBot(jid, password, room, nick, address) crest = SweetieCrest(config.crest_base_url, config.crest_client_id, config.crest_client_secret, config.crest_refresh_token) lookup = SweetieLookup(bot, crest) admin = SweetieAdmin(bot, config.chatroom) mq = SweetieMQ(config) de = SweetieDe(bot, admin, mq, ResponsesFile('data/deowl_fails.txt')) actions = ResponsesFile('data/actions.txt') sass = ResponsesFile('data/sass.txt') cadmusic = ResponsesFile('data/cadmusic.txt') tell = SweetieTell(bot, redis_conn) dictionary = SweetieDictionary(bot) chat = SweetieChat(bot, actions, sass, config.chatroom, cadmusic, tell, dictionary) roulette = SweetieRoulette(bot, admin) pings = SweetiePings(bot, redis_conn) moon = SweetieMoon(bot) if config.twitter_key is not None: twitter = TwitterClient.get_client(config.twitter_key, config.twitter_secret) watchers = list(map(twitter.get_timeline_watcher, ['EVE_Status'])) else: watchers = [] watchers.append( AtomWatcher.get_watcher( 'http://eveion.blogspot.com/feeds/posts/default')) seen = SweetieSeen(bot, redis_conn) sweet = Sweetiebot(config.nickname, bot, lookup, mq, admin, chat, roulette, de, pings, watchers, moon) return sweet
def listbans(self, message): '''List currently-banned users''' id = 'banlist'+randomstr() query = SweetieAdmin.query_element() item = ET.SubElement(query, 'item') item.set('affiliation', 'outcast') iq = self.bot.create_iq(id, 'get', query) response = iq.send() res = "" items = response.findall('.//{'+self.QUERY_NS+'}item') log.debug('banlist items: '+str(items)) for item in items: if item.get('jid') is not None: res += "\n" + item.get('jid') + ": "+str(item[0].text) if not res: return 'Bans list is empty' return res
def build_sweetiebot(config=None): if config is None: import config resource = config.nickname + randomstr() if config.fake_redis: log.debug('faking out redis') redis_conn = FakeRedis() else: redis_conn = redis.Redis('localhost') jid = config.username + '/' + resource nick = config.nickname room = config.chatroom password = config.password bot = MUCJabberBot(jid, password, room, nick) crest = SweetieCrest(config.crest_base_url, config.crest_client_id, config.crest_client_secret, config.crest_refresh_token) lookup = SweetieLookup(bot, crest) admin = SweetieAdmin(bot, config.chatroom, config.mods) mq = SweetieMQ(config) de = SweetieDe(bot, admin, mq, ResponsesFile('data/deowl_fails.txt')) actions = ResponsesFile('data/actions.txt') sass = ResponsesFile('data/sass.txt') cadmusic = ResponsesFile('data/cadmusic.txt') markov = SweetieMarkov(redis_conn, 'data/banned_keywords.txt', 'data/preferred_keywords.txt', 'data/swap_words.txt') tell = SweetieTell(bot, redis_conn) dictionary = SweetieDictionary(bot) chat = SweetieChat(bot, actions, sass, config.chatroom, markov, cadmusic, tell, dictionary) roulette = SweetieRoulette(bot, admin) pings = SweetiePings(bot, redis_conn) if hasattr(config, 'twitter_key'): twitter = TwitterClient.get_client(config.twitter_key, config.twitter_secret) watchers = list(map(twitter.get_timeline_watcher, ['EVE_Status', 'EVEOnline'])) else: watchers = [] watchers.append(AtomWatcher.get_watcher('http://eveion.blogspot.com/feeds/posts/default')) seen = SweetieSeen(bot, redis_conn) sweet = Sweetiebot(config.nickname, bot, lookup, mq, admin, chat, roulette, de, pings, watchers) return sweet
def build_sweetiebot(config=None): if config is None: import config resource = config.nickname + randomstr() if config.fake_redis: log.debug('faking out redis') redis_conn = FakeRedis() else: redis_conn = redis.from_url(config.redis_url) jid = config.username + '/' + resource nick = config.nickname room = config.chatroom password = config.password if config.hostname is not None: address = (config.hostname, config.port) else: address = () bot = MUCJabberBot(jid, password, room, nick, address) lookup = SweetieLookup(bot) admin = SweetieAdmin(bot, config.chatroom) de = SweetieDe(bot, admin, ResponsesFile('data/deowl_fails.txt')) actions = ResponsesFile('data/actions.txt') sass = ResponsesFile('data/sass.txt') cadmusic = ResponsesFile('data/cadmusic.txt') tell = SweetieTell(bot, redis_conn) dictionary = SweetieDictionary(bot) chat = SweetieChat(bot, actions, sass, config.chatroom, cadmusic, tell, dictionary) roulette = SweetieRoulette(bot, admin) pings = SweetiePings(bot, redis_conn) moon = SweetieMoon(bot) if config.twitter_key is not None: twitter = TwitterClient.get_client(config.twitter_key, config.twitter_secret) watchers = list(map(twitter.get_timeline_watcher, ['EVE_Status'])) else: watchers = [] watchers.append(AtomWatcher.get_watcher('http://eveion.blogspot.com/feeds/posts/default')) seen = SweetieSeen(bot, redis_conn) sweet = Sweetiebot(config.nickname, bot, lookup, admin, chat, roulette, de, pings, watchers, moon) return sweet
def set_affiliation(self, jid=None, nick=None, atype='role', value=None, reason=None, on_success=None, on_failure=None): """ Change room affiliation.""" values = ('outcast', 'member', 'admin', 'owner', 'none') if value not in values: raise TypeError('value must be one of '+repr(values)) atypes = ('role', 'affiliation') if atype not in atypes: raise TypeError('atype must be one of '+repr(atypes)) query = ET.Element('{http://jabber.org/protocol/muc#admin}query') if nick is not None: item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {atype:value, 'nick':nick}) elif jid is not None: item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {atype:value, 'jid':jid}) else: raise Exception("nick or jid needs to be set") query.append(item) if reason is not None: r = ET.Element('{http://jabber.org/protocol/muc#admin}reason') r.text = reason item.append(r) id = 'setaffil'+randomstr() iq = self.bot.create_iq(id, 'set', query) log.debug('created affiliation iq: '+str(iq)) try: response = iq.send() if on_success is not None: on_success() log.debug('got affiliation response: '+str(response)) except IqError as iqe: log.debug('got iqerror: '+str(iqe)) if on_failure is not None: on_failure() if iqe.text: return iqe.text error_condition = iqe.iq['error']['condition'] if error_condition: return 'Operation failed on {}: {}'.format(nick or jid, error_condition) return 'Operation failed on {}'.format(nick or jid) except IqTimeout: if on_failure is not None: on_failure() return 'did that work? I timed out for a moment there'
def listbans(self, message): '''List currently-banned users''' id = 'banlist'+randomstr() query = SweetieAdmin.query_element() item = ET.SubElement(query, 'item') item.set('affiliation', 'outcast') iq = self.bot.create_iq(id, 'get', query) response = iq.send() res = "" items = response.findall('.//{'+self.QUERY_NS+'}item') log.debug('banlist items: '+str(items)) for item in items: if item.get('jid') is not None: try: reason = str(item[0].text) except: reason = "[reason unavailable]" res += "\n" + item.get('jid') + ": "+reason if not res: return 'Bans list is empty' return res