handle QUIT cmd

This commit is contained in:
Nikita Vilunov 2023-02-17 00:38:34 +01:00
parent e813fb7c69
commit 266eafe6e6
2 changed files with 30 additions and 3 deletions

View File

@ -226,7 +226,9 @@ async fn handle_registered_socket<'a>(
} else { } else {
len len
}; };
handle_incoming_message(&buffer[0..len], &config, &user, &rooms, &mut connection, writer).await?; if let HandleResult::Leave = handle_incoming_message(&buffer[0..len], &config, &user, &rooms, &mut connection, writer).await? {
break;
}
buffer.clear(); buffer.clear();
}, },
update = connection.receiver.recv() => { update = connection.receiver.recv() => {
@ -239,6 +241,15 @@ async fn handle_registered_socket<'a>(
} }
} }
} }
ServerMessage {
tags: vec![],
sender: Some(config.server_name.as_bytes().to_vec()),
body: ServerMessageBody::Error { reason: b"Leaving the server".to_vec() },
}
.write_async(writer)
.await?;
writer.flush().await?;
connection.terminate().await; connection.terminate().await;
Ok(()) Ok(())
} }
@ -335,6 +346,11 @@ async fn handle_update(
Ok(()) Ok(())
} }
enum HandleResult {
Continue,
Leave,
}
async fn handle_incoming_message( async fn handle_incoming_message(
buffer: &[u8], buffer: &[u8],
config: &ServerConfig, config: &ServerConfig,
@ -342,7 +358,7 @@ async fn handle_incoming_message(
rooms: &RoomRegistry, rooms: &RoomRegistry,
user_handle: &mut PlayerConnection, user_handle: &mut PlayerConnection,
writer: &mut (impl AsyncWrite + Unpin), writer: &mut (impl AsyncWrite + Unpin),
) -> Result<()> { ) -> Result<HandleResult> {
let parsed = client_message(buffer); let parsed = client_message(buffer);
match parsed { match parsed {
Ok((_, msg)) => match msg { Ok((_, msg)) => match msg {
@ -481,6 +497,10 @@ async fn handle_incoming_message(
} }
} }
} }
ClientMessage::Quit { reason } => {
log::info!("Received QUIT");
return Ok(HandleResult::Leave);
}
cmd => { cmd => {
log::warn!("Not implemented handler for client command: {cmd:?}"); log::warn!("Not implemented handler for client command: {cmd:?}");
} }
@ -489,7 +509,7 @@ async fn handle_incoming_message(
log::warn!("Failed to parse IRC message: {err}"); log::warn!("Failed to parse IRC message: {err}");
} }
} }
Ok(()) Ok(HandleResult::Continue)
} }
fn user_to_who_msg( fn user_to_who_msg(

View File

@ -61,6 +61,9 @@ pub enum ServerMessageBody {
}, },
Join(Chan), Join(Chan),
Part(Chan), Part(Chan),
Error {
reason: ByteVec,
},
N001Welcome { N001Welcome {
client: ByteVec, client: ByteVec,
text: ByteVec, text: ByteVec,
@ -168,6 +171,10 @@ impl ServerMessageBody {
writer.write_all(b"PART ").await?; writer.write_all(b"PART ").await?;
chan.write_async(writer).await?; chan.write_async(writer).await?;
} }
ServerMessageBody::Error { reason } => {
writer.write_all(b"ERROR :").await?;
writer.write_all(&reason).await?;
}
ServerMessageBody::N001Welcome { client, text } => { ServerMessageBody::N001Welcome { client, text } => {
writer.write_all(b"001 ").await?; writer.write_all(b"001 ").await?;
writer.write_all(&client).await?; writer.write_all(&client).await?;