I forgot what I added, lol, nick change, reloadable config...ish, exec, anything else? lol
This commit is contained in:
parent
68ee237c14
commit
e96f884424
24
bot.py
24
bot.py
|
@ -3,6 +3,7 @@ import ircstates,socket,ssl
|
||||||
|
|
||||||
from config import *
|
from config import *
|
||||||
from stuff import stuff
|
from stuff import stuff
|
||||||
|
from util import Util
|
||||||
|
|
||||||
class bot:
|
class bot:
|
||||||
server=ircstates.Server(config.server.name)
|
server=ircstates.Server(config.server.name)
|
||||||
|
@ -12,12 +13,19 @@ class bot:
|
||||||
if __name__==__module__=="__main__":
|
if __name__==__module__=="__main__":
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
if config.server.ssl:
|
if config.server.ssl:
|
||||||
with socket.create_connection((self.host, self.port)) as sock_raw:
|
with socket.create_connection((self.host, self.port)) as sock_raw:
|
||||||
ctx=ssl.create_default_context()
|
ctx=ssl.create_default_context()
|
||||||
with ctx.wrap_socket(sock_raw, server_hostname=self.host) as sock:
|
with ctx.wrap_socket(sock_raw, server_hostname=self.host) as sock:
|
||||||
stuff(self,sock)
|
try:
|
||||||
else:
|
util=Util(sock,config)
|
||||||
with socket.create_connection((self.host, self.port)) as sock:
|
stuff(self,sock)
|
||||||
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()
|
bot()
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
from util import Util
|
from util import Util
|
||||||
class Command:
|
class Command:
|
||||||
def __init__(self,sock):
|
def __init__(self,sock,config):
|
||||||
self.sock=sock
|
self.sock=sock
|
||||||
self.util=Util(sock)
|
self.config=config
|
||||||
|
self.util=Util(sock,config)
|
||||||
def mesg(self,msg): self.util.mesg(msg)
|
def mesg(self,msg): self.util.mesg(msg)
|
||||||
def err_perm(self,level="admin"): mesg("Error: insufficient privileges, you lack {level} access")
|
def err_perm(self,level="admin"): mesg("Error: insufficient privileges, you lack {level} access")
|
||||||
def exec_cmd(self,command,extra=[None]):
|
def exec_cmd(self,command,extra=[None]):
|
||||||
|
|
|
@ -24,4 +24,4 @@ class config:
|
||||||
class cmd:
|
class cmd:
|
||||||
prefixes=["6","'"]
|
prefixes=["6","'"]
|
||||||
enabled_commands=["help","echo"]
|
enabled_commands=["help","echo"]
|
||||||
admin_commands=["eval","quit","reload"]
|
admin_commands=["eval","exec","quit"]
|
||||||
|
|
41
stuff.py
41
stuff.py
|
@ -5,23 +5,29 @@ from commands import Command
|
||||||
import sys, importlib
|
import sys, importlib
|
||||||
def stuff(bot,sock):
|
def stuff(bot,sock):
|
||||||
config=Config
|
config=Config
|
||||||
chan=config.server.channel
|
util=Util(sock,config)
|
||||||
util=Util(sock)
|
command=Command(sock,config)
|
||||||
command=Command(sock)
|
|
||||||
server=bot.server
|
server=bot.server
|
||||||
send=util.send
|
send=util.send
|
||||||
quit=util.quit
|
quit=util.quit
|
||||||
mesg=util.mesg
|
mesg=util.mesg
|
||||||
|
|
||||||
server_caps=[]
|
server_caps=[]
|
||||||
wanted_caps=config.capabilities
|
wanted_caps=config.capabilities
|
||||||
mode="init"
|
chan=config.server.channel #autojoin channel
|
||||||
prefixes=config.cmd.prefixes
|
|
||||||
admin_accounts=config.admin.accounts
|
|
||||||
admin_users=config.admin.hostmasks
|
|
||||||
is_pm=False
|
is_pm=False
|
||||||
enabled_commands=config.cmd.enabled_commands
|
mode="init"
|
||||||
admin_commands=config.cmd.admin_commands
|
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("NICK", [config.self.nick]).format())
|
||||||
send(irctokens.build("USER", [config.self.username,"0","*",config.self.realname]).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())
|
send(irctokens.build("JOIN", [chan]).format())
|
||||||
mode="normal"
|
mode="normal"
|
||||||
elif mode=="normal":
|
elif mode=="normal":
|
||||||
|
if line.command == "433":
|
||||||
|
mesg("nick in use!",chan)
|
||||||
|
util.nick(config.self.nick)
|
||||||
if line.command == "INVITE":
|
if line.command == "INVITE":
|
||||||
send(irctokens.build("JOIN", [line.params[1]]).format())
|
send(irctokens.build("JOIN", [line.params[1]]).format())
|
||||||
elif line.command == "PRIVMSG":
|
elif line.command == "PRIVMSG":
|
||||||
|
@ -84,6 +93,7 @@ def stuff(bot,sock):
|
||||||
command.pm=is_pm
|
command.pm=is_pm
|
||||||
command.cmd=cmd
|
command.cmd=cmd
|
||||||
command.admin=is_adm
|
command.admin=is_adm
|
||||||
|
command.config=config
|
||||||
if cmd.startswith("echo "):
|
if cmd.startswith("echo "):
|
||||||
command.exec_cmd("echo")
|
command.exec_cmd("echo")
|
||||||
if cmd=="help":
|
if cmd=="help":
|
||||||
|
@ -96,9 +106,7 @@ def stuff(bot,sock):
|
||||||
elif is_adm and (cmd.startswith("q ") or cmd.startswith("quit")):
|
elif is_adm and (cmd.startswith("q ") or cmd.startswith("quit")):
|
||||||
quit(cmd.split(" ",1)[1])
|
quit(cmd.split(" ",1)[1])
|
||||||
elif is_adm and cmd.startswith("reload"):
|
elif is_adm and cmd.startswith("reload"):
|
||||||
# command.exec_cmd("reload")
|
command,config,util,prefixes,admin_accounts,admin_users,enabled_command,admin_commands=configure()
|
||||||
command=importlib.reload(sys.modules["commands"]).Command(sock)
|
|
||||||
config=importlib.reload(sys.modules["config"]).config
|
|
||||||
mesg("reloaded")
|
mesg("reloaded")
|
||||||
elif cmd.startswith("eval "):
|
elif cmd.startswith("eval "):
|
||||||
if(is_adm):
|
if(is_adm):
|
||||||
|
@ -107,3 +115,10 @@ def stuff(bot,sock):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
mesg("Error: "+str(e))
|
mesg("Error: "+str(e))
|
||||||
else: mesg("Error: you're not authorized to eval")
|
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")
|
||||||
|
|
6
util.py
6
util.py
|
@ -1,7 +1,8 @@
|
||||||
import irctokens
|
import irctokens
|
||||||
class Util:
|
class Util:
|
||||||
def __init__(self,sock):
|
def __init__(self,sock,config):
|
||||||
self.sock=sock
|
self.sock=sock
|
||||||
|
self.config=config
|
||||||
self.target=""
|
self.target=""
|
||||||
def send(self,raw: str):
|
def send(self,raw: str):
|
||||||
print(f"> {raw}")
|
print(f"> {raw}")
|
||||||
|
@ -9,6 +10,9 @@ class Util:
|
||||||
def quit(self,msg=None):
|
def quit(self,msg=None):
|
||||||
if msg!=None: self.send("QUIT :"+msg)
|
if msg!=None: self.send("QUIT :"+msg)
|
||||||
else: self.send("QUIT")
|
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):
|
def mesg(self,msg: str,t=None):
|
||||||
if t==None: t=self.target
|
if t==None: t=self.target
|
||||||
msg=msg.partition("\n")[0]
|
msg=msg.partition("\n")[0]
|
||||||
|
|
Loading…
Reference in New Issue