From c910f08b1e176b5b3ac4045be0d06659b1f74871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Mon, 1 Jan 2018 00:26:54 +0200 Subject: [PATCH] Add flood protection --- ircbot.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ircbot.py b/ircbot.py index 5d19e02..69af3c1 100644 --- a/ircbot.py +++ b/ircbot.py @@ -2,6 +2,7 @@ import select import socket import threading +import time from collections import namedtuple import channel @@ -109,6 +110,9 @@ class ServerThread(threading.Thread): self.server_socket_write_lock = threading.Lock() + self.last_send = 0 + self.last_send_lock = threading.Lock() + self.nick = None self.nick_lock = threading.Lock() @@ -121,6 +125,20 @@ class ServerThread(threading.Thread): # Sanitize line just in case line = line.replace(b'\r', b'').replace(b'\n', b'')[:510] + with self.last_send_lock: + now = time.monotonic() + if now - self.last_send < 1: + # Schedule our message sending one second after the last one + self.last_send += 1 + wait = self.last_send - now + + else: + self.last_send = now + wait = 0 + + if wait > 0: + time.sleep(wait) + with self.server_socket_write_lock: self.server_socket.sendall(line + b'\r\n')