util/parse.py

113 lines
5.5 KiB
Python

import socket, asyncio, time, re, ircstates, importlib
from config import *
import shared
def _send(msg: str, log=True):
shared.sock.send(f"{msg}\n".encode('utf-8'))
if log == True: print(f"> {msg}")
def usermodes(chan, user, srv):
try: return srv.channels[chan].users[user].modes
except KeyError: return []
#def _send(msg: str, log=True):
#s.send(f"{msg}\n".encode('utf-8'))
#if log == True: print(f"> {msg}")
def botdesc():
return fileload('desc')
db['oper'].insert(dict(hostmask='julian!julian@envs.net'))
def is_admin(source):
#return 'julian@envs.net' == source.split('!')[1]
#db['oper'].insert(dict(hostmask='friend!good@person'))
#db['oper'].delete(hostmask='nick!badowner@bad.opers')
try:
for i in db['oper'].all():
if source == i['hostmask']: return True
else: continue
except:
_send(f"PRIVMSG julian :{source} isn't operator, permission denied for {source.split('!')[0]}.")
return False
return False
def nslogin():
_send(f"PRIVMSG NickServ@services.tilde.chat :IDENTIFY {NICK} {SERVICES_PW}", log=False)
print("Logged into services")
_send(f"MODE {NICK} +B")
Chan.joins()
class Chan:
def joins():
for i in db['chan'].all():
_send(f"JOIN {','.join([i['name']])}")
def join(channel):
_send(f"JOIN {channel}")
db['chan'].insert(dict(name=channel))
def leave(channel, msg="", kick=False):
if not kick: _send(f"PART {channel} :{msg}")
db['chan'].delete(name=channel)
class Command:
def ExplicitCommandParser(line, srv):
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 == "help": _send(f"PRIVMSG {chan} :{botdesc()}")
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, srv) or 'q' in usermodes(args[0], usernick, srv): _send(f"PRIVMSG {args[0]} :\x0307**PSA:\x0F {' '.join(args[1:])}")
else: _send(f"PRIVMSG {chan} :{usernick}: Sorry, you aren't allowed to make public service anouncements.")
elif command == "psa" and not args[0].startswith('#'): _send(f"PRIVMSG {chan} :Sorry, the syntax for PSA is: *.psa <channel> <MESSAGE>")
elif 'o' in usermodes(chan, usernick, srv) or 'q' in usermodes(chan, usernick, srv):
if command == "part": Chan.leave(chan, msg=f"Leaving per request of {usernick}")
if command == "topic": _send(f"TOPIC {chan} :{' '.join(args[0:])}")
if command.endswith("op"):
cmd = command.split("op")[0]
if cmd == 'de': _send("MODE {} -vhoaq {}".format(chan, 5*f"{usernick} "))
elif command == "op": _send("MODE {} +vhoa {}".format(chan, 4*f"{usernick} "))
elif cmd == "deh": _send("MODE {} -h {}".format(chan, 1*f"{usernick} "))
elif cmd == "dev": _send("MODE {} -v {}".format(chan, 1*f"{usernick} "))
elif cmd == "dea": _send("MODE {} -a {}".format(chan, 1*f"{usernick} "))
elif cmd == "deo": _send("MODE {} -o {}".format(chan, 1*f"{usernick} "))
def AdminCommandParser(line, srv):
cmdargs = line.params[1].split(' ')
cmd = ''.join(cmdargs[0].split('%')[1:])
args = cmdargs[1:]
usernick = line.source.split('!')[0]
chan = line.params[0]
if cmd == "mode": _send(f"MODE {chan} {' '.join(args)}")
elif cmd == "quit": _send(f"QUIT :{' '.join(args)}")
elif cmd == "msg": _send(f"PRIVMSG {args[0]} :{' '.join(args[1:])}")
elif cmd == "raw": _send(f"{' '.join(args)}")
elif cmd == "join": Chan.join(args[0])
elif cmd == "part": Chan.leave(args[0])
elif cmd == "regdrop":
_send(f"PRIVMSG ChanServ :REGISTER {chan}")
_send(f"PRIVMSG ChanServ :DROP {chan} {chan}")
_send(f"MODE -o {usernick}")
elif cmd == "eval":
try: _send(f"PRIVMSG {chan} :{eval(' '.join(args))}")
except Exception as e: _send(f"PRIVMSG {chan} :Exception({e})")
def privmsg(line, srv):
if line.params[1].startswith('%') and is_admin(line.source): Command.AdminCommandParser(line, srv)
elif line.params[0] != NICK and line.params[1] == "!botlist": _send(f"PRIVMSG {line.params[0]} :{botdesc()}")
elif line.params[0] == NICK and line.params[1] == "!botlist": _send(f"PRIVMSG {line.source.split('!')[0]} :{botdesc()}")
if line.params[1].startswith('*.'): Command.ExplicitCommandParser(line, srv)
def Parse(_send=None, srv=None, line=None):
if not line.command == "PING": print(f"< {line.format()}")
if line.command == "PING":
_send(f"PONG :{line.params[0]}", log=False)
if shared.nick_has_changed: _send(f"NICK {NICK}")
elif line.command == "433": _send(f"NICK {NICK}_"); shared.nick_has_changed = True
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): Chan.join(line.params[1])
elif line.command == "KICK" and line.params[1] == NICK: Chan.leave(line.params[0], kick=True)
elif line.command == "PRIVMSG": Command.privmsg(line, srv)