Skip to content

Commit

Permalink
[core] Count messages loop and flood protection per channel, not glob…
Browse files Browse the repository at this point in the history
…ally

Also few tweaks to make loop protection a bit less frustrating for users.
  • Loading branch information
Elad Alfassa committed May 2, 2014
1 parent cdaa913 commit 6a6edca
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions willie/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(self, config):
self.channels = []
"""The list of channels Willie is currently in."""

self.stack = []
self.stack = {}
self.ca_certs = ca_certs
self.hasquit = False

Expand Down Expand Up @@ -466,28 +466,33 @@ def msg(self, recipient, text, max_messages=1):

# No messages within the last 3 seconds? Go ahead!
# Otherwise, wait so it's been at least 0.8 seconds + penalty
if self.stack:
elapsed = time.time() - self.stack[-1][0]

recipient_id = Nick(recipient)

if recipient_id not in self.stack:
self.stack[recipient_id] = []
elif self.stack[recipient_id]:
elapsed = time.time() - self.stack[recipient_id][-1][0]
if elapsed < 3:
penalty = float(max(0, len(text) - 50)) / 70
wait = 0.8 + penalty
wait = 0.7 + penalty
if elapsed < wait:
time.sleep(wait - elapsed)

# Loop detection
messages = [m[1] for m in self.stack[-8:]]
messages = [m[1] for m in self.stack[recipient_id][-8:]]

# If what we about to send repeated at least 5 times in the
# last 5 minutes, replace with '...'
if messages.count(text) >= 5 and elapsed < 300:
# last 2 minutes, replace with '...'
if messages.count(text) >= 5 and elapsed < 120:
text = '...'
if messages.count('...') >= 3:
# If we said '...' 3 times, discard message
return

self.write(('PRIVMSG', recipient), text)
self.stack.append((time.time(), self.safe(text)))
self.stack = self.stack[-10:]
self.stack[recipient_id].append((time.time(), self.safe(text)))
self.stack[recipient_id] = self.stack[recipient_id][-10:]
finally:
self.sending.release()
# Now that we've sent the first part, we need to send the rest. Doing
Expand Down

0 comments on commit 6a6edca

Please sign in to comment.