def callback(self, cb, bot, ievent): """ callback cb with bot and ievent as arguments """ try: # see if the callback pre requirement succeeds if cb.prereq: rlog(-10, 'callback', 'excecuting in loop %s' % str(cb.prereq)) if not cb.prereq(bot, ievent): return # check if callback function is there if not cb.func: return # log and stats rlog(0, 'callback', 'excecuting callback %s' % str(cb.func)) stats.up('callbacks', getname(cb.func)) stats.up('callbacks', cb.plugname) # launcn the callback .. either threaded or dispatched at runners if cb.threaded: start_new_thread(cb.func, (bot, ievent), cb.kwargs) else: cbrunners[10 - cb.speed].put("cb-%s" % cb.plugname, cb.func, bot, ievent, **cb.kwargs) except Exception, ex: handle_exception()
def callback(self, cb, bot, ievent): """ callback cb with bot and ievent as arguments """ try: # see if the callback pre requirement succeeds if cb.prereq: rlog(-10, "callback", "excecuting in loop %s" % str(cb.prereq)) if not cb.prereq(bot, ievent): return # check if callback function is there if not cb.func: return # log and stats rlog(0, "callback", "excecuting callback %s" % str(cb.func)) stats.up("callbacks", getname(cb.func)) stats.up("callbacks", cb.plugname) # launcn the callback .. either threaded or dispatched at runners if cb.threaded: start_new_thread(cb.func, (bot, ievent), cb.kwargs) else: cbrunners[10 - cb.speed].put("cb-%s" % cb.plugname, cb.func, bot, ievent, **cb.kwargs) except Exception, ex: handle_exception()
def handle(self, *args, **kwargs): """ fire monitor callbacks. """ for i in self.outs: # check if precondition is met try: if i[2]: stats.up('monitors', thr.getname(str(i[2]))) rlog(-10, 'jabbermonitor', 'checking inloop %s' % str(i[2])) doit = i[2](*args, **kwargs) else: doit = 1 except Exception, ex: handle_exception() doit = 0 if doit: # run monitor callback in its own thread rlog(0, 'jabbermonitor', 'excecuting jabbermonitor callback \ %s' % i[0]) stats.up('monitors', thr.getname(str(i[1]))) if not i[3]: cbrunners[5].put("monitor-%s" % i[0], i[1], *args) else: thr.start_new_thread(i[1], args, kwargs)
def start(self): """ start the eventhandler thread. """ self.stopped = False if not self.running: start_new_thread(self.handleloop, ()) self.running = True
def dispatch(self, com, txt): """ dispatch command. """ if com.threaded: start_new_thread(com.func, (txt, )) else: cmndrunners[10 - com.speed].put(com.name, com.func, txt) return 1
def dispatch(self, com, txt): """ dispatch command. """ if com.threaded: start_new_thread(com.func, (txt, )) else: cmndrunners[10-com.speed].put(com.name, com.func, txt) return 1
def checkloop(self): """ main loop of the periodical scheduler. """ while self.run: for job in self.jobs: if job.next <= time.time(): thr.start_new_thread(self.runjob, (job, )) time.sleep(self.SLEEPTIME)
def register(self, catch, queue, timeout=15): """ register wait for privmsg. """ rlog(1, 'jabberwait', 'registering for %s' % catch) self.ticket += 1 self.waitlist.append((catch, queue, self.ticket)) if timeout: thr.start_new_thread(self.dotimeout, (timeout, self.ticket)) return self.ticket
def register(self, cmnd, catch, queue, timeout=15): """ register wait for cmnd. """ rlog(1, 'wait', 'registering for cmnd ' + cmnd) self.ticket += 1 self.waitlist.insert(0, (cmnd, catch, queue, self.ticket)) if timeout: # start timeout thread thr.start_new_thread(self.dotimeout, (timeout, self.ticket)) return self.ticket
def start(self, botlist=[], enable=False): """ startup the bots. """ # scan the fleet datadir for bots dirs = [] got = [] for bot in botlist: dirs.append(self.datadir + os.sep + bot) if not dirs: dirs = glob.glob(self.datadir + os.sep + "*") for fleetdir in dirs: if fleetdir.endswith('fleet'): continue rlog(10, 'fleet', 'found bot: ' + fleetdir) cfg = Config(fleetdir, 'config') if not cfg: rlog(10, 'fleet', "can't read %s config file" % fleetdir) continue name = fleetdir.split(os.sep)[-1] if not name: rlog(10, 'fleet', "can't read botname from %s config file" % \ fleetdir) continue if not enable and not cfg['enable']: rlog(10, 'fleet', '%s bot is disabled' % name) continue else: rlog(10, 'fleet', '%s bot is enabled' % name) if not name in fleetdir: rlog(10, 'fleet', 'bot name in config file doesnt match dir name') continue bot = self.makebot(name, cfg) if bot: start_new_thread(bot.connectwithjoin, ()) self.addbot(bot) got.append(bot) return got # set startok event self.startok.set()
def dispatch(self, callback, txt): """ dispatch callback on txt. """ try: result = re.search(callback.compiled, txt) if result: if callback.threaded: thr.start_new_thread(callback.func, (txt, result.groups())) else: cmndrunners.put(callback.plugname, callback.func, txt, \ result.groups()) return 1 except Exception, ex: handle_exception()
def cmndall(self, event, cmnd): """ do a command on all bots. """ threads = [] for i in self.bots: thread = start_new_thread(self.cmnd, (event, i.name, cmnd)) threads.append(thread) for i in threads: i.join(10)
def resume(self, sessionfile): """ resume bot from session file. """ # read JSON session file session = load(open(sessionfile)) # resume bots in session file for name in session['bots'].keys(): reto = None if name == session['name']: reto = session['channel'] start_new_thread(self.resumebot, (name, session['bots'][name], reto)) # allow 5 seconds for bots to resurrect time.sleep(5) # set start event self.startok.set()
def cmnd(self, event, name, cmnd): """ do command on a bot by name. """ name = name.lower() bot = self.byname(name) if not bot: return 0 from gozerbot.eventbase import EventBase j = EventBase(event) j.txt = cmnd q = Queue.Queue() j.queues = [q] j.speed = 3 start_new_thread(plugins.trydispatch, (bot, j)) result = waitforqueue(q) if not result: return res = ["[%s]" % bot.name, ] res += result event.reply(res)
def cmnd(self, event, name, cmnd): """ do command on a bot by name. """ name = name.lower() bot = self.byname(name) if not bot: return 0 from gozerbot.eventbase import EventBase j = EventBase(event) j.txt = cmnd q = Queue.Queue() j.queues = [q] j.speed = 3 start_new_thread(plugins.trydispatch, (bot, j)) result = waitforqueue(q) if not result: return res = [ "[%s]" % bot.name, ] res += result event.reply(res)
def resumebot(self, botname, data={}, printto=None): """ resume individual bot. """ # see if we need to exit the old bot oldbot = self.byname(botname) if oldbot: oldbot.exit() # recreate config file of the bot cfg = Config(datadir + os.sep + 'fleet' + os.sep + botname, 'config') # make the bot and resume (IRC) or reconnect (Jabber) bot = self.makebot(botname, cfg) if bot: if oldbot: self.replace(oldbot, bot) else: self.bots.append(bot) if not bot.jabber: bot._resume(data, printto) else: start_new_thread(bot.connectwithjoin, ())
def exit(self): """ call registered plugins save. """ self.save() threadlist = [] # call shutdown on all plugins for name, plug in self.plugs.iteritems(): try: shutdown = getattr(plug, 'shutdown') thread = start_new_thread(shutdown, ()) threadlist.append((name, thread)) try: self.initcalled.remove(name) except ValueError: pass except AttributeError: continue except Exception, ex: rlog(10, 'plugins', 'error shutting down %s: %s' % (name, str(ex)))
rlog(20, 'plugins', "no gozerplugs package found") plugs = None if plugs: for i in plugs.__plugs__: if i not in avail: try: self.regplugin('gozerplugs', i) avail.append(i) except Exception, ex: handle_exception() else: rlog(10, 'plugins', '%s already loaded' % i) self.ondisk.extend(avail) self.readoverload() start_new_thread(self.showregistered, ()) def readoverload(self): """ see if there is a permoverload file and if so use it to overload permissions based on function name. """ try: overloadfile = open(datadir + os.sep + 'permoverload', 'r') except IOError: return try: for i in overloadfile: i = i.strip() splitted = i.split(',') try: funcname = splitted[0].strip()
def start(self): """ start the periodical scheduler. """ thr.start_new_thread(self.checkloop, ())
rlog(20, 'plugins', "no gozerplugs package found") plugs = None if plugs: for i in plugs.__plugs__: if i not in avail: try: self.regplugin('gozerplugs', i) avail.append(i) except Exception, ex: handle_exception() else: rlog(10, 'plugins', '%s already loaded' % i) self.ondisk.extend(avail) self.readoverload() start_new_thread(self.showregistered, ()) def readoverload(self): """ see if there is a permoverload file and if so use it to overload permissions based on function name. """ try: overloadfile = open(datadir + os.sep + 'permoverload', 'r') except IOError: return try: for i in overloadfile: i = i.strip() splitted = i.split(',') try: