2023-11-17 16:43:29 +00:00
|
|
|
use clap::{Args, Parser, Subcommand};
|
2023-11-13 16:01:23 +00:00
|
|
|
use telnet::Telnet;
|
2023-11-17 16:43:29 +00:00
|
|
|
|
2023-11-13 16:01:23 +00:00
|
|
|
use crate::parameter::{Parameter, ParameterList};
|
2023-11-17 16:37:25 +00:00
|
|
|
use crate::response::channel::ResponseChannel;
|
|
|
|
use crate::response::client::ResponseClient;
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-11-26 21:30:59 +00:00
|
|
|
wrappers::find_channel(connection, channel, self.strict_channel)
|
2023-11-17 16:32:15 +00:00
|
|
|
} else {
|
|
|
|
Err("No channel specified.".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn client(&self, connection: &mut Telnet) -> Result<Option<ResponseClient>, String> {
|
|
|
|
if let Some(client) = &self.client {
|
2023-11-26 21:30:59 +00:00
|
|
|
wrappers::find_client(connection, 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, self.strict_client)? {
|
2023-11-26 21:40:41 +00:00
|
|
|
return Ok(SendTextMessageTarget::Client(client.clid));
|
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 {
|
|
|
|
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<ResponseChannel>, String> {
|
2023-11-26 21:30:59 +00:00
|
|
|
wrappers::find_channel(connection, &self.channel, self.strict_channel)
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
pub fn client(&self, connection: &mut Telnet) -> Result<Option<ResponseClient>, String> {
|
|
|
|
match &self.client {
|
|
|
|
Some(client) => {
|
2023-11-26 21:30:59 +00:00
|
|
|
wrappers::find_client(connection, 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 = Vec::new();
|
|
|
|
|
|
|
|
if let Some(name) = &self.name {
|
2023-11-26 21:30:59 +00:00
|
|
|
params.push(Parameter::new("client_nickname", name));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(away) = &self.away {
|
2023-11-26 21:30:59 +00:00
|
|
|
params.push(Parameter::new("client_away_message", away));
|
|
|
|
params.push(Parameter::new("client_away", "1"));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
if self.back {
|
2023-11-26 21:30:59 +00:00
|
|
|
params.push(Parameter::new("client_away", "0"));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(microphone) = self.microphone {
|
2023-11-26 21:30:59 +00:00
|
|
|
params.push(Parameter::new("client_input_muted", &u8::from(!microphone).to_string()));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
if let Some(speakers) = self.speakers {
|
2023-11-26 21:30:59 +00:00
|
|
|
params.push(Parameter::new("client_output_muted", &u8::from(!speakers).to_string()));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init() -> Commands {
|
|
|
|
Cli::parse().command
|
|
|
|
}
|