Skip to content

Instantly share code, notes, and snippets.

@LongHairedHacker
Created April 6, 2016 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LongHairedHacker/fcb1e7295d55798c5ead05731bed348a to your computer and use it in GitHub Desktop.
Save LongHairedHacker/fcb1e7295d55798c5ead05731bed348a to your computer and use it in GitHub Desktop.
BashBot IRC-Bot v0.3
#!/bin/bash
channel="#c3kl"
topic=""
odate="$(date '+%A %d.%m.%Y %H:%M')"
function log {
echo "$1" >> ./log.txt
}
function send {
log "BOT: $1";
echo "$1";
sleep 2
}
date > ./log.txt;
first_ping=1;
admin=0;
sleep 5
send "NICK BashBot";
sleep 5
send "USER netcat localhost irc.hackint.org : Ich";
log "ACTION: authed myself";
while read msg; do
log "SERVER: $msg";
cmd=$(expr "$msg" : '\(.*\) \:.*');
if [ "$(expr "$msg" : '.*TOPIC $channel \(.*\)')" ]; then
topic="$(expr "$msg" : '.*TOPIC $channel \(.*\)')";
#topic="$(expr "$topic" : '\:\(.*\)')";
log "TOPIC CHANGE : $topic";
fi
if [ "$(expr "$topic" : '\(.*\)\[.*\]')" ]; then
topic="$(expr "$topic" : '\(.*\)\[.*\]')";
fi
if [ "$(expr "$msg" : ':.* PRIVMSG #.* :%\([a-zA-Z0-9]*\)')" ]; then
cmd="$(expr "$msg" : ':.* PRIVMSG #.* :%\([a-zA-Z0-9]*\)')";
cmd="USER_$cmd";
if [ $(expr "$msg" : ':sebastian\!.*') != 0 ]; then
admin=1;
log "Admin CMD";
else
admin=0;
fi
fi
if [ "$(expr "$msg" : ':.* PRIVMSG #.* :%\([a-zA-Z0-9]*\) .*')" ]; then
cmd="$(expr "$msg" : ':.* PRIVMSG #.* :%\([a-zA-Z0-9]*\) .*')";
para="$(expr "$msg" : ':.* PRIVMSG #.* :%[a-zA-Z0-9]* \(.*\)')"
para=$(echo "$para" | awk -v RS="\n" -v ORS='' {print} | awk -v RS="\r" -v ORS='' {print} )
log "PARA: |$para|";
cmd="USER_$cmd";
if [ $(expr "$msg" : ':sebastian\!.*') != 0 ]; then
admin=1;
log "Admin CMD";
else
admin=0;
fi
fi
case "$cmd" in
"PING")
time=$(expr "$msg" : 'PING :\(.*\)');
send "PONG :$time";
log "ACTION: answered PING";
if [ $first_ping = 1 ]; then
first_ping=0;
sleep 10
send "JOIN $channel";
log "ACTION: joining channel"
send "MODE BashBot +B"
#send "PRIVMSG $channel :Hi this is Sebastains Bashbased ircbot Version 0.0.3";
fi;
;;
"USER_help")
send "PRIVMSG $channel : Avaiable commands are help, date(current date), fortune(unix command fortune),simpsonsquote(random simpsons quote),deen(translation de->en), ende(translation en->de), ping <ip> (pings <ip>), xmas <name>(give a present to <name>), serve <stuff> (start serving <stuff> in channel).";
;;
"USER_date")
send "PRIVMSG $channel :$(date)";
;;
"USER_fortune")
fortune -s > tmp &
while read fortune; do
send "PRIVMSG $channel :$fortune";
done <tmp;
;;
"USER_simpsonsquote")
./simpsonsquote.py > tmp &
while read line; do
send "PRIVMSG $channel :$line";
done <tmp;
;;
"USER_deen")
if [ "$para" ]; then
egrep -h "$para :: * " ./de-en.txt | head -n 3 > tmp &
send "PRIVMSG $channel :Looking in : ./de-en...";
while read line; do
send "PRIVMSG $channel :$c:$line";
done <tmp;
fi
;;
"USER_ende")
if [ "$para" ]; then
egrep -h " * :: $para" ./de-en.txt | head -n 3 > tmp &
send "PRIVMSG $channel :Looking in : ./de-en...";
while read line; do
send "PRIVMSG $channel :$line";
done <tmp;
fi
;;
"USER_settopic")
if [ "$para" ]; then
send "TOPIC $channel :$para";
fi
;;
"USER_serve")
if [ "$para" ]; then
send "PRIVMSG $channel :ACTION starts serving $para in $channel";
fi
;;
"USER_xmas")
if [ "$para" ]; then
send "PRIVMSG $channel :ACTION gives $para a christmas present. ";
fi
;;
"USER_say")
if [ "$para" ]; then
send "PRIVMSG $channel :$para"
fi
;;
"USER_ping")
if [ $admin = 1 ]; then
if [ "$para" ]; then
ping -c 1 "$para" | head -n 2 > tmp &
while read line; do
send "PRIVMSG $channel :$line";
done <tmp;
fi
else
send "PRIVMSG $channel :No ping for normal users";
fi
;;
"USER_quit")
if [ $admin = 1 ]; then
send "QUIT :Cya!";
else
send "PRIVMSG $channel :Haha ... it's not so easy";
fi
;;
*)
log " --> DUMPED";
;;
esac
cmd="";
para="";
done <./out;
#!/bin/bash
# in and out should be named pipes, use mkfifo
nc irc.hackint.org 6667 <in >out &
./behaviour.sh > in;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment