teamspeak-query-lib/src/cli.rs

142 lines
3.8 KiB
Rust
Raw Normal View History

2023-11-13 16:01:23 +00:00
use clap::{Parser, Subcommand, Args};
use telnet::Telnet;
use crate::parameter::{Parameter, ParameterList};
use crate::response::channel::ResponseChannel;
use crate::response::client::ResponseClient;
2023-11-13 16:01:23 +00:00
use crate::utils;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
Channels(ChannelsArgs),
Clients,
Fetch(FetchArgs),
Move(MoveArgs),
Update(UpdateArgs),
}
#[derive(Args)]
pub struct ChannelsArgs {
#[arg(long)]
pub spacers: bool,
}
#[derive(Args)]
pub struct FetchArgs {
#[arg(long)]
strict_client: bool,
2023-11-17 16:32:15 +00:00
#[arg(long)]
strict_channel: bool,
#[arg(long)]
channel: Option<String>,
#[arg(long)]
client: Option<String>,
2023-11-13 16:01:23 +00:00
}
#[derive(Args)]
pub struct MoveArgs {
#[arg(long)]
strict_client: bool,
#[arg(long)]
strict_channel: bool,
channel: String,
client: Option<String>,
}
#[derive(Args)]
pub struct UpdateArgs {
#[arg(long, short)]
name: Option<String>,
#[arg(long, short)]
away: Option<String>,
#[arg(long, short)]
back: bool,
#[arg(long, short)]
microphone: Option<bool>,
#[arg(long, short)]
speakers: Option<bool>,
}
impl FetchArgs {
2023-11-17 16:32:15 +00:00
pub fn want_channel(&self) -> bool {
self.channel.is_some()
2023-11-13 16:01:23 +00:00
}
2023-11-17 16:32:15 +00:00
pub fn want_client(&self) -> bool {
self.client.is_some()
}
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)
} 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)
} else {
Err("No client specified.".to_string())
}
}
2023-11-13 16:01:23 +00:00
}
impl MoveArgs {
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<ResponseChannel>, String> {
utils::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)
}
None => {
match utils::find_self(connection) {
Ok(client) => Ok(Some(client)),
Err(msg) => Err(msg)
}
}
}
}
}
impl UpdateArgs {
pub fn to_parameter_list(&self) -> ParameterList {
let mut params: ParameterList = Vec::new();
if let Some(name) = &self.name {
params.push(Parameter::new(String::from("client_nickname"), name.clone()));
}
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")));
}
if self.back {
params.push(Parameter::new(String::from("client_away"), String::from("0")));
}
if let Some(microphone) = self.microphone {
let muted = u8::from(!microphone).to_string();
params.push(Parameter::new(String::from("client_input_muted"), muted));
}
if let Some(speakers) = self.speakers {
let muted = u8::from(!speakers).to_string();
params.push(Parameter::new(String::from("client_output_muted"), muted));
}
params
}
}
pub fn init() -> Commands {
Cli::parse().command
}