bashy2 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env bash
  2. # This file is part of bashy2, an irc bot used on irc.sdf.org.
  3. # Copyright (C) 2015-2016 mlaine@sdfeu.org, grugly@sdf.org
  4. # Copyright (C) 2017 mlaine@sdfeu.org
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. #------------------------------------------------------------------------------
  21. debug=1
  22. server="$1"
  23. irc_port=6667
  24. irc_host='irc.sdf.org'
  25. irc_pass="$HOME/.oline"
  26. modes=12
  27. cmd_dir='./bits/commands'
  28. if [[ "$server" = 'us' ]]; then
  29. auth_host='sdf.org'
  30. auth_host2='sdf-eu.org'
  31. bot_user='BASHy2'
  32. bot_nick='BASHy2'
  33. bot_nick2='BASHy2-EU'
  34. bot_real='A BASH Bot'
  35. elif [[ "$server" = 'eu' ]]; then
  36. auth_host='sdf-eu.org'
  37. auth_host2='sdf.org'
  38. bot_user='BASHy2-EU'
  39. bot_nick='BASHy2-EU'
  40. bot_nick2='BASHy2'
  41. bot_real='BASHy2 in Europe'
  42. else
  43. printf '\n%s\n\n' 'Supply argument us|eu if running from sdf-us|sdf-eu.'
  44. exit 1
  45. fi
  46. function init {
  47. source './bits/vitalbits.sh'
  48. source './bits/nonvitalbits.sh'
  49. exec 3<>"/dev/tcp/$irc_host/$irc_port" || return 1
  50. send 'user' "$bot_user" "$bot_real"
  51. send 'nick' "$bot_nick"
  52. }
  53. function recon {
  54. if (( "$i" <= '10' )); then
  55. sleep 30 && init
  56. let "i++"
  57. else
  58. sleep 300 && init
  59. fi
  60. }
  61. function main {
  62. i=0; init
  63. while true; do
  64. IFS=$' \t\r' read -ru3 -a data
  65. if [[ ! "$data" ]]; then
  66. recon
  67. else
  68. debug 'IN:' "${data[@]}"
  69. if [[ "${data[0]}" = 'PING' ]]; then
  70. send 'pong' "${data[1]}"
  71. continue
  72. fi
  73. case "${data[1]}" in
  74. 'PRIVMSG')
  75. priv "${data[@]}" ;;
  76. 'JOIN')
  77. user_join "${data[@]}" ;;
  78. 'QUIT')
  79. user_quit "${data[@]}" ;;
  80. '001')
  81. send 'oper' "$bot_user" "$(head -1 "$irc_pass")"
  82. join && eval i=0
  83. sleep 15 && chtopic &&
  84. sleep 15 && serv 'upall' & ;;
  85. '332'|'331')
  86. chtopic "${data[@]}" ;;
  87. '352'|'315')
  88. upall "${data[@]}" ;;
  89. '311'|'319')
  90. up "${data[@]}" ;;
  91. esac
  92. fi
  93. done
  94. }
  95. main