fixed console mode

This commit is contained in:
tcsenpai 2025-02-19 11:31:38 +01:00
parent 8c2f34d273
commit f6a36b689c

71
main.py
View File

@ -51,48 +51,40 @@ def process_input_byte(byte, display_buffer, conn):
def process_complete_line(line, addr, active_connections, conn): def process_complete_line(line, addr, active_connections, conn):
"""Process a complete line of input and broadcast if valid.""" """Process a complete line of input and broadcast if valid."""
try: try:
# Clean up the input by processing backspaces message = line.decode("ascii").strip()
cleaned = [] if not message:
for char in line: return
if char in (8, 127): # Backspace/delete
if cleaned:
cleaned.pop()
else:
cleaned.append(char)
message = bytes(cleaned).decode("ascii", "ignore").strip() # Check if it's a command
print(f"Client {addr}: {message}")
# Process commands
if message.startswith("/"): if message.startswith("/"):
response = command_processor.process_command(message[1:], addr) response = command_processor.process_command(message[1:], addr)
# Handle special responses
if response.startswith("@KICK@"):
_, kick_addr = response.split("@", 2)
# Convert string addr back to tuple
kick_addr = eval(kick_addr) # Safe here as we control the input
if kick_addr in active_connections:
active_connections[kick_addr].sendall(
b"\r\nYou have been kicked from the server.\r\n"
)
active_connections[kick_addr].close()
return
if response.startswith("@BROADCAST@"):
parts = response.split("@", 2) # Split into max 3 parts
broadcast_msg = parts[2] if len(parts) > 2 else ""
username = user_manager.get_username(addr)
broadcast_message(
active_connections, f"[BROADCAST] {username}: {broadcast_msg}"
)
return
if response.startswith("@QUIT@"): if response.startswith("@QUIT@"):
if conn: # Only for real connections
conn.sendall(b"\r\nGoodbye!\r\n") conn.sendall(b"\r\nGoodbye!\r\n")
conn.close() conn.close()
return return
if response.startswith("@BROADCAST@"):
parts = response.split("@", 2)
broadcast_msg = parts[2] if len(parts) > 2 else ""
username = user_manager.get_username(addr)
broadcast_message(
active_connections,
f"[BROADCAST] {username}: {broadcast_msg}",
system_msg=True,
)
return
if response.startswith("@KICK@"):
# Handle kick command
target_addr = eval(
response.split("@")[2]
) # Safe since we control the string
if target_addr in active_connections:
active_connections[target_addr].sendall(
b"\r\nYou have been kicked.\r\n"
)
active_connections[target_addr].close()
return
if response and conn: # Only send response if there's a connection
conn.sendall(f"\r\n{response}\r\n".encode("ascii")) conn.sendall(f"\r\n{response}\r\n".encode("ascii"))
else: else:
# Regular chat message # Regular chat message
@ -100,6 +92,7 @@ def process_complete_line(line, addr, active_connections, conn):
# Check if user is authenticated (not a guest) # Check if user is authenticated (not a guest)
if username.startswith("guest_"): if username.startswith("guest_"):
if conn: # Only send if there's a connection
conn.sendall( conn.sendall(
b"\r\nYou must be logged in to chat. Use /help for commands.\r\n" b"\r\nYou must be logged in to chat. Use /help for commands.\r\n"
) )
@ -107,6 +100,7 @@ def process_complete_line(line, addr, active_connections, conn):
# Check rate limit # Check rate limit
if user_manager.is_rate_limited(addr): if user_manager.is_rate_limited(addr):
if conn: # Only send if there's a connection
conn.sendall(b"\r\nRate limit exceeded. Please wait a moment.\r\n") conn.sendall(b"\r\nRate limit exceeded. Please wait a moment.\r\n")
return return
@ -122,8 +116,11 @@ def process_complete_line(line, addr, active_connections, conn):
addr, addr,
room, room,
) )
# Send back to sender
conn.sendall(f"\r\n{message_with_user}\r\n".encode("ascii")) # For console, print locally instead of sending
if not conn:
print(f"\r\n{message_with_user}\r\n")
except UnicodeDecodeError: except UnicodeDecodeError:
print("UnicodeDecodeError: ", line) print("UnicodeDecodeError: ", line)