From 162b9ef709ec311c0499c55acbe3da017d0db9d6 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Tue, 24 Dec 2013 14:37:44 +0000 Subject: [PATCH] Aka: Add length limit of command names in order to limit the number of database queries. There were a lot of database queries (as much as words in the whole command) for non-aka commands. --- plugins/Aka/config.py | 4 ++++ plugins/Aka/plugin.py | 5 ++++- plugins/Aka/test.py | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/Aka/config.py b/plugins/Aka/config.py index 4642d7793..60a054b5b 100644 --- a/plugins/Aka/config.py +++ b/plugins/Aka/config.py @@ -51,6 +51,10 @@ Aka = conf.registerPlugin('Aka') # This is where your configuration variables (if any) should go. For example: # conf.registerGlobalValue(Aka, 'someConfigVariableName', # registry.Boolean(False, _("""Help for someConfigVariableName."""))) +conf.registerGlobalValue(Aka, 'maximumWordsInName', + registry.Integer(5, _("""The maximum number of words allowed in a + command name. Setting this to an high value may slow down your bot + on long commands."""))) # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/plugins/Aka/plugin.py b/plugins/Aka/plugin.py index 7a0895ee4..9b943f63a 100644 --- a/plugins/Aka/plugin.py +++ b/plugins/Aka/plugin.py @@ -276,7 +276,8 @@ class Aka(callbacks.Plugin): ret = self.getCommand(args[1:], False) if ret: return [first] + ret - for i in xrange(1, len(args)+1): + max_length = self.registryValue('maximumWordsInName') + for i in xrange(1, min(len(args)+1, max_length)): if self.isCommandMethod(callbacks.formatCommand(args[0:i])): return args[0:i] return [] @@ -365,6 +366,8 @@ class Aka(callbacks.Plugin): 'this plugin.')) if self._db.has_aka(channel, name): raise AkaError(_('This Aka already exists.')) + if len(name.split(' ')) > self.registryValue('maximumWordsInName'): + raise AkaError(_('This Aka has too many spaces in its name.')) biggestDollar = findBiggestDollar(alias) biggestAt = findBiggestAt(alias) wildcard = '$*' in alias diff --git a/plugins/Aka/test.py b/plugins/Aka/test.py index b413c56b3..b051456cb 100644 --- a/plugins/Aka/test.py +++ b/plugins/Aka/test.py @@ -178,6 +178,10 @@ class AkaChannelTestCase(ChannelPluginTestCase): class AkaTestCase(PluginTestCase): plugins = ('Aka', 'Alias', 'User', 'Utilities') + def testMaximumLength(self): + self.assertNotError('aka add "foo bar baz qux quux" "echo test"') + self.assertError('aka add "foo bar baz qux quux corge" "echo test"') + def testAkaLockedHelp(self): self.assertNotError('register evil_admin foo')