initial upload to git
This commit is contained in:
parent
314d70d45e
commit
695163453c
|
@ -1,3 +1,4 @@
|
|||
venv
|
||||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
#remember to install the libraries, and you probably should use venvs anyway
|
||||
pip --require-venv install -r requirements.txt
|
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env python3
|
||||
import ircstates,socket,ssl
|
||||
|
||||
from config import *
|
||||
from stuff import stuff
|
||||
|
||||
class bot:
|
||||
server=ircstates.Server(config.server.name)
|
||||
host=config.server.host
|
||||
port=config.server.port
|
||||
|
||||
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)
|
||||
bot()
|
|
@ -0,0 +1,37 @@
|
|||
from util import Util
|
||||
class Command:
|
||||
def __init__(self,sock):
|
||||
self.sock=sock
|
||||
self.util=Util(sock)
|
||||
def mesg(self,msg): self.util.mesg(msg)
|
||||
def exec_cmd(self,command,extra=[None]):
|
||||
if extra!=[None]:
|
||||
eval(f"self.{command}(self.prefix,self.cmd,self.pm,self.line,self.admin,extra)")
|
||||
else:
|
||||
eval(f"self.{command}(self.prefix,self.cmd,self.pm,self.line,self.admin)")
|
||||
def echo(self,prefix,cmd,pm,line,admin):
|
||||
"""simple echo command"""
|
||||
self.util.mesg(cmd.split(" ",1)[1])
|
||||
def help(self,prefix,cmd,pm,line,admin,extra):
|
||||
mesg=self.mesg
|
||||
prefixes=", ".join(extra[0])
|
||||
enabled_commands=extra[1]
|
||||
admin_commands=", ".join(extra[2])
|
||||
try: topic=extra[3]
|
||||
except IndexError: topic=None
|
||||
abs_topics={
|
||||
"prefixes":"available prefixes are {prefixes} or my name"
|
||||
}
|
||||
if topic==None:
|
||||
mesg(f"available topics: "+", ".join(enabled_commands+list(abs_topics.keys())))
|
||||
if admin:
|
||||
mesg(f"admin commands: {admin_commands}")
|
||||
else:
|
||||
try: mesg(f"{topic}: "+eval(f"self.{topic}.__doc__"))
|
||||
except TypeError:
|
||||
if topic in abs_topics:
|
||||
mesg(f"{topic}: "+abs_topics[topic])
|
||||
else:
|
||||
mesg(f"no help available for \"{topic}\"...")
|
||||
except AttributeError: mesg(f"there's no such thing as \"{topic}\"!")
|
||||
except Exception as e: mesg(str(e.__class__)+" "+str(e))
|
|
@ -0,0 +1,27 @@
|
|||
class config:
|
||||
class self:
|
||||
nick="bot6"
|
||||
username="jan6_bot"
|
||||
realname="jan6's bot"
|
||||
class server:
|
||||
name="tilde.chat"
|
||||
host="tilde.chat"
|
||||
port=6697
|
||||
ssl=True
|
||||
channel="#botsix"
|
||||
capabilities=[
|
||||
"message-tags",
|
||||
"multi-prefix",
|
||||
"account-tag",
|
||||
"batch",
|
||||
"account-notify",
|
||||
"chghost",
|
||||
"away-notify"
|
||||
]
|
||||
class admin:
|
||||
accounts=["jan6"]
|
||||
hostmasks=["jan6!jan6@mischievous.deity"]
|
||||
class cmd:
|
||||
prefixes=["6","'"]
|
||||
enabled_commands=["help","echo"]
|
||||
admin_commands=["eval","quit"]
|
|
@ -0,0 +1 @@
|
|||
ircstates ~=0.11.9
|
|
@ -0,0 +1,102 @@
|
|||
import irctokens
|
||||
from config import config
|
||||
from util import Util
|
||||
from command import Command
|
||||
def stuff(bot,sock):
|
||||
chan=config.server.channel
|
||||
util=Util(sock)
|
||||
command=Command(sock)
|
||||
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
|
||||
is_pm=False
|
||||
enabled_commands=config.cmd.enabled_commands
|
||||
admin_commands=config.cmd.admin_commands
|
||||
|
||||
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":
|
||||
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":
|
||||
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:
|
||||
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
|
||||
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
|
||||
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]
|
||||
command.exec_cmd("help",[prefixes,enabled_commands,admin_commands,topic])
|
||||
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])
|
||||
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")
|
|
@ -0,0 +1,18 @@
|
|||
import irctokens
|
||||
class Util:
|
||||
def __init__(self,sock):
|
||||
self.sock=sock
|
||||
self.target=""
|
||||
def send(self,raw: str):
|
||||
print(f"> {raw}")
|
||||
self.sock.sendall(f"{raw}\r\n".encode("utf8"))
|
||||
def quit(self,msg=None):
|
||||
if msg!=None: self.send("QUIT :"+msg)
|
||||
else: self.send("QUIT")
|
||||
def mesg(self,msg: str,t=None):
|
||||
if t==None: t=self.target
|
||||
msg=msg.partition("\n")[0]
|
||||
if len(msg)>=900:
|
||||
msg=msg[:900]
|
||||
mesg("message too long!")
|
||||
self.send(irctokens.build("PRIVMSG", [t,str(msg)]).format())
|
Loading…
Reference in New Issue