teamspeak-query-lib/src/cli.rs

184 lines
4.7 KiB
Rust
Raw Normal View History

2024-06-02 23:39:37 +00:00
use clap::{Args, Parser, Subcommand, ValueEnum};
2023-11-13 16:01:23 +00:00
use telnet::Telnet;
2023-11-17 16:43:29 +00:00
use crate::parameter::ParameterList;
use crate::models::{Channel, EventType};
2023-11-26 21:46:08 +00:00
use crate::models::Client;
2023-11-26 21:30:59 +00:00
use crate::utils::SendTextMessageTarget;
use crate::wrappers;
2023-11-13 16:01:23 +00:00
#[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),
2023-11-26 21:30:59 +00:00
Message(MessageArgs),
2023-11-13 16:01:23 +00:00
Move(MoveArgs),
Update(UpdateArgs),
Events(EventArgs),
2023-11-13 16:01:23 +00:00
}
#[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
}
2023-11-26 21:30:59 +00:00
#[derive(Args)]
pub struct MessageArgs {
#[arg(long)]
strict_client: bool,
#[arg(long)]
client: Option<String>,
#[arg(long)]
server: bool,
pub message: 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>,
}
2024-06-02 23:39:37 +00:00
#[derive(Clone, ValueEnum)]
pub enum EventArgsFilter {
All,
Mine,
Others,
}
#[derive(Args)]
pub struct EventArgs {
pub event: Vec<EventType>,
2024-06-02 23:39:37 +00:00
#[arg(long, short)]
pub filter: Option<EventArgsFilter>,
}
2023-11-13 16:01:23 +00:00
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()
}
2023-11-26 21:46:08 +00:00
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<Channel>, String> {
2023-11-17 16:32:15 +00:00
if let Some(channel) = &self.channel {
wrappers::find_channel(connection, "channel_name", channel, self.strict_channel)
2023-11-17 16:32:15 +00:00
} else {
Err("No channel specified.".to_string())
}
}
2023-11-26 21:46:08 +00:00
pub fn client(&self, connection: &mut Telnet) -> Result<Option<Client>, String> {
2023-11-17 16:32:15 +00:00
if let Some(client) = &self.client {
wrappers::find_client(connection, "client_nickname", client, self.strict_client)
2023-11-17 16:32:15 +00:00
} else {
Err("No client specified.".to_string())
}
}
2023-11-13 16:01:23 +00:00
}
2023-11-26 21:30:59 +00:00
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_nickname", client, self.strict_client)? {
2023-11-26 21:46:08 +00:00
return Ok(SendTextMessageTarget::Client(client));
2023-11-26 21:30:59 +00:00
}
return Err("Could not find client.".to_string());
} else {
Ok(SendTextMessageTarget::Channel)
}
}
}
2023-11-13 16:01:23 +00:00
impl MoveArgs {
2023-11-26 21:46:08 +00:00
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<Channel>, String> {
wrappers::find_channel(connection, "channel_name", &self.channel, self.strict_channel)
2023-11-13 16:01:23 +00:00
}
2023-11-26 21:46:08 +00:00
pub fn client(&self, connection: &mut Telnet) -> Result<Option<Client>, String> {
2023-11-13 16:01:23 +00:00
match &self.client {
Some(client) => {
wrappers::find_client(connection, "client_nickname", client, self.strict_client)
2023-11-13 16:01:23 +00:00
}
None => {
2023-11-26 21:30:59 +00:00
match wrappers::find_self(connection) {
2023-11-13 16:01:23 +00:00
Ok(client) => Ok(Some(client)),
Err(msg) => Err(msg)
}
}
}
}
}
impl UpdateArgs {
pub fn to_parameter_list(&self) -> ParameterList {
let mut params: ParameterList = ParameterList::new();
2023-11-13 16:01:23 +00:00
if let Some(name) = &self.name {
params.insert(String::from("client_nickname"), name.clone());
2023-11-13 16:01:23 +00:00
}
if let Some(away) = &self.away {
params.insert(String::from("client_away_message"), away.clone());
params.insert(String::from("client_away"), String::from("1"));
2023-11-13 16:01:23 +00:00
}
if self.back {
params.insert(String::from("client_away"), String::from("0"));
2023-11-13 16:01:23 +00:00
}
if let Some(microphone) = self.microphone {
params.insert(String::from("client_input_muted"), u8::from(!microphone).to_string());
2023-11-13 16:01:23 +00:00
}
if let Some(speakers) = self.speakers {
params.insert(String::from("client_output_muted"), u8::from(!speakers).to_string());
2023-11-13 16:01:23 +00:00
}
params
}
}
pub fn init() -> Commands {
Cli::parse().command
}