def callback(self, cb, bot, event): """ do the actual callback with provided bot and event as arguments. """ if event.stop: logging.info("callbacks - event is stopped.") ; return event.calledfrom = cb.modname try: if event.status == "done": logging.debug("callback - event is done .. ignoring") return if event.chan and cb.plugname in event.chan.data.denyplug: logging.warn("%s denied in %s - %s" % (cb.modname, event.channel, event.auth)) return if cb.prereq: logging.debug('callbacks - executing in loop %s' % str(cb.prereq)) if not cb.prereq(bot, event): return if not cb.func: return if event.isremote(): logging.info('%s - executing REMOTE %s - %s' % (bot.name, getname(cb.func), event.cbtype)) elif event.cbtype == "TICK": logging.debug('LOCAL - %s - executing %s - %s' % (bot.name, getname(cb.func), event.cbtype)) else: logging.info('%s - executing %s - %s' % (bot.name, getname(cb.func), event.cbtype)) event.iscallback = True logging.debug("callback - %s - trail - %s" % (getname(cb.func), callstack(sys._getframe())[::-1])) #if not event.direct and cb.threaded and not bot.isgae: start_new_thread(cb.func, (bot, event)) if cb.threaded and not bot.isgae: start_new_thread(cb.func, (bot, event)) else: if bot.isgae or event.direct: cb.func(bot, event) else: from runner import callbackrunner callbackrunner.put(cb.modname, cb.func, bot, event) return True except Exception, ex: handle_exception()
def startall(self, bots=None, usethreads=True): target = bots or self.bots for bot in target: logging.info('starting %s bot (%s)' % (bot.cfg.name, bot.type)) if bot.type == "console": continue if usethreads: start_new_thread(bot.reconnect, (True,)) ; continue try: bot.reconnect(True) except Excepton, ex: handle_exception() time.sleep(3)
def resume(self, sessionfile): """ resume bot from session file. """ session = json.load(open(sessionfile)) for name in session['bots'].keys(): reto = None if session['name'] == name: reto = session['channel'] start_new_thread(self.resumebot, (name, session['bots'][name], reto)) time.sleep(5) self.startok.set()
def boot(self, botnames=[], exclude=[], all=False): if not botnames and all: botnames = self.getenabled(exclude) bots = self.loadall(botnames) todo = [] done = [] for bot in bots: if not bot: continue logging.debug("%s bot type is %s" % (bot.cfg.name, bot.type)) if bot.type not in exclude: todo.append(bot) ; done.append(bot.cfg.name) if todo: start_new_thread(self.startall, (todo, )) if done: logging.warn("fleet bots are %s" % ", ".join(done)) else: logging.warn("no fleet bots are enabled, see %s" % getdatadir() + os.sep + "config" + os.sep +'fleet')
def joinchannels(self): """ join channels. """ time.sleep(Config().waitforjoin or 5) for i in self.state['joinedchannels']: try: logging.debug("%s - joining %s" % (self.name, i)) channel = ChannelBase(i) if channel: key = channel.getpass() else: key=None start_new_thread(self.join, (i, key)) time.sleep(1) except Exception, ex: logging.warn('%s - failed to join %s: %s' % (self.name, i, str(ex))) handle_exception()
def joinchannels(self): """ join channels. """ time.sleep(Config().waitforjoin or 5) for i in self.state['joinedchannels']: try: logging.debug("%s - joining %s" % (self.name, i)) channel = ChannelBase(i) if channel: key = channel.getpass() else: key = None start_new_thread(self.join, (i, key)) time.sleep(1) except Exception, ex: logging.warn('%s - failed to join %s: %s' % (self.name, i, str(ex))) handle_exception()
def resumebot(self, botname, data={}, printto=None): """ resume single bot. """ logging.warn("fleet - resuming %s bot" % botname) # see if we need to exit the old bot if data['type'] == "console": logging.warn("not resuming console bot %s" % botname) ; return oldbot = self.byname(botname) if oldbot and data['type'] == "sxmpp": oldbot.exit() cfg = Config('fleet' + os.sep + stripname(botname) + os.sep + 'config') logging.warn("fleet - resuming %s bot - %s - %s" % (botname, str(data), data['type'])) bot = self.makebot(data['type'], botname) if data['type'] != "sxmpp": if oldbot: self.replace(oldbot, bot) bot._resume(data, printto) bot.start(False) else: start_new_thread(bot.start, ())
def resume(self, sessionfile, exclude=[]): """ resume bot from session file. """ session = json.load(open(sessionfile, 'r')) chan = session.get("channel") for name, cfg in session['bots'].iteritems(): dont = False for ex in exclude: if ex in name: dont = True if dont: continue cfg = LazyDict(cfg) try: if not cfg.disable: logging.info("resuming %s" % cfg) start_new_thread(self.resumebot, (cfg, chan)) except: handle_exception() ; return time.sleep(10) self.startok.set()
def callback(self, cb, bot, event): """ do the actual callback with provided bot and event as arguments. """ #if event.stop: logging.info("callbacks - event is stopped.") ; return if event.cbtype in bot.nocbs and not cb.force: logging.warn("%s in nocbs list, skipping" % event.cbtype) ; return event.calledfrom = cb.modname if not event.bonded: event.bind(bot) try: if event.status == "done": if not event.nolog: logging.debug("callback - event is done .. ignoring") stats.upitem("ignored") return if event.chan and cb.plugname in event.chan.data.denyplug: logging.warn("%s denied in %s - %s" % (cb.modname, event.channel, event.auth)) stats.upitem("denied") return if cb.prereq: if not event.nolog: logging.info('executing in loop %s' % str(cb.prereq)) if not cb.prereq(bot, event): return if not cb.func: return if event.isremote(): logging.info('%s - executing REMOTE %s - %s' % (bot.cfg.name, getname(cb.func), event.cbtype)) elif not event.nolog: logging.info('%s - executing %s - %s' % (bot.cfg.name, getname(cb.func), event.cbtype)) event.iscallback = True if not event.nolog: logging.debug("%s - %s - trail - %s" % (bot.cfg.name, getname(cb.func), callstack(sys._getframe())[::-1])) time.sleep(0.01) if cb.threaded: logging.info("PURE THREAD STARTED %s" % str(cb.func)) ; start_new_thread(cb.func, (bot, event)) else: display = "%s/%s/%s" % (cb.plugname, bot.cfg.name, event.nick or event.channel) if event.cbtype == "API": from runner import apirunner apirunner.put(event.speed or cb.speed, display, cb.func, bot, event) elif event.direct: cb.func(bot, event) elif not event.dolong: from runner import callbackrunner callbackrunner.put(event.speed or cb.speed, display, cb.func, bot, event) else: from runner import longrunner longrunner.put(event.speed or cb.speed, display, cb.func, bot, event) stats.upitem(event.cbtype) stats.upitem("nrcallbacks") return True except Exception, ex: handle_exception()
def callback(self, cb, bot, event): """ do the actual callback with provided bot and event as arguments. """ if event.stop: logging.info("callbacks - event is stopped.") return event.calledfrom = cb.modname try: if event.status == "done": logging.debug("callback - event is done .. ignoring") return if event.chan and cb.plugname in event.chan.data.denyplug: logging.warn("%s denied in %s - %s" % (cb.modname, event.channel, event.auth)) return if cb.prereq: logging.debug('callbacks - executing in loop %s' % str(cb.prereq)) if not cb.prereq(bot, event): return if not cb.func: return if event.isremote(): logging.info('%s - executing REMOTE %s - %s' % (bot.name, getname(cb.func), event.cbtype)) elif event.cbtype == "TICK": logging.debug('LOCAL - %s - executing %s - %s' % (bot.name, getname(cb.func), event.cbtype)) else: logging.info('%s - executing %s - %s' % (bot.name, getname(cb.func), event.cbtype)) event.iscallback = True logging.debug("callback - %s - trail - %s" % (getname(cb.func), callstack(sys._getframe())[::-1])) #if not event.direct and cb.threaded and not bot.isgae: start_new_thread(cb.func, (bot, event)) if cb.threaded and not bot.isgae: start_new_thread(cb.func, (bot, event)) else: if bot.isgae or event.direct: cb.func(bot, event) else: from runner import callbackrunner callbackrunner.put(cb.modname, cb.func, bot, event) return True except Exception, ex: handle_exception()
def joinchannels(self): """ join channels. """ time.sleep(getmainconfig().waitforjoin or 1) target = self.cfg.channels try: for i in self.state['joinedchannels']: if i not in target: target.append(i) except: pass if not target: target = self.state['joinedchannels'] for i in target: try: logging.debug("%s - joining %s" % (self.cfg.name, i)) channel = ChannelBase(i, self.cfg.name) if channel: key = channel.data.key else: key = None if channel.data.nick: self.ids.append("%s/%s" % (i, channel.data.nick)) start_new_thread(self.join, (i, key)) except Exception, ex: logging.warn('%s - failed to join %s: %s' % (self.cfg.name, i, str(ex))) handle_exception() time.sleep(3)
def startall(self, bots, usethreads=True): threads = [] target = bots or self.bots for bot in target: logging.warn('starting %s bot (%s)' % (bot.cfg.name, bot.type)) if usethreads or bot.type in ["sleek", "tornado"]: threads.append(start_new_thread(bot.boot, ())) ; continue try: bot.boot() except Excepton, ex: handle_exception() time.sleep(1) if usethreads: for t in threads: t.join(15) time.sleep(5) self.startok.set()
def start(self, connect=True): """ start the mainloop of the bot. """ if not self.isgae: if connect: self.connect() start_new_thread(self._outloop, ()) start_new_thread(self._eventloop, ()) start_new_thread(self._readloop, ()) if connect: self.connectok.wait(120) if self.connectok.isSet(): logging.warn('%s - logged on !' % self.name) start_new_thread(self.joinchannels, ()) else: logging.warn("%s - failed to logon - connectok is not set" % self.name) self.status == "running" self.dostart(self.botname, self.type)
def start(self, connect=True): """ start the mainloop of the bot. """ if not self.isgae: if connect: self.connect() start_new_thread(self._outloop, ()) start_new_thread(self._eventloop, ()) start_new_thread(self._readloop, ()) if connect: self.connectok.wait(120) if self.connectok.isSet(): logging.warn('%s - logged on !' % self.name) start_new_thread(self.joinchannels, ()) else: logging.warn( "%s - failed to logon - connectok is not set" % self.name) self.status == "running" self.dostart(self.botname, self.type)
def exit(self, name=None, jabber=False): """ call exit on all bots. """ if not name: threads = [] for bot in self.bots: if jabber and bot.type != 'sxmpp' and bot.type != 'jabber': continue threads.append(start_new_thread(bot.exit, ())) for thread in threads: thread.join() return for bot in self.bots: if bot.cfg.name == name: if jabber and bot.type != 'sxmpp' and bot.type != 'jabber': continue try: bot.exit() except: handle_exception() self.remove(bot) return True return False
def loadall(self, names=[]): """ load all bots. """ target = names or self.data.names if not target: logging.error("fleet - no bots in fleet") ; return else: logging.warning("fleet - loading %s" % ", ".join(target)) threads = [] for name in target: if not name: logging.debug("fleet - name is not set") ; continue try: if self.data.types[name] == "console": logging.warn("fleet- skipping console bot %s" % name) ; continue except KeyError: continue try: import waveapi ; self.makebot(self.data.types[name], name) except ImportError: try: threads.append(start_new_thread(self.makebot, (self.data.types[name], name))) except: handle_exception() ; continue except BotNotEnabled: pass except KeyError: logging.error("no type know for %s bot" % name) except Exception, ex: handle_exception() for t in threads: t.join()
def start(self, connect=True, join=True): """ start the mainloop of the bot. """ if self.started: logging.warn("%s - already started" % self.cfg.name) ; return self.started = True self.status == "running" if not self.isgae: if connect: try: if not self.connect(): return False except Exception, ex: logging.error("%s - can't connect: %s" % (self.cfg.name, str(ex))) ; return False start_new_thread(self._outloop, ()) start_new_thread(self._eventloop, ()) start_new_thread(self._readloop, ()) if connect: self.connectok.wait(120) if self.stopped: logging.warn("bot is stopped") ; return if self.connectok.isSet(): logging.warn('%s - logged on !' % self.cfg.name) if join: start_new_thread(self.joinchannels, ()) elif self.type not in ["console", "base"]: logging.warn("%s - failed to logon - connectok is not set" % self.cfg.name) self.status == "running" self.dostart(self.cfg.name, self.type) return True
def start(self): """ start the thread. """ if not self.running and not self.stopped: self.running = True if not self in threadloops: threadloops.append(self) return start_new_thread(self._loop, ())
def callback(self, cb, bot, event): """ do the actual callback with provided bot and event as arguments. """ #if event.stop: logging.info("callbacks - event is stopped.") ; return if event.cbtype in bot.nocbs and not cb.force: logging.warn("%s in nocbs list, skipping" % event.cbtype) return event.calledfrom = cb.modname if not event.bonded: event.bind(bot) try: if event.status == "done": if not event.nolog: logging.debug("callback - event is done .. ignoring") stats.upitem("ignored") return if event.chan and cb.plugname in event.chan.data.denyplug: logging.warn("%s denied in %s - %s" % (cb.modname, event.channel, event.auth)) stats.upitem("denied") return if cb.prereq: if not event.nolog: logging.info('executing in loop %s' % str(cb.prereq)) if not cb.prereq(bot, event): return if not cb.func: return if event.isremote(): logging.info('%s - executing REMOTE %s - %s' % (bot.cfg.name, getname(cb.func), event.cbtype)) elif not event.nolog: logging.info('%s - executing %s - %s' % (bot.cfg.name, getname(cb.func), event.cbtype)) event.iscallback = True if not event.nolog: logging.debug("%s - %s - trail - %s" % (bot.cfg.name, getname( cb.func), callstack(sys._getframe())[::-1])) time.sleep(0.01) if cb.threaded: logging.info("PURE THREAD STARTED %s" % str(cb.func)) start_new_thread(cb.func, (bot, event)) else: display = "%s/%s/%s" % (cb.plugname, bot.cfg.name, event.nick or event.channel) if event.cbtype == "API": from runner import apirunner apirunner.put(event.speed or cb.speed, display, cb.func, bot, event) elif event.direct: cb.func(bot, event) elif not event.dolong: from runner import callbackrunner callbackrunner.put(event.speed or cb.speed, display, cb.func, bot, event) else: from runner import longrunner longrunner.put(event.speed or cb.speed, display, cb.func, bot, event) stats.upitem(event.cbtype) stats.upitem("nrcallbacks") return True except Exception, ex: handle_exception()
def start(self): """ start the thread. """ if not self.running and not self.stopped: start_new_thread(self._loop, ())
def start(self, connect=True, join=True): """ start the mainloop of the bot. """ if self.started: logging.warn("%s - already started" % self.cfg.name) ; return tickloop.start(self) self.stopped = False self.stopreadloop = False self.stopoutloop = False self.stopeventloop = False self.status = "start" if not self.eventlooprunning: start_new_thread(self._eventloop, ()) start_new_thread(self._outloop, ()) if connect: if not self.connect() : return False start_new_thread(self._readloop, ()) self.connectok.wait() if self.stopped: logging.warn("bot is stopped") ; return True if self.connectok.isSet(): logging.warn('%s - logged on !' % self.cfg.name) if join: start_new_thread(self.joinchannels, ()) if self.type in ["sxmpp", "xmpp", "sleek"]: start_new_thread(self._keepalive, ()) if self.cfg.keepchannelsalive: start_new_thread(self._keepchannelsalive, ()) elif self.type not in ["console", "base"]: logging.warn("%s - failed to logon - connectok is not set" % self.cfg.name) fleet = getfleet() fleet.addbot(self) self.status == "started" self.started = True self.dostart(self.cfg.name, self.type) return True
def start(self): """ start the eventhandler thread. """ self.stopped = False if not self.running: start_new_thread(self.handleloop, ()) self.running = True
def startall(self): for bot in self.bots: start_new_thread(bot.start, ())
def start(self): """ start the thread. """ if not self.running and not self.stopped: return start_new_thread(self._loop, ())
def start(self, connect=True, join=True): """ start the mainloop of the bot. """ if self.started: logging.warn("%s - already started" % self.cfg.name) return tickloop.start(self) self.stopped = False self.stopreadloop = False self.stopoutloop = False self.stopeventloop = False self.status = "start" if not self.eventlooprunning: start_new_thread(self._eventloop, ()) start_new_thread(self._outloop, ()) if connect: if not self.connect(): return False start_new_thread(self._readloop, ()) self.connectok.wait() if self.stopped: logging.warn("bot is stopped") return True if self.connectok.isSet(): logging.warn('%s - logged on !' % self.cfg.name) if join: start_new_thread(self.joinchannels, ()) if self.type in ["sxmpp", "xmpp", "sleek"]: start_new_thread(self._keepalive, ()) if self.cfg.keepchannelsalive: start_new_thread(self._keepchannelsalive, ()) elif self.type not in ["console", "base"]: logging.warn("%s - failed to logon - connectok is not set" % self.cfg.name) fleet = getfleet() fleet.addbot(self) self.status == "started" self.started = True self.dostart(self.cfg.name, self.type) return True