maple/storage/bot/plugins/base_plugin.py
2023-03-26 15:46:52 -05:00

569 lines
24 KiB
Python

# -*- coding: utf-8 -*- ############################################################### SOF
###########################################################################################
from irc3.plugins.command import command
from irc3.utils import IrcString
import irc3
import os
import sys
import time
import re
import functools
from random import shuffle
import string
from irc3.plugins.cron import cron
from tool_colors_plugin import colorform as print
###########################################################################################
###########################################################################################
USER_STRING_MATCH = re.compile('(.+)!(.+)@(.+)')
###########################################################################################
###########################################################################################
@irc3.plugin
class Plugin:
#######################################################################################
#######################################################################################
def __init__(self,bot):
print('[ loaded ]')
self.bot=bot
staff_set=set()
self.bot.auto_kick_list = set(self.bot.db.getlist("auto_kick_list", []))
self.bot.ignore_list = set(self.bot.db.getlist("ignore_nicks_list", []))
self.bot.staff_list = set(self.bot.db.getlist("staff_list", []))
current_masks = self.bot.config.get('irc3.plugins.command.masks')
for k, v in current_masks.items():
if v in ["staff", "all_permissions"]:
staff_set.add(k)
if not self.bot.ignore_list:
self.bot.ignore_list = set(self.bot.config.get('irc3.plugins.command.masks', {}).get("ignore_list", []))
if not self.bot.staff_list:
self.bot.staff_list = set(staff_set)
self.bot.db.setlist("staff_list", self.bot.staff_list)
for hostname in self.bot.staff_list:
if not current_masks.get(hostname):
self.bot.config['irc3.plugins.command.masks'][hostname] = "staff"
#######################################################################################
#######################################################################################
@irc3.extend
def check_if_ignored(self, hostmask):
global_hostmask = hostmask.replace(hostmask.nick, ".+")
nick_mask = hostmask.replace(hostmask.host,".+@.+")
host_pattern = re.compile(global_hostmask)
nick_pattern = re.compile(nick_mask)
for mask in self.bot.ignore_list:
if host_pattern.match(mask):
return True
if nick_pattern.match(mask):
return True
return False
#######################################################################################
#######################################################################################
@irc3.event(irc3.rfc.JOIN)
def auto_kick_handler(self, mask, channel, **kw):
msg = "g1mp'n ain't easy unless you're maple"
if mask.nick in self.bot.auto_kick_list:
cmd = f"MODE {channel} +b {mask.nick}!*@*"
self.bot.send(cmd)
self.bot.kick(channel, mask.nick, msg)
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mdeban(self, mask, target, args):
"""removes all present bans from channel
%%mdeban
"""
self.target=target
###############################################################################
###############################################################################
def handle_bans(self, response):
result = response.result()
[ self.bot.send(f'MODE {self.target} -b {x["mask"]}') for x in result['bans'] ]
###############################################################################
###############################################################################
try:
task = self.bot.channel_bans(self.target)
task.add_done_callback(functools.partial(handle_bans, self))
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def lockdown(self, mask, target, args):
"""lockdown channel
%%lockdown
"""
try:
yek=list(string.ascii_letters+string.digits)
shuffle(yek)
self.bot.send(f'MODE {target} +mik {"".join(yek[0:32])}')
self.mv(mask,target,args)
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def unlockdown(self, mask, target, args):
"""unlockdown channel
%%unlockdown
"""
try:
self.bot.send(f'MODE {target} -mik')
except Exception as e:
pass
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def unban(self, mask, target, args):
"""unbans hostmask from channel
%%unban <channel> <hostmask>
"""
channel=args.get('<channel>')
hostmask=args.get('<hostmask>')
try:
self.bot.send(f'MODE {channel} -b {hostmask}')
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def ban(self, mask, target, args):
"""bans user from channel
%%ban <nick>
"""
self.target=target
###############################################################################
###############################################################################
def handle_who(self, response):
result = response.result()
self.bot.send(f"MODE {self.target} +b *!{result['user']}@*")
###############################################################################
###############################################################################
try:
USER=args['<nick>']
task = self.bot.who(USER)
task.add_done_callback(functools.partial(handle_who, self))
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mban(self, mask, target, args):
"""Bans all present users from channel
%%mban
"""
self.target=target
###############################################################################
###############################################################################
def handle_who(self, response):
result = response.result()
self.bot.send(f"MODE {self.target} +b *!{result['user']}@*")
###############################################################################
###############################################################################
try:
for USER in self.bot.channels[target]:
task = self.bot.who(USER)
task.add_done_callback(functools.partial(handle_who, self))
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mkick(self, mask, target, args):
"""Kicks all present users from channel
%%mkick <noise>...
"""
msg = ' '.join(args.get('<noise>'))
try:
[self.bot.kick(target,x,self.bot.emo(msg)) for x in self.bot.channels[target]]
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def kick(self, mask, target, args):
"""Kicks user from channel
%%kick <noise>...
"""
nick=' '.join(args.get('<noise>')[:1])
msg=' '.join(args.get('<noise>')[1:])
try:
self.bot.kick(target,nick,self.bot.emo(msg))
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mv(self, mask, target, args):
"""Voices all present users in channel
%%mv
"""
try:
[ self.bot.send(f'MODE {target} +v {x}') for x in self.bot.channels[target]]
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mdv(self, mask, target, args):
"""DeVoices all present users in channel
%%mdv
"""
try:
[ self.bot.send(f'MODE {target} -v {x}') for x in self.bot.channels[target]]
except:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def v(self, mask, target, args):
"""Voices user in channel
%%v <nick>
"""
nick = args['<nick>']
try:
self.bot.send(f'MODE {target} +v {nick}')
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def dv(self, mask, target, args):
"""DeVoices user in channel
%%dv <nick>
"""
nick = args['<nick>']
try:
self.bot.send(f'MODE {target} -v {nick}')
except:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mop(self, mask, target, args):
"""Ops all present users in channel
%%mop
"""
try:
[ self.bot.send(f'MODE {target} +o {x}') for x in self.bot.channels[target]]
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mdeop(self, mask, target, args):
"""DeOps all present users in channel
%%mdeop
"""
try:
[ self.bot.send(f'MODE {target} -o {x}') for x in self.bot.channels[target]]
except:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def op(self, mask, target, args):
"""Ops user
%%op <nick>
"""
nick = args['<nick>']
try:
self.bot.send(f'MODE {target} +o {nick}')
except:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def deop(self, mask, target, args):
"""Deops user
%%deop <nick>
"""
nick = args['<nick>']
try:
self.bot.send(f'MODE {target} -o {nick}')
except:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mhop(self, mask, target, args):
"""Hops all present users in channel
%%mhop
"""
try:
[ self.bot.send(f'MODE {target} +h {x}') for x in self.bot.channels[target]]
except Exception as e:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def mdehop(self, mask, target, args):
"""DeHops all present users in channel
%%mdehop
"""
try:
[ self.bot.send(f'MODE {target} -h {x}') for x in self.bot.channels[target]]
except:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def hop(self, mask, target, args):
"""Hops user
%%hop <nick>
"""
nick = args['<nick>']
try:
self.bot.send(f'MODE {target} +h {nick}')
except:
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def dehop(self, mask, target, args):
"""Dehops user
%%dehop <nick>
"""
nick = args['<nick>']
try:
self.bot.send(f'MODE {target} -h {nick}')
except:
pass
#######################################################################################
#######################################################################################
@command(permission="admin", show_in_help_list=False)
def stats(self, mask, target, args):
"""Stats
%%stats
"""
channel = target
if channel in self.bot.channels:
channel = self.bot.channels[channel]
message = f'{len(channel)} users'
for mode, nicknames in sorted(channel.modes.items()):
message += f' - {mode}({len(nicknames)})'
self.bot.privmsg(target, message)
#######################################################################################
#######################################################################################
@command(permission='admin', show_in_help_list=False)
def reload(self, mask, target, args):
"""reload
%%reload <noise>
"""
noise = f"plugins.{args.get('<noise>')}_plugin"
self.bot.privmsg(target, f'reloading plugin: {noise}')
try:
self.bot.reload(noise)
except:
self.bot.privmsg(target, f'error: reloading plugin: {noise} failed')
#######################################################################################
#######################################################################################
@command(permission='admin', public=False, show_in_help_list=False)
def uptime(self, mask, target, args):
pass
#######################################################################################
#######################################################################################
@command(permission='admin', public=False, show_in_help_list=False)
def ping(self, mask, target, args):
pass
#######################################################################################
#######################################################################################
@command(permission='staff', public=True, show_in_help_list=False)
def staff(self, mask, target, args):
"""staff
%%staff <noise>...
"""
###################################################################################
###################################################################################
def __staff_list(self, mask, target, args):
current_masks = self.bot.config.get("irc3.plugins.command.masks", "")
current_staff_list = [k for k,v in current_masks.items() if v in ["all_permissions", "staff"]]
self.bot.privmsg(target, "\x02༺Staff List༻\x0F\x02\x0303 > \x0F\x0302{}".format(", ".join(current_staff_list)))
###################################################################################
###################################################################################
def __staff_del(self, mask, target, args):
###############################################################################
###############################################################################
def del_hostmask(self, host_string):
current_masks = self.bot.config.get("irc3.plugins.command.masks", "")
if host_string in current_masks.keys():
mask_list = current_masks.get(host_string, "").split(",")
if "all_permissions" in mask_list:
self.bot.privmsg(target, "Don't fuck with the admin")
return
elif "staff" in mask_list:
del(self.bot.config["irc3.plugins.command.masks"][host_string])
self.bot.staff_list.remove(host_string)
self.bot.db.setlist("staff_list", self.bot.staff_list)
self.bot.privmsg(target, f'{host_string} removed from staff')
return
else:
pass # pass for brevarity
else:
self.bot.privmsg(target, f'{host_string} is not a staff member')
###############################################################################
###############################################################################
def handle_who(self, response):
result = response.result()
nick = result.get("nick")
mask = result.get('mask')
host_string = mask.replace(nick,"*")
del_hostmask(self, host_string)
###############################################################################
###############################################################################
nick = args.get("<nick>")
match_list = USER_STRING_MATCH.findall(nick)
if match_list and len(match_list[0]) == 3:
del_hostmask(self, nick)
return
else:
task = self.bot.who(nick)
task.add_done_callback(functools.partial(handle_who, self))
return
###################################################################################
###################################################################################
def __staff_add(self, mask, target, args):
###############################################################################
###############################################################################
def add_hostmask(self, host_string):
current_masks = self.bot.config.get("irc3.plugins.command.masks", "")
if host_string in current_masks.keys():
mask_list = current_masks.get(host_string, "").split(",")
if "staff" or "all_permissions" in mask_list:
self.bot.privmsg(target, f"{host_string} is already a staff member")
return
else:
self.bot.config["irc3.plugins.command.masks"][host_string] = "staff"
self.bot.staff_list.add(host_string)
self.bot.db.setlist("staff_list", self.bot.staff_list)
self.bot.privmsg(target, f'{host_string} added to staff')
###############################################################################
###############################################################################
def handle_who(self, response):
result = response.result()
nick = result.get("nick")
mask = result.get('mask')
host_string = mask.replace(nick,"*")
add_hostmask(self, host_string)
###############################################################################
###############################################################################
nick = args.get("<nick>")
match_list = USER_STRING_MATCH.findall(nick)
###############################################################################
###############################################################################
if match_list and len(match_list[0]) == 3:
add_hostmask(self, nick)
return
else:
task = self.bot.who(nick)
task.add_done_callback(functools.partial(handle_who, self))
return
###################################################################################
###################################################################################
cmd = ' '.join(args['<noise>'])[0]
args = ' '.join(args['<noise>'])[1:]
###################################################################################
###################################################################################
if not cmd:
self.bot.privmsg(target, "please specify add/del/list")
elif cmd in ["d","del","delete","remove"]:
__staff_del(self, mask, target, args)
elif cmd in ["l", "list"]:
__staff_list(self, mask, target, args)
elif cmd in ["w", "write","a", "add"]:
__staff_add(self, mask, target, args)
###########################################################################################
####################################################################################### EOF