2024-12-08 16:54:04 -08:00
|
|
|
#!/usr/bin/python3
|
2024-12-08 20:06:57 -08:00
|
|
|
__version__ = "0.0.1-pre-alpha"
|
2024-12-08 16:54:04 -08:00
|
|
|
print(f"INTERNET RELAY CAT v{__version__}")
|
|
|
|
print("Welcome! /ᐠ ˵> ⩊ <˵マ")
|
2024-12-08 19:04:37 -08:00
|
|
|
import socket, time, threading, traceback, sys, os, yaml
|
2024-12-08 16:54:04 -08:00
|
|
|
from requests import get
|
2024-12-08 18:55:43 -08:00
|
|
|
if not len(sys.argv) == 2:
|
|
|
|
print("IRCat requires the following arguments: config.yml")
|
2024-12-08 19:04:37 -08:00
|
|
|
sys.exit(1)
|
|
|
|
server = "127.0.0.1"
|
|
|
|
displayname = "foo"
|
|
|
|
with open(sys.argv[1], 'r') as file:
|
|
|
|
data = yaml.safe_load(file)
|
|
|
|
try: server = data["host"]
|
|
|
|
except: print("using fallback server address")
|
|
|
|
try: displayname = data["name"]
|
|
|
|
except: print("using fallback display name")
|
|
|
|
file.close()
|
2024-12-08 20:03:55 -08:00
|
|
|
print("Successfully loaded config!")
|
2024-12-08 16:54:04 -08:00
|
|
|
ip = get('https://api.ipify.org').content.decode('utf8')
|
2024-12-08 17:36:59 -08:00
|
|
|
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2024-12-08 16:54:04 -08:00
|
|
|
server_address = ('0.0.0.0', 6667)
|
|
|
|
tcp_socket.bind(server_address)
|
|
|
|
tcp_socket.listen(1)
|
|
|
|
nickname_list = {}
|
|
|
|
channels_list = {}
|
|
|
|
flags_list = {}
|
|
|
|
print("Now listening on port 6667")
|
|
|
|
def session(connection, client):
|
2024-12-09 09:30:20 -08:00
|
|
|
pending = "*" # The nickname of the client
|
2024-12-08 20:02:59 -08:00
|
|
|
already_set = False # If the client gave the server a NICK packet
|
|
|
|
ready = False # If the client gave the server a USER packet
|
|
|
|
finished = False # If the server gave the client its information, indicating it's ready.
|
|
|
|
username = "oreo"
|
|
|
|
hostname = client[0]
|
2024-12-08 16:54:04 -08:00
|
|
|
try:
|
|
|
|
print("Connected to client IP: {}".format(client))
|
|
|
|
connection.sendall(bytes(f":{server} NOTICE * :*** Looking for your hostname...\r\n","UTF-8"))
|
|
|
|
connection.sendall(bytes(f":{server} NOTICE * :*** Oof! Can't find your hostname, using IP...\r\n","UTF-8"))
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data = connection.recv(2048)
|
|
|
|
except:
|
|
|
|
print("Disconnected.")
|
|
|
|
print("Received data: {}".format(data))
|
|
|
|
try:
|
|
|
|
textt = data.decode()
|
|
|
|
for text in textt.split("\r\n"):
|
|
|
|
if text.split(" ")[0] == "NICK":
|
|
|
|
pending = text.split(" ")[1]
|
|
|
|
if pending in nickname_list:
|
|
|
|
connection.sendall(bytes(f":{server} 433 * {pending} :Nickname is already in use.\r\n","UTF-8"))
|
2024-12-09 09:30:20 -08:00
|
|
|
pending = "*"
|
2024-12-08 16:54:04 -08:00
|
|
|
else:
|
|
|
|
if not already_set:
|
|
|
|
nickname_list[pending] = connection
|
|
|
|
already_set = True
|
2024-12-08 20:21:29 -08:00
|
|
|
elif text.split(" ")[0].upper() == "USER":
|
2024-12-08 16:54:04 -08:00
|
|
|
if not ready:
|
|
|
|
username = text.split(" ")[1]
|
2024-12-08 20:06:57 -08:00
|
|
|
ready = True
|
2024-12-09 09:30:20 -08:00
|
|
|
elif "CAP LS 302" in text:
|
|
|
|
connection.sendall(bytes(f":{server} CAP * LS :, "UTF-8"))
|
2024-12-08 20:02:59 -08:00
|
|
|
elif (ready and already_set) and not finished:
|
|
|
|
connection.sendall(bytes(f":{server} 001 {pending} :Welcome to the {displayname} Internet Relay Chat Network {pending}\r\n", "UTF-8"))
|
|
|
|
connection.sendall(bytes(f":{server} 002 {pending} :Your host is {server}[{ip}/6667], running version IRCat-v{__version__}\r\n", "UTF-8"))
|
|
|
|
connection.sendall(bytes(f":{server} 004 {pending} {server} IRCat-{__version__} iow ovmsitnlbkq\r\n", "UTF-8"))
|
2024-12-08 20:21:29 -08:00
|
|
|
connection.sendall(bytes(f":{server} 005 {pending} CHANMODES=bq NETWORK={displayname} CHANTYPES=# :are supported by this server\r\n", "UTF-8"))
|
2024-12-08 20:02:59 -08:00
|
|
|
|
|
|
|
connection.sendall(bytes(f":{pending} MODE {pending} +iw\r\n","UTF-8"))
|
|
|
|
finished = True
|
2024-12-08 21:44:32 -08:00
|
|
|
elif text.split(" ")[0].upper() == "PING":
|
|
|
|
e = text.split(" ")[1]
|
|
|
|
print("Replied to PING.")
|
|
|
|
connection.sendall(bytes(f"PONG {e}\r\n","UTF-8"))
|
2024-12-08 20:02:59 -08:00
|
|
|
elif (ready and already_set) and finished:
|
2024-12-08 20:21:29 -08:00
|
|
|
if text.split(" ")[0].upper() == "JOIN":
|
2024-12-08 20:02:59 -08:00
|
|
|
channel = text.split(" ")[1]
|
|
|
|
success = True
|
2024-12-08 22:28:45 -08:00
|
|
|
if channel in channels_list:
|
|
|
|
if pending in channels_list[channel]:
|
|
|
|
success=False
|
|
|
|
print(f"{pending} is already in {channel} , ignoring JOIN request.")
|
2024-12-08 20:02:59 -08:00
|
|
|
if success:
|
|
|
|
try:
|
|
|
|
if channel in channels_list:
|
|
|
|
channels_list[channel].append(pending)
|
|
|
|
else:
|
|
|
|
channels_list[channel] = [pending]
|
|
|
|
except:
|
|
|
|
connection.sendall(bytes(f":{server} NOTICE * :*** Could not join {channel}\r\n","UTF-8"))
|
2024-12-08 22:28:45 -08:00
|
|
|
print(channels_list)
|
|
|
|
for i in channels_list[channel]:
|
|
|
|
try:
|
|
|
|
nickname_list[i].sendall(bytes(f":{pending}!~{username}@{client[0]} JOIN {channel}\r\n","UTF-8"))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
# Code re-used in the WHO command
|
|
|
|
if channel in channels_list:
|
|
|
|
if pending in channels_list[channel]:
|
|
|
|
users = " ".join(channels_list[channel])
|
|
|
|
connection.sendall(bytes(f":{server} 353 {pending} = {channel} :{users}\r\n","UTF-8"))
|
|
|
|
connection.sendall(bytes(f":{server} 366 {pending} {channel} :End of /NAMES list.\r\n","UTF-8"))
|
2024-12-08 20:21:29 -08:00
|
|
|
elif text.split(" ")[0].upper() == "PART":
|
2024-12-08 20:02:59 -08:00
|
|
|
channel = text.split(" ")[1]
|
|
|
|
for i in channels_list[channel]:
|
|
|
|
try:
|
|
|
|
nickname_list[i].sendall(bytes(f":{pending}!~{username}@{client[0]} {text}\r\n","UTF-8"))
|
|
|
|
except:
|
|
|
|
pass
|
2024-12-08 16:54:04 -08:00
|
|
|
try:
|
2024-12-08 20:02:59 -08:00
|
|
|
channels_list[channel].remove(pending)
|
2024-12-08 16:54:04 -08:00
|
|
|
except:
|
2024-12-08 20:02:59 -08:00
|
|
|
print(traceback.format_exc())
|
2024-12-08 20:21:29 -08:00
|
|
|
elif text.split(" ")[0].upper() == "WHO":
|
2024-12-08 20:02:59 -08:00
|
|
|
channel = text.split(" ")[1]
|
|
|
|
if channel in channels_list:
|
|
|
|
if pending in channels_list[channel]:
|
|
|
|
users = " ".join(channels_list[channel])
|
|
|
|
connection.sendall(bytes(f":{server} 353 {pending} = {channel} :{users}\r\n","UTF-8"))
|
|
|
|
connection.sendall(bytes(f":{server} 366 {pending} {channel} :End of /NAMES list.\r\n","UTF-8"))
|
2024-12-08 20:21:29 -08:00
|
|
|
elif text.split(" ")[0].upper() == "PRIVMSG":
|
2024-12-08 20:02:59 -08:00
|
|
|
target = text.split(" ")[1]
|
|
|
|
if target in channels_list:
|
|
|
|
if pending in channels_list[target]:
|
|
|
|
for i in channels_list[channel]:
|
|
|
|
try:
|
|
|
|
if i != pending:
|
|
|
|
nickname_list[i].sendall(bytes(f":{pending}!~{username}@{hostname} {text}\r\n","UTF-8"))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
elif target in nickname_list:
|
|
|
|
nickname_list[target].sendall(bytes(f":{pending}!~{username}@{hostname} {text}\r\n","UTF-8"))
|
|
|
|
else:
|
|
|
|
nickname_list[target].sendall(bytes(f":{server} 401 {pending} {target} :No such nick/channel\r\n","UTF-8"))
|
|
|
|
nickname_list[i].sendall(bytes(f":{server} 366 {pending} {channel} :End of /NAMES list.\r\n","UTF-8"))
|
2024-12-08 20:21:29 -08:00
|
|
|
elif text.split(" ")[0].upper() == "QUIT":
|
2024-12-08 20:02:59 -08:00
|
|
|
# Parse the quit message.
|
|
|
|
done = []
|
|
|
|
msg = text.split(" ")[1:]
|
|
|
|
if len(msg) > 0:
|
|
|
|
mse = " ".join(msg)
|
|
|
|
msg = f"Quit: {mse}"
|
|
|
|
else:
|
|
|
|
msg = "Client Quit"
|
|
|
|
text = f"QUIT :{msg}"
|
2024-12-08 20:21:29 -08:00
|
|
|
# Broadcast all users in the joined channels that the person quit.
|
2024-12-08 20:02:59 -08:00
|
|
|
for i, users in channels_list.items():
|
|
|
|
if pending in users:
|
|
|
|
for j in users:
|
|
|
|
if j != pending and not j in done:
|
|
|
|
nickname_list[j].sendall(bytes(f":{pending}!~{username}@{client[0]} {text}\r\n","UTF-8"))
|
|
|
|
done.append(j)
|
|
|
|
# Remove the quitting user from the channel.
|
2024-12-08 16:54:04 -08:00
|
|
|
try:
|
2024-12-08 20:02:59 -08:00
|
|
|
channels_list[i].remove(pending)
|
2024-12-08 16:54:04 -08:00
|
|
|
except:
|
2024-12-08 20:02:59 -08:00
|
|
|
print(traceback.format_exc())
|
2024-12-08 20:48:30 -08:00
|
|
|
# Confirm QUIT and close the socket.
|
|
|
|
connection.sendall(bytes(f":{pending}!~{username}@{client[0]} {text}\r\n","UTF-8"))
|
|
|
|
connection.sendall(bytes(f"ERROR :Closing Link: {client[0]} ({msg})\r\n","UTF-8"))
|
|
|
|
connection.close()
|
2024-12-08 20:02:59 -08:00
|
|
|
break
|
2024-12-09 09:30:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
cmd = text.split(" ")[0]
|
|
|
|
connection.sendall(bytes(f":{server} 421 {pending} {cmd} :Unknown command\r\n","UTF-8"))
|
2024-12-08 21:44:32 -08:00
|
|
|
|
2024-12-08 16:54:04 -08:00
|
|
|
except:
|
|
|
|
print(traceback.format_exc())
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
finally:
|
|
|
|
connection.close()
|
2024-12-09 09:30:20 -08:00
|
|
|
if pending != "*":
|
2024-12-08 16:54:04 -08:00
|
|
|
del nickname_list[pending]
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
connection, client = tcp_socket.accept()
|
|
|
|
threading.Thread(target=session, daemon=True, args=[connection, client]).start()
|
|
|
|
except:
|
|
|
|
print("Shutting down...")
|
|
|
|
time.sleep(2)
|
2024-12-08 17:36:59 -08:00
|
|
|
tcp_socket.shutdown(1)
|
2024-12-08 16:54:04 -08:00
|
|
|
tcp_socket.close()
|