/*
 * Away system for BWap
 *
 * Author: Brian Weiss <brian@got.net> - 2000
 */

/* NOTE: this script is very unfinished at the moment.. IT DOES NOT WORK */
/* Unless you fix it yourself.. I wouldn't recommend loading it :) */

echo *** THIS SCRIPT IS BROKEN. DO NOT USE.

/* remove this line if you want to load the script */
/* dont forget the closing brace at the bottom :) */
if (NOT_BROKEN)
{

assign AWAY_LOGFILE ~/.BWap/away.log
assign AWAY_MSGLOG 1
assign DEFAULT_AWAY_REASON gone
assign DEFAULT_BACK_REASON hey!
assign PUBLIC_AWAY admins

alias away (reason)
{
	if (!A)
	{
		assign A 1
		^local fd
		@ fd = open($AWAY_LOGFILE W)
		@ write($fd [MsgLog Started $strftime(%B %d %Y %X)])
		@ close($fd)
		if (PUBLIC_AWAY)
		{
			fe ($PUBLIC_AWAY) chan
			{
				echo $chan: ${reason ? reason : DEFAULT_AWAY_REASON}
			}
		}
	}{
		xecho -b You are already away!
	}
}

alias back (reason)
{
	if (A)
	{
		assign -A
		@ fd = open($AWAY_LOGFILE W)
		@ write($fd [MsgLog Stopped $strftime(%B %d %Y %X)])
		@ close($fd)
		if (PUBLIC_AWAY)
		{
			fe ($PUBLIC_AWAY) chan
			{
				echo $chan ${reason ? reason : DEFAULT_BACK_REASON}
			}
		}
		input "Read MsgLog now? (Y/n) "
		{
			switch ($tolower($left(1 $0)))
			{
				(y) {readlog}
				(n) {;}
				(*) {readlog}
			}
		}
	}{
		xecho -b You are not set away.
	}
}

alias readlog (void)
{
	^local done,fd,line,pause,rows,stop
	@ rows = winsize() - 1
	@ fd = open($AWAY_LOGFILE R)
	while (!eof($fd) && !stop)
	{
		for (@ line = [0], line < rows, @ line++)
		{
			if (eof($fd))
			{
				@ done = [1]
				@ line = rows
			}{
				echo $read($fd)
			}
		}
		input "Press 'q' to quit, or any other key to continue."
		{
			switch ($tolower($0))
			{
				(q) {@ stop = [1]}
				(*) {@ line = [0]}
			}
		}
	}
	@ close($fd)	
}

alias remlog (void)
{
	@ unlink($AWAY_LOGFILE)
	xecho -b Away log [$AWAY_LOGFILE] removed.
}

on ^msg "*" if (A && AWAY_MSGLOG)
{
	^local fd
	@ fd = open($AWAY_LOGFILE W)
	@ write($fd [$strftime($time() %x %X)] *$0* $1-)
	@ close($fd)
}
}