Fix events caused by yourself being shown
Some checks failed
/ build-upload (push) Has been cancelled

This commit is contained in:
Tobias Reisinger 2024-04-15 14:23:09 +02:00
parent ea7815535c
commit 70e0a1d0b4
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
5 changed files with 36 additions and 19 deletions

2
Cargo.lock generated
View file

@ -182,7 +182,7 @@ dependencies = [
[[package]] [[package]]
name = "teamspeak-query-lib" name = "teamspeak-query-lib"
version = "0.1.4" version = "0.1.5"
dependencies = [ dependencies = [
"clap", "clap",
"serde", "serde",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "teamspeak-query-lib" name = "teamspeak-query-lib"
version = "0.1.4" version = "0.1.5"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View file

@ -1,4 +1,5 @@
use telnet::Telnet; use telnet::Telnet;
use crate::{commands, wrappers}; use crate::{commands, wrappers};
use crate::models::{Client, Event, EventType}; use crate::models::{Client, Event, EventType};
use crate::response::Response; use crate::response::Response;
@ -12,29 +13,38 @@ pub fn register_events(connection: &mut Telnet, events: Vec<EventType>) -> Resul
Ok(()) Ok(())
} }
pub fn handle_event_response(connection: &mut Telnet, response: Response, known_clients: &mut Vec<Client>) { pub fn handle_event_response(connection: &mut Telnet, event: Event, known_clients: &mut Vec<Client>) {
if let Response::Event(event_type, params) = response { match serde_json::to_string(&event) {
Ok(json) => println!("{}", json),
let event = Event::new(connection, event_type, params, known_clients); Err(err) => {
match serde_json::to_string(&event) { // TODO: Handle serialization error
Ok(json) => println!("{}", json), eprintln!("Serialization error: {}", err);
Err(err) => {
// TODO: Handle serialization error
eprintln!("Serialization error: {}", err);
}
} }
}
if let Ok(new_clients) = wrappers::get_clients(connection) { if let Ok(new_clients) = wrappers::get_clients(connection) {
*known_clients = new_clients; *known_clients = new_clients;
}
} }
} }
pub fn loop_response_reader(connection: &mut Telnet) { pub fn loop_response_reader(connection: &mut Telnet, self_clid: i32) {
let mut known_clients = wrappers::get_clients(connection).unwrap_or_else(|_| Vec::new()); let mut known_clients = wrappers::get_clients(connection).unwrap_or_else(|_| Vec::new());
loop { loop {
match commands::read_response(connection, true, String::new()) { match commands::read_response(connection, true, String::new()) {
Ok(response) => handle_event_response(connection, response, &mut known_clients), Ok(response) => {
if let Response::Event(event_type, params) = response {
let event = Event::new(connection, event_type, params, &known_clients);
if let Some(client) = &event.client {
if client.clid == self_clid {
continue;
}
}
handle_event_response(connection, event, &mut known_clients);
}
}
Err(_) => { Err(_) => {
// print error? // print error?
return; return;

View file

@ -1,4 +1,5 @@
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
use std::num::ParseIntError;
use telnet::Telnet; use telnet::Telnet;
@ -147,7 +148,13 @@ fn main() -> Result<(), Error> {
loop { loop {
command_utils::events::register_events(&mut connection, args.event.clone()) command_utils::events::register_events(&mut connection, args.event.clone())
.map_err(to_other_error)?; .map_err(to_other_error)?;
command_utils::events::loop_response_reader(&mut connection);
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);
// loop_response_reader failed. Let's try to reconnect after 1 second. // loop_response_reader failed. Let's try to reconnect after 1 second.
std::thread::sleep(std::time::Duration::from_secs(1)); std::thread::sleep(std::time::Duration::from_secs(1));

View file

@ -110,7 +110,7 @@ pub fn get_channel_clients(connection: &mut Telnet, channel: &Channel) -> Result
} }
} }
fn get_self_clid(connection: &mut Telnet) -> Result<String, String> { pub fn get_self_clid(connection: &mut Telnet) -> Result<String, String> {
match commands::whoami(connection)? { match commands::whoami(connection)? {
Response::Data(params) => { Response::Data(params) => {
match parameter::parameter_find(&params, "clid") { match parameter::parameter_find(&params, "clid") {