commit 4ffaf939698764247c009ea6d4aaea246f16621b Author: Julian Marcos Date: Sun Dec 19 09:05:42 2021 +0000 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6c121f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +.*un~ +*un~ diff --git a/bot.py b/bot.py new file mode 100755 index 0000000..b0e07ac --- /dev/null +++ b/bot.py @@ -0,0 +1,123 @@ +#!/usr/bin/python3 +import socket, rich, asyncio, time, re +from config import * + +class bot: + def raw(msg): + """ + Sends a raw message using the socket s + """ + s.send(f"{msg}\n".encode('utf-8')) + + class logger: + async def log(msg=None, level=0, color=False): + if level <= DebugLVL: + if color: + rich.print(msg); + else: + print(msg); + async def handle(line): + if (line[0] == ':team.tilde.chat' and (line[1] == 'NOTICE' or line[1] == '001' or line[1] == '002' or line[1] == '003' or line[1] == '004' or line[1] == '005' or line[1] == '372' or line[1] == '251' or line[1] == '252' or line[1] == '253' or line[1] == '254' or line[1] == '255' or line[1] == '265' or line[1] == '266' or line[1] == '375' or line[1] == '376' or line[1] == '396') ): + loglevel = 450 + elif line[0] == ':team.tilde.chat': + loglevel = 441 + elif line[0] == ':NickServ!services@services.tilde.chat': + if (line[3] == ':This' or line[3] == ':nick,' or line[3] == ':please' or line[3] == ':If' or line[3] == ':Password'): + loglevel = 450 + else: + loglevel = 441 + elif line[0] == ':ChanServ!services@services.tilde.chat': + loglevel = 442 + elif line[0] == 'PING': + loglevel = 443 + else: + loglevel = 440 + await bot.logger.log(f"{' '.join(line)}", loglevel); + + class get: + def nick(hostmask): + return hostmask[1:].split('!')[0] + def ident(hostmask): + return hostmask[1:].split('!')[1].split('@')[0] + def host(hostmask): + return hostmask[1:].split('!')[1].split('@')[1] + + def nslogin(): + bot.raw(f"PRIVMSG NickServ@services.tilde.chat :IDENTIFY {NICK} {SERVICES_PW}") + bot.raw(f"MODE {NICK} +B") + + async def joinchans(): + bot.raw(f"JOIN {CHANNELS}") + await bot.logger.log(f"[bold]Joined channels: [magenta]{CHANNELS}[/magenta] per request of [magenta]-!-[/magenta][/bold]", color=True) + + async def join(channel, requser="-!-", prefixmsg="Joined"): + bot.raw(f"JOIN :{channel}") + await bot.logger.log(f"[bold]{prefixmsg} [magenta]{channel}[/magenta] per request of [magenta]{requser}[/magenta][/bold]", color=True) + + async def part(channel, msg="Bye, I'be worked enough", requser="-!-"): + bot.raw(f"PART {channel} :{msg}") + await bot.logger.log(f"[bold]Leaved [magenta]{channel}[/magenta] per request of [magenta]{requser}[/magenta][/bold]", color=True) + + async def topic(channel, topic="You should set a topic", requser="-!-"): + bot.raw(f"TOPIC {channel} :{topic}") + await bot.logger.log(f"[bold]Topic of [magenta]{channel}[/magenta] channeged to [blue]{topic}[/blue] per request of [magenta]{requser}[/magenta][/bold]", color=True) + time.sleep(1) + bot.raw(f"PRIVMSG {channel} :Changed topic of {channel}") + + async def quit(msg="Bye, I'be worked enough", requser="-!-"): + bot.raw(f"QUIT :{msg}") + await bot.logger.log(f"[bold]Exiting per request of [magenta]{requser}[/magenta] with message of [magenta]{msg}[/magenta][/bold]", color=True) + + async def setup(): + bot.nslogin() + await bot.join("#chaos") + #await bot.joinchans() + async def ConnectionSetup(): + bot.raw(f"NICK {NICK}") + bot.raw(f"USER {NICK} 0 * :{REALNAME}") + class Handeler: + async def newmsg(line): + if (line[0] == "PING"): + bot.raw(f"PONG {line[1]}") + elif (line[0] == ':NickServ!services@services.tilde.chat' and line[1] == 'NOTICE' and line[2] == 'util' and line[3] == ':If'): + await bot.setup() + elif (line[1] == "INVITE" and line[2] == NICK): + await bot.join(line[3][1:], line[0][1:], prefixmsg="Invited to"); + time.sleep(1) + bot.raw(f"PRIVMSG {line[3][1:]} :Hello, I'm a bot that joined because {line[0][1:]} invited me.") + elif (line[1] == 'PRIVMSG' and line[2] != NICK and line[3] == ":*.help"): + bot.raw(f"PRIVMSG {line[2]} :I am a utility bot.") + elif (line[1] == 'PRIVMSG' and line[3] == ":*.join"): + await bot.join(line[4], line[0][1:]); + time.sleep(1) + bot.raw(f"PRIVMSG {line[4]} :Hello, I'm a bot that joined because {bot.get.nick(line[0])} told me to.") + elif (line[1] == 'PRIVMSG' and line[3] == ":*.part" and ( (line[4] != "#lounge" and line[4] != "#lounge-ops" and line[4] != "#airstrip") or line[0][1:] == 'julian!julian@envs.net') ): + await bot.part(line[4], f"Leaving per request of {bot.get.nick(line[0])}", line[0][1:]); + elif (line[1] == 'PRIVMSG' and line[2] != NICK and line[3] == ":*.topic" and ((line[2]!="#lounge" and line[2]!="#lounge-ops" and line[2]!="#airstrip") or line[0][1:]=='julian!julian@envs.net') ): + await bot.topic(line[2], ' '.join(line[4:]) ,line[0][1:]) + elif (line[1] == 'PRIVMSG' and line[2] != NICK and line[3] == ":*.echo"): + bot.raw(f"PRIVMSG {line[2]} :\x033[Echo]\x0F \x032{bot.get.nick(line[0])}\x0F said: \x039{' '.join(line[4:])}") + elif (line[1] == 'PRIVMSG' and line[2] != NICK and line[3] == ":*.psa" and ((line[4]!="#lounge" and line[4]!="#lounge-ops" and line[4]!="#airstrip") or line[0][1:]=='julian!julian@envs.net') ): + if (line[4].startswith('#')): + bot.raw(f"PRIVMSG {line[4]} :\x0307[Public Service Anouncement]\x0F {' '.join(line[5:])}") + await bot.logger.log(f"[bold]PSA by [magenta]{line[0][1:]}[/magenta] -> [magenta]{line[4]}[/magenta] -> [blue]{' '.join(line[5:])}[/blue][/bold]", color=True) + else: + bot.raw(f"PRIVMSG {bot.get.nick(line[0])} :Sorry, the syntax for PSA is: *.psa ") + +readbuffer="" + +s = socket.socket() +s.connect((HOST, PORT)) +asyncio.run(bot.ConnectionSetup()) + +while True: + readbuffer = readbuffer + s.recv(2048).decode('utf-8') + temp = str.split(readbuffer, "\n") + readbuffer = temp.pop() + + for line in temp: + line = str.rstrip(line) + line = str.split(line) + + asyncio.run(bot.Handeler.newmsg(line)) + asyncio.run(bot.logger.handle(line)) diff --git a/conf/.gitignore b/conf/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/conf/.gitignore @@ -0,0 +1 @@ +* diff --git a/config.py b/config.py new file mode 100644 index 0000000..c74fc02 --- /dev/null +++ b/config.py @@ -0,0 +1,10 @@ +#!/usr/bin/python3 +from fileread import fileload + +DebugLVL = 442 +HOST = fileload('conf/host') +PORT = int(fileload('conf/port')) +NICK = fileload('conf/nick') +REALNAME = fileload('conf/realname') +SERVICES_PW = fileload('conf/pw') +CHANNELS = fileload('conf/chans') diff --git a/fileread.py b/fileread.py new file mode 100755 index 0000000..7baf26c --- /dev/null +++ b/fileread.py @@ -0,0 +1,3 @@ +def fileload(filename): + with open(filename) as f: + return f.read().strip() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3f382dd --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +rich