Try to implement the logic?
This commit is contained in:
parent
5534bab856
commit
2ea1d776fc
1 changed files with 17 additions and 19 deletions
|
|
@ -47,26 +47,24 @@ def handle_client(sock, client_address, socket_list, usernames, chat_rooms, clie
|
||||||
elif message.startswith("@") and "-ls" in message:
|
elif message.startswith("@") and "-ls" in message:
|
||||||
# Handle directory listing request
|
# Handle directory listing request
|
||||||
parts = message.split()
|
parts = message.split()
|
||||||
if len(parts) >= 2 and "-ls" in parts[1]:
|
if len(parts) == 2 and parts[1] == "-ls":
|
||||||
usernames_part = parts[0]
|
target_username = parts[0][1:] # Extract the username after '@'
|
||||||
if usernames_part.count("@") == 2:
|
target_sock = next((s for s, u in usernames.items() if u == target_username), None)
|
||||||
receiving_username, sending_username = usernames_part[1:].split("@")
|
if target_sock:
|
||||||
receiving_sock = next((s for s, u in usernames.items() if u == receiving_username), None)
|
user_path = user_paths.get(target_sock)
|
||||||
if receiving_sock:
|
|
||||||
user_path = user_paths.get(receiving_sock)
|
|
||||||
if user_path and os.path.isdir(user_path):
|
if user_path and os.path.isdir(user_path):
|
||||||
files = os.listdir(user_path)
|
files = os.listdir(user_path)
|
||||||
if files:
|
if files:
|
||||||
file_list = "\n".join(
|
file_list = "\n".join(
|
||||||
[format_file_info(os.path.join(user_path, f)) for f in files]
|
[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'))
|
sock.send(f"Files in {target_username}'s directory:\n{file_list}".encode('utf-8'))
|
||||||
else:
|
else:
|
||||||
sock.send(f"No files in {receiving_username}'s directory.".encode('utf-8'))
|
sock.send(f"No files in {target_username}'s directory.".encode('utf-8'))
|
||||||
else:
|
else:
|
||||||
sock.send(f"Error: {receiving_username} has no valid directory set.".encode('utf-8'))
|
sock.send(f"Error: {target_username} has no valid directory set.".encode('utf-8'))
|
||||||
else:
|
else:
|
||||||
sock.send(b"Error: Receiving user not found.")
|
sock.send(b"Error: Target user not found.")
|
||||||
else:
|
else:
|
||||||
sock.send(b"Error: Invalid message format.")
|
sock.send(b"Error: Invalid message format.")
|
||||||
elif message.startswith("@") and "-f" in message:
|
elif message.startswith("@") and "-f" in message:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue