Add split command

This commit is contained in:
Tobias Reisinger 2025-07-27 12:30:17 +02:00
parent 6b7b549029
commit 6dddeb9c2e
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
8 changed files with 91 additions and 17 deletions

2
Cargo.lock generated
View file

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

View file

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

View file

@ -1,6 +1,7 @@
actions=(
"quick"
"move"
"split"
"fetch-client"
"fetch-channel"
"migrate"

View file

@ -1,6 +1,6 @@
name: ts-control
help: Teamspeak query lib utility script
version: 0.1.0
version: 0.2.0
environment_variables:
- name: ts3_client_api_key
@ -11,6 +11,7 @@ commands:
- name: ask
default: force
- name: quick
- name: split
- name: move
args:
- name: channel

View file

@ -1,4 +1,4 @@
action=$($DMENU < "$XDG_CONFIG_HOME/ts-control-quick")
action=$($DMENU < "$XDG_CONFIG_HOME/ts-control/quick")
if [ -z "$action" ]; then
exit 1
fi

View file

@ -0,0 +1,64 @@
ts_clients=$(teamspeak-query-lib clients --uid)
ts_online=$(grep ~/.config/ts-control/steam_map.txt -Ff <(echo "$ts_clients" | awk '{print $1}'))
friend_ids=$(echo "$ts_online" | awk '{print $1}' | paste -sd ',')
friends=$(curl -s "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$STEAM_KEY&steamids=$friend_ids" \
| jq '.response.players[] | {steamid, gameid, gameextrainfo}')
steamid_to_tsname() {
local steamid="$1"
ts_uid=$(grep -F "$steamid" ~/.config/ts-control/steam_map.txt | awk '{print $2}')
echo "$ts_clients" | grep -F "$ts_uid" | cut -f2- -d' '
}
create_groups() {
echo "$friends" | jq -r '.gameid' | sort -u | while read -r gameid
do
if [ "$gameid" = "null" ]; then
continue
fi
game_name=$(echo $friends | jq -r --arg gameid "$gameid" 'select(.gameid == $gameid) | .gameextrainfo' | head -n 1)
players=$(echo $friends | jq -r --arg gameid "$gameid" 'select(.gameid == $gameid) | .steamid' | paste -sd ',')
jq -cn --argjson players "[$players]" --arg name "$game_name" --arg id "$gameid" '{id: $id, name: $name, players: $players}'
done
players=$(echo "$friends" | jq -r 'select(.gameid == null) | .steamid' | paste -sd ',')
jq -cn --argjson players "[$players]" '{id: null, name: "No game", players: $players}'
}
create_prompt() {
local groups="$1"
index=0
echo "$groups" | while read -r group
do
echo "$index $(echo "$group" | jq -r '.name')"
echo "$group" | jq -r '.players[]' | while read -r steamid
do
ts_name=$(steamid_to_tsname "$steamid")
echo "$index $ts_name"
done
index=$((index + 1))
done
}
groups=$(create_groups)
prompt=$(create_prompt "$groups")
selected=$(echo "$prompt" | $DMENU)
if [ -z "$selected" ]; then
exit 1
fi
target_channel=$(teamspeak-query-lib channels | $DMENU --prompt "Channel" || true)
if [ -z "$target_channel" ]; then
target_channel=$(teamspeak-query-lib channels --only-empty | head -n 1)
fi
selected_index=$(echo "$selected" | awk '{print $1}')
prefix="$selected_index "
readarray -t clients < <(echo "$prompt" | grep "^$prefix" | awk '{print $2}')
teamspeak-query-lib move --strict-client --strict-channel "$target_channel" "${clients[@]}"

View file

@ -73,7 +73,7 @@ pub struct MoveArgs {
#[arg(long)]
strict_channel: bool,
channel: String,
client: Option<String>,
clients: Vec<String>,
}
#[derive(Args)]
@ -148,17 +148,24 @@ impl MoveArgs {
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<Channel>, String> {
wrappers::find_channel(connection, "channel_name", &self.channel, self.strict_channel)
}
pub fn client(&self, connection: &mut Telnet) -> Result<Option<Client>, String> {
match &self.client {
Some(client) => {
wrappers::find_client(connection, "client_nickname", client, self.strict_client)
}
None => {
pub fn clients(&self, connection: &mut Telnet) -> Result<Vec<Client>, String> {
match self.clients.is_empty() {
true => {
match wrappers::find_self(connection) {
Ok(client) => Ok(Some(client)),
Ok(client) => Ok(vec![client]),
Err(msg) => Err(msg)
}
}
false => {
let clients = self.clients.iter().map(|client_nickname| {
wrappers::find_client(connection, "client_nickname", client_nickname, self.strict_client)
.unwrap_or(None)
})
.filter(|result| result.is_some())
.map(|result| result.unwrap())
.collect();
Ok(clients)
}
}
}
}

View file

@ -52,7 +52,7 @@ fn main() -> Result<(), Error> {
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,
@ -60,13 +60,13 @@ fn main() -> Result<(), Error> {
},
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) => {
match wrappers::get_channels(&mut connection, args.spacers) {
Ok(channels) => {
@ -149,9 +149,10 @@ 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 clients = args.clients(&mut connection)
.map_err(|msg| make_action_error("move clients", msg))?;
match wrappers::move_client(&mut connection, &channel, &[client]) {
match wrappers::move_client(&mut connection, &channel, &clients) {
Ok(resp) => println!("Successfully moved client: {}", resp),
Err(msg) => {
return Err(make_action_error("move client", msg));