teamspeak-query-lib/src/main.rs

155 lines
5.3 KiB
Rust
Raw Normal View History

use std::io::{Error, ErrorKind};
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
2024-06-02 23:39:37 +00:00
use crate::cli::{Commands, EventArgsFilter};
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;
mod command_utils;
2023-11-17 16:43:29 +00:00
fn result_or_error<T>(res: Result<Option<T>, String>, result_type: &str) -> Result<T, Error> {
let error_action = format!("find {}", result_type);
res.map_err(|err| make_action_error(&error_action, err))?
.ok_or_else(|| make_action_error(&error_action, String::from("Not Found.")))
2023-11-17 16:32:15 +00:00
}
2023-11-13 16:01:23 +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
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
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) => {
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) => {
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() {
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() {
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() {
let client = result_or_error(args.client(&mut connection), "client")?;
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) => {
return Err(make_action_error("fetch client", msg));
2023-11-17 16:32:15 +00:00
}
}
}
if args.want_channel() {
let channel = result_or_error(args.channel(&mut connection), "channel")?;
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) => {
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) => {
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) => {
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) => {
let channel = result_or_error(args.channel(&mut connection), "channel")?;
let client = result_or_error(args.client(&mut connection), "client")?;
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) => {
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) => {
return Err(make_action_error("update client", msg));
2023-11-13 16:01:23 +00:00
}
}
}
Commands::Events(args) => {
2024-06-02 23:39:37 +00:00
let filter = args.filter.unwrap_or(EventArgsFilter::All);
loop {
command_utils::events::register_events(&mut connection, args.event.clone())
.map_err(to_other_error)?;
2024-06-02 23:39:37 +00:00
command_utils::events::loop_response_reader(&mut connection, &filter);
// loop_response_reader failed. Let's try to reconnect after 1 second.
std::thread::sleep(std::time::Duration::from_secs(1));
connection = connect()?;
}
}
2023-11-13 16:01:23 +00:00
}
Ok(())
2023-11-13 16:01:23 +00:00
}