forked from lavina/lavina
1
0
Fork 0

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 {
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();
},
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;
Ok(())
}
@ -335,6 +346,11 @@ async fn handle_update(
Ok(())
}
enum HandleResult {
Continue,
Leave,
}
async fn handle_incoming_message(
buffer: &[u8],
config: &ServerConfig,
@ -342,7 +358,7 @@ async fn handle_incoming_message(
rooms: &RoomRegistry,
user_handle: &mut PlayerConnection,
writer: &mut (impl AsyncWrite + Unpin),
) -> Result<()> {
) -> Result<HandleResult> {
let parsed = client_message(buffer);
match parsed {
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 => {
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}");
}
}
Ok(())
Ok(HandleResult::Continue)
}
fn user_to_who_msg(

View File

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