g1mp/plugins/services/administration.py

225 lines
7.1 KiB
Python
Raw Normal View History

2025-02-13 06:35:15 +00:00
# -*- coding: utf-8 -*-
"""
IRC Bot Plugins: Voice, Kick, and Ban Management
This module provides three IRC bot plugins:
1. `VoicePlugin`: Handles granting and revoking voice (+v) privileges.
2. `KickPlugin`: Handles kicking users from the channel.
3. `BanPlugin`: Handles banning and unbanning users.
All commands require **admin** permissions.
Features:
- Voice a single user or all users in a channel.
- Devoice a single user or all users.
- Kick users with optional reasons.
- Ban and unban users.
2025-02-13 07:22:10 +00:00
Author: Zodiac (Modified for verbose logging by bot.log)
2025-02-13 06:35:15 +00:00
"""
import asyncio
2025-02-13 04:55:42 +00:00
import irc3
from irc3.plugins.command import command
2025-02-13 06:35:15 +00:00
2025-02-13 04:55:42 +00:00
@irc3.plugin
class VoicePlugin:
2025-02-13 06:35:15 +00:00
"""A plugin to manage voice (+v) privileges in an IRC channel."""
2025-02-13 04:55:42 +00:00
def __init__(self, bot):
2025-02-13 06:35:15 +00:00
"""
Initialize the VoicePlugin.
Args:
bot (irc3.IrcBot): The IRC bot instance.
"""
2025-02-13 04:55:42 +00:00
self.bot = bot
@command(permission='admin')
async def voice(self, mask, target, args):
2025-02-13 06:35:15 +00:00
"""
Grant voice to a specific user or all users in the channel.
2025-02-13 04:55:42 +00:00
2025-02-13 06:35:15 +00:00
Usage:
2025-02-13 04:55:42 +00:00
%%voice [<nick>]
"""
nick = args.get('<nick>')
if nick:
2025-02-13 07:22:10 +00:00
self.bot.log.info("Request to grant voice to %s in %s", nick, target)
2025-02-13 04:55:42 +00:00
await self.give_voice(target, nick)
else:
2025-02-13 07:22:10 +00:00
self.bot.log.info("Request to grant voice to all users in %s", target)
2025-02-13 04:55:42 +00:00
await self.give_voice_all(target)
@command(permission='admin')
async def devoice(self, mask, target, args):
2025-02-13 06:35:15 +00:00
"""
Remove voice from a specific user or all users in the channel.
2025-02-13 04:55:42 +00:00
2025-02-13 06:35:15 +00:00
Usage:
2025-02-13 04:55:42 +00:00
%%devoice [<nick>]
"""
nick = args.get('<nick>')
if nick:
2025-02-13 07:22:10 +00:00
self.bot.log.info("Request to remove voice from %s in %s", nick, target)
2025-02-13 04:55:42 +00:00
await self.remove_voice(target, nick)
else:
2025-02-13 07:22:10 +00:00
self.bot.log.info("Request to remove voice from all users in %s", target)
2025-02-13 04:55:42 +00:00
await self.remove_voice_all(target)
async def give_voice(self, target, nick):
2025-02-13 06:35:15 +00:00
"""
Grant voice to a specific user.
Args:
target (str): The IRC channel.
nick (str): The nickname of the user.
"""
2025-02-13 07:22:10 +00:00
self.bot.log.info("Granting voice to user %s in channel %s", nick, target)
2025-02-13 04:55:42 +00:00
self.bot.send(f'MODE {target} +v {nick}')
async def remove_voice(self, target, nick):
2025-02-13 06:35:15 +00:00
"""
Remove voice from a specific user.
Args:
target (str): The IRC channel.
nick (str): The nickname of the user.
"""
2025-02-13 07:22:10 +00:00
self.bot.log.info("Removing voice from user %s in channel %s", nick, target)
2025-02-13 04:55:42 +00:00
self.bot.send(f'MODE {target} -v {nick}')
async def give_voice_all(self, target):
2025-02-13 06:35:15 +00:00
"""
Grant voice to all users in the channel who do not have it.
Args:
target (str): The IRC channel.
"""
2025-02-13 07:22:10 +00:00
self.bot.log.info("Granting voice to all non-voiced users in channel %s", target)
2025-02-13 04:55:42 +00:00
names = await self.bot.async_cmds.names(target)
for user in names['names']:
2025-02-13 06:35:15 +00:00
if not user.startswith(("+", "@")): # Ignore voiced/opped users
2025-02-13 07:22:10 +00:00
self.bot.log.info("Granting voice to user %s", user)
2025-02-13 04:55:42 +00:00
self.bot.send(f'MODE {target} +v {user}')
2025-02-13 06:35:15 +00:00
await asyncio.sleep(0.07) # Prevent server flooding
2025-02-13 04:55:42 +00:00
async def remove_voice_all(self, target):
2025-02-13 06:35:15 +00:00
"""
Remove voice from all users in the channel.
Args:
target (str): The IRC channel.
"""
2025-02-13 07:22:10 +00:00
self.bot.log.info("Removing voice from all voiced users in channel %s", target)
2025-02-13 04:55:42 +00:00
names = await self.bot.async_cmds.names(target)
for user in names['names']:
2025-02-13 06:35:15 +00:00
if user.startswith("+"): # Only devoice voiced users
2025-02-13 07:22:10 +00:00
actual_user = user.lstrip("+")
self.bot.log.info("Removing voice from user %s", actual_user)
self.bot.send(f'MODE {target} -v {actual_user}')
2025-02-13 06:35:15 +00:00
await asyncio.sleep(0.07) # Prevent server flooding
2025-02-13 04:55:42 +00:00
@irc3.plugin
class KickPlugin:
2025-02-13 06:35:15 +00:00
"""A plugin to kick users from an IRC channel."""
2025-02-13 04:55:42 +00:00
def __init__(self, bot):
2025-02-13 06:35:15 +00:00
"""
Initialize the KickPlugin.
Args:
bot (irc3.IrcBot): The IRC bot instance.
"""
2025-02-13 04:55:42 +00:00
self.bot = bot
@command(permission='admin')
async def kick(self, mask, target, args):
2025-02-13 06:35:15 +00:00
"""
Kick a specific user from the channel with an optional reason.
2025-02-13 04:55:42 +00:00
2025-02-13 06:35:15 +00:00
Usage:
2025-02-13 04:55:42 +00:00
%%kick <nick> [<reason>]
"""
nick = args.get('<nick>')
reason = args.get('<reason>') or 'Kicked by admin'
if nick:
2025-02-13 07:22:10 +00:00
self.bot.log.info("Request to kick user %s from channel %s for reason: %s", nick, target, reason)
2025-02-13 04:55:42 +00:00
await self.kick_user(target, nick, reason)
async def kick_user(self, target, nick, reason):
2025-02-13 06:35:15 +00:00
"""
Kick a user from the channel using ChanServ.
Args:
target (str): The IRC channel.
nick (str): The nickname of the user.
reason (str): The reason for kicking the user.
"""
2025-02-13 07:22:10 +00:00
self.bot.log.info("Kicking user %s from channel %s", nick, target)
2025-02-13 04:55:42 +00:00
self.bot.send(f'PRIVMSG ChanServ :KICK {target} {nick} {reason}')
2025-02-13 06:35:15 +00:00
2025-02-13 04:55:42 +00:00
@irc3.plugin
class BanPlugin:
2025-02-13 06:35:15 +00:00
"""A plugin to ban and unban users in an IRC channel."""
2025-02-13 04:55:42 +00:00
def __init__(self, bot):
2025-02-13 06:35:15 +00:00
"""
Initialize the BanPlugin.
Args:
bot (irc3.IrcBot): The IRC bot instance.
"""
2025-02-13 04:55:42 +00:00
self.bot = bot
@command(permission='admin')
async def ban(self, mask, target, args):
2025-02-13 06:35:15 +00:00
"""
Ban a specific user from the channel.
2025-02-13 04:55:42 +00:00
2025-02-13 06:35:15 +00:00
Usage:
2025-02-13 04:55:42 +00:00
%%ban <nick>
"""
nick = args.get('<nick>')
if nick:
2025-02-13 07:22:10 +00:00
self.bot.log.info("Request to ban user %s from channel %s", nick, target)
2025-02-13 04:55:42 +00:00
await self.ban_user(target, nick)
@command(permission='admin')
async def unban(self, mask, target, args):
2025-02-13 06:35:15 +00:00
"""
Unban a specific user from the channel.
2025-02-13 04:55:42 +00:00
2025-02-13 06:35:15 +00:00
Usage:
2025-02-13 04:55:42 +00:00
%%unban <nick>
"""
nick = args.get('<nick>')
if nick:
2025-02-13 07:22:10 +00:00
self.bot.log.info("Request to unban user %s from channel %s", nick, target)
2025-02-13 04:55:42 +00:00
await self.unban_user(target, nick)
async def ban_user(self, target, nick):
2025-02-13 06:35:15 +00:00
"""
Ban a specific user from the channel.
Args:
target (str): The IRC channel.
nick (str): The nickname of the user.
"""
2025-02-13 07:22:10 +00:00
self.bot.log.info("Banning user %s in channel %s", nick, target)
2025-02-13 04:55:42 +00:00
self.bot.send(f'MODE {target} +b {nick}')
async def unban_user(self, target, nick):
2025-02-13 06:35:15 +00:00
"""
Unban a specific user from the channel.
Args:
target (str): The IRC channel.
nick (str): The nickname of the user.
"""
2025-02-13 07:22:10 +00:00
self.bot.log.info("Unbanning user %s in channel %s", nick, target)
2025-02-13 06:35:15 +00:00
self.bot.send(f'MODE {target} -b {nick}')