Compare commits
No commits in common. "915c2ccbbfa8bf7c3561fc1a50752630720fb66b" and "70e0a1d0b45c1645235dcd39ae617cbe3a45d7a0" have entirely different histories.
915c2ccbbf
...
70e0a1d0b4
6 changed files with 31 additions and 61 deletions
4
Makefile
4
Makefile
|
@ -2,13 +2,11 @@
|
|||
|
||||
INSTALL_DIR = $(HOME)/.local/bin
|
||||
|
||||
export PATH := target/debug:$(PATH)
|
||||
|
||||
build:
|
||||
@cargo build
|
||||
|
||||
run: build
|
||||
./ts-control
|
||||
@PATH=$(PWD)/target/debug:$(PATH) ./ts-control
|
||||
|
||||
install:
|
||||
mkdir -p "$(INSTALL_DIR)"
|
||||
|
|
11
src/cli.rs
11
src/cli.rs
|
@ -1,4 +1,4 @@
|
|||
use clap::{Args, Parser, Subcommand, ValueEnum};
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use telnet::Telnet;
|
||||
|
||||
use crate::parameter::ParameterList;
|
||||
|
@ -79,18 +79,9 @@ pub struct UpdateArgs {
|
|||
speakers: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Clone, ValueEnum)]
|
||||
pub enum EventArgsFilter {
|
||||
All,
|
||||
Mine,
|
||||
Others,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct EventArgs {
|
||||
pub event: Vec<EventType>,
|
||||
#[arg(long, short)]
|
||||
pub filter: Option<EventArgsFilter>,
|
||||
}
|
||||
|
||||
impl FetchArgs {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use telnet::Telnet;
|
||||
|
||||
use crate::{commands, wrappers};
|
||||
use crate::cli::EventArgsFilter;
|
||||
use crate::models::{Client, Event, EventType};
|
||||
use crate::response::Response;
|
||||
|
||||
|
@ -28,31 +27,20 @@ pub fn handle_event_response(connection: &mut Telnet, event: Event, known_client
|
|||
}
|
||||
}
|
||||
|
||||
fn try_get_self_clid(connection: &mut Telnet) -> Option<i32> {
|
||||
wrappers::get_self_clid(connection)
|
||||
.unwrap_or_default()
|
||||
.parse()
|
||||
.map(Some)
|
||||
.unwrap_or(None)
|
||||
}
|
||||
|
||||
pub fn loop_response_reader(connection: &mut Telnet, filter: &EventArgsFilter) {
|
||||
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 self_clid: Option<i32> = try_get_self_clid(connection);
|
||||
|
||||
loop {
|
||||
match commands::read_response(connection, true, String::new()) {
|
||||
Ok(response) => {
|
||||
if let Response::Event(event_type, params) = response {
|
||||
let event = Event::new(connection, event_type, params, &known_clients);
|
||||
|
||||
if self_clid.is_none() {
|
||||
self_clid = try_get_self_clid(connection);
|
||||
}
|
||||
if !event.should_handle(filter, self_clid) {
|
||||
if let Some(client) = &event.client {
|
||||
if client.clid == self_clid {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
handle_event_response(connection, event, &mut known_clients);
|
||||
}
|
||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -1,8 +1,11 @@
|
|||
use std::io::{Error, ErrorKind};
|
||||
use std::num::ParseIntError;
|
||||
|
||||
use telnet::Telnet;
|
||||
|
||||
use crate::cli::{Commands, EventArgsFilter};
|
||||
use crate::cli::Commands;
|
||||
use crate::models::Channel;
|
||||
use crate::models::Client;
|
||||
|
||||
mod wrappers;
|
||||
mod commands;
|
||||
|
@ -13,10 +16,14 @@ mod models;
|
|||
mod response;
|
||||
mod command_utils;
|
||||
|
||||
fn result_or_error<T>(res: Result<Option<T>, String>, result_type: &str) -> Result<T, Error> {
|
||||
let error_action = format!("find {}", result_type);
|
||||
res.map_err(|err| make_action_error(&error_action, err))?
|
||||
.ok_or_else(|| make_action_error(&error_action, String::from("Not Found.")))
|
||||
fn channel_or_error(channel_res: Result<Option<Channel>, String>) -> Result<Channel, Error> {
|
||||
channel_res.map_err(|err| make_action_error("find channel", err))?
|
||||
.ok_or_else(|| make_action_error("find channel", String::from("Not Found.")))
|
||||
}
|
||||
|
||||
fn client_or_error(client_res: Result<Option<Client>, String>) -> Result<Client, Error> {
|
||||
client_res.map_err(|err| make_action_error("find client", err))?
|
||||
.ok_or_else(|| make_action_error("find client", String::from("Not Found.")))
|
||||
}
|
||||
|
||||
fn connect() -> Result<Telnet, Error> {
|
||||
|
@ -82,7 +89,7 @@ fn main() -> Result<(), Error> {
|
|||
}
|
||||
|
||||
if args.want_client() {
|
||||
let client = result_or_error(args.client(&mut connection), "client")?;
|
||||
let client = client_or_error(args.client(&mut connection))?;
|
||||
|
||||
match wrappers::fetch_client(&mut connection, &[client]) {
|
||||
Ok(_) => println!("Successfully fetched client."),
|
||||
|
@ -93,7 +100,7 @@ fn main() -> Result<(), Error> {
|
|||
}
|
||||
|
||||
if args.want_channel() {
|
||||
let channel = result_or_error(args.channel(&mut connection), "channel")?;
|
||||
let channel = channel_or_error(args.channel(&mut connection))?;
|
||||
|
||||
match wrappers::fetch_channel(&mut connection, channel) {
|
||||
Ok(_) => println!("Successfully fetched channel."),
|
||||
|
@ -117,8 +124,8 @@ fn main() -> Result<(), Error> {
|
|||
}
|
||||
|
||||
Commands::Move(args) => {
|
||||
let channel = result_or_error(args.channel(&mut connection), "channel")?;
|
||||
let client = result_or_error(args.client(&mut connection), "client")?;
|
||||
let channel = channel_or_error(args.channel(&mut connection))?;
|
||||
let client = client_or_error(args.client(&mut connection))?;
|
||||
|
||||
match wrappers::move_client(&mut connection, &channel, &[client]) {
|
||||
Ok(resp) => println!("Successfully moved client: {}", resp),
|
||||
|
@ -138,12 +145,16 @@ fn main() -> Result<(), Error> {
|
|||
}
|
||||
|
||||
Commands::Events(args) => {
|
||||
let filter = args.filter.unwrap_or(EventArgsFilter::All);
|
||||
loop {
|
||||
command_utils::events::register_events(&mut connection, args.event.clone())
|
||||
.map_err(to_other_error)?;
|
||||
|
||||
command_utils::events::loop_response_reader(&mut connection, &filter);
|
||||
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));
|
||||
|
|
|
@ -4,7 +4,6 @@ use std::str::FromStr;
|
|||
use serde::{Deserialize, Serialize, Serializer};
|
||||
use serde::ser::SerializeStruct;
|
||||
use telnet::Telnet;
|
||||
use crate::cli::EventArgsFilter;
|
||||
|
||||
use crate::models::{Channel, Client};
|
||||
use crate::parameter::{parameter_find, ParameterList};
|
||||
|
@ -266,23 +265,6 @@ impl Event {
|
|||
_ => 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 {
|
||||
|
|
|
@ -99,7 +99,7 @@ case $action in
|
|||
teamspeak-query-lib message --strict-client --client "$user" "$message"
|
||||
;;
|
||||
"events")
|
||||
teamspeak-query-lib events --filter=others NotifyClientMoved NotifyClientEnterView NotifyClientLeftView NotifyTextMessage NotifyClientPoke \
|
||||
teamspeak-query-lib events NotifyClientMoved NotifyClientEnterView NotifyClientLeftView NotifyTextMessage NotifyClientPoke \
|
||||
| jq -r --unbuffered '.message' \
|
||||
| tee >(xargs -I{} notify-send "TS3 Event" "{}")
|
||||
esac
|
||||
|
|
Loading…
Reference in a new issue