Improve error handling for events and in general
All checks were successful
/ build-upload (push) Successful in 1m8s
All checks were successful
/ build-upload (push) Successful in 1m8s
This commit is contained in:
parent
8d1e813c0b
commit
f860fe3689
6 changed files with 114 additions and 104 deletions
40
src/command_utils/events.rs
Normal file
40
src/command_utils/events.rs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
use telnet::Telnet;
|
||||||
|
use crate::commands;
|
||||||
|
use crate::models::{Event, EventType};
|
||||||
|
use crate::response::Response;
|
||||||
|
|
||||||
|
pub fn register_events(connection: &mut Telnet, events: Vec<EventType>) -> Result<(), String> {
|
||||||
|
for event in events {
|
||||||
|
if commands::clientnotifyregister(connection, 1, event).is_err() {
|
||||||
|
return Err(String::from("Failed to register event listener."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_event_response(connection: &mut Telnet, response: Response) {
|
||||||
|
if let Response::Event(event_type, params) = response {
|
||||||
|
|
||||||
|
let event = Event::new(connection, event_type, params);
|
||||||
|
match serde_json::to_string(&event) {
|
||||||
|
Ok(json) => println!("{}", json),
|
||||||
|
Err(err) => {
|
||||||
|
// TODO: Handle serialization error
|
||||||
|
eprintln!("Serialization error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn loop_response_reader(connection: &mut Telnet) {
|
||||||
|
loop {
|
||||||
|
match commands::read_response(connection, true, String::new()) {
|
||||||
|
Ok(response) => handle_event_response(connection, response),
|
||||||
|
Err(_) => {
|
||||||
|
// print error?
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/command_utils/mod.rs
Normal file
1
src/command_utils/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod events;
|
|
@ -1,5 +1,6 @@
|
||||||
|
use std::time::Duration;
|
||||||
use crate::utils::SendTextMessageTarget;
|
use crate::utils::SendTextMessageTarget;
|
||||||
use telnet::Event::Data;
|
use telnet::Event::{Data, TimedOut};
|
||||||
use telnet::Telnet;
|
use telnet::Telnet;
|
||||||
use crate::models::EventType;
|
use crate::models::EventType;
|
||||||
|
|
||||||
|
@ -14,18 +15,21 @@ fn to_single_response(resp: Response) -> Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_part(connection: &mut Telnet) -> Result<String, String> {
|
fn read_part(connection: &mut Telnet) -> Result<String, String> {
|
||||||
match connection.read() {
|
match connection.read_timeout(Duration::new(5, 0)) {
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
match event {
|
match event {
|
||||||
Data(bytes) => Ok(String::from_utf8(bytes.to_vec())
|
Data(bytes) => Ok(String::from_utf8(bytes.to_vec())
|
||||||
.map_err(|_| "Teamspeak returned a badly formatted models.")?),
|
.map_err(|_| "Teamspeak returned an invalid response.")?),
|
||||||
|
TimedOut => {
|
||||||
|
Ok(String::new())
|
||||||
|
},
|
||||||
_ => {
|
_ => {
|
||||||
Err(String::from("Received unknown event from Teamspeak."))
|
Err(String::from("Received unknown event from Teamspeak."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(err) => {
|
||||||
Err(String::from("Failed to read from Teamspeak."))
|
Err(format!("Failed to read from Teamspeak: {}", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
134
src/main.rs
134
src/main.rs
|
@ -1,11 +1,10 @@
|
||||||
use std::process::exit;
|
use std::io::{Error, ErrorKind};
|
||||||
|
|
||||||
use telnet::Telnet;
|
use telnet::Telnet;
|
||||||
|
|
||||||
use crate::cli::Commands;
|
use crate::cli::Commands;
|
||||||
use crate::models::{Channel, Event};
|
use crate::models::Channel;
|
||||||
use crate::models::Client;
|
use crate::models::Client;
|
||||||
use crate::response::Response;
|
|
||||||
|
|
||||||
mod wrappers;
|
mod wrappers;
|
||||||
mod commands;
|
mod commands;
|
||||||
|
@ -14,41 +13,42 @@ mod cli;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod models;
|
mod models;
|
||||||
mod response;
|
mod response;
|
||||||
|
mod command_utils;
|
||||||
|
|
||||||
fn channel_or_exit(channel_res: Result<Option<Channel>, String>) -> Channel {
|
fn channel_or_error(channel_res: Result<Option<Channel>, String>) -> Result<Channel, Error> {
|
||||||
channel_res.unwrap_or_else(|err| {
|
channel_res.map_err(|err| make_action_error("find channel", err))?
|
||||||
println!("Failed to find channel: {}", err);
|
.ok_or_else(|| make_action_error("find channel", String::from("Not Found.")))
|
||||||
exit(1);
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
println!("Failed to find channel.");
|
|
||||||
exit(1);
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn client_or_exit(client_res: Result<Option<Client>, String>) -> Client {
|
fn client_or_error(client_res: Result<Option<Client>, String>) -> Result<Client, Error> {
|
||||||
client_res.unwrap_or_else(|err| {
|
client_res.map_err(|err| make_action_error("find client", err))?
|
||||||
println!("Failed to find client: {}", err);
|
.ok_or_else(|| make_action_error("find client", String::from("Not Found.")))
|
||||||
exit(1);
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
println!("Failed to find client.");
|
|
||||||
exit(1);
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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.")))?;
|
||||||
|
|
||||||
|
wrappers::skip_welcome(&mut connection)
|
||||||
|
.map_err(to_other_error)?;
|
||||||
|
wrappers::login(&mut connection)
|
||||||
|
.map_err(|msg| Error::new(ErrorKind::PermissionDenied, msg))?;
|
||||||
|
|
||||||
|
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 cli = cli::init();
|
||||||
|
|
||||||
let connection = Telnet::connect(("127.0.0.1", 25639), 512 * 1024);
|
let mut connection = connect()?;
|
||||||
if connection.is_err() {
|
|
||||||
println!("Failed to connect to Teamspeak.");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
let mut connection = connection.unwrap();
|
|
||||||
|
|
||||||
wrappers::skip_welcome(&mut connection);
|
|
||||||
wrappers::login(&mut connection);
|
|
||||||
|
|
||||||
// You can check for the existence of subcommands, and if found use their
|
// You can check for the existence of subcommands, and if found use their
|
||||||
// matches just as you would the top level cmd
|
// matches just as you would the top level cmd
|
||||||
|
@ -61,8 +61,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
println!("Failed to get channels: {}", msg);
|
return Err(make_action_error("get channels", msg));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,71 +74,62 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
println!("Failed to get clients: {}", msg);
|
return Err(make_action_error("get clients", msg));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Fetch(args) => {
|
Commands::Fetch(args) => {
|
||||||
if args.want_client() && args.want_channel() {
|
if args.want_client() && args.want_channel() {
|
||||||
println!("Fetching both clients and channels is not supported.");
|
return Err(Error::new(ErrorKind::InvalidInput, "Fetching both clients and channels is not supported."));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
if !args.want_client() && !args.want_channel() {
|
if !args.want_client() && !args.want_channel() {
|
||||||
println!("No clients or channels specified.");
|
return Err(Error::new(ErrorKind::InvalidInput, "No clients or channels specified."));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.want_client() {
|
if args.want_client() {
|
||||||
let client = client_or_exit(args.client(&mut connection));
|
let client = client_or_error(args.client(&mut connection))?;
|
||||||
|
|
||||||
match wrappers::fetch_client(&mut connection, &[client]) {
|
match wrappers::fetch_client(&mut connection, &[client]) {
|
||||||
Ok(_) => println!("Successfully fetched client."),
|
Ok(_) => println!("Successfully fetched client."),
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
println!("Failed to fetch client: {}", msg);
|
return Err(make_action_error("fetch client", msg));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.want_channel() {
|
if args.want_channel() {
|
||||||
let channel = channel_or_exit(args.channel(&mut connection));
|
let channel = channel_or_error(args.channel(&mut connection))?;
|
||||||
|
|
||||||
match wrappers::fetch_channel(&mut connection, channel) {
|
match wrappers::fetch_channel(&mut connection, channel) {
|
||||||
Ok(_) => println!("Successfully fetched channel."),
|
Ok(_) => println!("Successfully fetched channel."),
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
println!("Failed to fetch channel: {}", msg);
|
return Err(make_action_error("fetch channel", msg));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Message(args) => {
|
Commands::Message(args) => {
|
||||||
let target = args.target(&mut connection).unwrap_or_else(|err| {
|
let target = args.target(&mut connection)
|
||||||
println!("Failed to get message target: {}", err);
|
.map_err(|msg| make_action_error("message target", msg))?;
|
||||||
exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
match wrappers::send_text_message(&mut connection, target, args.message) {
|
match wrappers::send_text_message(&mut connection, target, args.message) {
|
||||||
Ok(_) => println!("Successfully sent message."),
|
Ok(_) => println!("Successfully sent message."),
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
println!("Failed to send message: {}", msg);
|
return Err(make_action_error("send message", msg));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Move(args) => {
|
Commands::Move(args) => {
|
||||||
let channel = channel_or_exit(args.channel(&mut connection));
|
let channel = channel_or_error(args.channel(&mut connection))?;
|
||||||
let client = client_or_exit(args.client(&mut connection));
|
let client = client_or_error(args.client(&mut connection))?;
|
||||||
|
|
||||||
match wrappers::move_client(&mut connection, &channel, &[client]) {
|
match wrappers::move_client(&mut connection, &channel, &[client]) {
|
||||||
Ok(resp) => println!("Successfully moved client: {}", resp),
|
Ok(resp) => println!("Successfully moved client: {}", resp),
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
println!("Failed to move client: {}", msg);
|
return Err(make_action_error("move client", msg));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,40 +138,22 @@ fn main() {
|
||||||
match wrappers::update_client(&mut connection, args.to_parameter_list()) {
|
match wrappers::update_client(&mut connection, args.to_parameter_list()) {
|
||||||
Ok(_) => println!("Successfully updated client."),
|
Ok(_) => println!("Successfully updated client."),
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
println!("Failed to update client: {}", msg);
|
return Err(make_action_error("update client", msg));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Events(args) => {
|
Commands::Events(args) => {
|
||||||
for event in args.event {
|
|
||||||
if commands::clientnotifyregister(&mut connection, 1, event).is_err() {
|
|
||||||
println!("Failed to register event listener.");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match commands::read_response(&mut connection, true, String::new()) {
|
command_utils::events::register_events(&mut connection, args.event.clone())
|
||||||
Ok(response) => {
|
.map_err(to_other_error)?;
|
||||||
if let Response::Event(event_type, params) = response {
|
command_utils::events::loop_response_reader(&mut connection);
|
||||||
|
|
||||||
let event = Event::new(&mut connection, event_type, params);
|
// loop_response_reader failed. Let's try to reconnect after 1 second.
|
||||||
match serde_json::to_string(&event) {
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||||
Ok(json) => println!("{}", json),
|
connection = connect()?;
|
||||||
Err(_) => {
|
|
||||||
// TODO: Handle error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
// TODO: Handle error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
|
@ -1,4 +1,3 @@
|
||||||
use std::process::exit;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use telnet::Event::TimedOut;
|
use telnet::Event::TimedOut;
|
||||||
|
@ -11,34 +10,28 @@ use crate::models::Client;
|
||||||
use crate::response::Response;
|
use crate::response::Response;
|
||||||
use crate::utils::SendTextMessageTarget;
|
use crate::utils::SendTextMessageTarget;
|
||||||
|
|
||||||
pub fn skip_welcome(connection: &mut Telnet) {
|
pub fn skip_welcome(connection: &mut Telnet) -> Result<(), String> {
|
||||||
loop {
|
loop {
|
||||||
let event_result = connection.read_timeout(Duration::from_millis(100));
|
let event_result = connection.read_timeout(Duration::from_millis(100));
|
||||||
match event_result {
|
match event_result {
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
if let TimedOut = event {
|
if let TimedOut = event {
|
||||||
break;
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => println!("Failed to read from Teamspeak."),
|
Err(_) => return Err(String::from("Failed to read from Teamspeak.")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn login(connection: &mut Telnet) {
|
pub fn login(connection: &mut Telnet) -> Result<(), String> {
|
||||||
// read api key from environment variable
|
// read api key from environment variable
|
||||||
let apikey = std::env::var("TS3_CLIENT_API_KEY").unwrap_or_else(|_| {
|
let apikey = std::env::var("TS3_CLIENT_API_KEY")
|
||||||
println!("No API key found in environment variable TS3_CLIENT_API_KEY.");
|
.map_err(|_| String::from("No API key found in environment variable TS3_CLIENT_API_KEY."))?;
|
||||||
exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
match commands::login(connection, &apikey) {
|
commands::login(connection, &apikey)
|
||||||
Ok(_) => {}
|
.map(|_| ())
|
||||||
Err(msg) => {
|
.map_err(|err| format!("Failed to authenticate with Teamspeak: {}", err))
|
||||||
println!("Failed to authenticate with Teamspeak: {}", msg);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_channels(connection: &mut Telnet, spacers: bool) -> Result<Vec<Channel>, String> {
|
pub fn get_channels(connection: &mut Telnet, spacers: bool) -> Result<Vec<Channel>, String> {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env sh
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
actions="quick
|
actions="quick
|
||||||
move
|
move
|
||||||
|
@ -101,5 +101,5 @@ case $action in
|
||||||
"events-move")
|
"events-move")
|
||||||
teamspeak-query-lib events NotifyClientMoved NotifyClientEnterView \
|
teamspeak-query-lib events NotifyClientMoved NotifyClientEnterView \
|
||||||
| jq -r --unbuffered '.client.client_nickname + " joined " + .channel.channel_name // "the server"' \
|
| jq -r --unbuffered '.client.client_nickname + " joined " + .channel.channel_name // "the server"' \
|
||||||
| xargs -I{} notify-send "TS3 movement" "{}"
|
| tee >(xargs -I{} notify-send "TS3 movement" "{}")
|
||||||
esac
|
esac
|
||||||
|
|
Loading…
Reference in a new issue