Skip to content

Commit

Permalink
bot, config: anti-flood configuration code style
Browse files Browse the repository at this point in the history
Co-authored-by: deathbybandaid <sam@deathbybandaid.net>
  • Loading branch information
2 people authored and dgw committed May 13, 2019
1 parent 282d332 commit f4636eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
36 changes: 17 additions & 19 deletions sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '...'
Expand All @@ -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
Expand Down
14 changes: 5 additions & 9 deletions sopel/config/core_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

0 comments on commit f4636eb

Please sign in to comment.