RateLimit: Add optional error message.

master
Valentin Lorentz 2013-08-13 16:07:12 +02:00
parent 80253dab8f
commit 711c9e506f
3 changed files with 11 additions and 0 deletions

View File

@ -51,6 +51,9 @@ RateLimit = conf.registerPlugin('RateLimit')
# This is where your configuration variables (if any) should go. For example:
# conf.registerGlobalValue(RateLimit, 'someConfigVariableName',
# registry.Boolean(False, _("""Help for someConfigVariableName.""")))
conf.registerChannelValue(RateLimit, 'error',
registry.Boolean(False, _("""Determines whether an error message will
be sent if a user reaches the rate limit.""")))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -138,6 +138,10 @@ class RateLimit(callbacks.Plugin):
if len(list_) >= record.count:
self.log.info('Throttling command %r call (rate limited).',
command)
if self.registryValue('error', msg.args[0]):
irc.error(_('This command has been called more than {0} '
'times in the last {1} seconds.').format(
record.count, record.interval))
return True
list_.append((user, time.time()))
self._history[command] = list_

View File

@ -29,6 +29,7 @@
###
from supybot.test import *
import supybot.conf as conf
class RateLimitTestCase(PluginTestCase):
plugins = ('RateLimit', 'User', 'Utilities')
@ -52,6 +53,9 @@ class RateLimitTestCase(PluginTestCase):
self.assertResponse('echo spam', 'spam', frm='foo!a@a')
self.assertNoResponse('echo spam', frm='foo!a@a')
self.assertResponse('echo spam', 'spam', frm='bar!a@a')
with conf.supybot.plugins.RateLimit.Error.context(True):
self.assertRegexp('echo spam', 'called more than 3 times',
frm='foo!a@a')
time.sleep(1.1)