+ for cmd, func in self.hooks.items():
+ parent.unhook(cmd, func)
+ parent.unhook("%s.%s" % (self.name, cmd), func)
+ for num, func in self.numhooks.items():
+ parent.unhooknum(num, func)
+ for chan, func in self.chanhooks.items():
+ parent.unhookchan(chan, func)
+ for exc, func in self.exceptionhooks:
+ parent.unhookexception(exc, func)
+
+ for func, args, kwargs in self.helps:
+ try:
+ self.mod('help').dereghelp(func, *args, **kwargs)
+ except:
+ pass
+ return True
+
+ def hookexception(self, exc):
+ def realhook(func):
+ self.exceptionhooks.append((exc, func))
+ if self.parent is not None:
+ self.parent.hookexception(exc, func)
+ return func
+ return realhook
+
+ def hooknum(self, num):
+ def realhook(func):
+ self.numhooks[str(num)] = func
+ if self.parent is not None:
+ self.parent.hooknum(str(num), func)
+ return func
+ return realhook
+
+ def hookchan(self, chan, glevel=ANYONE, clevel=PUBLIC):
+ def realhook(func):
+ self.chanhooks[chan] = func
+ if self.parent is not None:
+ self.parent.hookchan(chan, func)
+ return func
+ return realhook
+
+ def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None):
+ if wantchan is None: wantchan = needchan
+ _cmd = cmd #save this since it gets wiped out...
+ def realhook(func):
+ cmd = _cmd #...and restore it
+ if cmd is None:
+ cmd = func.__name__ # default to function name
+ if isinstance(cmd, stringbase):
+ cmd = (cmd,)
+
+ if clevel > self.PUBLIC and not needchan:
+ raise Exception('clevel must be left at default if needchan is False')
+
+ func.needchan = needchan
+ func.wantchan = wantchan
+ func.reqglevel = glevel
+ func.reqclevel = clevel
+ func.cmd = cmd
+ func.module = func.__module__.split('.')[1]
+
+ for c in cmd:
+ self.hooks[c] = func
+ if self.parent is not None:
+ self.parent.hook(c, func)
+ self.parent.hook("%s.%s" % (self.name, c), func)
+ return func
+ return realhook
+
+ def mod(self, modname):
+ if self.parent is not None:
+ return self.parent.module(modname)
+ else:
+ return error('unknown parent')
+
+ def argsEQ(self, num):
+ def realhook(func):
+ @wraps(func)
+ def checkargs(bot, user, chan, realtarget, *args):
+ if len(args) == num:
+ return func(bot, user, chan, realtarget, *args)
+ else:
+ bot.msg(user, self.WRONGARGS)
+ return checkargs
+ return realhook
+
+ def argsGE(self, num):
+ def realhook(func):
+ @wraps(func)
+ def checkargs(bot, user, chan, realtarget, *args):
+ if len(args) >= num:
+ return func(bot, user, chan, realtarget, *args)
+ else:
+ bot.msg(user, self.WRONGARGS)
+ return checkargs
+ return realhook