util/bot.py

70 lines
3.2 KiB
Python
Executable File

#!/usr/bin/python3
import socket, rich, asyncio, time, re, ssl, ircstates
from config import *
def _send(msg: str, log=True):
s.send(f"{msg}\n".encode('utf-8'))
if log == True: print(f"> {msg}")
def usermodes(chan, user):
try:
return srv.channels[chan].users[user].modes
except:
return []
def is_admin(source):
return 'julian@envs.net' == source.split('!')[1]
def nslogin():
_send(f"PRIVMSG NickServ@services.tilde.chat :IDENTIFY {NICK} {SERVICES_PW}", log=False)
print("Logged into services")
_send(f"MODE {NICK} +B")
_send(f"JOIN {CHANNELS}")
for i in db['chan'].all():
_send(f"JOIN {[i['name']]}")
def joinchannel(channel):
_send(f"JOIN {channel}")
db['chan'].insert(dict(name=i))
srv = ircstates.Server(NETNAME)
s = socket.socket()
ctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
if tls == True:
s = ctx.wrap_socket(s)
s.connect((HOST, PORT))
_send(f"NICK {NICK}")
_send(f"USER {NICK} 0 * :{REALNAME}")
try:
while True:
recv_data = s.recv(1024)
recv_lines = srv.recv(recv_data)
for line in recv_lines:
srv.parse_tokens(line)
if line.command == "PING": _send(f"PONG :{line.params[0]}", log=False)
elif (line.source == f"NickServ!{SERVICES_HOSTMASK}" and line.command == 'NOTICE' and line.params == ['util','If you do not change within 1 minute, I will change your nick.']): nslogin()
elif (line.command == "INVITE" and line.params[0] == NICK): joinchannel(line.params[1])
elif line.command == "!botlist": _send(f"PRIVMSG {line.source} :I am a utility bot. Public commands *.echo | Ran by ~julian@~team/~jmjl@{~town,~club}")
elif (line.command == "PRIVMSG" and line.params[1].startswith('*.') ):
commandargs = line.params[1].split(' ')
command = ''.join(commandargs[0].split('*.')[1:])
args = commandargs[1:]
usernick = line.source.split('!')[0]
chan = line.params[0]
if command == "join" and is_admin(line.source): joinchannel(args[0])
elif command == "help": _send(f"PRIVMSG {chan} :I am a utility bot. Ran by ~julian@~team/~jmjl@{~town,~club}")
elif command == "echo": _send(f"PRIVMSG {chan} :\x033[Echo]\x0F \x032{usernick}\x0F said: \x039{' '.join(args[0:])}")
elif command == "psa" and args[0].startswith('#'):
if 'o' in usermodes(args[0], usernick) or 'q' in usermodes(args[0], usernick): _send(f"PRIVMSG {args[0]} :\x0307**PSA:\x0F {' '.join(args[1:])}")
else: _send(f"PRIVMSG {usernick} :Sorry, you aren't allowed to make public service anouncements.")
elif command == "psa" and not args[0].startswith('#'): _send(f"PRIVMSG {usernick} :Sorry, the syntax for PSA is: *.psa <channel> <MESSAGE>")
elif ('o' in usermodes(chan, usernick) or 'q' in usermodes(chan, usernick)) or is_admin(line.source):
if command == "part": _send(f"PART {args[0]} :Leaving per request of {usernick}")
if command == "topic": _send(f"TOPIC {chan} :{' '.join(args[0:])}")
except KeyboardInterrupt: _send("QUIT :^C")