summaryrefslogtreecommitdiff
path: root/shbrain.sh
blob: 3b1f700ee6a3a74c490f1f3321d38946ad2993fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash

while true
do
	read LINE || break
	### First of all we get rid of a potential trailing \n
	LEN=`expr ${#LINE} - 1`
	LINE=${LINE[@]:0:$LEN}

	### Parsing potential shcmd
	if [[ $LINE =~ :([^!]*)!.*:shcmd ]]; then
		EXPAND=(${LINE})
		CMD=${EXPAND[4]#:}
		ARGS=${EXPAND[*]:5}
		if [ "$CMD" = "advert" ]; then
			echo "Listen to shbot, the one true Bot!"
		elif [ "$CMD" = "join" ]; then
			echo "JOIN $ARGS" >&3
		elif [ "$CMD" = "part" ]; then
			echo "PART $ARGS" >&3
		elif [ "$CMD" = "quote" ]; then
			./shquote.sh ${ARGS[*]}
		elif [ "$CMD" = "trains" ]; then
			./shtrains.sh ${ARGS[*]}
		elif [ "$CMD" = "duck" ]; then
			./shduck.sh
		elif [ "$CMD" = "help" ]; then
			./shhelp.sh $ARGS
		else
			echo "Use \"shcmd help\" to list available commands."
		fi
		break
	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
				echo `eval echo $RESPONSE`
				#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