2022-07-28 20:31:10 +00:00
|
|
|
use tracing::{error, info, trace};
|
2022-07-01 19:32:20 +00:00
|
|
|
use tokio::io::AsyncWriteExt;
|
2022-06-05 14:08:13 +00:00
|
|
|
|
2022-06-20 21:22:45 +00:00
|
|
|
use crate::plugins::PluginManagerType;
|
2022-06-04 19:34:53 +00:00
|
|
|
|
|
|
|
use super::Client;
|
|
|
|
|
2022-06-05 20:10:23 +00:00
|
|
|
/// Handle Client connection
|
2022-06-04 19:34:53 +00:00
|
|
|
pub async fn handle_connection(
|
|
|
|
mut client: Client,
|
2022-06-20 21:22:45 +00:00
|
|
|
plugin_manager: PluginManagerType,
|
2022-06-04 19:34:53 +00:00
|
|
|
) -> anyhow::Result<()> {
|
2022-06-26 10:35:58 +00:00
|
|
|
info!("New Client: {}", client.stream.peer_addr()?);
|
2022-06-04 19:34:53 +00:00
|
|
|
|
2022-06-16 17:31:09 +00:00
|
|
|
// run `onConnect` events from plugins
|
2022-06-25 21:35:34 +00:00
|
|
|
check_event(&mut client, &plugin_manager, "onConnect").await?;
|
2022-06-16 17:31:09 +00:00
|
|
|
|
2022-06-04 19:34:53 +00:00
|
|
|
loop {
|
|
|
|
// read client message/buffer
|
2022-07-01 19:32:20 +00:00
|
|
|
let buf = client.read().await?;
|
2022-06-04 19:34:53 +00:00
|
|
|
|
2022-06-16 17:31:09 +00:00
|
|
|
// run `onSend` events from plugins
|
2022-06-25 21:35:34 +00:00
|
|
|
check_event(&mut client, &plugin_manager, "onSend").await?;
|
2022-06-16 17:31:09 +00:00
|
|
|
|
2022-06-25 21:35:34 +00:00
|
|
|
// split the message by whitespaces and collect it into Vec<&str>
|
|
|
|
let mut args = buf.split_ascii_whitespace().collect::<Vec<&str>>();
|
2022-06-17 20:32:07 +00:00
|
|
|
|
|
|
|
// client sent an empty buffer
|
|
|
|
if args.is_empty() {
|
2022-07-01 19:32:20 +00:00
|
|
|
client.send("empty buffer").await?;
|
2022-06-17 20:32:07 +00:00
|
|
|
|
|
|
|
// don't execute the following commands because it causes panic
|
2022-06-20 21:22:45 +00:00
|
|
|
continue;
|
2022-06-17 20:32:07 +00:00
|
|
|
}
|
2022-06-04 19:34:53 +00:00
|
|
|
|
|
|
|
// get command from args
|
|
|
|
let cmd = args[0];
|
|
|
|
|
2022-06-25 21:35:34 +00:00
|
|
|
// remove command name from args
|
|
|
|
args = args[1..args.len()].to_vec();
|
|
|
|
|
2022-06-04 19:34:53 +00:00
|
|
|
// search if a command exists
|
2022-06-20 21:22:45 +00:00
|
|
|
for command in plugin_manager.commands.iter() {
|
2022-06-04 19:34:53 +00:00
|
|
|
// if this is the entered command
|
|
|
|
if cmd == command.name() {
|
2022-06-05 14:08:13 +00:00
|
|
|
trace!("Executing a command `{}`", command.name());
|
2022-06-16 17:31:09 +00:00
|
|
|
|
|
|
|
// execute command
|
2022-06-25 21:35:34 +00:00
|
|
|
match command.execute(&mut client, args, &plugin_manager).await {
|
2022-06-23 09:07:42 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => {
|
|
|
|
error!("failed to execute command `{cmd}`, error message = `{err}`");
|
|
|
|
|
2022-07-01 19:32:20 +00:00
|
|
|
client.send(&format!("error: {err}")).await?;
|
2022-06-25 14:22:33 +00:00
|
|
|
}
|
2022-06-23 09:07:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 19:34:53 +00:00
|
|
|
// don't search for more commands
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if an I/O or EOF error, abort the connection
|
2022-07-01 19:32:20 +00:00
|
|
|
if client.stream.flush().await.is_err() {
|
2022-06-04 19:34:53 +00:00
|
|
|
// terminate connection
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-06-16 17:31:09 +00:00
|
|
|
|
2022-06-17 11:43:23 +00:00
|
|
|
/// Search for a events and execute it
|
2022-06-25 21:35:34 +00:00
|
|
|
async fn check_event(
|
|
|
|
client: &mut Client,
|
|
|
|
events: &PluginManagerType,
|
|
|
|
event_name: &str,
|
|
|
|
) -> anyhow::Result<()> {
|
2022-06-16 17:31:09 +00:00
|
|
|
for event in events.events.iter() {
|
|
|
|
// check if this event should be started
|
|
|
|
if event.name() == event_name {
|
|
|
|
trace!("Executing a event `{}`", event.name());
|
|
|
|
|
|
|
|
// execute event
|
2022-06-25 21:35:34 +00:00
|
|
|
match event.execute(client).await {
|
2022-06-23 09:07:42 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => {
|
|
|
|
error!("failed to execute event `{event_name}`, error message = `{err}`");
|
|
|
|
|
2022-07-01 19:32:20 +00:00
|
|
|
client.send(&format!("error: {err}")).await?;
|
2022-06-25 14:22:33 +00:00
|
|
|
}
|
2022-06-23 09:07:42 +00:00
|
|
|
}
|
2022-06-16 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-25 21:35:34 +00:00
|
|
|
|
|
|
|
Ok(())
|
2022-06-16 17:31:09 +00:00
|
|
|
}
|