Add Events filter
This commit is contained in:
parent
895f5f9373
commit
915c2ccbbf
5 changed files with 39 additions and 12 deletions
11
src/cli.rs
11
src/cli.rs
|
@ -1,4 +1,4 @@
|
||||||
use clap::{Args, Parser, Subcommand};
|
use clap::{Args, Parser, Subcommand, ValueEnum};
|
||||||
use telnet::Telnet;
|
use telnet::Telnet;
|
||||||
|
|
||||||
use crate::parameter::ParameterList;
|
use crate::parameter::ParameterList;
|
||||||
|
@ -79,9 +79,18 @@ pub struct UpdateArgs {
|
||||||
speakers: Option<bool>,
|
speakers: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, ValueEnum)]
|
||||||
|
pub enum EventArgsFilter {
|
||||||
|
All,
|
||||||
|
Mine,
|
||||||
|
Others,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
pub struct EventArgs {
|
pub struct EventArgs {
|
||||||
pub event: Vec<EventType>,
|
pub event: Vec<EventType>,
|
||||||
|
#[arg(long, short)]
|
||||||
|
pub filter: Option<EventArgsFilter>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FetchArgs {
|
impl FetchArgs {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use telnet::Telnet;
|
use telnet::Telnet;
|
||||||
|
|
||||||
use crate::{commands, wrappers};
|
use crate::{commands, wrappers};
|
||||||
|
use crate::cli::EventArgsFilter;
|
||||||
use crate::models::{Client, Event, EventType};
|
use crate::models::{Client, Event, EventType};
|
||||||
use crate::response::Response;
|
use crate::response::Response;
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ fn try_get_self_clid(connection: &mut Telnet) -> Option<i32> {
|
||||||
.unwrap_or(None)
|
.unwrap_or(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn loop_response_reader(connection: &mut Telnet) {
|
pub fn loop_response_reader(connection: &mut Telnet, filter: &EventArgsFilter) {
|
||||||
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());
|
||||||
|
|
||||||
let mut self_clid: Option<i32> = try_get_self_clid(connection);
|
let mut self_clid: Option<i32> = try_get_self_clid(connection);
|
||||||
|
@ -46,14 +47,12 @@ pub fn loop_response_reader(connection: &mut Telnet) {
|
||||||
if let Response::Event(event_type, params) = response {
|
if let Response::Event(event_type, params) = response {
|
||||||
let event = Event::new(connection, event_type, params, &known_clients);
|
let event = Event::new(connection, event_type, params, &known_clients);
|
||||||
|
|
||||||
if let Some(client) = &event.client {
|
|
||||||
if self_clid.is_none() {
|
if self_clid.is_none() {
|
||||||
self_clid = try_get_self_clid(connection);
|
self_clid = try_get_self_clid(connection);
|
||||||
}
|
}
|
||||||
if Some(client.clid) == self_clid {
|
if !event.should_handle(filter, self_clid) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
handle_event_response(connection, event, &mut known_clients);
|
handle_event_response(connection, event, &mut known_clients);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::io::{Error, ErrorKind};
|
||||||
|
|
||||||
use telnet::Telnet;
|
use telnet::Telnet;
|
||||||
|
|
||||||
use crate::cli::Commands;
|
use crate::cli::{Commands, EventArgsFilter};
|
||||||
|
|
||||||
mod wrappers;
|
mod wrappers;
|
||||||
mod commands;
|
mod commands;
|
||||||
|
@ -138,11 +138,12 @@ fn main() -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Events(args) => {
|
Commands::Events(args) => {
|
||||||
|
let filter = args.filter.unwrap_or(EventArgsFilter::All);
|
||||||
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);
|
command_utils::events::loop_response_reader(&mut connection, &filter);
|
||||||
|
|
||||||
// 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));
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::str::FromStr;
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
use serde::ser::SerializeStruct;
|
use serde::ser::SerializeStruct;
|
||||||
use telnet::Telnet;
|
use telnet::Telnet;
|
||||||
|
use crate::cli::EventArgsFilter;
|
||||||
|
|
||||||
use crate::models::{Channel, Client};
|
use crate::models::{Channel, Client};
|
||||||
use crate::parameter::{parameter_find, ParameterList};
|
use crate::parameter::{parameter_find, ParameterList};
|
||||||
|
@ -265,6 +266,23 @@ impl Event {
|
||||||
_ => String::from(""),
|
_ => String::from(""),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_mine(&self, self_clid: Option<i32>) -> bool {
|
||||||
|
if let Some(self_clid) = self_clid {
|
||||||
|
if let Some(client) = &self.client {
|
||||||
|
return client.clid == self_clid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn should_handle(&self, filter: &EventArgsFilter, self_clid: Option<i32>) -> bool {
|
||||||
|
match filter {
|
||||||
|
EventArgsFilter::All => true,
|
||||||
|
EventArgsFilter::Mine => self.is_mine(self_clid),
|
||||||
|
EventArgsFilter::Others => !self.is_mine(self_clid),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for Event {
|
impl Serialize for Event {
|
||||||
|
|
|
@ -99,7 +99,7 @@ case $action in
|
||||||
teamspeak-query-lib message --strict-client --client "$user" "$message"
|
teamspeak-query-lib message --strict-client --client "$user" "$message"
|
||||||
;;
|
;;
|
||||||
"events")
|
"events")
|
||||||
teamspeak-query-lib events NotifyClientMoved NotifyClientEnterView NotifyClientLeftView NotifyTextMessage NotifyClientPoke \
|
teamspeak-query-lib events --filter=others NotifyClientMoved NotifyClientEnterView NotifyClientLeftView NotifyTextMessage NotifyClientPoke \
|
||||||
| jq -r --unbuffered '.message' \
|
| jq -r --unbuffered '.message' \
|
||||||
| tee >(xargs -I{} notify-send "TS3 Event" "{}")
|
| tee >(xargs -I{} notify-send "TS3 Event" "{}")
|
||||||
esac
|
esac
|
||||||
|
|
Loading…
Reference in a new issue