Advertisement
Guest User

Untitled

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