2021-09-16 17:35:06 +00:00
|
|
|
import irctokens
|
2021-09-19 16:56:17 +00:00
|
|
|
from config import config as Config
|
2021-09-16 17:35:06 +00:00
|
|
|
from util import Util
|
2021-09-19 16:56:17 +00:00
|
|
|
from commands import Command
|
|
|
|
import sys, importlib
|
2021-09-16 17:35:06 +00:00
|
|
|
def stuff(bot,sock):
|
2021-09-19 16:56:17 +00:00
|
|
|
config=Config
|
2021-09-19 18:33:30 +00:00
|
|
|
util=Util(sock,config)
|
|
|
|
command=Command(sock,config)
|
2021-09-16 17:35:06 +00:00
|
|
|
server=bot.server
|
|
|
|
send=util.send
|
|
|
|
quit=util.quit
|
|
|
|
mesg=util.mesg
|
|
|
|
server_caps=[]
|
|
|
|
wanted_caps=config.capabilities
|
2021-09-19 18:33:30 +00:00
|
|
|
chan=config.server.channel #autojoin channel
|
2021-09-16 17:35:06 +00:00
|
|
|
is_pm=False
|
2021-09-19 18:33:30 +00:00
|
|
|
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()
|
2021-09-16 17:35:06 +00:00
|
|
|
|
|
|
|
send(irctokens.build("NICK", [config.self.nick]).format())
|
|
|
|
send(irctokens.build("USER", [config.self.username,"0","*",config.self.realname]).format())
|
|
|
|
while True:
|
|
|
|
self_nick = server.nickname
|
|
|
|
recv_data = sock.recv(1024)
|
|
|
|
recv_lines = server.recv(recv_data)
|
|
|
|
|
|
|
|
for line in recv_lines:
|
|
|
|
server.parse_tokens(line)
|
|
|
|
print(f"< {line.format()}")
|
|
|
|
|
|
|
|
if line.command == "PING":
|
|
|
|
send(f"PONG :{line.params[0]}")
|
|
|
|
|
|
|
|
if mode=="init":
|
2021-09-21 17:41:31 +00:00
|
|
|
if line.command == "433":
|
|
|
|
util.nick(config.self.nick+"_")
|
2021-09-16 17:35:06 +00:00
|
|
|
if line.command == "376":
|
|
|
|
send(irctokens.build("CAP", ["LS","302"]).format())
|
|
|
|
elif line.command=="CAP" and line.params[1]=="LS":
|
|
|
|
if server_caps==[]: server_caps=line.params[2].split()
|
|
|
|
caps=[value for value in wanted_caps if value in server_caps]
|
|
|
|
#single-send is more efficient
|
|
|
|
#BUT a single invalid cap == no caps enabled at all!!!
|
|
|
|
send(irctokens.build("CAP", ["REQ"," ".join(caps)]).format())
|
|
|
|
#for i in caps:
|
|
|
|
# send(irctokens.build("CAP", ["REQ",i]).format())
|
|
|
|
send(irctokens.build("CAP", ["END"]).format())
|
|
|
|
mode="boot"
|
|
|
|
elif mode=="boot":
|
|
|
|
send(irctokens.build("JOIN", [chan]).format())
|
|
|
|
mode="normal"
|
|
|
|
elif mode=="normal":
|
2021-09-19 18:33:30 +00:00
|
|
|
if line.command == "433":
|
|
|
|
mesg("nick in use!",chan)
|
|
|
|
util.nick(config.self.nick)
|
2021-09-16 17:35:06 +00:00
|
|
|
if line.command == "INVITE":
|
|
|
|
send(irctokens.build("JOIN", [line.params[1]]).format())
|
|
|
|
elif line.command == "PRIVMSG":
|
|
|
|
if not "batch" in line.tags:
|
|
|
|
is_pm=False
|
|
|
|
target=line.params[0]
|
|
|
|
if target==self_nick:
|
|
|
|
target=line.source.split("!")[0]
|
|
|
|
is_pm=True
|
|
|
|
util.target=target
|
|
|
|
command.util.target=target
|
|
|
|
if is_pm or line.params[1].startswith(self_nick) or line.params[1][0] in prefixes:
|
2021-09-21 17:41:31 +00:00
|
|
|
try:
|
|
|
|
cmd=line.params[1]
|
|
|
|
#if message in a channel, remove prefixes
|
|
|
|
if not is_pm:
|
|
|
|
if cmd[0] in prefixes:
|
|
|
|
cmd=cmd.replace(cmd[0], '', 1)
|
|
|
|
command.prefix=cmd[0]
|
|
|
|
elif cmd.startswith(self_nick+":"):
|
|
|
|
cmd=cmd.replace(self_nick+":", '', 1)
|
|
|
|
command.prefix=self_nick
|
|
|
|
elif cmd.startswith(self_nick):
|
|
|
|
cmd=cmd.replace(self_nick, '', 1)
|
|
|
|
command.prefix=self_nick
|
|
|
|
except IndexError:
|
|
|
|
cmd="none of yer biznez"
|
2021-09-16 17:35:06 +00:00
|
|
|
cmd=cmd.strip()
|
|
|
|
is_adm=line.source in admin_users or line.tags["account"] in admin_accounts
|
|
|
|
command.line=line
|
|
|
|
command.pm=is_pm
|
|
|
|
command.cmd=cmd
|
|
|
|
command.admin=is_adm
|
2021-09-19 18:33:30 +00:00
|
|
|
command.config=config
|
2021-09-16 17:35:06 +00:00
|
|
|
if cmd.startswith("echo "):
|
|
|
|
command.exec_cmd("echo")
|
|
|
|
if cmd=="help":
|
|
|
|
command.exec_cmd("help",[prefixes,enabled_commands,admin_commands])
|
|
|
|
if cmd.startswith("help "):
|
|
|
|
topic=cmd.split(" ",1)[1]
|
2021-09-21 17:41:31 +00:00
|
|
|
command.exec_cmd("help",[prefixes,enabled_commands,admin_commands,topic,self_nick])
|
2021-09-16 17:35:06 +00:00
|
|
|
elif is_adm and (cmd=="q" or cmd=="quit"):
|
|
|
|
quit()
|
|
|
|
elif is_adm and (cmd.startswith("q ") or cmd.startswith("quit")):
|
|
|
|
quit(cmd.split(" ",1)[1])
|
2021-09-19 16:56:17 +00:00
|
|
|
elif is_adm and cmd.startswith("reload"):
|
2021-09-19 18:33:30 +00:00
|
|
|
command,config,util,prefixes,admin_accounts,admin_users,enabled_command,admin_commands=configure()
|
2021-09-19 16:56:17 +00:00
|
|
|
mesg("reloaded")
|
2021-09-16 17:35:06 +00:00
|
|
|
elif cmd.startswith("eval "):
|
|
|
|
if(is_adm):
|
|
|
|
try:
|
|
|
|
result=eval(cmd[len("eval "):].strip() or "None")
|
|
|
|
except Exception as e:
|
|
|
|
mesg("Error: "+str(e))
|
|
|
|
else: mesg("Error: you're not authorized to eval")
|
2021-09-19 18:33:30 +00:00
|
|
|
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")
|