forked from lavina/lavina
1
0
Fork 0

make irc update handler a separate function

This commit is contained in:
Nikita Vilunov 2023-02-16 16:33:44 +01:00
parent 1e17e017cf
commit 30db029390
1 changed files with 66 additions and 50 deletions

View File

@ -221,58 +221,15 @@ async fn handle_registered_socket<'a>(
} else { } else {
len len
}; };
handle_incoming_message(&buffer[0..len], &config, &user, &mut connection, &player_id, writer).await?; handle_incoming_message(&buffer[0..len], &config, &user, &mut connection, writer).await?;
buffer.clear(); buffer.clear();
}, },
update = connection.receiver.recv() => { update = connection.receiver.recv() => {
match update.unwrap() { if let Some(update) = update {
Updates::RoomJoined { new_member_id, room_id } => { handle_update(&config, &user, &player_id, writer, &rooms, update).await?;
if player_id == new_member_id { } else {
if let Some(room) = rooms.get_room(&room_id) { log::warn!("Player is terminated, must terminate the connection");
let room_info = room.get_room_info().await; break;
let chan = Chan::Global(room_id.as_bytes().clone());
produce_on_join_cmd_messages(&config, &user, &chan, &room_info, writer).await?;
writer.flush().await?;
} else {
log::warn!("Received join to a non-existant room");
}
} else {
ServerMessage {
tags: vec![],
sender: Some(new_member_id.as_bytes().clone()),
body: ServerMessageBody::Join(Chan::Global(room_id.as_bytes().clone())),
}.write_async(writer).await?;
writer.flush().await?
}
},
Updates::RoomLeft { room_id, former_member_id } => {
ServerMessage {
tags: vec![],
sender: Some(former_member_id.as_bytes().clone()),
body: ServerMessageBody::Part(Chan::Global(room_id.as_bytes().clone())),
}.write_async(writer).await?;
writer.flush().await?
},
Updates::NewMessage { author_id, room_id, body } => {
ServerMessage {
tags: vec![],
sender: Some(author_id.as_bytes().clone()),
body: ServerMessageBody::PrivateMessage { target: Recipient::Chan(Chan::Global(room_id.as_bytes().clone())), body: body.as_bytes().to_vec() }
}.write_async(writer).await?;
writer.flush().await?
},
Updates::RoomTopicChanged { room_id, new_topic } => {
ServerMessage {
tags: vec![],
sender: Some(config.server_name.as_bytes().to_vec()),
body: ServerMessageBody::N332Topic {
client: user.nickname.clone(),
chat: Chan::Global(room_id.as_bytes().clone()),
topic: new_topic,
},
}.write_async(writer).await?;
writer.flush().await?
},
} }
} }
} }
@ -281,12 +238,71 @@ async fn handle_registered_socket<'a>(
Ok(()) Ok(())
} }
async fn handle_update(
config: &ServerConfig,
user: &RegisteredUser,
player_id: &PlayerId,
writer: &mut (impl AsyncWrite + Unpin),
rooms: &RoomRegistry,
update: Updates
) -> Result<()> {
match update {
Updates::RoomJoined { new_member_id, room_id } => {
if player_id == &new_member_id {
if let Some(room) = rooms.get_room(&room_id) {
let room_info = room.get_room_info().await;
let chan = Chan::Global(room_id.as_bytes().clone());
produce_on_join_cmd_messages(&config, &user, &chan, &room_info, writer).await?;
writer.flush().await?;
} else {
log::warn!("Received join to a non-existant room");
}
} else {
ServerMessage {
tags: vec![],
sender: Some(new_member_id.as_bytes().clone()),
body: ServerMessageBody::Join(Chan::Global(room_id.as_bytes().clone())),
}.write_async(writer).await?;
writer.flush().await?
}
},
Updates::RoomLeft { room_id, former_member_id } => {
ServerMessage {
tags: vec![],
sender: Some(former_member_id.as_bytes().clone()),
body: ServerMessageBody::Part(Chan::Global(room_id.as_bytes().clone())),
}.write_async(writer).await?;
writer.flush().await?
},
Updates::NewMessage { author_id, room_id, body } => {
ServerMessage {
tags: vec![],
sender: Some(author_id.as_bytes().clone()),
body: ServerMessageBody::PrivateMessage { target: Recipient::Chan(Chan::Global(room_id.as_bytes().clone())), body: body.as_bytes().to_vec() }
}.write_async(writer).await?;
writer.flush().await?
},
Updates::RoomTopicChanged { room_id, new_topic } => {
ServerMessage {
tags: vec![],
sender: Some(config.server_name.as_bytes().to_vec()),
body: ServerMessageBody::N332Topic {
client: user.nickname.clone(),
chat: Chan::Global(room_id.as_bytes().clone()),
topic: new_topic,
},
}.write_async(writer).await?;
writer.flush().await?
},
}
Ok(())
}
async fn handle_incoming_message( async fn handle_incoming_message(
buffer: &[u8], buffer: &[u8],
config: &ServerConfig, config: &ServerConfig,
user: &RegisteredUser, user: &RegisteredUser,
user_handle: &mut PlayerConnection, user_handle: &mut PlayerConnection,
player_id: &PlayerId,
writer: &mut (impl AsyncWrite + Unpin), writer: &mut (impl AsyncWrite + Unpin),
) -> Result<()> { ) -> Result<()> {
let parsed = client_message(buffer); let parsed = client_message(buffer);