2024-03-27 14:37:52 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
2024-04-15 12:23:09 +00:00
|
|
|
use std::num::ParseIntError;
|
2023-11-17 16:43:29 +00:00
|
|
|
|
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::cli::Commands;
|
2024-03-27 14:37:52 +00:00
|
|
|
use crate::models::Channel;
|
2023-11-26 21:46:08 +00:00
|
|
|
use crate::models::Client;
|
2023-11-17 16:32:15 +00:00
|
|
|
|
2023-11-26 21:30:59 +00:00
|
|
|
mod wrappers;
|
2023-11-17 16:43:29 +00:00
|
|
|
mod commands;
|
|
|
|
mod parameter;
|
|
|
|
mod cli;
|
2023-11-26 21:30:59 +00:00
|
|
|
mod utils;
|
2023-11-26 21:46:08 +00:00
|
|
|
mod models;
|
|
|
|
mod response;
|
2024-03-27 14:37:52 +00:00
|
|
|
mod command_utils;
|
2023-11-17 16:43:29 +00:00
|
|
|
|
2024-03-27 14:37:52 +00:00
|
|
|
fn channel_or_error(channel_res: Result<Option<Channel>, String>) -> Result<Channel, Error> {
|
|
|
|
channel_res.map_err(|err| make_action_error("find channel", err))?
|
|
|
|
.ok_or_else(|| make_action_error("find channel", String::from("Not Found.")))
|
2023-11-17 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 14:37:52 +00:00
|
|
|
fn client_or_error(client_res: Result<Option<Client>, String>) -> Result<Client, Error> {
|
|
|
|
client_res.map_err(|err| make_action_error("find client", err))?
|
|
|
|
.ok_or_else(|| make_action_error("find client", String::from("Not Found.")))
|
2023-11-17 16:32:15 +00:00
|
|
|
}
|
2023-11-13 16:01:23 +00:00
|
|
|
|
2024-03-27 14:37:52 +00:00
|
|
|
fn connect() -> Result<Telnet, Error> {
|
|
|
|
let mut connection = Telnet::connect(("127.0.0.1", 25639), 512 * 1024)
|
|
|
|
.map_err(|_| Error::new(ErrorKind::AddrNotAvailable, String::from("Failed to connect to Teamspeak.")))?;
|
2023-11-13 16:01:23 +00:00
|
|
|
|
2024-03-27 14:37:52 +00:00
|
|
|
wrappers::skip_welcome(&mut connection)
|
|
|
|
.map_err(to_other_error)?;
|
|
|
|
wrappers::login(&mut connection)
|
|
|
|
.map_err(|msg| Error::new(ErrorKind::PermissionDenied, msg))?;
|
2023-11-13 16:01:23 +00:00
|
|
|
|
2024-03-27 14:37:52 +00:00
|
|
|
Ok(connection)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_other_error(msg: String) -> Error {
|
|
|
|
Error::new(ErrorKind::Other, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_action_error(action: &str, msg: String) -> Error {
|
|
|
|
Error::new(ErrorKind::Other, format!("Failed to {}: {}", action, msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
let cli = cli::init();
|
|
|
|
|
|
|
|
let mut connection = connect()?;
|
2023-11-13 16:01:23 +00:00
|
|
|
|
|
|
|
// You can check for the existence of subcommands, and if found use their
|
|
|
|
// matches just as you would the top level cmd
|
2023-11-26 21:30:59 +00:00
|
|
|
match cli {
|
2023-11-13 16:01:23 +00:00
|
|
|
Commands::Channels(args) => {
|
2023-11-26 21:30:59 +00:00
|
|
|
match wrappers::get_channels(&mut connection, args.spacers) {
|
2023-11-13 16:01:23 +00:00
|
|
|
Ok(channels) => {
|
|
|
|
for channel in channels {
|
|
|
|
println!("{}", channel.channel_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(msg) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(make_action_error("get channels", msg));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Commands::Clients => {
|
2023-11-26 21:30:59 +00:00
|
|
|
match wrappers::get_clients(&mut connection) {
|
2023-11-13 16:01:23 +00:00
|
|
|
Ok(clients) => {
|
|
|
|
for client in clients {
|
|
|
|
println!("{}", client.client_nickname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(msg) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(make_action_error("get clients", msg));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Commands::Fetch(args) => {
|
2023-11-17 16:32:15 +00:00
|
|
|
if args.want_client() && args.want_channel() {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(Error::new(ErrorKind::InvalidInput, "Fetching both clients and channels is not supported."));
|
2023-11-17 16:32:15 +00:00
|
|
|
}
|
|
|
|
if !args.want_client() && !args.want_channel() {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(Error::new(ErrorKind::InvalidInput, "No clients or channels specified."));
|
2023-11-17 16:32:15 +00:00
|
|
|
}
|
2023-11-13 16:01:23 +00:00
|
|
|
|
2023-11-17 16:32:15 +00:00
|
|
|
if args.want_client() {
|
2024-03-27 14:37:52 +00:00
|
|
|
let client = client_or_error(args.client(&mut connection))?;
|
2023-11-17 16:32:15 +00:00
|
|
|
|
2023-11-26 21:30:59 +00:00
|
|
|
match wrappers::fetch_client(&mut connection, &[client]) {
|
2023-11-17 16:32:15 +00:00
|
|
|
Ok(_) => println!("Successfully fetched client."),
|
|
|
|
Err(msg) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(make_action_error("fetch client", msg));
|
2023-11-17 16:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.want_channel() {
|
2024-03-27 14:37:52 +00:00
|
|
|
let channel = channel_or_error(args.channel(&mut connection))?;
|
2023-11-17 16:32:15 +00:00
|
|
|
|
2023-11-26 21:30:59 +00:00
|
|
|
match wrappers::fetch_channel(&mut connection, channel) {
|
2023-11-17 16:32:15 +00:00
|
|
|
Ok(_) => println!("Successfully fetched channel."),
|
|
|
|
Err(msg) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(make_action_error("fetch channel", msg));
|
2023-11-17 16:32:15 +00:00
|
|
|
}
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 21:30:59 +00:00
|
|
|
Commands::Message(args) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
let target = args.target(&mut connection)
|
|
|
|
.map_err(|msg| make_action_error("message target", msg))?;
|
2023-11-26 21:30:59 +00:00
|
|
|
|
|
|
|
match wrappers::send_text_message(&mut connection, target, args.message) {
|
|
|
|
Ok(_) => println!("Successfully sent message."),
|
|
|
|
Err(msg) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(make_action_error("send message", msg));
|
2023-11-26 21:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 16:01:23 +00:00
|
|
|
Commands::Move(args) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
let channel = channel_or_error(args.channel(&mut connection))?;
|
|
|
|
let client = client_or_error(args.client(&mut connection))?;
|
2023-11-13 16:01:23 +00:00
|
|
|
|
2023-11-26 21:30:59 +00:00
|
|
|
match wrappers::move_client(&mut connection, &channel, &[client]) {
|
2023-11-13 16:01:23 +00:00
|
|
|
Ok(resp) => println!("Successfully moved client: {}", resp),
|
|
|
|
Err(msg) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(make_action_error("move client", msg));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Commands::Update(args) => {
|
2023-11-26 21:30:59 +00:00
|
|
|
match wrappers::update_client(&mut connection, args.to_parameter_list()) {
|
2023-11-13 16:01:23 +00:00
|
|
|
Ok(_) => println!("Successfully updated client."),
|
|
|
|
Err(msg) => {
|
2024-03-27 14:37:52 +00:00
|
|
|
return Err(make_action_error("update client", msg));
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 02:52:30 +00:00
|
|
|
|
|
|
|
Commands::Events(args) => {
|
|
|
|
loop {
|
2024-03-27 14:37:52 +00:00
|
|
|
command_utils::events::register_events(&mut connection, args.event.clone())
|
|
|
|
.map_err(to_other_error)?;
|
2024-04-15 12:23:09 +00:00
|
|
|
|
|
|
|
let self_clid: i32 = wrappers::get_self_clid(&mut connection)
|
|
|
|
.map_err(|msg| make_action_error("get self clid", msg))?
|
|
|
|
.parse()
|
|
|
|
.map_err(|err: ParseIntError| make_action_error("parse clid", err.to_string()))?;
|
|
|
|
|
|
|
|
command_utils::events::loop_response_reader(&mut connection, self_clid);
|
2024-03-27 14:37:52 +00:00
|
|
|
|
|
|
|
// loop_response_reader failed. Let's try to reconnect after 1 second.
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
connection = connect()?;
|
2024-03-05 02:52:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
2024-03-27 14:37:52 +00:00
|
|
|
Ok(())
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|