Update server.py

This commit is contained in:
Nova Cat 2025-02-05 18:59:40 -08:00
parent 95f13965dc
commit 3b7ae6f42a

193
server.py
View file

@ -209,7 +209,7 @@ def pinger(nick, connection):
time.sleep(0.5) time.sleep(0.5)
try: try:
connection.sendall(bytes(f"PING {server}\r\n","UTF-8")) connection.sendall(bytes(f"PING {server}\r\n","UTF-8"))
except (SSL.WantReadError, SSL.WantWriteError, SSL.WantX509LookupError): except (SSL.WantReadError, SSL.WantWriteError, SSL.WantX509LookupError, SSL.SysCallError):
pass pass
except Exception as ex: except Exception as ex:
print(traceback.format_exc()) print(traceback.format_exc())
@ -256,6 +256,13 @@ def session(connection, client, ip, isssl=False):
# ^^^^^^^^^ # ^^^^^^^^^
# Cutting these off the string # Cutting these off the string
return tags + (" " if tags != "" else "") return tags + (" " if tags != "" else "")
def dosend(content):
if content.__class__.__name__ != "bytes":
content = bytes(content, "UTF-8")
try:
connection.sendall(content)
except (SSL.WantReadError, SSL.WantWriteError, SSL.WantX509LookupError, SSL.SysCallError):
print("Soft error occurred")
def tags_diffclient(nick:str): # Get tags of another client def tags_diffclient(nick:str): # Get tags of another client
othercap = property_list[nick]["v3cap"] othercap = property_list[nick]["v3cap"]
tags = "" tags = ""
@ -269,27 +276,27 @@ def session(connection, client, ip, isssl=False):
# tlsver = connection.version() # tlsver = connection.version()
# print(f"Got SSL version: {tlsver}") # print(f"Got SSL version: {tlsver}")
connection.settimeout(None) connection.settimeout(None)
connection.sendall(bytes(f":{server} NOTICE * :*** Looking for your hostname...\r\n","UTF-8")) dosend(bytes(f":{server} NOTICE * :*** Looking for your hostname...\r\n","UTF-8"))
connection.sendall(bytes(f":{server} NOTICE * :*** Checking your ident...\r\n","UTF-8")) dosend(bytes(f":{server} NOTICE * :*** Checking your ident...\r\n","UTF-8"))
try: try:
hostname = socket.gethostbyaddr(client[0])[0] hostname = socket.gethostbyaddr(client[0])[0]
connection.sendall(bytes(f":{server} NOTICE * :*** Got hostname! {hostname}\r\n","UTF-8")) dosend(bytes(f":{server} NOTICE * :*** Got hostname! {hostname}\r\n","UTF-8"))
except: except:
hostname = client[0] hostname = client[0]
connection.sendall(bytes(f":{server} NOTICE * :*** Oof! Can't find your hostname, using IP...\r\n","UTF-8")) dosend(bytes(f":{server} NOTICE * :*** Oof! Can't find your hostname, using IP...\r\n","UTF-8"))
try: try:
identQuery = getident(hostname, client[1], isssl) identQuery = getident(hostname, client[1], isssl)
responseee = identQuery["response"] responseee = identQuery["response"]
print(identQuery) print(identQuery)
if not identQuery["success"]: if not identQuery["success"]:
connection.sendall(bytes(f":{server} NOTICE * :*** Uhm, Couldn't find your ident: {responseee}\r\n","UTF-8")) dosend(bytes(f":{server} NOTICE * :*** Uhm, Couldn't find your ident: {responseee}\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f":{server} NOTICE * :*** Got ident! {responseee}\r\n","UTF-8")) dosend(bytes(f":{server} NOTICE * :*** Got ident! {responseee}\r\n","UTF-8"))
clident = responseee clident = responseee
rident = responseee rident = responseee
except: except:
print(traceback.format_exc()) print(traceback.format_exc())
connection.sendall(bytes(f":{server} NOTICE * :*** Uhm, Couldn't find your ident: Unknown error.\r\n","UTF-8")) dosend(bytes(f":{server} NOTICE * :*** Uhm, Couldn't find your ident: Unknown error.\r\n","UTF-8"))
while True: while True:
try: try:
data = connection.recv(2048) data = connection.recv(2048)
@ -319,7 +326,7 @@ def session(connection, client, ip, isssl=False):
# print(f"Sending ping msg to {pending}") # print(f"Sending ping msg to {pending}")
# ping_pending = True # ping_pending = True
# time.sleep(0.5) # time.sleep(0.5)
# connection.sendall(bytes(f"PING {server}\r\n","UTF-8")) # dosend(bytes(f"PING {server}\r\n","UTF-8"))
# elif ping_pending and (time.time() - last_ping) > ping_timeout: # elif ping_pending and (time.time() - last_ping) > ping_timeout:
# cause = f"Ping timeout: {ping_timeout} seconds" # cause = f"Ping timeout: {ping_timeout} seconds"
# print(f"{pending} timed out.") # print(f"{pending} timed out.")
@ -338,10 +345,10 @@ def session(connection, client, ip, isssl=False):
pending = text.split(" ")[1] pending = text.split(" ")[1]
if pending[0] == ":": pending = pending[1:] if pending[0] == ":": pending = pending[1:]
if not isalphanumeric(pending): if not isalphanumeric(pending):
connection.sendall(bytes(f"{tags()}:{server} 432 * {pending} :Erroneus nickname\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 432 * {pending} :Erroneus nickname\r\n","UTF-8"))
pending = "*" pending = "*"
elif pending.lower() in lower_nicks: elif pending.lower() in lower_nicks:
connection.sendall(bytes(f"{tags()}:{server} 433 * {pending} :Nickname is already in use.\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 433 * {pending} :Nickname is already in use.\r\n","UTF-8"))
pending = "*" pending = "*"
else: else:
already_set = True already_set = True
@ -353,7 +360,7 @@ def session(connection, client, ip, isssl=False):
ready = True ready = True
elif command == "CAP": elif command == "CAP":
if args[0].upper() == "LS": if args[0].upper() == "LS":
connection.sendall(bytes(f"{tags()}:{server} CAP * LS :server-time\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} CAP * LS :server-time\r\n", "UTF-8"))
elif args[0].upper() == "REQ": elif args[0].upper() == "REQ":
usesIRCv3 = True # Halt the registration process until CAP END usesIRCv3 = True # Halt the registration process until CAP END
capabilities = " ".join(args[1:])[1:].split(" ") capabilities = " ".join(args[1:])[1:].split(" ")
@ -366,9 +373,9 @@ def session(connection, client, ip, isssl=False):
break break
capper = " ".join(capabilities) capper = " ".join(capabilities)
if capsuccess: if capsuccess:
connection.sendall(bytes(f":{server} CAP * ACK :{capper}\r\n", "UTF-8")) dosend(bytes(f":{server} CAP * ACK :{capper}\r\n", "UTF-8"))
else: else:
connection.sendall(bytes(f":{server} CAP * NAK :{capper}\r\n", "UTF-8")) dosend(bytes(f":{server} CAP * NAK :{capper}\r\n", "UTF-8"))
elif args[0].upper() == "END": elif args[0].upper() == "END":
CAPEND = True CAPEND = True
elif command == "WEBIRC" and not finished: elif command == "WEBIRC" and not finished:
@ -376,9 +383,9 @@ def session(connection, client, ip, isssl=False):
if args[0] == data2["webirc_pass"]: if args[0] == data2["webirc_pass"]:
hostname = args[2] hostname = args[2]
client = (args[3], client[1]) client = (args[3], client[1])
connection.sendall(bytes(f"{tags()}:{server} NOTICE * :*** WebIRC detected, welcome to IRC!\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} NOTICE * :*** WebIRC detected, welcome to IRC!\r\n", "UTF-8"))
if hostname != client[0]: if hostname != client[0]:
connection.sendall(bytes(f"{tags()}:{server} NOTICE * :*** Got WebIRC hostname! {hostname}\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} NOTICE * :*** Got WebIRC hostname! {hostname}\r\n", "UTF-8"))
except: except:
print(traceback.format_exc()) print(traceback.format_exc())
break break
@ -394,28 +401,28 @@ def session(connection, client, ip, isssl=False):
threading.Thread(target=pinger, args=[pending, connection]).start() threading.Thread(target=pinger, args=[pending, connection]).start()
if clident == None: if clident == None:
rident = f"~{username}" rident = f"~{username}"
connection.sendall(bytes(f"{tags()}:{server} 001 {pending} :Welcome to the {displayname} Internet Relay Chat Network {pending}\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 001 {pending} :Welcome to the {displayname} Internet Relay Chat Network {pending}\r\n", "UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 002 {pending} :Your host is {server}[{ip}/6667], running version IRCat-v{__version__}\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 002 {pending} :Your host is {server}[{ip}/6667], running version IRCat-v{__version__}\r\n", "UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 004 {pending} {server} IRCat-{__version__} iow msitnR lfovkqb\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 004 {pending} {server} IRCat-{__version__} iow msitnR lfovkqb\r\n", "UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 005 {pending} CHANMODES=bq,k,fl,irmnpst CHANTYPES=# NETWORK={displayname} :are supported by this server\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 005 {pending} CHANMODES=bq,k,fl,irmnpst CHANTYPES=# NETWORK={displayname} :are supported by this server\r\n", "UTF-8"))
# connection.sendall(bytes(f":{server} 251 {pending} :There are {allusers} users and {allinvis} invisible in {servers} servers\r\n", "UTF-8")) Not supported as there isn't multi-server capability (yet) # dosend(bytes(f":{server} 251 {pending} :There are {allusers} users and {allinvis} invisible in {servers} servers\r\n", "UTF-8")) Not supported as there isn't multi-server capability (yet)
ops = 0 # Placeholder, will replace with caclulating how much people have +o ops = 0 # Placeholder, will replace with caclulating how much people have +o
connection.sendall(bytes(f"{tags()}:{server} 252 {pending} {ops} :IRC Operators online\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 252 {pending} {ops} :IRC Operators online\r\n", "UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 253 {pending} 0 :unknown connection(s)\r\n", "UTF-8")) # Replace 0 with a variable of not setup clients. dosend(bytes(f"{tags()}:{server} 253 {pending} 0 :unknown connection(s)\r\n", "UTF-8")) # Replace 0 with a variable of not setup clients.
chans = len(channels_list) chans = len(channels_list)
connection.sendall(bytes(f"{tags()}:{server} 254 {pending} {chans} :channels formed\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 254 {pending} {chans} :channels formed\r\n", "UTF-8"))
cleints = len(nickname_list) cleints = len(nickname_list)
servers = 1 servers = 1
connection.sendall(bytes(f"{tags()}:{server} 255 {pending} :I have {cleints} clients and {servers} servers\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 255 {pending} :I have {cleints} clients and {servers} servers\r\n", "UTF-8"))
# Start the MOTD # Start the MOTD
if motd_file != None: if motd_file != None:
motd = open(motd_file).read() motd = open(motd_file).read()
connection.sendall(bytes(f"{tags()}:{server} 375 {pending} :- {server} Message of the Day -\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 375 {pending} :- {server} Message of the Day -\r\n", "UTF-8"))
for i in motd.rstrip().split("\n"): for i in motd.rstrip().split("\n"):
connection.sendall(bytes(f"{tags()}:{server} 372 {pending} :- {i}\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 372 {pending} :- {i}\r\n", "UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 376 {pending} :End of /MOTD command\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 376 {pending} :End of /MOTD command\r\n", "UTF-8"))
# End the MOTD # End the MOTD
connection.sendall(bytes(f"{tags()}:{pending} MODE {pending} +iw\r\n","UTF-8")) dosend(bytes(f"{tags()}:{pending} MODE {pending} +iw\r\n","UTF-8"))
finished = True finished = True
elif command == "PING": elif command == "PING":
try: try:
@ -427,10 +434,10 @@ def session(connection, client, ip, isssl=False):
elif command == "MOTD": elif command == "MOTD":
if motd_file != None: if motd_file != None:
motd = open(motd_file).read() motd = open(motd_file).read()
connection.sendall(bytes(f"{tags()}:{server} 375 {pending} :- {server} Message of the Day -\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 375 {pending} :- {server} Message of the Day -\r\n", "UTF-8"))
for i in motd.rstrip().split("\n"): for i in motd.rstrip().split("\n"):
connection.sendall(bytes(f"{tags()}:{server} 372 {pending} :- {i}\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 372 {pending} :- {i}\r\n", "UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 376 {pending} :End of /MOTD command\r\n", "UTF-8")) dosend(bytes(f"{tags()}:{server} 376 {pending} :End of /MOTD command\r\n", "UTF-8"))
elif finished: elif finished:
pendingCommands += text pendingCommands += text
for comd in pendingCommands.replace("\r", "").split("\n"): for comd in pendingCommands.replace("\r", "").split("\n"):
@ -444,11 +451,11 @@ def session(connection, client, ip, isssl=False):
if "identify" in cmdrun: if "identify" in cmdrun:
if cmdrun["identify"] == "logout": if cmdrun["identify"] == "logout":
if "o" in property_list[pending]["modes"]: if "o" in property_list[pending]["modes"]:
connection.sendall(bytes(f"{tags()}:{pending} MODE {pending} -o\r\n","UTF-8")) dosend(bytes(f"{tags()}:{pending} MODE {pending} -o\r\n","UTF-8"))
if not "i" in property_list[pending]["modes"]: if not "i" in property_list[pending]["modes"]:
connection.sendall(bytes(f"{tags()}:{pending} MODE {pending} +i\r\n","UTF-8")) dosend(bytes(f"{tags()}:{pending} MODE {pending} +i\r\n","UTF-8"))
if not "w" in property_list[pending]["modes"]: if not "w" in property_list[pending]["modes"]:
connection.sendall(bytes(f"{tags()}:{pending} MODE {pending} +w\r\n","UTF-8")) dosend(bytes(f"{tags()}:{pending} MODE {pending} +w\r\n","UTF-8"))
property_list[pending]["modes"] = "iw" property_list[pending]["modes"] = "iw"
property_list[pending]["identified"] = False property_list[pending]["identified"] = False
else: else:
@ -456,7 +463,7 @@ def session(connection, client, ip, isssl=False):
property_list[pending]["identusername"] = cmdrun["identify"][0] property_list[pending]["identusername"] = cmdrun["identify"][0]
temp_mode = cmdrun["identify"][1] temp_mode = cmdrun["identify"][1]
property_list[pending]["modes"] = temp_mode property_list[pending]["modes"] = temp_mode
connection.sendall(bytes(f"{tags()}:{pending} MODE {pending} +{temp_mode}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{pending} MODE {pending} +{temp_mode}\r\n","UTF-8"))
processedExternally = True processedExternally = True
break break
if processedExternally: if processedExternally:
@ -484,7 +491,7 @@ def session(connection, client, ip, isssl=False):
lower_chans[channel.lower()] = channel lower_chans[channel.lower()] = channel
topic_list[channel] = "Topic is not implemented." topic_list[channel] = "Topic is not implemented."
except: except:
connection.sendall(bytes(f"{tags()}:{server} NOTICE * :*** Could not join {channel}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} NOTICE * :*** Could not join {channel}\r\n","UTF-8"))
print(channels_list) print(channels_list)
for i in channels_list[channel]: for i in channels_list[channel]:
try: try:
@ -495,17 +502,17 @@ def session(connection, client, ip, isssl=False):
if channel in channels_list: if channel in channels_list:
if pending in channels_list[channel]: if pending in channels_list[channel]:
users = " ".join(channels_list[channel]) users = " ".join(channels_list[channel])
connection.sendall(bytes(f":{server} 353 {pending} = {channel} :{users}\r\n","UTF-8")) dosend(bytes(f":{server} 353 {pending} = {channel} :{users}\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 366 {pending} {channel} :End of /NAMES list.\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 366 {pending} {channel} :End of /NAMES list.\r\n","UTF-8"))
print("Successfully pre-loaded /NAMES list") print("Successfully pre-loaded /NAMES list")
else: else:
connection.sendall(bytes(f"{tags()}:{server} 479 {pending} {channel} :Channel has erroneus characters\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 479 {pending} {channel} :Channel has erroneus characters\r\n","UTF-8"))
elif command == "LIST": elif command == "LIST":
connection.sendall(bytes(f"{tags()}:{server} 321 {pending} Channel :Users Name\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 321 {pending} Channel :Users Name\r\n","UTF-8"))
for key, value in topic_list.items(): for key, value in topic_list.items():
usersin = len(channels_list[key]) usersin = len(channels_list[key])
connection.sendall(bytes(f"{tags()}:{server} 322 {pending} {key} {usersin} :{value}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 322 {pending} {key} {usersin} :{value}\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 323 {pending} :End of /LIST\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 323 {pending} :End of /LIST\r\n","UTF-8"))
elif command == "PONG": elif command == "PONG":
e = text.split(" ")[1] e = text.split(" ")[1]
if e == server or e == f":{server}": if e == server or e == f":{server}":
@ -514,19 +521,19 @@ def session(connection, client, ip, isssl=False):
property_list[pending]["ping_pending"] = False property_list[pending]["ping_pending"] = False
elif command == "NICK": elif command == "NICK":
if len(args) == 0: if len(args) == 0:
connection.sendall(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
elif text.split(" ")[1] == pending: elif text.split(" ")[1] == pending:
pass pass
else: else:
pending2 = text.split(" ")[1] pending2 = text.split(" ")[1]
if pending2[0] == ":": pending2 = pending2[1:] if pending2[0] == ":": pending2 = pending2[1:]
if not isalphanumeric(pending2): if not isalphanumeric(pending2):
connection.sendall(bytes(f"{tags()}:{server} 432 {pending} {pending2} :Erroneus nickname\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 432 {pending} {pending2} :Erroneus nickname\r\n","UTF-8"))
elif pending2.lower() in lower_nicks: elif pending2.lower() in lower_nicks:
connection.sendall(bytes(f"{tags()}:{server} 433 {pending} {pending2} :Nickname is already in use.\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 433 {pending} {pending2} :Nickname is already in use.\r\n","UTF-8"))
else: else:
print("Sending nickname change...") print("Sending nickname change...")
connection.sendall(bytes(f"{tags()}:{pending}!{rident}@{hostname} NICK {pending2}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{pending}!{rident}@{hostname} NICK {pending2}\r\n","UTF-8"))
# Broadcast the nickname change # Broadcast the nickname change
done = [] done = []
for i, users in channels_list.items(): for i, users in channels_list.items():
@ -557,7 +564,7 @@ def session(connection, client, ip, isssl=False):
print("Broadcasting nickname change...") print("Broadcasting nickname change...")
elif command == "PART": elif command == "PART":
if len(args) == 0: if len(args) == 0:
connection.sendall(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
else: else:
channel = text.split(" ")[1] channel = text.split(" ")[1]
for i in channels_list[channel]: for i in channels_list[channel]:
@ -572,16 +579,16 @@ def session(connection, client, ip, isssl=False):
elif command == "AWAY": elif command == "AWAY":
if len(args) == 0: if len(args) == 0:
property_list[pending]["away"] = False property_list[pending]["away"] = False
connection.sendall(bytes(f"{tags()}:{server} 305 {pending} :You are no longer marked as being away\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 305 {pending} :You are no longer marked as being away\r\n","UTF-8"))
else: else:
reasons = " ".join(args) reasons = " ".join(args)
if reasons[0] == ":": reasons = reasons[1:] if reasons[0] == ":": reasons = reasons[1:]
property_list[pending]["away"] = True property_list[pending]["away"] = True
property_list[pending]["reason"] = reasons property_list[pending]["reason"] = reasons
connection.sendall(bytes(f"{tags()}:{server} 306 {pending} :You have been marked as being away\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 306 {pending} :You have been marked as being away\r\n","UTF-8"))
elif command == "WHO": elif command == "WHO":
if len(args) == 0: if len(args) == 0:
connection.sendall(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
else: else:
channel = text.split(" ")[1] channel = text.split(" ")[1]
if channel in channels_list: if channel in channels_list:
@ -590,18 +597,18 @@ def session(connection, client, ip, isssl=False):
who_user = property_list[i]["username"] who_user = property_list[i]["username"]
who_realname = property_list[i]["realname"] who_realname = property_list[i]["realname"]
who_away = "G" if property_list[i]["away"] else "H" who_away = "G" if property_list[i]["away"] else "H"
connection.sendall(bytes(f"{tags()}:{server} 352 {pending} {channel} {who_user} {who_host} {server} {i} {who_away} :0 {who_realname}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 352 {pending} {channel} {who_user} {who_host} {server} {i} {who_away} :0 {who_realname}\r\n","UTF-8"))
elif channel in nickname_list: elif channel in nickname_list:
who_host = property_list[channel]["host"] who_host = property_list[channel]["host"]
who_user = property_list[channel]["username"] who_user = property_list[channel]["username"]
who_realname = property_list[channel]["realname"] who_realname = property_list[channel]["realname"]
who_away = "G" if property_list[channel]["away"] else "H" who_away = "G" if property_list[channel]["away"] else "H"
connection.sendall(bytes(f"{tags()}:{server} 352 {pending} * {who_user} {who_host} {server} {channel} {who_away} :0 {who_realname}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 352 {pending} * {who_user} {who_host} {server} {channel} {who_away} :0 {who_realname}\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 315 {pending} {channel} :End of /WHO list.\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 315 {pending} {channel} :End of /WHO list.\r\n","UTF-8"))
elif command == "WHOIS": elif command == "WHOIS":
if len(args) == 0: if len(args) == 0:
connection.sendall(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
else: else:
target = text.split(" ")[1] target = text.split(" ")[1]
if target.lower() in lower_nicks: if target.lower() in lower_nicks:
@ -620,33 +627,33 @@ def session(connection, client, ip, isssl=False):
who_flags = property_list[target]["modes"] who_flags = property_list[target]["modes"]
except: except:
who_flags = None who_flags = None
connection.sendall(bytes(f"{tags()}:{server} 311 {pending} {target} {who_user} {who_host} * :{who_realname}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 311 {pending} {target} {who_user} {who_host} * :{who_realname}\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 312 {pending} {target} {server} :{identifier}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 312 {pending} {target} {server} :{identifier}\r\n","UTF-8"))
if "o" in who_flags: connection.sendall(bytes(f":{server} 313 {pending} {target} :is an IRC operator\r\n","UTF-8")) if "o" in who_flags: dosend(bytes(f":{server} 313 {pending} {target} :is an IRC operator\r\n","UTF-8"))
who_away = property_list[target]["away"] who_away = property_list[target]["away"]
if who_away: if who_away:
who_reason = who_away = property_list[target]["reason"] who_reason = who_away = property_list[target]["reason"]
connection.sendall(bytes(f"{tags()}:{server} 301 {pending} {target} :{who_reason}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 301 {pending} {target} :{who_reason}\r\n","UTF-8"))
if who_identified: if who_identified:
connection.sendall(bytes(f"{tags()}:{server} 330 {pending} {target} {who_identifying} :is logged in as\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 330 {pending} {target} {who_identifying} :is logged in as\r\n","UTF-8"))
if who_ssl: if who_ssl:
connection.sendall(bytes(f"{tags()}:{server} 671 {pending} {target} :is using a secure connection\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 671 {pending} {target} :is using a secure connection\r\n","UTF-8"))
#connection.sendall(bytes(f":{server} 317 {pending} {target} {time} :seconds idle\r\n","UTF-8")) # I haven't implemented idle time yet. #dosend(bytes(f":{server} 317 {pending} {target} {time} :seconds idle\r\n","UTF-8")) # I haven't implemented idle time yet.
if who_flags != None and who_flags != "iw": if who_flags != None and who_flags != "iw":
connection.sendall(bytes(f"{tags()}:{server} 379 {pending} {target} :Is using modes +{who_flags}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 379 {pending} {target} :Is using modes +{who_flags}\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 318 {pending} {target} :End of /WHOIS list\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 318 {pending} {target} :End of /WHOIS list\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f"{tags()}:{server} 401 {pending} {target} :No such nick/channel\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 401 {pending} {target} :No such nick/channel\r\n","UTF-8"))
elif command == "NAMES": elif command == "NAMES":
if len(args) == 0: if len(args) == 0:
connection.sendall(bytes(f":{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f":{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
else: else:
channel = text.split(" ")[1] channel = text.split(" ")[1]
if channel in channels_list: if channel in channels_list:
if pending in channels_list[channel]: if pending in channels_list[channel]:
users = " ".join(channels_list[channel]) users = " ".join(channels_list[channel])
connection.sendall(bytes(f"{tags()}:{server} 353 {pending} = {channel} :{users}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 353 {pending} = {channel} :{users}\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:{server} 366 {pending} {channel} :End of /NAMES list.\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 366 {pending} {channel} :End of /NAMES list.\r\n","UTF-8"))
elif command == "NOTICE": elif command == "NOTICE":
if len(args) >= 2: if len(args) >= 2:
target = text.split(" ")[1] target = text.split(" ")[1]
@ -663,9 +670,9 @@ def session(connection, client, ip, isssl=False):
elif target in nickname_list: elif target in nickname_list:
nickname_list[target].sendall(bytes(f"{tags_diffclient(target)}:{pending}!{rident}@{hostname} {text}\r\n","UTF-8")) nickname_list[target].sendall(bytes(f"{tags_diffclient(target)}:{pending}!{rident}@{hostname} {text}\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f"{tags()}:{server} 401 {pending} {target} :No such nick/channel\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 401 {pending} {target} :No such nick/channel\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
elif command == "QUIT": elif command == "QUIT":
# Parse the quit message. # Parse the quit message.
done = [] done = []
@ -693,8 +700,8 @@ def session(connection, client, ip, isssl=False):
print(traceback.format_exc()) print(traceback.format_exc())
# Confirm QUIT and close the socket. # Confirm QUIT and close the socket.
try: try:
connection.sendall(bytes(f"{tags()}:{pending}!{rident}@{hostname} {text}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{pending}!{rident}@{hostname} {text}\r\n","UTF-8"))
connection.sendall(bytes(f"ERROR :Closing Link: {hostname} ({msg})\r\n","UTF-8")) dosend(bytes(f"ERROR :Closing Link: {hostname} ({msg})\r\n","UTF-8"))
finally: finally:
connection.close() connection.close()
safe_quit = True safe_quit = True
@ -702,29 +709,29 @@ def session(connection, client, ip, isssl=False):
elif command == "MODE": elif command == "MODE":
target = args[0] target = args[0]
if len(args) == 0: if len(args) == 0:
connection.sendall(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
elif len(args) == 1: elif len(args) == 1:
if args[0] == pending: if args[0] == pending:
yourmodes = property_list[pending]["modes"] yourmodes = property_list[pending]["modes"]
connection.sendall(bytes(f"{tags()}:{server} 221 {pending} +{yourmodes}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 221 {pending} +{yourmodes}\r\n","UTF-8"))
elif args[0] in channels_list: elif args[0] in channels_list:
if args[0] in property_list: if args[0] in property_list:
if "modes" in property_list[args[0]]: if "modes" in property_list[args[0]]:
# Get the modes + parameters, then print them out. # Get the modes + parameters, then print them out.
modes = property_list[args[0]]["modes"] modes = property_list[args[0]]["modes"]
params = property_list[args[0]]["params"] params = property_list[args[0]]["params"]
connection.sendall(bytes(f"{tags()}:{server} 221 {pending} {target} +{modes} {params}\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 221 {pending} {target} +{modes} {params}\r\n","UTF-8"))
else: else:
# Default channel mode # Default channel mode
connection.sendall(bytes(f"{tags()}:{server} 324 {pending} {target} +n\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 324 {pending} {target} +n\r\n","UTF-8"))
else: else:
# Default channel mode # Default channel mode
connection.sendall(bytes(f"{tags()}:{server} 324 {pending} {target} +n\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 324 {pending} {target} +n\r\n","UTF-8"))
else: else:
if args[0][0] == "#": if args[0][0] == "#":
connection.sendall(bytes(f"{tags()}:{server} 403 {pending} {target} :No such channel\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 403 {pending} {target} :No such channel\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f"{tags()}:{server} 505 {pending} :Cant change mode for other users\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 505 {pending} :Cant change mode for other users\r\n","UTF-8"))
elif command == "CATSERV" or (command == "PRIVMSG" and args[0].lower() == "catserv"): elif command == "CATSERV" or (command == "PRIVMSG" and args[0].lower() == "catserv"):
if command == "PRIVMSG": if command == "PRIVMSG":
@ -732,26 +739,26 @@ def session(connection, client, ip, isssl=False):
if args[0][0] == ":": if args[0][0] == ":":
args[0] = args[0][1:] args[0] = args[0][1:]
if len(args) == 0: if len(args) == 0:
connection.sendall(bytes(f":{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f":{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
elif args[0].upper() == "PULL": elif args[0].upper() == "PULL":
updater = subprocess.run(["git", "pull"], stdout=subprocess.PIPE) updater = subprocess.run(["git", "pull"], stdout=subprocess.PIPE)
if updater.stdout.decode().strip() == "Already up to date.": if updater.stdout.decode().strip() == "Already up to date.":
connection.sendall(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :Codename IRCat is already up-to-date.\r\n","UTF-8")) dosend(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :Codename IRCat is already up-to-date.\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :Done, it is recommended to use /RESTART if you're an IRC op\r\n","UTF-8")) dosend(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :Done, it is recommended to use /RESTART if you're an IRC op\r\n","UTF-8"))
elif args[0].upper() == "VERSION": elif args[0].upper() == "VERSION":
connection.sendall(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :Codename IRCat version {__version__}\r\n","UTF-8")) dosend(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :Codename IRCat version {__version__}\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :This is Codename IRCat's integrated services.\r\n","UTF-8")) dosend(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :This is Codename IRCat's integrated services.\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :CatServ Usage:\r\n","UTF-8")) dosend(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :CatServ Usage:\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :PULL - Pulls the latest version of Codename IRCat\r\n","UTF-8")) dosend(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :PULL - Pulls the latest version of Codename IRCat\r\n","UTF-8"))
connection.sendall(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :VERSION - Gets the version number of this service.\r\n","UTF-8")) dosend(bytes(f"{tags()}:CatServ!Meow@IRCatCore NOTICE {pending} :VERSION - Gets the version number of this service.\r\n","UTF-8"))
elif command == "RESTART": elif command == "RESTART":
if "o" in property_list[pending]["modes"]: if "o" in property_list[pending]["modes"]:
global opened global opened
opened = False opened = False
else: else:
connection.sendall(bytes(f"{tags()}:{server} 481 {pending} :Permission Denied- You're not an IRC operator\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 481 {pending} :Permission Denied- You're not an IRC operator\r\n","UTF-8"))
elif command == "PRIVMSG": elif command == "PRIVMSG":
if len(args) >= 2: if len(args) >= 2:
target = text.split(" ")[1] target = text.split(" ")[1]
@ -774,23 +781,23 @@ def session(connection, client, ip, isssl=False):
elif target in nickname_list: elif target in nickname_list:
nickname_list[target].sendall(bytes(f"{tags_diffclient(target)}:{pending}!{rident}@{hostname} {text}\r\n","UTF-8")) nickname_list[target].sendall(bytes(f"{tags_diffclient(target)}:{pending}!{rident}@{hostname} {text}\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f"{tags()}:{server} 401 {pending} {target} :No such nick/channel\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 401 {pending} {target} :No such nick/channel\r\n","UTF-8"))
else: else:
connection.sendall(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 461 {pending} {command} :Not enough parameters\r\n","UTF-8"))
# Ignore empty text # Ignore empty text
elif text.split(" ")[0] == "": elif text.split(" ")[0] == "":
pass pass
else: else:
# Unknown command # Unknown command
cmd = text.split(" ")[0] cmd = text.split(" ")[0]
connection.sendall(bytes(f"{tags()}:{server} 421 {pending} {cmd} :Unknown command\r\n","UTF-8")) dosend(bytes(f"{tags()}:{server} 421 {pending} {cmd} :Unknown command\r\n","UTF-8"))
pendingCommands = "" pendingCommands = ""
else: else:
pendingCommands += text pendingCommands += text
textt = "" textt = ""
connection.sendall(bytes(pendingSend, "UTF-8")) dosend(bytes(pendingSend, "UTF-8"))
pendingSend = "" pendingSend = ""
except (SSL.WantReadError, SSL.WantWriteError, SSL.WantX509LookupError): except (SSL.WantReadError, SSL.WantWriteError, SSL.WantX509LookupError, SSL.SysCallError):
print("Skipable error occurred.") print("Skipable error occurred.")
pass pass
except SSL.ZeroReturnError: except SSL.ZeroReturnError: