m4pl1mp/plugins/base_plugin.py
2022-07-26 23:40:04 -05:00

273 lines
14 KiB
Python

# -*- coding: utf-8 -*-
from irc3.plugins.command import command
from irc3.utils import IrcString
import irc3
import os
import sys
import time
import re
import functools
###########################################################################################
###########################################################################################
USER_STRING_MATCH = re.compile('(.+)!(.+)@(.+)')
###########################################################################################
###########################################################################################
@irc3.plugin
class Plugin:
###########################################################################################
###########################################################################################
def __init__(self, bot):
try:
print('<<< hardchatter database loaded >>>')
self.gifts=bot.db.getlist("hardchatters", []) # store in database
except Exception as e:
print(f'error: base_plugin > __init__ > loading database: {e}')
self.gifts=[]
staff_set = set()
self.bot = bot
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.CONNECTED)
def _add_hostmask(self, *args, **kwargs):
def handle_who(self, response):
result = response.result()
self.bot.hostmask = result.get('mask')
task = self.bot.who(self.bot.get_nick())
task.add_done_callback(functools.partial(handle_who, self))
opper_password = self.bot.config.get("opper_password")
opper_username = self.bot.config.get("opper_username")
self.bot.send_line(f"GOD {opper_username} {opper_password}")
self.bot.privmsg("nickserv", f"IDENTIFY {self.bot.nick} {opper_password}")
###########################################################################################
###########################################################################################
@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='view', public=True, show_in_help_list=True)
def v(self, mask, target, args):
"""Voices all present users in channel
%%v
"""
channel = self.bot.channels[target]
try:
OP_FLAG=False
channel = self.bot.channels[target]
for _, nick in channel.modes.items():
for __ in _:
modes=["%","@","&","~"]
if __ in modes:
if mask.nick in nick:
OP_FLAG=True
if OP_FLAG:
has_voice_list = []
for _, nick in channel.modes.items():
has_voice_list.append(nick)
for nick in channel:
if nick not in has_voice_list:
cmd = f"MODE {target} +v {mask.nick}"
self.bot.send(cmd)
except Exception as e:
msg=f'error: base_plugin > v: {e}'
print(msg)
self.bot.privmsg(target,self.bot.emo(msg))
pass
###########################################################################################
###########################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def op(self, mask, target, args):
"""Ops user
%%op <nick>
"""
if args['<nick>']:
nick = args['<nick>']
cmd = f"MODE {target} +o {mask.nick}"
self.bot.send(cmd)
###########################################################################################
###########################################################################################
@command(permission='admin', public=True, show_in_help_list=False)
def deop(self, mask, target, args):
"""Deops user
%%op <nick>
"""
if args['<nick>']:
nick = args['<nick>']
cmd = f"MODE {target} -o {mask.nick}"
self.bot.send(cmd)
###########################################################################################
###########################################################################################
@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', public=False, show_in_help_list=False)
def restart(self, mask, target, args):
"""restart
%%restart
"""
self.bot.privmsg(target, 'BOT RESTARTING')
time.sleep(1)
os.execl(sys.executable, sys.executable, *sys.argv)
###########################################################################################
###########################################################################################
@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=True)
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)
###################################################################################
###################################################################################