Enhance README with CLI room checking instructions and implement room name resolution in command handling. Updated handle_init_command and handle_join_command to use resolved room names, improving room access validation. Added CLI command handling in main.rs to list available rooms from the database or fallback configuration.
This commit is contained in:
@@ -148,7 +148,7 @@ async fn handle_init_command(
|
||||
let room_name = normalize_room_name(command.room.as_deref().unwrap_or("lobby"));
|
||||
let room_password = command.password.clone().unwrap_or_default();
|
||||
|
||||
let (token, user_name) = {
|
||||
let (token, user_name, actual_room_name) = {
|
||||
let mut guard = state.write().await;
|
||||
if guard.logged_in_names.contains(&requested_user_name)
|
||||
&& guard
|
||||
@@ -160,8 +160,13 @@ async fn handle_init_command(
|
||||
send_error(client_id, Arc::clone(&state), "loggedin").await;
|
||||
return;
|
||||
}
|
||||
let Some(resolved_room_name) = resolve_room_name(&guard, &room_name) else {
|
||||
drop(guard);
|
||||
send_error(client_id, Arc::clone(&state), "room_not_found_or_join_failed").await;
|
||||
return;
|
||||
};
|
||||
if !room_access_allowed(
|
||||
guard.room_meta.get(&room_name),
|
||||
guard.room_meta.get(&resolved_room_name),
|
||||
&profile.display_name,
|
||||
profile.falukant_user_id,
|
||||
profile.age,
|
||||
@@ -187,7 +192,7 @@ async fn handle_init_command(
|
||||
client.age = profile.age;
|
||||
client.rights = profile.rights.clone();
|
||||
client.logged_in = true;
|
||||
client.room = room_name.clone();
|
||||
client.room = resolved_room_name.clone();
|
||||
|
||||
let mut new_token = None;
|
||||
if client.token.is_none() {
|
||||
@@ -218,8 +223,12 @@ async fn handle_init_command(
|
||||
members.remove(&client_id);
|
||||
}
|
||||
}
|
||||
guard.rooms.entry(room_name.clone()).or_default().insert(client_id);
|
||||
(token, user_name)
|
||||
guard
|
||||
.rooms
|
||||
.entry(resolved_room_name.clone())
|
||||
.or_default()
|
||||
.insert(client_id);
|
||||
(token, user_name, resolved_room_name)
|
||||
};
|
||||
|
||||
state::send_to_client(
|
||||
@@ -232,13 +241,13 @@ async fn handle_init_command(
|
||||
state::send_to_client(
|
||||
client_id,
|
||||
Arc::clone(&state),
|
||||
json!({"type":5, "message":"room_entered", "to": room_name}),
|
||||
json!({"type":5, "message":"room_entered", "to": actual_room_name}),
|
||||
)
|
||||
.await;
|
||||
state::broadcast_room(
|
||||
&room_name,
|
||||
&actual_room_name,
|
||||
Arc::clone(&state),
|
||||
json!({"type":"system","message": format!("{user_name} joined {room_name}")}),
|
||||
json!({"type":"system","message": format!("{user_name} joined {actual_room_name}")}),
|
||||
Some(client_id),
|
||||
)
|
||||
.await;
|
||||
@@ -255,8 +264,13 @@ async fn handle_join_command(
|
||||
}
|
||||
let room = normalize_room_name(command.room.as_deref().or(command.name.as_deref()).unwrap_or("lobby"));
|
||||
let password = command.password.clone().unwrap_or_default();
|
||||
let (from_room, user_name) = {
|
||||
let (from_room, user_name, actual_room_name) = {
|
||||
let mut guard = state.write().await;
|
||||
let Some(resolved_room) = resolve_room_name(&guard, &room) else {
|
||||
drop(guard);
|
||||
send_error(client_id, Arc::clone(&state), "room_not_found_or_join_failed").await;
|
||||
return;
|
||||
};
|
||||
let (name_for_check, falukant_user_id, age) = {
|
||||
let Some(client) = guard.clients.get(&client_id) else {
|
||||
return;
|
||||
@@ -264,7 +278,7 @@ async fn handle_join_command(
|
||||
(client.user_name.clone(), client.falukant_user_id, client.age)
|
||||
};
|
||||
if !room_access_allowed(
|
||||
guard.room_meta.get(&room),
|
||||
guard.room_meta.get(&resolved_room),
|
||||
&name_for_check,
|
||||
falukant_user_id,
|
||||
age,
|
||||
@@ -279,14 +293,14 @@ async fn handle_join_command(
|
||||
};
|
||||
let from_room = client.room.clone();
|
||||
let user_name = client.user_name.clone();
|
||||
client.room = room.clone();
|
||||
client.room = resolved_room.clone();
|
||||
if !from_room.is_empty() {
|
||||
if let Some(members) = guard.rooms.get_mut(&from_room) {
|
||||
members.remove(&client_id);
|
||||
}
|
||||
}
|
||||
guard.rooms.entry(room.clone()).or_default().insert(client_id);
|
||||
(from_room, user_name)
|
||||
guard.rooms.entry(resolved_room.clone()).or_default().insert(client_id);
|
||||
(from_room, user_name, resolved_room)
|
||||
};
|
||||
|
||||
if !from_room.is_empty() && from_room != room {
|
||||
@@ -301,7 +315,7 @@ async fn handle_join_command(
|
||||
state::send_to_client(
|
||||
client_id,
|
||||
Arc::clone(&state),
|
||||
json!({"type":5, "message":"room_entered", "to": room}),
|
||||
json!({"type":5, "message":"room_entered", "to": actual_room_name}),
|
||||
)
|
||||
.await;
|
||||
state::send_room_list(client_id, Arc::clone(&state)).await;
|
||||
@@ -754,7 +768,7 @@ async fn handle_unknown_command(client_id: ClientId, command: &Command, state: A
|
||||
|
||||
fn room_access_allowed(
|
||||
room_meta: Option<&RoomMeta>,
|
||||
user_name: &str,
|
||||
_user_name: &str,
|
||||
falukant_user_id: Option<i32>,
|
||||
age: Option<i32>,
|
||||
provided_password: &str,
|
||||
@@ -792,17 +806,21 @@ fn room_access_allowed(
|
||||
}
|
||||
}
|
||||
|
||||
if !room.is_public {
|
||||
if room.owner_id.is_none() {
|
||||
return false;
|
||||
}
|
||||
if user_name.trim().is_empty() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn resolve_room_name(state: &ChatState, requested: &str) -> Option<String> {
|
||||
if state.room_meta.contains_key(requested) {
|
||||
return Some(requested.to_string());
|
||||
}
|
||||
let requested_lower = requested.to_lowercase();
|
||||
state
|
||||
.room_meta
|
||||
.keys()
|
||||
.find(|name| name.to_lowercase() == requested_lower)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
fn normalize_room_name(input: &str) -> String {
|
||||
let trimmed = input.trim();
|
||||
if trimmed.is_empty() {
|
||||
|
||||
Reference in New Issue
Block a user