From f4636ebf507e4e8bc94555fe2d5db1503ba14c4b Mon Sep 17 00:00:00 2001 From: kwaaak Date: Tue, 26 Mar 2019 22:07:12 +0100 Subject: [PATCH] bot, config: anti-flood configuration code style Co-authored-by: deathbybandaid --- sopel/bot.py | 36 +++++++++++++++++------------------- sopel/config/core_section.py | 14 +++++--------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/sopel/bot.py b/sopel/bot.py index c374f6eaf7..a6d3303237 100644 --- a/sopel/bot.py +++ b/sopel/bot.py @@ -397,28 +397,26 @@ def say(self, text, recipient, max_messages=1): recipient_id = Identifier(recipient) - reciprec = self.stack.get(recipient_id) - if not reciprec: - reciprec = self.stack[recipient_id] = { - 'messages': [], - 'burst': self.config.core.bucket_burst_tokens, - } - - if not reciprec['burst']: - elapsed = time.time() - reciprec['messages'][-1][0] - reciprec['burst'] = min( - self.config.core.bucket_burst_tokens, - int(elapsed) * self.config.core.bucket_refill_rate) - - if not reciprec['burst']: - elapsed = time.time() - reciprec['messages'][-1][0] + recipient_stack = self.stack.setdefault(recipient_id, { + 'messages': [], + 'flood_left': self.config.core.flood_burst_lines, + }) + + if not recipient_stack['flood_left']: + elapsed = time.time() - recipient_stack['messages'][-1][0] + recipient_stack['flood_left'] = min( + self.config.core.flood_burst_lines, + int(elapsed) * self.config.core.flood_refill_rate) + + if not recipient_stack['flood_left']: + elapsed = time.time() - recipient_stack['messages'][-1][0] penalty = float(max(0, len(text) - 50)) / 70 wait = min(self.config.core.flood_empty_wait + penalty, 2) # Maximum wait time is 2 sec if elapsed < wait: time.sleep(wait - elapsed) # Loop detection - messages = [m[1] for m in reciprec['messages'][-8:]] + messages = [m[1] for m in recipient_stack['messages'][-8:]] # If what we about to send repeated at least 5 times in the # last 2 minutes, replace with '...' @@ -429,9 +427,9 @@ def say(self, text, recipient, max_messages=1): return self.write(('PRIVMSG', recipient), text) - reciprec['burst'] = max(0, reciprec['burst'] - 1) - reciprec['messages'].append((time.time(), self.safe(text))) - reciprec['messages'] = reciprec['messages'][-10:] + recipient_stack['flood_left'] = max(0, recipient_stack['flood_left'] - 1) + recipient_stack['messages'].append((time.time(), self.safe(text))) + recipient_stack['messages'] = recipient_stack['messages'][-10:] finally: self.sending.release() # Now that we've sent the first part, we need to send the rest. Doing diff --git a/sopel/config/core_section.py b/sopel/config/core_section.py index c22b057b74..f81617d20a 100644 --- a/sopel/config/core_section.py +++ b/sopel/config/core_section.py @@ -259,15 +259,11 @@ def homedir(self): verify_ssl = ValidatedAttribute('verify_ssl', bool, default=True) """Whether to require a trusted SSL certificate for SSL connections.""" - bucket_burst_tokens = ValidatedAttribute('bucket_burst_tokens', int, - default=4) + flood_burst_lines = ValidatedAttribute('flood_burst_lines', int, default=4) """How many messages can be sent in burst mode.""" - bucket_refill_rate = ValidatedAttribute('bucket_refill_rate', int, - default=1) - """How many tokens/second to add to the token bucket.""" + flood_empty_wait = ValidatedAttribute('flood_empty_wait', float, default=0.7) + """How long to wait between sending messages when not in burst mode, in seconds.""" - bucket_empty_wait = ValidatedAttribute('bucket_empty_wait', float, - default=0.7) - """How long to wait before sending a messaging when not in burst - mode.""" + flood_refill_rate = ValidatedAttribute('flood_refill_rate', int, default=1) + """How quickly burst mode recovers, in messages per second."""