mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-03-29 10:56:57 +01:00
add config for required capabilities to manage the regexp db, and check for them before taking action.
This commit is contained in:
parent
bb44d433f5
commit
69c7774431
@ -39,7 +39,6 @@ def configure(advanced):
|
|||||||
from supybot.questions import expect, anything, something, yn
|
from supybot.questions import expect, anything, something, yn
|
||||||
conf.registerPlugin('MessageParser', True)
|
conf.registerPlugin('MessageParser', True)
|
||||||
|
|
||||||
|
|
||||||
MessageParser = conf.registerPlugin('MessageParser')
|
MessageParser = conf.registerPlugin('MessageParser')
|
||||||
# This is where your configuration variables (if any) should go. For example:
|
# This is where your configuration variables (if any) should go. For example:
|
||||||
# conf.registerGlobalValue(MessageParser, 'someConfigVariableName',
|
# conf.registerGlobalValue(MessageParser, 'someConfigVariableName',
|
||||||
@ -57,5 +56,13 @@ conf.registerChannelValue(MessageParser, 'rankListLength',
|
|||||||
conf.registerChannelValue(MessageParser, 'requireVacuumCapability',
|
conf.registerChannelValue(MessageParser, 'requireVacuumCapability',
|
||||||
registry.String('admin', """Determines the capability required (if any) to
|
registry.String('admin', """Determines the capability required (if any) to
|
||||||
vacuum the database."""))
|
vacuum the database."""))
|
||||||
|
conf.registerChannelValue(MessageParser, 'requireManageCapability',
|
||||||
|
registry.String('admin; channel,op',
|
||||||
|
"""Determines the
|
||||||
|
capabilities required (if any) to manage the regexp database,
|
||||||
|
including add, remove, lock, unlock. Use 'channel,capab' for
|
||||||
|
channel-level capabilities.
|
||||||
|
Note that absence of an explicit anticapability means user has
|
||||||
|
capability."""))
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
@ -117,13 +117,27 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
|
|||||||
|
|
||||||
def _runCommandFunction(self, irc, msg, command):
|
def _runCommandFunction(self, irc, msg, command):
|
||||||
"""Run a command from message, as if command was sent over IRC."""
|
"""Run a command from message, as if command was sent over IRC."""
|
||||||
# need to encode it from unicode, since sqlite stores text as unicode.
|
|
||||||
tokens = callbacks.tokenize(command)
|
tokens = callbacks.tokenize(command)
|
||||||
try:
|
try:
|
||||||
self.Proxy(irc.irc, msg, tokens)
|
self.Proxy(irc.irc, msg, tokens)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
log.exception('Uncaught exception in scheduled function:')
|
log.exception('Uncaught exception in scheduled function:')
|
||||||
|
|
||||||
|
def _checkManageCapabilities(self, irc, msg, channel):
|
||||||
|
"""Check if the user has any of the required capabilities to manage
|
||||||
|
the regexp database."""
|
||||||
|
capabilities = self.registryValue('requireManageCapability')
|
||||||
|
if capabilities:
|
||||||
|
for capability in re.split(r'\s*;\s*', capabilities):
|
||||||
|
if capability.startswith('channel,'):
|
||||||
|
capability = ircdb.makeChannelCapability(channel, capability[8:])
|
||||||
|
if capability and ircdb.checkCapability(msg.prefix, capability):
|
||||||
|
#print "has capability:", capability
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
def doPrivmsg(self, irc, msg):
|
def doPrivmsg(self, irc, msg):
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
if not irc.isChannel(channel):
|
if not irc.isChannel(channel):
|
||||||
@ -157,6 +171,9 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
|
|||||||
necessary if the message isn't sent on the channel
|
necessary if the message isn't sent on the channel
|
||||||
itself. Action is echoed upon regexp match, with variables $1, $2,
|
itself. Action is echoed upon regexp match, with variables $1, $2,
|
||||||
etc. being interpolated from the regexp match groups."""
|
etc. being interpolated from the regexp match groups."""
|
||||||
|
if not self._checkManageCapabilities(irc, msg, channel):
|
||||||
|
capabilities = self.registryValue('requireManageCapability')
|
||||||
|
irc.errorNoCapability(capabilities, Raise=True)
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("SELECT id, usage_count, locked FROM triggers WHERE regexp=?", (regexp,))
|
cursor.execute("SELECT id, usage_count, locked FROM triggers WHERE regexp=?", (regexp,))
|
||||||
@ -194,6 +211,9 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
|
|||||||
the message isn't sent in the channel itself.
|
the message isn't sent in the channel itself.
|
||||||
If option --id specified, will retrieve by regexp id, not content.
|
If option --id specified, will retrieve by regexp id, not content.
|
||||||
"""
|
"""
|
||||||
|
if not self._checkManageCapabilities(irc, msg, channel):
|
||||||
|
capabilities = self.registryValue('requireManageCapability')
|
||||||
|
irc.errorNoCapability(capabilities, Raise=True)
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
target = 'regexp'
|
target = 'regexp'
|
||||||
@ -227,6 +247,9 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
|
|||||||
removed or overwritten to. <channel> is only necessary if the message isn't
|
removed or overwritten to. <channel> is only necessary if the message isn't
|
||||||
sent in the channel itself.
|
sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
|
if not self._checkManageCapabilities(irc, msg, channel):
|
||||||
|
capabilities = self.registryValue('requireManageCapability')
|
||||||
|
irc.errorNoCapability(capabilities, Raise=True)
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("SELECT id FROM triggers WHERE regexp=?", (regexp,))
|
cursor.execute("SELECT id FROM triggers WHERE regexp=?", (regexp,))
|
||||||
@ -246,6 +269,9 @@ class MessageParser(callbacks.Plugin, plugins.ChannelDBHandler):
|
|||||||
removed or overwritten. <channel> is only necessary if the message isn't
|
removed or overwritten. <channel> is only necessary if the message isn't
|
||||||
sent in the channel itself.
|
sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
|
if not self._checkManageCapabilities(irc, msg, channel):
|
||||||
|
capabilities = self.registryValue('requireManageCapability')
|
||||||
|
irc.errorNoCapability(capabilities, Raise=True)
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("SELECT id FROM triggers WHERE regexp=?", (regexp,))
|
cursor.execute("SELECT id FROM triggers WHERE regexp=?", (regexp,))
|
||||||
|
@ -52,6 +52,22 @@ class MessageParserTestCase(ChannelPluginTestCase):
|
|||||||
self.assertNotError('messageparser add "stuff" "echo i saw no stuff"') #overwrite existing regexp
|
self.assertNotError('messageparser add "stuff" "echo i saw no stuff"') #overwrite existing regexp
|
||||||
self.assertRegexp('messageparser show "stuff"', '.*i saw no stuff.*')
|
self.assertRegexp('messageparser show "stuff"', '.*i saw no stuff.*')
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
world.testing = False
|
||||||
|
origuser = self.prefix
|
||||||
|
self.prefix = 'stuff!stuff@stuff'
|
||||||
|
self.assertNotError('register nottester stuff', private=True)
|
||||||
|
|
||||||
|
self.assertError('messageparser add "aoeu" "echo vowels are nice"')
|
||||||
|
origconf = conf.supybot.plugins.MessageParser.requireManageCapability()
|
||||||
|
conf.supybot.plugins.MessageParser.requireManageCapability.setValue('')
|
||||||
|
self.assertNotError('messageparser add "aoeu" "echo vowels are nice"')
|
||||||
|
finally:
|
||||||
|
world.testing = True
|
||||||
|
self.prefix = origuser
|
||||||
|
conf.supybot.plugins.MessageParser.requireManageCapability.setValue(origconf)
|
||||||
|
|
||||||
def testShow(self):
|
def testShow(self):
|
||||||
self.assertNotError('messageparser add "stuff" "echo i saw some stuff"')
|
self.assertNotError('messageparser add "stuff" "echo i saw some stuff"')
|
||||||
self.assertRegexp('messageparser show "nostuff"', 'there is no such regexp trigger')
|
self.assertRegexp('messageparser show "nostuff"', 'there is no such regexp trigger')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user