mirror of
https://github.com/tcsenpai/telnet_retro_chat.git
synced 2025-06-06 19:25:35 +00:00
courtesy commands
This commit is contained in:
parent
0b70e446c3
commit
8c2f34d273
@ -20,6 +20,7 @@ class CommandProcessor:
|
|||||||
"join": self.cmd_join,
|
"join": self.cmd_join,
|
||||||
"rooms": self.cmd_rooms,
|
"rooms": self.cmd_rooms,
|
||||||
"createroom": self.cmd_createroom,
|
"createroom": self.cmd_createroom,
|
||||||
|
"quit": self.cmd_quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
def process_command(self, message: str, addr: Tuple) -> str:
|
def process_command(self, message: str, addr: Tuple) -> str:
|
||||||
@ -46,6 +47,7 @@ class CommandProcessor:
|
|||||||
"login": "Login with username and password",
|
"login": "Login with username and password",
|
||||||
"whoami": "Show current username",
|
"whoami": "Show current username",
|
||||||
"register": "Register new user",
|
"register": "Register new user",
|
||||||
|
"quit": "Disconnect from server",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Commands for authenticated users
|
# Commands for authenticated users
|
||||||
@ -260,3 +262,7 @@ class CommandProcessor:
|
|||||||
if self.room_manager.create_room(name, description):
|
if self.room_manager.create_room(name, description):
|
||||||
return f"Room {name} created successfully"
|
return f"Room {name} created successfully"
|
||||||
return "Room already exists"
|
return "Room already exists"
|
||||||
|
|
||||||
|
def cmd_quit(self, args, addr):
|
||||||
|
"""Disconnect from the server."""
|
||||||
|
return "@QUIT@"
|
||||||
|
32
main.py
32
main.py
@ -88,6 +88,11 @@ def process_complete_line(line, addr, active_connections, conn):
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if response.startswith("@QUIT@"):
|
||||||
|
conn.sendall(b"\r\nGoodbye!\r\n")
|
||||||
|
conn.close()
|
||||||
|
return
|
||||||
|
|
||||||
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
|
||||||
@ -207,18 +212,37 @@ def cleanup_client_connection(addr):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def handle_server_input():
|
||||||
|
"""Handle input from server console."""
|
||||||
|
fake_addr = ("console", 0)
|
||||||
|
user_manager.register_session(fake_addr, "admin")
|
||||||
|
room_manager.join_room(fake_addr, "lounge")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
message = input()
|
||||||
|
if message.strip():
|
||||||
|
process_complete_line(
|
||||||
|
message.encode("ascii"), fake_addr, active_connections, None
|
||||||
|
)
|
||||||
|
except EOFError:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
def start_server():
|
def start_server():
|
||||||
"""
|
"""Starts the server and listens for incoming connections."""
|
||||||
Starts the server and listens for incoming connections.
|
|
||||||
"""
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
|
||||||
server.bind((HOST, PORT))
|
server.bind((HOST, PORT))
|
||||||
server.listen(MAX_CONNECTIONS)
|
server.listen(MAX_CONNECTIONS)
|
||||||
print(f"[TELTCSERVER] Listening on port {PORT}...")
|
print(f"[TELTCSERVER] Listening on port {PORT}...")
|
||||||
print(f"[TELTCSERVER] Maximum connections allowed: {MAX_CONNECTIONS}")
|
print(f"[TELTCSERVER] Maximum connections allowed: {MAX_CONNECTIONS}")
|
||||||
|
|
||||||
|
# Start server console input thread
|
||||||
|
console_thread = threading.Thread(target=handle_server_input)
|
||||||
|
console_thread.daemon = True
|
||||||
|
console_thread.start()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# Accept new connection
|
|
||||||
conn, addr = server.accept()
|
conn, addr = server.accept()
|
||||||
|
|
||||||
# Check if maximum connections reached
|
# Check if maximum connections reached
|
||||||
|
Loading…
x
Reference in New Issue
Block a user