Update server.py

This commit is contained in:
Nova Cat 2024-12-15 19:29:27 -08:00
parent 7e40c8a422
commit fd38daa28d

View file

@ -78,15 +78,15 @@ print("Now listening on port 6667")
def pinger(nick, connection): def pinger(nick, connection):
global property_list global property_list
while nick in property_list: while nick in property_list:
if (time.time() - property_list[nick]["last_ping"]) > 60 and not property_list[nick]["ping_pending"]: if (time.time() - property_list[nick]["last_ping"]) > 30 and not property_list[nick]["ping_pending"]:
if nick in property_list: if nick in property_list:
print("Sent ping message to " + nick) print("Sent ping message to " + nick)
property_list[nick]["ping_pending"] = True property_list[nick]["ping_pending"] = True
time.sleep(0.5) time.sleep(0.5)
connection.sendall(bytes(f"PING {server}\r\n","UTF-8")) connection.sendall(bytes(f"PING {server}\r\n","UTF-8"))
elif property_list[nick]["ping_pending"] and ((time.time() - property_list[nick]["last_ping"]) > 255): elif property_list[nick]["ping_pending"] and ((time.time() - property_list[nick]["last_ping"]) > ping_timeout):
if nick in property_list: if nick in property_list:
property_list[nick]["cause"] = "Ping timeout: 255 seconds" property_list[nick]["cause"] = f"Ping timeout: {ping_timeout} seconds"
print("SHUTTING DOWN FOR " + nick) print("SHUTTING DOWN FOR " + nick)
connection.shutdown(socket.SHUT_WR) connection.shutdown(socket.SHUT_WR)
connection.close() connection.close()
@ -147,6 +147,7 @@ def session(connection, client):
if args[0] == "LS": if args[0] == "LS":
connection.sendall(bytes(f":{server} CAP * LS :ircat.xyz/foo\r\n", "UTF-8")) connection.sendall(bytes(f":{server} CAP * LS :ircat.xyz/foo\r\n", "UTF-8"))
elif (ready and already_set) and not finished: elif (ready and already_set) and not finished:
cleanup_manual()
print(f"User {pending} successfully logged in.") print(f"User {pending} successfully logged in.")
nickname_list[pending] = connection nickname_list[pending] = connection
property_list[pending] = {"host": hostname, "username": username, "realname": realname, "modes": "iw", "last_ping": time.time(), "ping_pending": False} property_list[pending] = {"host": hostname, "username": username, "realname": realname, "modes": "iw", "last_ping": time.time(), "ping_pending": False}
@ -492,6 +493,7 @@ def session(connection, client):
break break
finally: finally:
connection.close() connection.close()
try:
if "cause" in property_list[pending]: if "cause" in property_list[pending]:
cause = property_list[pending]["cause"] cause = property_list[pending]["cause"]
if pending != "*": if pending != "*":
@ -511,6 +513,35 @@ def session(connection, client):
channels_list[i].remove(pending) channels_list[i].remove(pending)
except: except:
print(traceback.format_exc()) print(traceback.format_exc())
except:
pass
cleanup_manual()
def cleanup():
global channels_list
global property_list
while True:
time.sleep(15)
print("Cleaning up...")
for i, j in channels_list.items():
for h in i:
if not h in property_list:
print("Found a detached connection: " + h)
i.remove(h)
for k in channels_list[j]:
if k != h and k in nickname_list:
nickname[k].sendall(f":{h}!~DISCONNECTED@DISCONNECTED PART {j} :IRCat Cleanup: Found missing connection!!\r\n")
def cleanup_manual():
global channels_list
global property_list
print("Cleaning up...")
for i, j in channels_list.items():
for h in i:
if not h in property_list:
print("Found a detached connection: " + h)
i.remove(h)
for k in channels_list[j]:
if k != h and k in nickname_list:
nickname[k].sendall(f":{h}!~DISCONNECTED@DISCONNECTED PART {j} :IRCat Cleanup: Found missing connection!!\r\n")
try: try:
while opened: while opened:
print("Waiting for connection...") print("Waiting for connection...")