def whereis(self, cmnd): """ return plugin name in which command is implemented. """ logging.warn("looking for %s" % cmnd) try: cmndlist = getshorttable()[cmnd] if len(cmndlist) == 1: cmnd = cmndlist[0] except KeyError: pass try: return getcmndtable()[cmnd] except KeyError: return ""
def reloadcheck(self, bot, event, target=None): """ check if event requires a plugin to be reloaded. if so reload the plugin. """ from boot import getcmndtable from boot import plugblacklist plugloaded = None plugin = None try: if not target: target = event.iscmnd().split()[0] if not target: target = evemt.txt.split()[0] except Exception, ex: target = None if not target: logging.debug("can't find target in %s" % event.txt) ; return from jsb.lib.aliases import getaliases aliases = getaliases() try: target = aliases[target] except KeyError: try: target = event.chan.data.aliases[target] except (AttributeError, KeyError, TypeError): pass cmndtable = getcmndtable() if not cmndtable.has_key(target): try: short = getshorttable() if short.has_key(target): cmndlist = short[target] if len(cmndlist) == 1: target = cmndlist[0] except Exception, ex: handle_exception() if target: target = target.split()[0] logging.info("checking for reload of %s" % target) try: plugin = cmndtable[target] except KeyError: logging.warn("no cmnd for %s .. trying REGEX" % target) try: retable = getretable() for regex, mod in retable.iteritems(): if re.search(regex, event.stripcc() or event.txt): plugin = mod ; break except Exception, ex: handle_exception() logging.info("plugin is %s" % plugin) if not plugin: logging.debug("can't find plugin to reload for %s" % target) ; return if plugin in bot.plugs: logging.info("%s already loaded" % plugin) ; return plugloaded elif plugin in plugblacklist.data: return plugloaded elif bot.cfg.loadlist and plugin not in bot.cfg.loadlist: logging.warn("plugin %s is blacklisted" % plugin) ; return plugloaded logging.info("loaded %s on demand" % plugin) plugloaded = bot.plugs.reload(plugin) return plugloaded
def woulddispatch(self, bot, event): """ dispatch an event if cmnd exists and user is allowed to exec this command. """ event.bind(bot) aliased = False try: cmnd = event.stripcc().split()[0] if not cmnd: cmnd = event.execstr.split()[0] if not cmnd: cmnd = event.txt.split()[0] except Exception, ex: logging.warn("can't determine command from %s" % event.txt) ; return None try: a = event.chan.data.aliases[cmnd] if a: cmnd = a.split()[0] ; aliased = True except (KeyError, TypeError): try: a = getaliases()[cmnd] if a: cmnd = a.split()[0] ; aliased = True except (KeyError, TypeError): if not self.has_key(cmnd): try: short = getshorttable() if short.has_key(cmnd): cmndlist = short[cmnd] if len(cmndlist) == 1: cmnd = cmndlist[0] else: event.reply("choose one of: ", cmndlist) ; return except Exception, ex: handle_exception() logging.info("trying for %s" % cmnd) result = None try: result = self[cmnd] except KeyError: pass logging.debug("woulddispatch result: %s" % result) if result: event.bloh() ; event.makeargs() if aliased: event.usercmnd = cmnd return result
class Commands(LazyDict): """ the commands object holds all commands of the bot. """ regex = [] def add(self, cmnd, func, perms, threaded=False, wait=False, orig=None, how=None, speed=None, regex=False, needcc=False, *args, **kwargs): """ add a command. """ modname = calledfrom(sys._getframe()) try: prev = self[cmnd] except KeyError: prev = None target = Command(modname, cmnd, func, perms, threaded, wait, orig, how, speed=speed, needcc=needcc) if regex: logging.info("regex command detected - %s" % cmnd) self.regex.append(target) target.regex = cmnd return self self[cmnd] = target try: p = cmnd.split('-')[0] if not self.pre: self.pre = LazyDict() if self.pre.has_key(p): if not self.pre[p]: self.pre[p] = [] if prev in self.pre[p]: self.pre[p].remove(prev) if target not in self.pre[p]: self.pre[p].append(target) else: self.pre[p] = [ target, ] except IndexError: pass return self def checkre(self, bot, event): gotcc = event.gotcc() for r in self.regex: if not r.enable: continue if r.needcc and not event.gotcc(): logging.debug("RE needs cc but not found") continue s = re.search(r.cmnd, event.txt) if s: logging.info("regex matches %s" % r.cmnd) event.groups = list(s.groups()) return r def wouldmatchre(self, bot, event, cmnd=""): groups = self.checkre(bot, event) if groups: return groups def woulddispatch(self, bot, event): """ dispatch an event if cmnd exists and user is allowed to exec this command. """ event.bind(bot) aliased = False try: cmnd = event.stripcc().split()[0] if not cmnd: cmnd = event.execstr.split()[0] if not cmnd: cmnd = event.txt.split()[0] except Exception, ex: logging.warn("can't determine command from %s" % event.txt) return None try: a = event.chan.data.aliases[cmnd] if a: cmnd = a.split()[0] aliased = True except (KeyError, TypeError): try: a = getaliases()[cmnd] if a: cmnd = a.split()[0] aliased = True except (KeyError, TypeError): if not self.has_key(cmnd): try: short = getshorttable() if short.has_key(cmnd): cmndlist = short[cmnd] if len(cmndlist) == 1: cmnd = cmndlist[0] else: event.reply("choose one of: ", cmndlist) return except Exception, ex: handle_exception()
if not target: logging.debug("can't find target in %s" % event.txt) return from jsb.lib.aliases import getaliases aliases = getaliases() try: target = aliases[target] except KeyError: try: target = event.chan.data.aliases[target] except (AttributeError, KeyError, TypeError): pass cmndtable = getcmndtable() if not cmndtable.has_key(target): try: short = getshorttable() if short.has_key(target): cmndlist = short[target] if len(cmndlist) == 1: target = cmndlist[0] except Exception, ex: handle_exception() if target: target = target.split()[0] logging.info("checking for reload of %s" % target) try: plugin = cmndtable[target] except KeyError: logging.warn("no cmnd for %s .. trying REGEX" % target) try: retable = getretable() for regex, mod in retable.iteritems(): if re.search(regex, event.stripcc() or event.txt):