From 2ea1d776fc73733eec704aa137a3117b36a560d2 Mon Sep 17 00:00:00 2001 From: cdez Date: Mon, 21 Apr 2025 20:02:22 -0700 Subject: [PATCH] Try to implement the logic? --- chatserver/server.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/chatserver/server.py b/chatserver/server.py index b678d32..46f58ad 100644 --- a/chatserver/server.py +++ b/chatserver/server.py @@ -47,28 +47,26 @@ def handle_client(sock, client_address, socket_list, usernames, chat_rooms, clie elif message.startswith("@") and "-ls" in message: # Handle directory listing request parts = message.split() - if len(parts) >= 2 and "-ls" in parts[1]: - usernames_part = parts[0] - if usernames_part.count("@") == 2: - receiving_username, sending_username = usernames_part[1:].split("@") - receiving_sock = next((s for s, u in usernames.items() if u == receiving_username), None) - if receiving_sock: - user_path = user_paths.get(receiving_sock) - if user_path and os.path.isdir(user_path): - files = os.listdir(user_path) - if files: - file_list = "\n".join( - [format_file_info(os.path.join(user_path, f)) for f in files] - ) - sock.send(f"Files in {receiving_username}'s directory:\n{file_list}".encode('utf-8')) - else: - sock.send(f"No files in {receiving_username}'s directory.".encode('utf-8')) + if len(parts) == 2 and parts[1] == "-ls": + target_username = parts[0][1:] # Extract the username after '@' + target_sock = next((s for s, u in usernames.items() if u == target_username), None) + if target_sock: + user_path = user_paths.get(target_sock) + if user_path and os.path.isdir(user_path): + files = os.listdir(user_path) + if files: + file_list = "\n".join( + [format_file_info(os.path.join(user_path, f)) for f in files] + ) + sock.send(f"Files in {target_username}'s directory:\n{file_list}".encode('utf-8')) else: - sock.send(f"Error: {receiving_username} has no valid directory set.".encode('utf-8')) + sock.send(f"No files in {target_username}'s directory.".encode('utf-8')) else: - sock.send(b"Error: Receiving user not found.") + sock.send(f"Error: {target_username} has no valid directory set.".encode('utf-8')) else: - sock.send(b"Error: Invalid message format.") + sock.send(b"Error: Target user not found.") + else: + sock.send(b"Error: Invalid message format.") elif message.startswith("@") and "-f" in message: # Handle file request or transfer parts = message.split()