Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Created by Malobre
- #
- ts_serverID="1"
- ts_serverQueryHost="localhost"
- ts_serverQueryPort="10011"
- ts_serverQueryUser="queryUser"
- ts_serverQueryPass="queryPass"
- ts_afkChannelId="1"
- ts_idleTimeBeforeMove="30"
- sendCommand() {
- echo "${1}" > inpipe
- response=""
- while read -r tmp; do
- if [[ "${tmp}" =~ error\ id=([0-9]+)\ msg=(.*) ]]; then
- errorId=${BASH_REMATCH[1]}
- errorMsg=${BASH_REMATCH[2]}
- if [[ errorId -ne 0 ]]; then
- echo "Error ${errorId}: $(sed "s/\\\s/ /g" <<< "${errorMsg}")"
- fi
- break
- fi
- response="${response} $(echo "${tmp}" | tr -d "\r")"
- done < outpipe
- }
- while true; do
- if [[ ! -p inpipe ]]; then mkfifo inpipe; fi
- if [[ ! -p outpipe ]]; then mkfifo outpipe; fi
- sleep infinity > inpipe &
- sleep infinity > outpipe &
- nc -v $ts_serverQueryHost $ts_serverQueryPort < inpipe > outpipe &
- netcat_PID=$!
- sendCommand "use $ts_serverID"
- sendCommand "login $ts_serverQueryUser $ts_serverQueryPass"
- sendCommand "clientupdate client_nickname=AFK\sbot"
- while ps -p $netcat_PID > /dev/null; do
- sleep 1
- sendCommand "clientlist -voice"
- clientsConnected=()
- while read -r client; do
- if [[ ! $client =~ clid=([0-9]+).*cid=([0-9]+).*client_nickname=([^ ]+).*client_output_muted=([01]) ]]; then continue; fi
- clid=${BASH_REMATCH[1]}
- cid=${BASH_REMATCH[2]}
- client_nickname="$(sed "s/\\\s/ /g" <<< "${BASH_REMATCH[3]}")"
- client_output_muted=${BASH_REMATCH[4]}
- clientsConnected[${clid}]=${clid}
- if [[ $client_output_muted -eq 1 ]]; then
- if [[ $cid -ne $ts_afkChannelId ]]; then
- if [[ ! ${clientsIdleTime[${clid}]+foobar} ]]; then clientsIdleTime[${clid}]=$(date +%s); fi
- if [[ $(($(date +%s) - clientsIdleTime[clid])) -ge $ts_idleTimeBeforeMove ]]; then
- clientsLastChannel[${clid}]=$cid
- echo "Moving ${client_nickname} to channel ${ts_afkChannelId} after $(($(date +%s) - clientsIdleTime[clid])) seconds of inactivity."
- sendCommand "clientmove clid=${clid} cid=${ts_afkChannelId}"
- unset "clientsIdleTime[${clid}]"
- fi
- elif [[ ${clientsIdleTime[${clid}]+foobar} ]]; then
- unset "clientsIdleTime[${clid}]"
- fi
- elif [[ ${clientsLastChannel[${clid}]+foobar} ]] && [[ ${clientsLastChannel[${clid}]} -ne $cid ]]; then
- echo "${client_nickname} is not AFK anymore, moving back."
- sendCommand "clientmove clid=${clid} cid=${clientsLastChannel[${clid}]}"
- unset "clientsLastChannel[${clid}]"
- fi
- done < <(echo "${response}" | tr "|" "\n")
- for clid in "${!clientsIdleTime[@]}"; do
- if [[ ! "${clientsConnected[@]}" =~ ${clid} ]]; then
- unset "clientsIdleTime[${clid}]"
- fi
- done
- done
- pkill -TERM -P $$
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement