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]]
name = "teamspeak-query-lib"
version = "0.1.4"
version = "0.1.5"
dependencies = [
"clap",
"serde",

View file

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

View file

@ -1,4 +1,5 @@
use telnet::Telnet;
use crate::{commands, wrappers};
use crate::models::{Client, Event, EventType};
use crate::response::Response;
@ -12,10 +13,7 @@ pub fn register_events(connection: &mut Telnet, events: Vec<EventType>) -> Resul
Ok(())
}
pub fn handle_event_response(connection: &mut Telnet, response: Response, known_clients: &mut Vec<Client>) {
if let Response::Event(event_type, params) = response {
let event = Event::new(connection, event_type, params, known_clients);
pub fn handle_event_response(connection: &mut Telnet, event: Event, known_clients: &mut Vec<Client>) {
match serde_json::to_string(&event) {
Ok(json) => println!("{}", json),
Err(err) => {
@ -27,14 +25,26 @@ pub fn handle_event_response(connection: &mut Telnet, response: Response, known_
if let Ok(new_clients) = wrappers::get_clients(connection) {
*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());
loop {
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(_) => {
// print error?
return;

View file

@ -1,4 +1,5 @@
use std::io::{Error, ErrorKind};
use std::num::ParseIntError;
use telnet::Telnet;
@ -147,7 +148,13 @@ fn main() -> Result<(), Error> {
loop {
command_utils::events::register_events(&mut connection, args.event.clone())
.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.
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)? {
Response::Data(params) => {
match parameter::parameter_find(&params, "clid") {