#!/bin/bash

#       d e m o  I R C  s c r i p t  f o r  - s u l a -
#       http://www.geocities.com/SiliconValley/Peaks/8771
#	fotang@techie.com
#	to run this script, type:
#		/run <filename>
#	inside control window.
#	to shut it down:
#		/dump -quit
#	to temporaly put it out of action:
#		/dump -sleep
#	COPYING: free, with warranty. na!

version=0.2
trap 'echo _SIGCONT_' SIGCONT
prog=$0
prog=${prog##*/}
echo "_CMD_ $prog controls $prog"

printf "\n\
@m@C7@iFilename: $prog\n\
@C7@iDescription:
@C7@i	-Logs all private messages while you are away.
@C7@i	-Trashes annoying messages each time you join channel:
@C7@i	 you no longer see who set channel topic or
@C7@i	 who created channel	
@C7@i	-Shouts when a question is asked..\n\
@C7@i	-play sound
@C7@f	(type /l for stats)
@C7@iAuthor:\n\
@C7@iProgramming: shell (bash)
@C7@iVersion: $version\n\
@C7@iCopying: free, no (g)warranty
@C7@iCommand(s):
@C7@f	play	-play(1) sound file
@C7@f	$prog	-control program.\n\
@C7@f		(/$prog -? for help)
"
#	If you just want to get rid of certain messages then it
#	is probably better to use the ON command:
#		/ON 329 * :
#		/on 333 * :
#		/on privmsg fool\!* :	# all private messages from nick "fool"
echo "_EVENT_ 	1 	329 	1"		# topic was set by..
#echo "_EVENT_ 1 332 1"				# topic is..
echo "_EVENT_ 	1 	333 	1"		# channel was created by..
echo "_EVENT_ 	2	PUBMSG	0 *\?"		#better pattern: *\?*
echo "_CMD_	play	play music.."
echo "_EVENT_	3	305"				#no longer marked away
echo "_EVENT_	-3	306"				# you are away

sound_dir=/u1/daten/sounds
#where to log private mesages when u're away:
AwayLog="away.log"	
while read w cmd server args; do
	[ "$cmd" = "" ] && continue;
	case $cmd in
	2)
		echo "$w /echo -2 @ia question! a question!"
		continue;;
	-3)
		echo "_EVENT_ 306 privmsg 0 *"	#send me private mesages now!!
		continue;;
	3)
		echo "_REVENT_ 306"		# no more private messages please! 
		continue;;
	306)	# you are away so i have to log private messages
		#log as:
                #       (Sat Feb 7 22:10:24) nick!login@domain: Message         
                 message=${args#*:}
		 sender=${args%PRIVMSG*}
                 when=$(date | cut -d" " -f1,2,4,5)
                echo \("$when"\) "$sender": "$message" >> "$AwayLog"
                continue;;
	"$prog")
		if [ "$args" = "" ]; then continue; fi
		case $args in
			"-?"|-[hH]*)
				echo "$w /echo Usage: $prog -quit  |  -sleep  |  -?"
				continue;;
			-[sS]*)
				kill -SIGSTOP $$  #"$prog"
				continue;;
			-[qQ]*)		# shut down 
				echo "$w /echo @iyou have killed $$, you idiot." 
				exit 0;;
		esac
		continue;;
	play)
		[ "$args" = "" ] && continue;
		if [ -r "$sound_dir"/"$args" ]; then
			#echo "_SLEEPING_"  not required since playing in background
			play "$sound_dir"/"$args" &
			#echo "_CONTINUE_"
		else echo "$w "/echo -2 "$prog": cannot read "$sound_dir"/"$args"
		fi
		continue;; 
		
	esac

done



