Try to implement the logic?

This commit is contained in:
Charles Descamps 2025-04-21 20:02:22 -07:00
parent 5534bab856
commit 2ea1d776fc

View file

@ -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()