2024-03-27 14:37:52 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-11-13 16:01:23 +00:00
|
|
|
|
2023-11-27 19:24:38 +00:00
|
|
|
actions="quick
|
|
|
|
move
|
2023-11-17 16:32:15 +00:00
|
|
|
fetch-client
|
|
|
|
fetch-channel
|
|
|
|
away
|
|
|
|
not away
|
2023-11-26 21:40:41 +00:00
|
|
|
back
|
|
|
|
message
|
2024-03-05 02:52:30 +00:00
|
|
|
message-user
|
2024-09-27 14:16:21 +00:00
|
|
|
events
|
|
|
|
ntfy-events"
|
2024-02-16 14:55:56 +00:00
|
|
|
|
2023-11-26 21:40:41 +00:00
|
|
|
_ts_control_get_entity() {
|
2024-02-16 15:27:38 +00:00
|
|
|
entity=$(_ts_control_single_or_dmenu "$(teamspeak-query-lib "$1s")" "$2")
|
2023-11-26 21:40:41 +00:00
|
|
|
if [ -z "$entity" ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "$entity"
|
|
|
|
}
|
2023-11-13 16:01:23 +00:00
|
|
|
|
2023-11-26 21:40:41 +00:00
|
|
|
_ts_control_get_message() {
|
2024-02-16 15:27:38 +00:00
|
|
|
if [ -n "$1" ];
|
|
|
|
then
|
|
|
|
message="$1"
|
|
|
|
else
|
|
|
|
message=$(printf "\n" | $DMENU -p "message")
|
|
|
|
fi
|
|
|
|
|
2023-11-26 21:40:41 +00:00
|
|
|
if [ -z "$message" ]; then
|
|
|
|
exit 1
|
2023-11-14 12:31:18 +00:00
|
|
|
fi
|
2024-02-16 15:27:38 +00:00
|
|
|
|
2023-11-26 21:40:41 +00:00
|
|
|
echo "$message"
|
|
|
|
}
|
|
|
|
|
|
|
|
_ts_control_move_self() {
|
2024-02-16 15:27:38 +00:00
|
|
|
channel=$(_ts_control_get_entity channel "$1" "$2")
|
2023-11-14 12:31:18 +00:00
|
|
|
teamspeak-query-lib move --strict-channel "$channel"
|
2023-11-13 16:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 21:30:59 +00:00
|
|
|
_ts_control_fetch() {
|
2024-02-16 15:27:38 +00:00
|
|
|
target=$(_ts_control_get_entity "$1" "$2")
|
2023-11-26 21:40:41 +00:00
|
|
|
teamspeak-query-lib fetch "--strict-$1" "--$1" "$target"
|
2023-11-17 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 15:27:38 +00:00
|
|
|
_ts_control_single_or_dmenu() {
|
2024-09-30 22:20:08 +00:00
|
|
|
filter="$2"
|
|
|
|
if [ "$filter" == "^$" ]; then
|
|
|
|
filter=""
|
|
|
|
fi
|
|
|
|
options=$(echo "$1" | grep "$filter")
|
2024-02-16 15:27:38 +00:00
|
|
|
count=$(echo "$options" | wc -l)
|
|
|
|
if [ "$count" -eq 1 ]; then
|
|
|
|
echo "$options"
|
|
|
|
else
|
2024-09-30 22:20:08 +00:00
|
|
|
echo "$1" | $DMENU
|
2024-02-16 15:27:38 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-09-27 14:16:21 +00:00
|
|
|
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')
|
2024-10-27 17:55:08 +00:00
|
|
|
client_nickname=$(echo "$data" | jq -r --unbuffered '.client.client_nickname')
|
2024-09-27 14:16:21 +00:00
|
|
|
|
|
|
|
echo "$data"
|
|
|
|
|
|
|
|
if [ "$type" = "NotifyTextMessage" ] && [ "$mode" != "1" ]; then
|
|
|
|
continue # Skip all messages that are not direct messages
|
|
|
|
fi
|
|
|
|
|
2024-10-27 17:55:08 +00:00
|
|
|
title="TS3 Event"
|
2024-09-27 14:16:21 +00:00
|
|
|
case $type in
|
|
|
|
"NotifyClientPoke")
|
2024-10-27 17:55:08 +00:00
|
|
|
title="TS3 Poke"
|
2024-09-27 14:16:21 +00:00
|
|
|
;;
|
|
|
|
"NotifyTextMessage")
|
2024-10-27 17:55:08 +00:00
|
|
|
title="TS3 Message"
|
2024-09-27 14:16:21 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2024-10-27 17:55:08 +00:00
|
|
|
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
|
|
|
|
}]
|
|
|
|
}')
|
2024-09-27 14:16:21 +00:00
|
|
|
|
|
|
|
curl -sSL \
|
|
|
|
-H "Authorization: Bearer $TS3_NTFY_TOKEN" \
|
2024-10-27 17:55:08 +00:00
|
|
|
-d "$payload" \
|
2024-09-27 14:16:21 +00:00
|
|
|
"$TS3_NTFY_HOST"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-09-30 22:20:08 +00:00
|
|
|
action=$(_ts_control_single_or_dmenu "$actions" "^$1$")
|
2024-02-16 15:27:38 +00:00
|
|
|
|
2023-11-13 16:01:23 +00:00
|
|
|
case $action in
|
2023-11-27 19:24:38 +00:00
|
|
|
"quick")
|
|
|
|
action=$($DMENU < "$XDG_CONFIG_HOME/ts-control-quick")
|
|
|
|
if [ -z "$action" ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
2024-02-16 15:27:38 +00:00
|
|
|
eval "$0 $action"
|
2023-11-27 19:24:38 +00:00
|
|
|
;;
|
2023-11-13 16:01:23 +00:00
|
|
|
"move")
|
2024-02-16 15:27:38 +00:00
|
|
|
_ts_control_move_self "$2"
|
2023-11-14 12:31:18 +00:00
|
|
|
;;
|
2023-11-17 16:32:15 +00:00
|
|
|
"fetch-client")
|
2024-02-16 15:27:38 +00:00
|
|
|
_ts_control_fetch client "$2"
|
2023-11-17 16:32:15 +00:00
|
|
|
;;
|
|
|
|
"fetch-channel")
|
2024-02-16 15:27:38 +00:00
|
|
|
_ts_control_fetch channel "$2"
|
2023-11-14 12:31:18 +00:00
|
|
|
;;
|
|
|
|
"not away")
|
|
|
|
teamspeak-query-lib move "Not Away From Keyboard"
|
|
|
|
teamspeak-query-lib update --microphone=false --speakers=false
|
2023-11-13 16:01:23 +00:00
|
|
|
;;
|
|
|
|
"away")
|
2023-11-26 21:40:41 +00:00
|
|
|
message=$(_ts_control_get_message)
|
2023-11-13 16:01:23 +00:00
|
|
|
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
|
2024-02-16 15:27:38 +00:00
|
|
|
_ts_control_move_self "$2"
|
2023-11-13 16:01:23 +00:00
|
|
|
teamspeak-query-lib update --microphone=true --speakers=true
|
|
|
|
;;
|
2023-11-26 21:40:41 +00:00
|
|
|
"message")
|
2024-02-16 15:27:38 +00:00
|
|
|
message=$(_ts_control_get_message "$2")
|
2023-11-26 21:40:41 +00:00
|
|
|
teamspeak-query-lib message "$message"
|
|
|
|
;;
|
|
|
|
"message-user")
|
2024-02-16 15:27:38 +00:00
|
|
|
user=$(_ts_control_get_entity client "$2")
|
|
|
|
message=$(_ts_control_get_message "$3")
|
2023-11-26 21:40:41 +00:00
|
|
|
teamspeak-query-lib message --strict-client --client "$user" "$message"
|
|
|
|
;;
|
2024-04-10 15:36:51 +00:00
|
|
|
"events")
|
2024-06-02 23:39:37 +00:00
|
|
|
teamspeak-query-lib events --filter=others NotifyClientMoved NotifyClientEnterView NotifyClientLeftView NotifyTextMessage NotifyClientPoke \
|
2024-04-10 15:36:51 +00:00
|
|
|
| jq -r --unbuffered '.message' \
|
|
|
|
| tee >(xargs -I{} notify-send "TS3 Event" "{}")
|
2024-09-27 14:16:21 +00:00
|
|
|
;;
|
|
|
|
"ntfy-events")
|
|
|
|
teamspeak-query-lib events --filter=others NotifyClientPoke NotifyTextMessage \
|
|
|
|
| handle_ntfy_events
|
|
|
|
;;
|
2023-11-13 16:01:23 +00:00
|
|
|
esac
|