From 6929fc1e8ebfc0a200f3b8c60ca0df16148d6314 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Tue, 13 Aug 2013 13:29:18 +0200 Subject: [PATCH] RateLimit: Add @unset. --- RateLimit/plugin.py | 31 +++++++++++++++++++++++++++++++ RateLimit/test.py | 13 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/RateLimit/plugin.py b/RateLimit/plugin.py index bfcf8e8..b3033cc 100644 --- a/RateLimit/plugin.py +++ b/RateLimit/plugin.py @@ -69,6 +69,17 @@ class RateLimitDB(dbi.DB): self.add(record) else: self.set(previous_record.id, record) + def unset_user_limit(self, channel, user, command): + try: + record = list(filter( + lambda x:x.user == user and + x.command == command and + x.channel == channel, + self))[0] + except IndexError: + raise + else: + self.remove(record.id) def get_limits(self, command): return filter(lambda x:x.command == command, self) @@ -148,6 +159,26 @@ class RateLimit(callbacks.Plugin): self.db.set_user_limit(None, user, count, interval, command) irc.replySuccess() + @wrap([optional(first('otherUser', ('literal', '*'))), + 'commandName', 'admin']) + def unset(self, irc, msg, args, user, command): + """[] + + Unsets the rate limit of the for the . + If is not given, the rate limit will be enforced globally, + and if * is given as the , the rate limit will be enforced + for everyone.""" + if user is None: + user = 'global' + elif user != '*': + user = user.id + try: + self.db.unset_user_limit(None, user, command) + except IndexError: + irc.error(_('This rate limit did not exist.')) + else: + irc.replySuccess() + @wrap(['commandName']) def get(self, irc, msg, args, command): """ diff --git a/RateLimit/test.py b/RateLimit/test.py index 39d93f6..9d5dc08 100644 --- a/RateLimit/test.py +++ b/RateLimit/test.py @@ -53,6 +53,19 @@ class RateLimitTestCase(PluginTestCase): self.assertNoResponse('echo spam', frm='foo!a@a') self.assertResponse('echo spam', 'spam', frm='bar!a@a') + time.sleep(1.1) + + self.assertNotError('ratelimit unset foo echo') + self.assertResponse('ratelimit get echo', + 'global: none, *: none') + self.assertResponse('echo spam', 'spam', frm='foo!a@a') + self.assertResponse('echo spam', 'spam', frm='foo!a@a') + self.assertResponse('echo spam', 'spam', frm='foo!a@a') + self.assertResponse('echo spam', 'spam', frm='foo!a@a') + + self.assertRegexp('ratelimit unset foo echo', + 'Error:.*did not exist') + def testStar(self): self.assertResponse('ratelimit get echo', 'global: none, *: none')