teamspeak-query-lib/src/main.rs

156 lines
4.8 KiB
Rust
Raw Normal View History

2023-11-13 16:01:23 +00:00
use std::process::exit;
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;
2023-11-26 21:46:08 +00:00
use crate::models::Channel;
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;
2023-11-17 16:43:29 +00:00
2023-11-26 21:46:08 +00:00
fn channel_or_exit(channel_res: Result<Option<Channel>, String>) -> Channel {
2023-11-17 16:32:15 +00:00
channel_res.unwrap_or_else(|err| {
println!("Failed to find channel: {}", err);
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find channel.");
exit(1);
})
}
2023-11-26 21:46:08 +00:00
fn client_or_exit(client_res: Result<Option<Client>, String>) -> Client {
2023-11-17 16:32:15 +00:00
client_res.unwrap_or_else(|err| {
println!("Failed to find client: {}", err);
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find client.");
exit(1);
})
}
2023-11-13 16:01:23 +00:00
fn main() {
let cli = cli::init();
let connection = Telnet::connect(("127.0.0.1", 25639), 512 * 1024);
if connection.is_err() {
println!("Failed to connect to Teamspeak.");
exit(1);
}
let mut connection = connection.unwrap();
2023-11-26 21:30:59 +00:00
wrappers::skip_welcome(&mut connection);
wrappers::login(&mut connection);
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) => {
println!("Failed to get channels: {}", msg);
exit(1);
}
}
}
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) => {
println!("Failed to get clients: {}", msg);
exit(1);
}
}
}
Commands::Fetch(args) => {
2023-11-17 16:32:15 +00:00
if args.want_client() && args.want_channel() {
println!("Fetching both clients and channels is not supported.");
2023-11-13 16:01:23 +00:00
exit(1);
2023-11-17 16:32:15 +00:00
}
if !args.want_client() && !args.want_channel() {
println!("No clients or channels specified.");
exit(1);
}
2023-11-13 16:01:23 +00:00
2023-11-17 16:32:15 +00:00
if args.want_client() {
let client = client_or_exit(args.client(&mut connection));
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) => {
println!("Failed to fetch client: {}", msg);
exit(1);
}
}
}
if args.want_channel() {
let channel = channel_or_exit(args.channel(&mut connection));
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) => {
println!("Failed to fetch channel: {}", msg);
exit(1);
}
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).unwrap_or_else(|err| {
println!("Failed to get message target: {}", err);
exit(1);
});
match wrappers::send_text_message(&mut connection, target, args.message) {
Ok(_) => println!("Successfully sent message."),
Err(msg) => {
println!("Failed to send message: {}", msg);
exit(1);
}
}
}
2023-11-13 16:01:23 +00:00
Commands::Move(args) => {
2023-11-17 16:32:15 +00:00
let channel = channel_or_exit(args.channel(&mut connection));
let client = client_or_exit(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) => {
println!("Failed to move client: {}", msg);
exit(1);
}
}
}
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) => {
println!("Failed to update client: {}", msg);
exit(1);
}
}
}
}
}