From e96f8844243d5f42f1fef84bb33a7fecc39f3f53 Mon Sep 17 00:00:00 2001 From: jan6 Date: Sun, 19 Sep 2021 21:33:30 +0300 Subject: [PATCH] I forgot what I added, lol, nick change, reloadable config...ish, exec, anything else? lol --- bot.py | 24 ++++++++++++++++-------- commands.py | 5 +++-- config.py | 2 +- stuff.py | 41 ++++++++++++++++++++++++++++------------- util.py | 6 +++++- 5 files changed, 53 insertions(+), 25 deletions(-) diff --git a/bot.py b/bot.py index d1de817..24b99db 100755 --- a/bot.py +++ b/bot.py @@ -3,6 +3,7 @@ import ircstates,socket,ssl from config import * from stuff import stuff +from util import Util class bot: server=ircstates.Server(config.server.name) @@ -12,12 +13,19 @@ class bot: if __name__==__module__=="__main__": @classmethod def __init__(self): - if config.server.ssl: - with socket.create_connection((self.host, self.port)) as sock_raw: - ctx=ssl.create_default_context() - with ctx.wrap_socket(sock_raw, server_hostname=self.host) as sock: - stuff(self,sock) - else: - with socket.create_connection((self.host, self.port)) as sock: - stuff(self,sock) + if config.server.ssl: + with socket.create_connection((self.host, self.port)) as sock_raw: + ctx=ssl.create_default_context() + with ctx.wrap_socket(sock_raw, server_hostname=self.host) as sock: + try: + util=Util(sock,config) + stuff(self,sock) + except KeyboardInterrupt: util.quit("^C") + else: + with socket.create_connection((self.host, self.port)) as sock: + try: + util=Util(sock,config) + stuff(self,sock) + except KeyboardInterrupt: util.quit("^C") +print("starting bot...") bot() diff --git a/commands.py b/commands.py index 70e5b60..6a8f0a7 100644 --- a/commands.py +++ b/commands.py @@ -1,8 +1,9 @@ from util import Util class Command: - def __init__(self,sock): + def __init__(self,sock,config): self.sock=sock - self.util=Util(sock) + self.config=config + self.util=Util(sock,config) def mesg(self,msg): self.util.mesg(msg) def err_perm(self,level="admin"): mesg("Error: insufficient privileges, you lack {level} access") def exec_cmd(self,command,extra=[None]): diff --git a/config.py b/config.py index 847d62e..521d4d2 100644 --- a/config.py +++ b/config.py @@ -24,4 +24,4 @@ class config: class cmd: prefixes=["6","'"] enabled_commands=["help","echo"] - admin_commands=["eval","quit","reload"] + admin_commands=["eval","exec","quit"] diff --git a/stuff.py b/stuff.py index 9835e56..aa4d251 100755 --- a/stuff.py +++ b/stuff.py @@ -5,23 +5,29 @@ from commands import Command import sys, importlib def stuff(bot,sock): config=Config - chan=config.server.channel - util=Util(sock) - command=Command(sock) + util=Util(sock,config) + command=Command(sock,config) server=bot.server send=util.send quit=util.quit mesg=util.mesg - server_caps=[] wanted_caps=config.capabilities - mode="init" - prefixes=config.cmd.prefixes - admin_accounts=config.admin.accounts - admin_users=config.admin.hostmasks + chan=config.server.channel #autojoin channel is_pm=False - enabled_commands=config.cmd.enabled_commands - admin_commands=config.cmd.admin_commands + mode="init" + def configure(): + config=importlib.reload(sys.modules["config"]).config + command=importlib.reload(sys.modules["commands"]).Command(sock,config) + util=Util(sock,config) + prefixes=config.cmd.prefixes + admin_accounts=config.admin.accounts + admin_users=config.admin.hostmasks + enabled_commands=config.cmd.enabled_commands + admin_commands=config.cmd.admin_commands + return command,config,util,prefixes,admin_accounts,admin_users,enabled_commands,admin_commands + + command,config,util,prefixes,admin_accounts,admin_users,enabled_commands,admin_commands=configure() send(irctokens.build("NICK", [config.self.nick]).format()) send(irctokens.build("USER", [config.self.username,"0","*",config.self.realname]).format()) @@ -54,6 +60,9 @@ def stuff(bot,sock): send(irctokens.build("JOIN", [chan]).format()) mode="normal" elif mode=="normal": + if line.command == "433": + mesg("nick in use!",chan) + util.nick(config.self.nick) if line.command == "INVITE": send(irctokens.build("JOIN", [line.params[1]]).format()) elif line.command == "PRIVMSG": @@ -84,6 +93,7 @@ def stuff(bot,sock): command.pm=is_pm command.cmd=cmd command.admin=is_adm + command.config=config if cmd.startswith("echo "): command.exec_cmd("echo") if cmd=="help": @@ -96,9 +106,7 @@ def stuff(bot,sock): elif is_adm and (cmd.startswith("q ") or cmd.startswith("quit")): quit(cmd.split(" ",1)[1]) elif is_adm and cmd.startswith("reload"): -# command.exec_cmd("reload") - command=importlib.reload(sys.modules["commands"]).Command(sock) - config=importlib.reload(sys.modules["config"]).config + command,config,util,prefixes,admin_accounts,admin_users,enabled_command,admin_commands=configure() mesg("reloaded") elif cmd.startswith("eval "): if(is_adm): @@ -107,3 +115,10 @@ def stuff(bot,sock): except Exception as e: mesg("Error: "+str(e)) else: mesg("Error: you're not authorized to eval") + elif cmd.startswith("exec "): + if(is_adm): + try: + result=exec(cmd[len("exec "):].strip() or "None") + except Exception as e: + mesg("Error: "+str(e)) + else: mesg("Error: you're not authorized to exec") diff --git a/util.py b/util.py index 930597b..f3d7485 100644 --- a/util.py +++ b/util.py @@ -1,7 +1,8 @@ import irctokens class Util: - def __init__(self,sock): + def __init__(self,sock,config): self.sock=sock + self.config=config self.target="" def send(self,raw: str): print(f"> {raw}") @@ -9,6 +10,9 @@ class Util: def quit(self,msg=None): if msg!=None: self.send("QUIT :"+msg) else: self.send("QUIT") + def nick(self,nick=None): + if nick==None: self.send("NICK "+self.config.self.nick) + else: self.send("NICK "+nick) def mesg(self,msg: str,t=None): if t==None: t=self.target msg=msg.partition("\n")[0]