├── README.md ├── bot.properties ├── bot.sh └── modules ├── args └── args.sh ├── azhu └── azhu.sh ├── help └── help.sh ├── ping └── ping.sh ├── rps └── rps.sh └── talk ├── talk.sh └── talk_bot.jar /README.md: -------------------------------------------------------------------------------- 1 | bash-irc-bot 2 | ============ 3 | 4 | A simple, modular IRC bot written in bash 5 | 6 | throw "modules" into /modules/module-name/module-name.sh 7 | and they will be loaded up during runtime. 8 | 9 | call modules with botname: test arg1 arg2 .... 10 | -------------------------------------------------------------------------------- /bot.properties: -------------------------------------------------------------------------------- 1 | server="irc.freenode.net" 2 | channel="linux" 3 | nick="bashbot" 4 | user="username hostname servername :realname" 5 | log="log.txt" 6 | -------------------------------------------------------------------------------- /bot.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . bot.properties 4 | input=".bot.cfg" 5 | echo "Starting session: $(date "+[%y:%m:%d %T]")">$log 6 | echo "NICK $nick" > $input 7 | echo "USER $user" >> $input 8 | echo "JOIN #$channel" >> $input 9 | 10 | tail -f $input | telnet $server 6667 | while read res 11 | do 12 | # log the session 13 | echo "$(date "+[%y:%m:%d %T]")$res" >> $log 14 | # do things when you see output 15 | case "$res" in 16 | # respond to ping requests from the server 17 | PING*) 18 | echo "$res" | sed "s/I/O/" >> $input 19 | ;; 20 | # for pings on nick/user 21 | *"You have not"*) 22 | echo "JOIN #$channel" >> $input 23 | ;; 24 | # run when someone joins 25 | *JOIN*) who=$(echo "$res" | perl -pe "s/:(.*)\!.*@.*/\1/") 26 | if [ "$who" = "$nick" ] 27 | then 28 | continue 29 | fi 30 | echo "MODE #$channel +o $who" >> $input 31 | ;; 32 | # run when a message is seen 33 | *PRIVMSG*) 34 | echo "$res" 35 | who=$(echo "$res" | perl -pe "s/:(.*)\!.*@.*/\1/") 36 | from=$(echo "$res" | perl -pe "s/.*PRIVMSG (.*[#]?([a-zA-Z]|\-)*) :.*/\1/") 37 | # "#" would mean it's a channel 38 | if [ "$(echo "$from" | grep '#')" ] 39 | then 40 | test "$(echo "$res" | grep ":$nick:")" || continue 41 | will=$(echo "$res" | perl -pe "s/.*:$nick:(.*)/\1/") 42 | else 43 | will=$(echo "$res" | perl -pe "s/.*$nick :(.*)/\1/") 44 | from=$who 45 | fi 46 | will=$(echo "$will" | perl -pe "s/^ //") 47 | com=$(echo "$will" | cut -d " " -f1) 48 | if [ -z "$(ls modules/ | grep -i -- "$com")" ] || [ -z "$com" ] 49 | then 50 | ./modules/help/help.sh $who $from >> $input 51 | continue 52 | fi 53 | ./modules/$com/$com.sh $who $from $(echo "$will" | cut -d " " -f2-99) >> $input 54 | ;; 55 | *) 56 | echo "$res" 57 | ;; 58 | esac 59 | done 60 | -------------------------------------------------------------------------------- /modules/args/args.sh: -------------------------------------------------------------------------------- 1 | echo "PRIVMSG $2 :$1: $@" 2 | -------------------------------------------------------------------------------- /modules/azhu/azhu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | versionString="azhu® version 2.0.2 by Andrew DiMarco" 4 | 5 | if [ $# == 3 ]; then 6 | if [ $3 == "--version" ]; then 7 | echo "PRIVMSG $2 :$1: "$versionString 8 | exit 9 | fi 10 | elif [ $# == 1 ]; then 11 | if [ $1 == "--version" ]; then 12 | echo $versionString 13 | exit 14 | fi 15 | fi 16 | 17 | . "bot.properties" 18 | 19 | lines=$(grep "!azhu" $log) 20 | name=$(echo $lines | sed "s/.*\]:\(.*\)![^!]*/\1/g") 21 | if [[ "$name" == "" ]]; then 22 | name="azhu" 23 | fi 24 | line=$(echo $lines | sed "s/.*\[\(.*\)\].*/\1/g") 25 | lastResponse_DayOfMonth=$(echo $line | sed "s/^.*:.*:\(.*\) .*:.*:.*$/\1/g") 26 | 27 | dayOfWeek=$(date +%a) 28 | dayOfMonth=$(date +%d) 29 | hour=$(date +%T | sed "s/\([^:]*\):.*/\1/" ) 30 | minute=$(date +%M) 31 | 32 | daysApart=$((10#$dayOfMonth - 10#$lastResponse_DayOfMonth)) 33 | if [ "$daysApart" -eq 0 ]; then 34 | lastResponse_hour=$(echo $line | sed "s/^.*:.*:.* \(.*\):.*:.*$/\1/g") 35 | lastResponse_minute=$(echo $line | sed "s/^.*:.*:.* .*:\(.*\):.*$/\1/g") 36 | 37 | minutesBetween=$(((10#$hour - 10#$lastResponse_hour)*60 + 10#$minute - 10#$lastResponse_minute)) 38 | fi 39 | 40 | responsePercent=0 41 | if [ $minutesBetween -ge 0 ]; then 42 | responsePercent=$(( -5000/($minutesBetween+40) - $minutesBetween/25 + 100 )) 43 | fi 44 | 45 | percent=0 46 | 47 | case $dayOfWeek in 48 | "Mon" ) 49 | percent=$(($percent+25));; 50 | "Tue" ) 51 | percent=$(($percent+20));; 52 | "Wed" ) 53 | percent=$(($percent+20));; 54 | "Thu" ) 55 | percent=$(($percent+25));; 56 | "Fri" ) 57 | percent=$(($percent+30));; 58 | "Sat" ) 59 | percent=$(($percent+100));; 60 | "Sun" ) 61 | percent=$(($percent+100));; 62 | esac 63 | 64 | hp=$((10#$hour*10#$hour*10#$hour/150)) 65 | 66 | percent=$((10#$percent+10#$hp+10#$minute/20)) 67 | percent=$(($percent + $responsePercent)) 68 | 69 | 70 | if [ "$percent" -le 0 ]; then 71 | percent=0 72 | elif [ "$percent" -ge 100 ]; then 73 | percent=99 74 | fi 75 | 76 | if [ $# -ge 1 ]; then 77 | if [ $1 == "azhu" ]; then 78 | percent=0; 79 | fi 80 | echo "PRIVMSG $2 :$1: There is a $percent% chance $name is sleeping right now." 81 | else 82 | echo "There is a $percent% chance $name is sleeping right now." 83 | fi 84 | 85 | 86 | -------------------------------------------------------------------------------- /modules/help/help.sh: -------------------------------------------------------------------------------- 1 | echo "PRIVMSG $2 :$1: The available modules I have are:" 2 | output=$(ls modules/) 3 | echo "PRIVMSG $2 :$1: "$output 4 | -------------------------------------------------------------------------------- /modules/ping/ping.sh: -------------------------------------------------------------------------------- 1 | echo "PRIVMSG $2 :$1: pong" 2 | -------------------------------------------------------------------------------- /modules/rps/rps.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 0 - rock 3 | # 1 - paper 4 | # 2 - scissor 5 | 6 | m_choice=$(($RANDOM%3)) 7 | o_choice=$(echo $3 | sed -r "s/^[rR].*/0/" | sed -r "s/^[pP].*/1/" | sed -r "s/^[sS].*/2/") 8 | 9 | #convert my random to either r/p/s 10 | case "$m_choice" in 11 | 0) 12 | mr_choice="rock" 13 | ;; 14 | 1) 15 | mr_choice="paper" 16 | ;; 17 | 2) 18 | mr_choice="scissor" 19 | ;; 20 | esac 21 | 22 | if [ "$m_choice" == "$o_choice" ] 23 | then 24 | outcome="tie" 25 | elif [ "$((($m_choice+1)%3))" == "$o_choice" ] 26 | then 27 | outcome="lose" 28 | else 29 | outcome="win" 30 | fi 31 | 32 | echo "PRIVMSG $2 :$1: $3 VS $mr_choice | I $outcome!" 33 | -------------------------------------------------------------------------------- /modules/talk/talk.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # Simple Question-Answering module 3 | # author Vy Nguyen 4 | # 5 | 6 | arg1=$1 # sender (the person I am talking to) 7 | arg2=$2 # unclear???? 8 | 9 | user_dir=modules/talk/logs/$1 10 | if [ ! -d "$user_dir" ]; then 11 | mkdir $user_dir 12 | fi 13 | 14 | # if the user said goodbye and changed her mind! 15 | if [ -f "$user_dir/GOODBYE" ]; then 16 | echo "PRIVMSG $arg2 :$arg1: I thought you just said goodbye!" 17 | rm $user_dir/GOODBYE 18 | fi 19 | 20 | shift 2 # chop off the first two args 21 | echo "$@" >> $user_dir/input 22 | cat $user_dir/input | java -jar modules/talk/talk_bot.jar > $user_dir/output 23 | 24 | # retcode of value 1 means the user said goodbye 25 | retcode="${PIPESTATUS[1]}" 26 | 27 | echo "PRIVMSG $arg2 :$arg1: `tail -n 1 $user_dir/output`" 28 | 29 | if [ "$retcode" -eq "1" ]; then 30 | echo "PRIVMSG $arg2 :$arg1: That was a joke! It's free to talk to me. But we do accept donations! ;D" 31 | # clean up old conversations 32 | stamp="`date +%s`" 33 | mv $user_dir/input $user_dir/"input_$stamp" 34 | mv $user_dir/output $user_dir/"output_$stamp" 35 | # leave good-bye flag (indicating the user already said goodbye) 36 | touch $user_dir/GOODBYE 37 | fi 38 | -------------------------------------------------------------------------------- /modules/talk/talk_bot.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Newbrict/bash-irc-bot/a1e3cb078f0c31a818b8b32d044dd5bba344cc1d/modules/talk/talk_bot.jar --------------------------------------------------------------------------------