Compare commits
8 commits
Author | SHA1 | Date | |
---|---|---|---|
7694bf365c | |||
93ebbbd5d2 | |||
21c1e3fbb3 | |||
3463907670 | |||
fb1e41ddc4 | |||
b827471e6d | |||
915c2ccbbf | |||
895f5f9373 |
29 changed files with 302 additions and 146 deletions
.envrc
.forgejo/workflows
.gitignoreCargo.lockCargo.tomlMakefilebashly-settings.ymlcontrol_src
ask_command.shaway_command.shback_command.shbashly.ymlevents_command.shevents_ntfy_command.shfetch_channel_command.shfetch_client_command.sh
shell.nixlib
message_client_command.shmessage_command.shmigrate_command.shmove_command.shnot_away_command.shquick_command.shsrc
ts-control
1
.envrc
Normal file
1
.envrc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
use nix
|
|
@ -16,17 +16,9 @@ jobs:
|
||||||
source "$HOME/.cargo/env"
|
source "$HOME/.cargo/env"
|
||||||
cargo build --release
|
cargo build --release
|
||||||
shell: bash
|
shell: bash
|
||||||
- uses: https://code.forgejo.org/actions/upload-artifact@v3
|
|
||||||
with:
|
|
||||||
name: teamspeak-query-lib
|
|
||||||
path: ${{ github.workspace }}/target/release/teamspeak-query-lib
|
|
||||||
- uses: https://code.forgejo.org/actions/download-artifact@v3
|
|
||||||
with:
|
|
||||||
name: teamspeak-query-lib
|
|
||||||
path: /tmp/artifacts
|
|
||||||
shell: bash
|
|
||||||
- id: copy-ts-control-artificat
|
- id: copy-ts-control-artificat
|
||||||
run: |
|
run: |
|
||||||
|
cp ${{ github.workspace }}/target/release/teamspeak-query-lib /tmp/artifacts
|
||||||
cp ${{ github.workspace }}/ts-control /tmp/artifacts
|
cp ${{ github.workspace }}/ts-control /tmp/artifacts
|
||||||
shell: bash
|
shell: bash
|
||||||
- uses: https://code.forgejo.org/actions/forgejo-release@v1
|
- uses: https://code.forgejo.org/actions/forgejo-release@v1
|
||||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
/target
|
/target
|
||||||
|
ts-control
|
||||||
|
|
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -182,7 +182,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "teamspeak-query-lib"
|
name = "teamspeak-query-lib"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "teamspeak-query-lib"
|
name = "teamspeak-query-lib"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
4
Makefile
4
Makefile
|
@ -2,11 +2,13 @@
|
||||||
|
|
||||||
INSTALL_DIR = $(HOME)/.local/bin
|
INSTALL_DIR = $(HOME)/.local/bin
|
||||||
|
|
||||||
|
export PATH := target/debug:$(PATH)
|
||||||
|
|
||||||
build:
|
build:
|
||||||
@cargo build
|
@cargo build
|
||||||
|
|
||||||
run: build
|
run: build
|
||||||
@PATH=$(PWD)/target/debug:$(PATH) ./ts-control
|
./ts-control
|
||||||
|
|
||||||
install:
|
install:
|
||||||
mkdir -p "$(INSTALL_DIR)"
|
mkdir -p "$(INSTALL_DIR)"
|
||||||
|
|
1
bashly-settings.yml
Normal file
1
bashly-settings.yml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
source_dir: control_src
|
20
control_src/ask_command.sh
Normal file
20
control_src/ask_command.sh
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
actions=(
|
||||||
|
"quick"
|
||||||
|
"move"
|
||||||
|
"fetch-client"
|
||||||
|
"fetch-channel"
|
||||||
|
"migrate"
|
||||||
|
"away"
|
||||||
|
"not-away"
|
||||||
|
"back"
|
||||||
|
"message"
|
||||||
|
"message-client"
|
||||||
|
"events"
|
||||||
|
"events-ntfy"
|
||||||
|
)
|
||||||
|
|
||||||
|
action=$(echo "${actions[@]}" | tr ' ' '\n' | $DMENU)
|
||||||
|
|
||||||
|
if [ -n "$action" ]; then
|
||||||
|
$0 "$action"
|
||||||
|
fi
|
4
control_src/away_command.sh
Normal file
4
control_src/away_command.sh
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
message=$(_ts_control_get_message "${args[channel]}")
|
||||||
|
teamspeak-query-lib move "Away From Keyboard"
|
||||||
|
teamspeak-query-lib update --away "$message"
|
||||||
|
teamspeak-query-lib update --microphone=false --speakers=false --away "$message"
|
3
control_src/back_command.sh
Normal file
3
control_src/back_command.sh
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
teamspeak-query-lib update --back
|
||||||
|
_ts_control_move_self "${args[channel]}"
|
||||||
|
teamspeak-query-lib update --microphone=true --speakers=true
|
59
control_src/bashly.yml
Normal file
59
control_src/bashly.yml
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
name: ts-control
|
||||||
|
help: Teamspeak query lib utility script
|
||||||
|
version: 0.1.0
|
||||||
|
|
||||||
|
environment_variables:
|
||||||
|
- name: ts3_client_api_key
|
||||||
|
help: Set your API key
|
||||||
|
required: true
|
||||||
|
|
||||||
|
commands:
|
||||||
|
- name: ask
|
||||||
|
default: force
|
||||||
|
- name: quick
|
||||||
|
- name: move
|
||||||
|
args:
|
||||||
|
- name: channel
|
||||||
|
required: false
|
||||||
|
- name: fetch-client
|
||||||
|
args:
|
||||||
|
- name: client
|
||||||
|
required: false
|
||||||
|
- name: fetch-channel
|
||||||
|
args:
|
||||||
|
- name: channel
|
||||||
|
required: false
|
||||||
|
- name: migrate
|
||||||
|
args:
|
||||||
|
- name: channel
|
||||||
|
required: false
|
||||||
|
- name: not-away
|
||||||
|
- name: away
|
||||||
|
args:
|
||||||
|
- name: message
|
||||||
|
required: false
|
||||||
|
- name: back
|
||||||
|
args:
|
||||||
|
- name: channel
|
||||||
|
required: false
|
||||||
|
- name: message
|
||||||
|
args:
|
||||||
|
- name: message
|
||||||
|
required: false
|
||||||
|
- name: message-client
|
||||||
|
args:
|
||||||
|
- name: client
|
||||||
|
required: false
|
||||||
|
- name: message
|
||||||
|
required: false
|
||||||
|
- name: events
|
||||||
|
- name: events-ntfy
|
||||||
|
environment_variables:
|
||||||
|
- name: ts3_ntfy_host
|
||||||
|
required: true
|
||||||
|
- name: ts3_ntfy_token
|
||||||
|
required: true
|
||||||
|
- name: ts3_ntfy_topic
|
||||||
|
required: true
|
||||||
|
- name: ts3_ntfy_webhook
|
||||||
|
required: true
|
5
control_src/events_command.sh
Normal file
5
control_src/events_command.sh
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
teamspeak-query-lib events --filter=others \
|
||||||
|
NotifyClientMoved NotifyClientEnterView NotifyClientLeftView \
|
||||||
|
NotifyTextMessage NotifyClientPoke \
|
||||||
|
| jq -r --unbuffered '.message' \
|
||||||
|
| tee >(xargs -I{} notify-send "TS3 Event" "{}")
|
52
control_src/events_ntfy_command.sh
Normal file
52
control_src/events_ntfy_command.sh
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
handle_ntfy_events() {
|
||||||
|
while read -r data; do
|
||||||
|
msg=$(echo "$data" | jq -r --unbuffered '.message')
|
||||||
|
type=$(echo "$data" | jq -r --unbuffered '.type')
|
||||||
|
mode=$(echo "$data" | jq -r --unbuffered '.params.targetmode')
|
||||||
|
client_nickname=$(echo "$data" | jq -r --unbuffered '.client.client_nickname')
|
||||||
|
|
||||||
|
echo "$data"
|
||||||
|
|
||||||
|
if [ "$type" = "NotifyTextMessage" ] && [ "$mode" != "1" ]; then
|
||||||
|
continue # Skip all messages that are not direct messages
|
||||||
|
fi
|
||||||
|
|
||||||
|
title="TS3 Event"
|
||||||
|
case $type in
|
||||||
|
"NotifyClientPoke")
|
||||||
|
title="TS3 Poke"
|
||||||
|
;;
|
||||||
|
"NotifyTextMessage")
|
||||||
|
title="TS3 Message"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
echo "($title) $target: $msg"
|
||||||
|
|
||||||
|
payload=$(jq -n \
|
||||||
|
--arg topic "$TS3_NTFY_TOPIC" \
|
||||||
|
--arg webhook "$TS3_NTFY_WEBHOOK&client=$client_nickname" \
|
||||||
|
--arg msg "$msg" \
|
||||||
|
--arg type "$title" \
|
||||||
|
'{
|
||||||
|
topic: $topic,
|
||||||
|
message: $msg,
|
||||||
|
title: $type,
|
||||||
|
actions: [{
|
||||||
|
action: "http",
|
||||||
|
label: "TS response",
|
||||||
|
method: "POST",
|
||||||
|
url: $webhook,
|
||||||
|
clear: true
|
||||||
|
}]
|
||||||
|
}')
|
||||||
|
|
||||||
|
curl -sSL \
|
||||||
|
-H "Authorization: Bearer $TS3_NTFY_TOKEN" \
|
||||||
|
-d "$payload" \
|
||||||
|
"$TS3_NTFY_HOST"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
teamspeak-query-lib events --filter=others NotifyClientPoke NotifyTextMessage \
|
||||||
|
| handle_ntfy_events
|
1
control_src/fetch_channel_command.sh
Normal file
1
control_src/fetch_channel_command.sh
Normal file
|
@ -0,0 +1 @@
|
||||||
|
_ts_control_fetch channel "${args[channel]}"
|
1
control_src/fetch_client_command.sh
Normal file
1
control_src/fetch_client_command.sh
Normal file
|
@ -0,0 +1 @@
|
||||||
|
_ts_control_fetch client "${args[client]}"
|
0
control_src/lib/ntfy.sh
Normal file
0
control_src/lib/ntfy.sh
Normal file
46
control_src/lib/simple.sh
Normal file
46
control_src/lib/simple.sh
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
_ts_control_get_entity() {
|
||||||
|
entity=$(_ts_control_single_or_dmenu "$(teamspeak-query-lib "$1s")" "$2")
|
||||||
|
if [ -z "$entity" ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "$entity"
|
||||||
|
}
|
||||||
|
|
||||||
|
_ts_control_get_message() {
|
||||||
|
if [ -n "$1" ];
|
||||||
|
then
|
||||||
|
message="$1"
|
||||||
|
else
|
||||||
|
message=$(printf "\n" | $DMENU -p "message")
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$message" ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$message"
|
||||||
|
}
|
||||||
|
|
||||||
|
_ts_control_move_self() {
|
||||||
|
channel=$(_ts_control_get_entity channel "$1" "$2")
|
||||||
|
teamspeak-query-lib move --strict-channel "$channel"
|
||||||
|
}
|
||||||
|
|
||||||
|
_ts_control_fetch() {
|
||||||
|
target=$(_ts_control_get_entity "$1" "$2")
|
||||||
|
teamspeak-query-lib fetch "--strict-$1" "--$1" "$target"
|
||||||
|
}
|
||||||
|
|
||||||
|
_ts_control_single_or_dmenu() {
|
||||||
|
filter="$2"
|
||||||
|
if [ "$filter" == "^$" ]; then
|
||||||
|
filter=""
|
||||||
|
fi
|
||||||
|
options=$(echo "$1" | grep "$filter")
|
||||||
|
count=$(echo "$options" | wc -l)
|
||||||
|
if [ "$count" -eq 1 ]; then
|
||||||
|
echo "$options"
|
||||||
|
else
|
||||||
|
echo "$1" | $DMENU
|
||||||
|
fi
|
||||||
|
}
|
3
control_src/message_client_command.sh
Normal file
3
control_src/message_client_command.sh
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
client=$(_ts_control_get_entity client "${args[client]}")
|
||||||
|
message=$(_ts_control_get_message "${args[message]}")
|
||||||
|
teamspeak-query-lib message --strict-client --client "$client" "$message"
|
2
control_src/message_command.sh
Normal file
2
control_src/message_command.sh
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
message=$(_ts_control_get_message "${args[message]}")
|
||||||
|
teamspeak-query-lib message "$message"
|
4
control_src/migrate_command.sh
Normal file
4
control_src/migrate_command.sh
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
target=$(_ts_control_get_entity "channel" "$1")
|
||||||
|
current_channel=$(teamspeak-query-lib info | grep "Channel: " | sed 's/Channel: //')
|
||||||
|
_ts_control_move_self "$target"
|
||||||
|
teamspeak-query-lib fetch "--strict-channel" "--channel" "$current_channel"
|
1
control_src/move_command.sh
Normal file
1
control_src/move_command.sh
Normal file
|
@ -0,0 +1 @@
|
||||||
|
_ts_control_move_self "${args[channel]}"
|
2
control_src/not_away_command.sh
Normal file
2
control_src/not_away_command.sh
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
teamspeak-query-lib move "Not Away From Keyboard"
|
||||||
|
teamspeak-query-lib update --microphone=false --speakers=false
|
5
control_src/quick_command.sh
Normal file
5
control_src/quick_command.sh
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
action=$($DMENU < "$XDG_CONFIG_HOME/ts-control-quick")
|
||||||
|
if [ -z "$action" ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
eval "$0 $action"
|
6
shell.nix
Normal file
6
shell.nix
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
with import <nixpkgs> {};
|
||||||
|
mkShell {
|
||||||
|
nativeBuildInputs = [
|
||||||
|
bashly
|
||||||
|
];
|
||||||
|
}
|
12
src/cli.rs
12
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;
|
||||||
|
@ -17,6 +17,7 @@ struct Cli {
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
pub enum Commands {
|
pub enum Commands {
|
||||||
|
Info,
|
||||||
Channels(ChannelsArgs),
|
Channels(ChannelsArgs),
|
||||||
Clients,
|
Clients,
|
||||||
Fetch(FetchArgs),
|
Fetch(FetchArgs),
|
||||||
|
@ -79,9 +80,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;
|
||||||
|
|
||||||
|
@ -27,19 +28,30 @@ pub fn handle_event_response(connection: &mut Telnet, event: Event, known_client
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn loop_response_reader(connection: &mut Telnet, self_clid: i32) {
|
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) {
|
||||||
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);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match commands::read_response(connection, true, String::new()) {
|
match commands::read_response(connection, true, String::new()) {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
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 client.clid == self_clid {
|
self_clid = try_get_self_clid(connection);
|
||||||
continue;
|
}
|
||||||
}
|
if !event.should_handle(filter, self_clid) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_event_response(connection, event, &mut known_clients);
|
handle_event_response(connection, event, &mut known_clients);
|
||||||
|
|
53
src/main.rs
53
src/main.rs
|
@ -1,11 +1,8 @@
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use std::num::ParseIntError;
|
|
||||||
|
|
||||||
use telnet::Telnet;
|
use telnet::Telnet;
|
||||||
|
|
||||||
use crate::cli::Commands;
|
use crate::cli::{Commands, EventArgsFilter};
|
||||||
use crate::models::Channel;
|
|
||||||
use crate::models::Client;
|
|
||||||
|
|
||||||
mod wrappers;
|
mod wrappers;
|
||||||
mod commands;
|
mod commands;
|
||||||
|
@ -16,14 +13,10 @@ mod models;
|
||||||
mod response;
|
mod response;
|
||||||
mod command_utils;
|
mod command_utils;
|
||||||
|
|
||||||
fn channel_or_error(channel_res: Result<Option<Channel>, String>) -> Result<Channel, Error> {
|
fn result_or_error<T>(res: Result<Option<T>, String>, result_type: &str) -> Result<T, Error> {
|
||||||
channel_res.map_err(|err| make_action_error("find channel", err))?
|
let error_action = format!("find {}", result_type);
|
||||||
.ok_or_else(|| make_action_error("find channel", String::from("Not Found.")))
|
res.map_err(|err| make_action_error(&error_action, err))?
|
||||||
}
|
.ok_or_else(|| make_action_error(&error_action, 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> {
|
fn connect() -> Result<Telnet, Error> {
|
||||||
|
@ -54,6 +47,26 @@ fn main() -> Result<(), Error> {
|
||||||
// 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
|
||||||
match cli {
|
match cli {
|
||||||
|
Commands::Info => {
|
||||||
|
let client = match wrappers::find_self(&mut connection) {
|
||||||
|
Ok(client) => client,
|
||||||
|
Err(msg) => return Err(make_action_error("find self", msg))
|
||||||
|
};
|
||||||
|
|
||||||
|
let channel_name: String = match wrappers::find_channel(&mut connection, "cid", &client.cid.to_string(), true) {
|
||||||
|
Ok(channel) => match channel {
|
||||||
|
Some(channel) => channel.channel_name,
|
||||||
|
None => return Err(to_other_error("Channel not found.".to_string()))
|
||||||
|
},
|
||||||
|
Err(msg) => return Err(make_action_error("find self", msg))
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Client: {}", client.client_nickname);
|
||||||
|
println!("Channel: {}", channel_name);
|
||||||
|
println!("Channel ID: {}", client.cid);
|
||||||
|
println!("Client ID: {}", client.clid);
|
||||||
|
}
|
||||||
|
|
||||||
Commands::Channels(args) => {
|
Commands::Channels(args) => {
|
||||||
match wrappers::get_channels(&mut connection, args.spacers) {
|
match wrappers::get_channels(&mut connection, args.spacers) {
|
||||||
Ok(channels) => {
|
Ok(channels) => {
|
||||||
|
@ -89,7 +102,7 @@ fn main() -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.want_client() {
|
if args.want_client() {
|
||||||
let client = client_or_error(args.client(&mut connection))?;
|
let client = result_or_error(args.client(&mut connection), "client")?;
|
||||||
|
|
||||||
match wrappers::fetch_client(&mut connection, &[client]) {
|
match wrappers::fetch_client(&mut connection, &[client]) {
|
||||||
Ok(_) => println!("Successfully fetched client."),
|
Ok(_) => println!("Successfully fetched client."),
|
||||||
|
@ -100,7 +113,7 @@ fn main() -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.want_channel() {
|
if args.want_channel() {
|
||||||
let channel = channel_or_error(args.channel(&mut connection))?;
|
let channel = result_or_error(args.channel(&mut connection), "channel")?;
|
||||||
|
|
||||||
match wrappers::fetch_channel(&mut connection, channel) {
|
match wrappers::fetch_channel(&mut connection, channel) {
|
||||||
Ok(_) => println!("Successfully fetched channel."),
|
Ok(_) => println!("Successfully fetched channel."),
|
||||||
|
@ -124,8 +137,8 @@ fn main() -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Commands::Move(args) => {
|
Commands::Move(args) => {
|
||||||
let channel = channel_or_error(args.channel(&mut connection))?;
|
let channel = result_or_error(args.channel(&mut connection), "channel")?;
|
||||||
let client = client_or_error(args.client(&mut connection))?;
|
let client = result_or_error(args.client(&mut connection), "client")?;
|
||||||
|
|
||||||
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),
|
||||||
|
@ -145,16 +158,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)?;
|
||||||
|
|
||||||
let self_clid: i32 = wrappers::get_self_clid(&mut connection)
|
command_utils::events::loop_response_reader(&mut connection, &filter);
|
||||||
.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));
|
||||||
|
|
|
@ -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 {
|
||||||
|
@ -276,6 +294,7 @@ impl Serialize for Event {
|
||||||
x.serialize_field("type", &self.event_type)?;
|
x.serialize_field("type", &self.event_type)?;
|
||||||
x.serialize_field("channel", &self.channel)?;
|
x.serialize_field("channel", &self.channel)?;
|
||||||
x.serialize_field("client", &self.client)?;
|
x.serialize_field("client", &self.client)?;
|
||||||
|
x.serialize_field("params", &self.params)?;
|
||||||
x.serialize_field("message", &self.get_message())?;
|
x.serialize_field("message", &self.get_message())?;
|
||||||
x.end()
|
x.end()
|
||||||
}
|
}
|
||||||
|
|
105
ts-control
105
ts-control
|
@ -1,105 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
actions="quick
|
|
||||||
move
|
|
||||||
fetch-client
|
|
||||||
fetch-channel
|
|
||||||
away
|
|
||||||
not away
|
|
||||||
back
|
|
||||||
message
|
|
||||||
message-user
|
|
||||||
events"
|
|
||||||
|
|
||||||
_ts_control_get_entity() {
|
|
||||||
entity=$(_ts_control_single_or_dmenu "$(teamspeak-query-lib "$1s")" "$2")
|
|
||||||
if [ -z "$entity" ]; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "$entity"
|
|
||||||
}
|
|
||||||
|
|
||||||
_ts_control_get_message() {
|
|
||||||
if [ -n "$1" ];
|
|
||||||
then
|
|
||||||
message="$1"
|
|
||||||
else
|
|
||||||
message=$(printf "\n" | $DMENU -p "message")
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -z "$message" ]; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "$message"
|
|
||||||
}
|
|
||||||
|
|
||||||
_ts_control_move_self() {
|
|
||||||
channel=$(_ts_control_get_entity channel "$1" "$2")
|
|
||||||
teamspeak-query-lib move --strict-channel "$channel"
|
|
||||||
}
|
|
||||||
|
|
||||||
_ts_control_fetch() {
|
|
||||||
target=$(_ts_control_get_entity "$1" "$2")
|
|
||||||
teamspeak-query-lib fetch "--strict-$1" "--$1" "$target"
|
|
||||||
}
|
|
||||||
|
|
||||||
_ts_control_single_or_dmenu() {
|
|
||||||
options=$(echo "$1" | grep "$2")
|
|
||||||
count=$(echo "$options" | wc -l)
|
|
||||||
if [ "$count" -eq 1 ]; then
|
|
||||||
echo "$options"
|
|
||||||
else
|
|
||||||
echo "$options" | $DMENU
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Add '$' to $1 to add 'End of line' to the regex for grep
|
|
||||||
action=$(_ts_control_single_or_dmenu "$actions" "$1$")
|
|
||||||
|
|
||||||
case $action in
|
|
||||||
"quick")
|
|
||||||
action=$($DMENU < "$XDG_CONFIG_HOME/ts-control-quick")
|
|
||||||
if [ -z "$action" ]; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
eval "$0 $action"
|
|
||||||
;;
|
|
||||||
"move")
|
|
||||||
_ts_control_move_self "$2"
|
|
||||||
;;
|
|
||||||
"fetch-client")
|
|
||||||
_ts_control_fetch client "$2"
|
|
||||||
;;
|
|
||||||
"fetch-channel")
|
|
||||||
_ts_control_fetch channel "$2"
|
|
||||||
;;
|
|
||||||
"not away")
|
|
||||||
teamspeak-query-lib move "Not Away From Keyboard"
|
|
||||||
teamspeak-query-lib update --microphone=false --speakers=false
|
|
||||||
;;
|
|
||||||
"away")
|
|
||||||
message=$(_ts_control_get_message)
|
|
||||||
teamspeak-query-lib move "Away From Keyboard"
|
|
||||||
teamspeak-query-lib update --away "$message"
|
|
||||||
teamspeak-query-lib update --microphone=false --speakers=false --away "$message"
|
|
||||||
;;
|
|
||||||
"back")
|
|
||||||
teamspeak-query-lib update --back
|
|
||||||
_ts_control_move_self "$2"
|
|
||||||
teamspeak-query-lib update --microphone=true --speakers=true
|
|
||||||
;;
|
|
||||||
"message")
|
|
||||||
message=$(_ts_control_get_message "$2")
|
|
||||||
teamspeak-query-lib message "$message"
|
|
||||||
;;
|
|
||||||
"message-user")
|
|
||||||
user=$(_ts_control_get_entity client "$2")
|
|
||||||
message=$(_ts_control_get_message "$3")
|
|
||||||
teamspeak-query-lib message --strict-client --client "$user" "$message"
|
|
||||||
;;
|
|
||||||
"events")
|
|
||||||
teamspeak-query-lib events NotifyClientMoved NotifyClientEnterView NotifyClientLeftView NotifyTextMessage NotifyClientPoke \
|
|
||||||
| jq -r --unbuffered '.message' \
|
|
||||||
| tee >(xargs -I{} notify-send "TS3 Event" "{}")
|
|
||||||
esac
|
|
Loading…
Add table
Add a link
Reference in a new issue