From 5910454cbf321a55dd5b33f24d577fe5d102605b Mon Sep 17 00:00:00 2001 From: Zodiac Date: Fri, 14 Feb 2025 19:36:35 -0800 Subject: [PATCH] update spam plugin to remove bans --- plugins/services/anti_spam.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/plugins/services/anti_spam.py b/plugins/services/anti_spam.py index 85a1939..ae033db 100644 --- a/plugins/services/anti_spam.py +++ b/plugins/services/anti_spam.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- """ -IRC3 Anti-Spam Plugin with Auto-Kick and Auto-Ban +IRC3 Anti-Spam Plugin with Auto-Kick, Auto-Ban, and Auto-Unban This plugin automatically detects and mitigates spam in IRC channels by monitoring messages for: @@ -10,12 +10,12 @@ by monitoring messages for: Actions: - **1st and 2nd offense**: User gets kicked. -- **3rd offense within 5 minutes**: User gets banned. +- **3rd offense within 5 minutes**: User gets banned and automatically unbanned after 5 minutes. Features: - Uses a dynamic WHO query to verify user modes before action. - Configurable limits for spam detection (via `antispam` settings). -- Periodic cleanup of inactive users every minute. +- Periodic cleanup of inactive users and expired bans every minute. Author: [Your Name] """ @@ -50,8 +50,9 @@ class AntiSpam: 'mentions': deque(maxlen=int(self.config.get('mention_limit', 2))) }) self.kick_history = defaultdict(deque) # Track kick timestamps per user + self.active_bans = [] # Track active bans with timestamps for auto-removal - self.exclude_list = ['ZodBot','g1mp','scroll','WILDWEST','CANCER','aibird'] # Bots that should be ignored + self.exclude_list = ['ZodBot','g1mp','scroll','WILDWEST','CANCER','aibird'] # Bots to ignore self.service_name = self.config.get('service_name', 'ChanServ') self.who_channel = WhoChannel(bot) # WHO query for user modes @@ -175,6 +176,13 @@ class AntiSpam: ban_mask = f'*!{mask.host}' self.bot.send(f"MODE {channel} +b {ban_mask}") + # Record ban for auto-removal + self.active_bans.append({ + 'ban_mask': ban_mask, + 'channel': channel, + 'timestamp': current_time + }) + self.bot.privmsg( self.service_name, f"KICK {channel} {nick} :Banned for repeated spamming" @@ -208,6 +216,17 @@ class AntiSpam: } self.bot.log.info("Cleaned up old spam records.") + @cron('* * * * *') + def remove_expired_bans(self): + """Remove bans that are older than 5 minutes.""" + current_time = time.time() + expired_bans = [ban for ban in self.active_bans if current_time - ban['timestamp'] >= 300] + for ban in expired_bans: + self.bot.send(f"MODE {ban['channel']} -b {ban['ban_mask']}") + self.bot.log.info(f"Auto-removed ban {ban['ban_mask']} from {ban['channel']} after 5 minutes.") + # Update active_bans to exclude expired ones + self.active_bans = [ban for ban in self.active_bans if current_time - ban['timestamp'] < 300] + def connection_made(self): """Initialize when bot connects.""" - self.bot.log.info("AntiSpam plugin loaded with automated kick-to-ban escalation.") + self.bot.log.info("AntiSpam plugin loaded with automated kick-to-ban escalation and auto-unban.") \ No newline at end of file