plantbot.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/bin/sh
  2. ################################################################################
  3. # Name: plantbot.sh
  4. # Lisc: GPLv3+
  5. # Main: jadedctrl || jadedml@openmailbox.org
  6. # Usage: plantbot.sh "#channel" $ii_root
  7. # Desc: Basic IRC chatbot for the suckless IRC client, ii.
  8. # It was made for Libre Game Night, so you might have to remove/edit
  9. # some stuff.
  10. ################################################################################
  11. alive=1
  12. root=$2
  13. channel="$1"
  14. infile="$root/$channel/in"
  15. outfile="$root/$channel/out"
  16. processing=$outfile.proc
  17. . /var/www/htdocs/lgn.oss/global.sh
  18. # Just checks for user commands
  19. function checkcommand {
  20. if [ "$author" == "plantbot" ]
  21. then
  22. echo "Oh well." > /dev/null
  23. elif echo "$4" | grep "^plantbot:" > /dev/null
  24. then
  25. command="$1"
  26. arguments="$2"
  27. author="$3"
  28. commandline="$4"
  29. echo $command
  30. case $command in
  31. time)
  32. echo "The time is $(date -u +%H:%M) UTC, mate. :)" > $infile
  33. ;;
  34. date)
  35. echo "The date is $(date -u +%Y-%m-%d), friend-o. ^_^" > $infile
  36. ;;
  37. countdown)
  38. sh lgn.sh > $infile
  39. ;;
  40. fortune)
  41. /usr/games/fortune -so > $infile
  42. ;;
  43. help)
  44. echo "I can do a few pretty useful things, honestly!" > $infile
  45. echo "Here's a list of commands like 'help'..." > $infile
  46. echo "date, time, kys, countdown, fortune, joke, thanks, tell [usr] [msg]" > $infile
  47. ;;
  48. joke)
  49. echo "$author" > $infile
  50. ;;
  51. kys)
  52. echo "Uh, no, how about *you* KYS?" > $infile
  53. echo "/ME has plenty of self-love & confidence, never falling for peer pressure" > $infile
  54. ;;
  55. source)
  56. echo "I'm libre AF, under the GPLv3+ license." > $infile
  57. echo "You can tickle my insides at https://notabug.org/jadedctrl/plantbot" > $infile
  58. ;;
  59. tell)
  60. recipient=$(echo $arguments | awk '{print $1}')
  61. message=$(echo $arguments | sed 's/.*'"$recipient"'//')
  62. echo "OK, when $recipient is online I'll pass along your message. =w=" > $infile
  63. echo "${recipient}|${author}|$message" >> ~/plantbot/share/messages
  64. ;;
  65. thanks)
  66. echo "You're welcome =w=">$infile
  67. ;;
  68. esac
  69. fi
  70. }
  71. # Checks for new users, users with messages, and for triggers in messages
  72. function checktrigger {
  73. author="$1"
  74. line="$2"
  75. if [ "$author" == "plantbot" ]
  76. then
  77. echo "Heck." > /dev/null
  78. elif echo $line | grep "has joined"
  79. then
  80. joiner=$(echo $line | awk '{print $4}' | sed 's/(.*//')
  81. if grep "^${joiner}" ~/plantbot/etc/optout
  82. then
  83. echo "What a downer...">/dev/null
  84. else
  85. echo "Welcome, bonvenon, ${joiner}! o/">$infile
  86. fi
  87. elif grep "^${author}" ~/plantbot/share/messages > /dev/null
  88. then
  89. recipient=$author
  90. if [ $(grep "^${recipient}" ~/plantbot/share/messages | wc -l) -gt 1 ]
  91. then
  92. echo "Oh, ${recipient}--">$infile
  93. echo "I have a few messages for you...">$infile
  94. while [ $(grep "^${recipient}" ~/plantbot/share/messages | wc -l) -ne 0 ]
  95. do
  96. message=$(grep "^${recipient}" ~/plantbot/share/messages | head -n1)
  97. author=$(echo $message | awk -F'|' '{print $2}')
  98. recipient=$(echo $message | awk -F'|' '{print $1}')
  99. messagetext=$(echo $message | sed 's/.*|//')
  100. grep -v "$messagetext" ~/plantbot/share/messages > ~/plantbot/share/messages.new
  101. mv ~/plantbot/share/messages.new ~/plantbot/share/messages
  102. cat ~/plantbot/share/messages
  103. echo "Here's one from ${author}: \"$messagetext\"">$infile
  104. done
  105. else
  106. echo "Oh, ${recipient}--">$infile
  107. message=$(grep "^${recipient}" ~/plantbot/share/messages)
  108. author=$(echo $message | awk -F'|' '{print $2}')
  109. recipient=$(echo $message | awk -F'|' '{print $1}')
  110. messagetext=$(echo $message | sed 's/.*|//')
  111. echo "I have a message for you from ${author}. Here it is:">$infile
  112. grep -v "$message" ~/plantbot/share/messages > ~/plantbot/share/messages
  113. echo "\"$messagetext\"">$infile
  114. echo ":)">$infile
  115. fi
  116. fi
  117. while read fileline
  118. do
  119. trigger=$(echo "$fileline" | awk '{ print $1 }')
  120. if echo "$line" | grep "$trigger" > /dev/null
  121. then
  122. number=$(echo "$line" | awk 'BEGIN {FS="\t"} END {print NF}' )
  123. number=$((number-1))
  124. randnumber=$(awk -v min=2 -v max=$number 'BEGIN{srand(); print int(min+rand()*(max-min+1))}')
  125. echo "$line" | awk -v phrase=$randnumber -F "\t" '{ print $phrase }'
  126. fi
  127. done < ~/plantbot/etc/triggers
  128. }
  129. # Init and main loop
  130. echo "*Waking up...* Plantbot at your service! Say 'plantbot: help' for info \\o/" > $infile
  131. while [ $alive -eq 1 ]
  132. do
  133. if [ -e $outfile ]
  134. then
  135. mv $outfile $processing
  136. while read line
  137. do
  138. author=$(echo $commandline | awk '{print $3}' | sed 's/<//' | sed 's/>//')
  139. command=$(echo $commandline | awk '{print $5}')
  140. arguments=$(echo $commandline | sed 's/.*'"$command"' //')
  141. checkcommands "$command" "$arguments" "$author" "$line"
  142. checkcommands "$author" "$line"
  143. done < $processing
  144. fi
  145. sleep 15
  146. done