Archived
1
0
Fork 0
This repository has been archived on 2025-03-10. You can view files and clone it, but cannot push or open issues or pull requests.
shbot/shbrain.sh

60 lines
1.3 KiB
Bash
Raw Normal View History

2009-07-06 17:26:39 +02:00
#!/bin/bash
while true
do
read LINE || break
2009-07-07 09:56:53 +02:00
### First of all we get rid of a potential trailing \n
2009-07-06 17:26:39 +02:00
LEN=`expr ${#LINE} - 1`
LINE=${LINE[@]:0:$LEN}
### Parsing potential shcmd
if [[ $LINE =~ :([^!]*)!.*:shcmd ]]; then
2009-07-06 17:26:39 +02:00
EXPAND=(${LINE})
CMD=${EXPAND[4]#:}
ARGS=${EXPAND[*]:5}
2009-08-18 08:28:39 +02:00
if [ "$CMD" = "advert" ]; then
2013-11-05 15:45:52 +02:00
echo "Listen to shbot, the one true Bot!"
2009-08-18 08:28:39 +02:00
elif [ "$CMD" = "join" ]; then
echo "JOIN $ARGS" >&3
elif [ "$CMD" = "part" ]; then
echo "PART $ARGS" >&3
2009-08-19 12:22:40 +02:00
elif [ "$CMD" = "quote" ]; then
./shquote.sh ${ARGS[*]}
2009-08-18 08:28:39 +02:00
elif [ "$CMD" = "trains" ]; then
2009-07-07 09:56:53 +02:00
./shtrains.sh ${ARGS[*]}
elif [ "$CMD" = "duck" ]; then
./shduck.sh
2009-07-06 17:26:39 +02:00
elif [ "$CMD" = "help" ]; then
./shhelp.sh $ARGS
2009-07-06 17:26:39 +02:00
else
2009-07-07 09:56:53 +02:00
echo "Use \"shcmd help\" to list available commands."
2009-07-06 17:26:39 +02:00
fi
2009-07-07 09:56:53 +02:00
break
2009-07-06 17:26:39 +02:00
fi
### Parsing brain file for potential replies
LINENUM=1
cat shbrain.txt | \
while true
do
read BRAIN || break
IFS=" " BRAIN=($BRAIN)
PATTERN=${BRAIN[0]}
PERCENT=${BRAIN[1]}
RESPONSE=${BRAIN[2]}
if [[ "$LINE" =~ ${PATTERN} ]]; then
if [[ "$((RANDOM / 320))" -lt ${PERCENT} ]]; then
2009-07-07 09:56:53 +02:00
echo `eval echo $RESPONSE`
2009-07-06 17:26:39 +02:00
#awk 'NR==$LINENUM{$0='"$RESPONSE"'}1' shbrain.txt
#sed "$LINENUM"'s\\'"${PATTERN} 10 ${RESPONSE}" -i shbrain.txt
break
fi
fi
(( LINENUM++ ))
done
done
exit 0