Refactor and add message command

This commit is contained in:
Tobias Reisinger 2023-11-26 22:30:59 +01:00
parent e87bed00a2
commit fa959136be
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
8 changed files with 370 additions and 263 deletions

View file

@ -4,7 +4,8 @@ use telnet::Telnet;
use crate::parameter::{Parameter, ParameterList};
use crate::response::channel::ResponseChannel;
use crate::response::client::ResponseClient;
use crate::utils;
use crate::utils::SendTextMessageTarget;
use crate::wrappers;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
@ -19,6 +20,7 @@ pub enum Commands {
Channels(ChannelsArgs),
Clients,
Fetch(FetchArgs),
Message(MessageArgs),
Move(MoveArgs),
Update(UpdateArgs),
}
@ -41,6 +43,17 @@ pub struct FetchArgs {
client: Option<String>,
}
#[derive(Args)]
pub struct MessageArgs {
#[arg(long)]
strict_client: bool,
#[arg(long)]
client: Option<String>,
#[arg(long)]
server: bool,
pub message: String,
}
#[derive(Args)]
pub struct MoveArgs {
#[arg(long)]
@ -76,31 +89,46 @@ impl FetchArgs {
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<ResponseChannel>, String> {
if let Some(channel) = &self.channel {
utils::find_channel(connection, channel, self.strict_channel)
wrappers::find_channel(connection, channel, self.strict_channel)
} else {
Err("No channel specified.".to_string())
}
}
pub fn client(&self, connection: &mut Telnet) -> Result<Option<ResponseClient>, String> {
if let Some(client) = &self.client {
utils::find_client(connection, client, self.strict_client)
wrappers::find_client(connection, client, self.strict_client)
} else {
Err("No client specified.".to_string())
}
}
}
impl MessageArgs {
pub fn target(&self, connection: &mut Telnet) -> Result<SendTextMessageTarget, String> {
if self.server {
Ok(SendTextMessageTarget::Server)
} else if let Some(client) = &self.client {
if let Some(client) = wrappers::find_client(connection, client, self.strict_client)? {
return Ok(SendTextMessageTarget::Client(client.cid));
}
return Err("Could not find client.".to_string());
} else {
Ok(SendTextMessageTarget::Channel)
}
}
}
impl MoveArgs {
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<ResponseChannel>, String> {
utils::find_channel(connection, &self.channel, self.strict_channel)
wrappers::find_channel(connection, &self.channel, self.strict_channel)
}
pub fn client(&self, connection: &mut Telnet) -> Result<Option<ResponseClient>, String> {
match &self.client {
Some(client) => {
utils::find_client(connection, client, self.strict_client)
wrappers::find_client(connection, client, self.strict_client)
}
None => {
match utils::find_self(connection) {
match wrappers::find_self(connection) {
Ok(client) => Ok(Some(client)),
Err(msg) => Err(msg)
}
@ -114,24 +142,22 @@ impl UpdateArgs {
let mut params: ParameterList = Vec::new();
if let Some(name) = &self.name {
params.push(Parameter::new(String::from("client_nickname"), name.clone()));
params.push(Parameter::new("client_nickname", name));
}
if let Some(away) = &self.away {
params.push(Parameter::new(String::from("client_away_message"), away.clone()));
params.push(Parameter::new(String::from("client_away"), String::from("1")));
params.push(Parameter::new("client_away_message", away));
params.push(Parameter::new("client_away", "1"));
}
if self.back {
params.push(Parameter::new(String::from("client_away"), String::from("0")));
params.push(Parameter::new("client_away", "0"));
}
if let Some(microphone) = self.microphone {
let muted = u8::from(!microphone).to_string();
params.push(Parameter::new(String::from("client_input_muted"), muted));
params.push(Parameter::new("client_input_muted", &u8::from(!microphone).to_string()));
}
if let Some(speakers) = self.speakers {
let muted = u8::from(!speakers).to_string();
params.push(Parameter::new(String::from("client_output_muted"), muted));
params.push(Parameter::new("client_output_muted", &u8::from(!speakers).to_string()));
}
params