IRC Bot in bash

Aus revampedia

Hier noch ein IRC Bot in _bash_, von Robin.

Es verwendet das Kommandozeilen tool: ii

#!/bin/bash

trap 'kill $(jobs -p) &> /dev/null ; exit 23' SIGINT SIGTERM

BASEDIR="$(dirname $0)"
USERNAME="Hodorbot"
REALNAME="Hodor"
SERVER="irc.freenode.net"
CHANNEL="#revamp"
#CHANNEL="#ubunteros.ch"

STRING="hodor"
STRINGFILE="$BASEDIR/hodor.txt"
OUTFILE="$BASEDIR/$SERVER/$CHANNEL/out"
INFILE="$BASEDIR/$SERVER/$CHANNEL/in"

hodor_func (){
	while kill -s 0 "$IIPID"
	do
		sleep 0.1
		NUMOFLINES=$(wc -l $OUTFILE | cut -d" " -f1)
		if [[ -z "$LINESTART" ]]
		then
			LINESTART="$NUMOFLINES"
		fi
		if [[ "$NUMOFLINES" -gt "$LINESTART" ]]
		then
			let LINESTART++
			LINESTRING=$(awk "NR==$LINESTART, NR==$NUMOFLINES" $OUTFILE)
#			echo "$LINESTRING"
			USER="$(echo $LINESTRING | cut -d " " -f3 | sed -e "s/<//" -e "s/>//")"
			MESSAGE="$(echo $LINESTRING | cut -d " " -f4- | tr '[:upper:]' '[:lower:]')"

			if ! [[ "$USER" = "$USERNAME" ]]
			then
#				echo "$MESSAGE"
				case "$MESSAGE" in
					*"${USERNAME,,}"*)
						if echo "$MESSAGE" | grep -qi "help"
						then
							echo "$USER: Hodor while hodoring" > "$INFILE"
						else
							echo "$USER: $(shuf -n 1 "$STRINGFILE")" > "$INFILE"
						fi
					;;
					*"$STRING"*)
						shuf -n 1 "$STRINGFILE" > "$INFILE"
					;;
				esac
#				echo "$MESSAGE" > "$INFILE"
			fi
		fi
	done
}

while true
do
	ii -s "$SERVER" -i "./" -n "$USERNAME" -f "$REALNAME" &
	IIPID="$!"
	sleep 5
	echo "/j $CHANNEL" > "$BASEDIR/$SERVER/in"
	hodor_func
	wait "$IIPID"
done