Allow callable capabilities.

This commit is contained in:
Jeremy Fincher 2004-09-17 03:33:04 +00:00
parent 7f9a72541b
commit 90d30f6bf1

View File

@ -101,14 +101,19 @@ def getArgs(args, required=1, optional=0):
return ret return ret
def checkCapability(f, capability): def checkCapability(f, capability):
"""Makes sure a user has a certain capability before a command will run.""" """Makes sure a user has a certain capability before a command will run.
capability can be either a string or a callable object which will be called
in order to produce a string for ircdb.checkCapability."""
def newf(self, irc, msg, args): def newf(self, irc, msg, args):
if ircdb.checkCapability(msg.prefix, capability): cap = capability
if callable(cap):
cap = cap()
if ircdb.checkCapability(msg.prefix, cap):
f(self, irc, msg, args) f(self, irc, msg, args)
else: else:
self.log.warning('%r attempted %s without %s.', self.log.info('%s attempted %s without %s.',
msg.prefix, f.func_name, capability) msg.prefix, f.func_name, cap)
irc.errorNoCapability(capability) irc.errorNoCapability(cap)
return utils.changeFunctionName(newf, f.func_name, f.__doc__) return utils.changeFunctionName(newf, f.func_name, f.__doc__)
def checkChannelCapability(f, capability): def checkChannelCapability(f, capability):
@ -118,14 +123,17 @@ def checkChannelCapability(f, capability):
""" """
def newf(self, irc, msg, args, *L, **kwargs): def newf(self, irc, msg, args, *L, **kwargs):
channel = getChannel(msg, args) channel = getChannel(msg, args)
chancap = ircdb.makeChannelCapability(channel, capability) cap = capability
if callable(cap):
cap = cap()
chancap = ircdb.makeChannelCapability(channel, cap)
if ircdb.checkCapability(msg.prefix, chancap): if ircdb.checkCapability(msg.prefix, chancap):
L += (channel,) L += (channel,)
ff = types.MethodType(f, self, self.__class__) ff = types.MethodType(f, self, self.__class__)
ff(irc, msg, args, *L, **kwargs) ff(irc, msg, args, *L, **kwargs)
else: else:
self.log.warning('%r attempted %s without %s.', self.log.info('%s attempted %s without %s.',
msg.prefix, f.func_name, capability) msg.prefix, f.func_name, chancap)
irc.errorNoCapability(chancap) irc.errorNoCapability(chancap)
return utils.changeFunctionName(newf, f.func_name, f.__doc__) return utils.changeFunctionName(newf, f.func_name, f.__doc__)