Advertisement
Guest User

Untitled

a guest
Sep 15th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Created by Malobre
  4. #
  5.  
  6. ts_serverID="1"
  7. ts_serverQueryHost="localhost"
  8. ts_serverQueryPort="10011"
  9. ts_serverQueryUser="queryUser"
  10. ts_serverQueryPass="queryPass"
  11. ts_afkChannelId="1"
  12. ts_idleTimeBeforeMove="30"
  13.  
  14. sendCommand() {
  15. echo "${1}" > inpipe
  16.  
  17. response=""
  18. while read -r tmp; do
  19. if [[ "${tmp}" =~ error\ id=([0-9]+)\ msg=(.*) ]]; then
  20. errorId=${BASH_REMATCH[1]}
  21. errorMsg=${BASH_REMATCH[2]}
  22. if [[ errorId -ne 0 ]]; then
  23. echo "Error ${errorId}: $(sed "s/\\\s/ /g" <<< "${errorMsg}")"
  24. fi
  25. break
  26. fi
  27. response="${response} $(echo "${tmp}" | tr -d "\r")"
  28. done < outpipe
  29. }
  30.  
  31. while true; do
  32. if [[ ! -p inpipe ]]; then mkfifo inpipe; fi
  33. if [[ ! -p outpipe ]]; then mkfifo outpipe; fi
  34. sleep infinity > inpipe &
  35. sleep infinity > outpipe &
  36.  
  37. nc -v $ts_serverQueryHost $ts_serverQueryPort < inpipe > outpipe &
  38.  
  39. netcat_PID=$!
  40.  
  41. sendCommand "use $ts_serverID"
  42. sendCommand "login $ts_serverQueryUser $ts_serverQueryPass"
  43. sendCommand "clientupdate client_nickname=AFK\sbot"
  44.  
  45. while ps -p $netcat_PID > /dev/null; do
  46. sleep 1
  47. sendCommand "clientlist -voice"
  48.  
  49. clientsConnected=()
  50. while read -r client; do
  51. if [[ ! $client =~ clid=([0-9]+).*cid=([0-9]+).*client_nickname=([^ ]+).*client_output_muted=([01]) ]]; then continue; fi
  52. clid=${BASH_REMATCH[1]}
  53. cid=${BASH_REMATCH[2]}
  54. client_nickname="$(sed "s/\\\s/ /g" <<< "${BASH_REMATCH[3]}")"
  55. client_output_muted=${BASH_REMATCH[4]}
  56. clientsConnected[${clid}]=${clid}
  57. if [[ $client_output_muted -eq 1 ]]; then
  58. if [[ $cid -ne $ts_afkChannelId ]]; then
  59. if [[ ! ${clientsIdleTime[${clid}]+foobar} ]]; then clientsIdleTime[${clid}]=$(date +%s); fi
  60. if [[ $(($(date +%s) - clientsIdleTime[clid])) -ge $ts_idleTimeBeforeMove ]]; then
  61. clientsLastChannel[${clid}]=$cid
  62. echo "Moving ${client_nickname} to channel ${ts_afkChannelId} after $(($(date +%s) - clientsIdleTime[clid])) seconds of inactivity."
  63. sendCommand "clientmove clid=${clid} cid=${ts_afkChannelId}"
  64. unset "clientsIdleTime[${clid}]"
  65. fi
  66. elif [[ ${clientsIdleTime[${clid}]+foobar} ]]; then
  67. unset "clientsIdleTime[${clid}]"
  68. fi
  69. elif [[ ${clientsLastChannel[${clid}]+foobar} ]] && [[ ${clientsLastChannel[${clid}]} -ne $cid ]]; then
  70. echo "${client_nickname} is not AFK anymore, moving back."
  71. sendCommand "clientmove clid=${clid} cid=${clientsLastChannel[${clid}]}"
  72. unset "clientsLastChannel[${clid}]"
  73. fi
  74. done < <(echo "${response}" | tr "|" "\n")
  75.  
  76. for clid in "${!clientsIdleTime[@]}"; do
  77. if [[ ! "${clientsConnected[@]}" =~ ${clid} ]]; then
  78. unset "clientsIdleTime[${clid}]"
  79. fi
  80. done
  81. done
  82. pkill -TERM -P $$
  83. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement