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() perms = [] for j in splitted[1:]: perms.append(j.strip()) except IndexError: rlog(10, 'plugins', "permoverload: can't set perms of %s" \ % i) continue if not funcname: rlog(10, 'plugins', "permoverload: no function provided") continue if not perms: rlog(10, 'plugins', "permoverload: no permissions \ provided for %s" % funcname) continue self.overloads[funcname] = perms 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 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 regplugins(self): """ register plugins. """ self.regcore() avail = [] # check for myplugs directory if os.path.isdir('myplugs'): avail.extend(self.regdir('myplugs')) for i in os.listdir('myplugs'): if i.startswith('.'): continue if os.path.isdir('myplugs' + os.sep + i): avail.extend(self.regdir('myplugs' + os.sep + i)) else: rlog(10, 'plugins', 'no myplugs directory found') # check for gozerplugs package try: plugs = gozer_import('gozerplugs') except ImportError: 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)
def handle(self, descr, func, bot, ievent, *args, **kwargs): """ schedule a job. """ self.working = True try: name = getname(str(func)) stats.up("runners", name) stats.up("runners", bot.name) rlog( 4, "runner", "%s (%s) running %s: %s at speed %s" % (ievent.nick, ievent.userhost, descr, str(func), ievent.speed), ) self.starttime = time.time() func(bot, ievent, *args, **kwargs) for queue in ievent.queues: queue.put_nowait(None) self.finished = time.time() self.elapsed = self.finished - self.starttime if self.elapsed > 3: rlog( 10, "runner", "ALERT %s %s job taking too long: %s \ seconds" % (descr, str(func), self.elapsed), ) except Exception, ex: handle_exception(ievent)
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() perms = [] for j in splitted[1:]: perms.append(j.strip()) except IndexError: rlog(10, 'plugins', "permoverload: can't set perms of %s" \ % i) continue if not funcname: rlog(10, 'plugins', "permoverload: no function provided") continue if not perms: rlog( 10, 'plugins', "permoverload: no permissions \ provided for %s" % funcname) continue self.overloads[funcname] = perms 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 resume(self, sessionfile): """ resume from session file. """ session = load(open(sessionfile)) try: reto = session['channel'] self._doresume(session, reto) except Exception, ex: handle_exception()
def save(self): """ call registered plugins save. """ for plug in self.plugs.values(): try: plug.save() except AttributeError: pass except Exception, ex: handle_exception()
def unloadnosave(self, plugname): """ unload plugin without saving. """ # call shutdown function try: self.plugs[plugname].shutdown() rlog(10, 'plugins', '%s shutdown called' % plugname) except (AttributeError, KeyError): pass except Exception, ex: handle_exception()
def save_cfgname(self, name): """ save persisted plugin config data. """ try: plug = self.plugs[name] cfg = getattr(plug, 'cfg') if isinstance(cfg, PersistConfig): try: cfg.save() except: handle_exception() except (AttributeError, KeyError): pass
def save_cfg(self): """ call registered plugins configuration save. """ for plug in self.plugs.values(): try: cfg = getattr(plug, 'cfg') if isinstance(cfg, PersistConfig): try: cfg.save() except: handle_exception() except AttributeError: continue
def regcore(self): """ register core plugins. """ self.plugdeny.init([]) self.plugallow.init([]) avail = [] plugs = gozer_import('gozerbot.plugs') for i in plugs.__plugs__: if i not in avail: try: self.regplugin('gozerbot.plugs', i) except Exception, ex: handle_exception() avail.append(i)
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 regdir(self, dirname, exclude=[]): """ register a directory. """ threads = [] plugs = [] for plug in plugnames(dirname): if plug in exclude or plug.startswith('.'): continue try: self.regplugin(dirname, plug) plugs.append(plug) except: handle_exception() self.ondisk.extend(plugs) return plugs
def dispatch(self, callback, bot, ievent): """ dispatch callback on ircevent. """ try: result = re.search(callback.compiled, ievent.txt.strip()) if result: ievent.groups = list(result.groups()) if callback.threaded: thr.start_bot_command(callback.func, (bot, ievent)) else: cmndrunners.put(callback.plugname, callback.func, bot, \ ievent) return 1 except Exception, ex: handle_exception(ievent)
def check(self): """ run check to see if job needs to be scheduled. """ if self.next <= time.time(): rlog(-15, 'periodical', 'running %s' % (str(self.func))) self.next = time.time() + self.interval # try the callback try: self.func(*self.args, **self.kw) except Exception, ex: handle_exception() self.counts += 1 if self.repeat > 0 and self.counts >= self.repeat: return False # remove this job
def handle(self, bot, event): """ fire jabber monitor callbacks. """ msg = unicode(event) try: if msg.startswith('<presence'): jmsg = Jabberpresence(msg) else: jmsg = Jabbermsg(msg) jmsg.toirc(bot) jmsg.botoutput = True Monitor.handle(self, bot, jmsg) except AttributeError: if config['debug']: handle_exception() return
def handle(self, descr, func, *args, **kwargs): """ schedule a job. """ self.working = True try: name = getname(str(func)) stats.up('runners', name) rlog(4, 'runner', 'running %s: %s' % (descr, name)) self.starttime = time.time() func(*args, **kwargs) self.finished = time.time() self.elapsed = self.finished - self.starttime if self.elapsed > 3: rlog( 10, 'runner', 'ALERT %s %s job taking too long: %s \ seconds' % (descr, str(func), self.elapsed)) except Exception, ex: handle_exception()
def dispatch(self, queue): """ dispatch functions from provided queue. """ try: todo = queue.get_nowait() except Queue.Empty: return try: (func, args, kwargs) = todo func(*args, **kwargs) except ValueError: try: (func, args) = todo func(*args) except ValueError: (func, ) = todo func() except: handle_exception()
def handle(self, descr, func, bot, ievent, *args, **kwargs): """ schedule a job. """ self.working = True try: name = getname(str(func)) stats.up('runners', name) stats.up('runners', bot.name) rlog(4, 'runner', '%s (%s) running %s: %s at speed %s' % \ (ievent.nick, ievent.userhost, descr, str(func), ievent.speed)) self.starttime = time.time() func(bot, ievent, *args, **kwargs) for queue in ievent.queues: queue.put_nowait(None) self.finished = time.time() self.elapsed = self.finished - self.starttime if self.elapsed > 3: rlog( 10, 'runner', 'ALERT %s %s job taking too long: %s \ seconds' % (descr, str(func), self.elapsed)) except Exception, ex: handle_exception(ievent)
def handle(self, descr, func, *args, **kwargs): """ schedule a job. """ self.working = True try: name = getname(str(func)) stats.up("runners", name) rlog(4, "runner", "running %s: %s" % (descr, name)) self.starttime = time.time() func(*args, **kwargs) self.finished = time.time() self.elapsed = self.finished - self.starttime if self.elapsed > 3: rlog( 10, "runner", "ALERT %s %s job taking too long: %s \ seconds" % (descr, str(func), self.elapsed), ) except Exception, ex: handle_exception()
self.initcalled.remove(name) except ValueError: pass except AttributeError: continue except Exception, ex: rlog(10, 'plugins', 'error shutting down %s: %s' % (name, str(ex))) # join shutdown threads try: for name, thread in threadlist: thread.join() rlog(10, 'plugins', '%s shutdown finished' % name) except: handle_exception() return def getoptions(self, command): """ return options entry of a command. """ return cmnds.getoptions(command) def getdepend(self, plugname): """ get plugins the plugin depends on. """ # try to import the plugin if plugname in self.plugs: plug = self.plugs[plugname] else: for mod in ['gozerbot.plugs', 'gozerplugs', 'myplugs']: try:
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))) # join shutdown threads try: for name, thread in threadlist: thread.join() rlog(10, 'plugins', '%s shutdown finished' % name) except: handle_exception() return def getoptions(self, command): """ return options entry of a command. """ return cmnds.getoptions(command) def getdepend(self, plugname): """ get plugins the plugin depends on. """ # try to import the plugin if plugname in self.plugs: plug = self.plugs[plugname] else: for mod in ['gozerbot.plugs', 'gozerplugs', 'myplugs']: