#!/opt/perl/bin/perl
########################################
# Thank you for downloading QuikChat...
# PLEASE DO NOT REDISTRIBUTE THIS SCRIPT
#
# This is a sample logoff script for 2.1

########################################
#	CONFIGURATION SECTION
# place the absolute path to your messages
# file here (where the chat messages are
# written to).  You will also have to change
# the permissions of that file to read- and
# writable.

	$file='/home/duc4/collear/public_html/qchat/messages.html';

########################################
# This script is almost identical to the
# regular QuikChat script.  The only
# difference is the message it sends to
# the user after he or she logs off.
# I left this stuff seperate from the
# main script so it could easily be
# changed.


########################################
# read in the form data

	&get_form_data;

########################################
# read in the old message file

	open (HTMLOLD, "$file");
	@lines=<HTMLOLD>;
	close(HTMLOLD);

########################################
# output new message file

	####### get the time #######
	$now_string = localtime;
	@thetime = split(/ +/,$now_string);
	@theclock = split(/:/,$thetime[3]);
	$ampm = 'am';
	if ($theclock[0] > 11)
	{
		$ampm = 'pm';
	}
	if ($theclock[0] == 0)
	{
		$theclock[0] = 12;
	}
	if ($theclock[0] > 12)
	{
		$theclock[0] -= 12;
	}
	else
	{
		$theclock[0] += 0;
	}
	############################

	open (NEW, ">$file");
	print NEW "\<HTML\>\<HEAD\>\<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5\"\>\</HEAD\>\<BODY BGCOLOR=\"\#FFFFFF\"\>\n";
	for ($i = 2; $i < 16; $i++)
	{
	    print NEW "$lines[$i]";
	}
	print NEW "\<P\>\<B\>$formdata{'username'}\</B\>";
	print NEW " says\,\"";
	print NEW "$formdata{'message'}";
	print NEW "\" \($thetime[0] $theclock[0]\:$theclock[1]$ampm\)";
	print NEW "\n\<BR\>\<FONT COLOR\=\#FFFFFF\>\<A NAME=\"END\"\>QuikChat \(c\) 1997 Matt Hahnfeld\</A\>\</FONT\>\</BODY\>\n";
	close(NEW);

########################################
# initialize the message form after logon

	print "Content-type: text/html\n\n";
	print "\<html\>\<title\>\</title\>\<BODY BGCOLOR\=\#000080 TEXT\=\#FFFFFF\>\n";
	print "\<CENTER\>";
########################################
# this is what the user sees after
# logging off...
	print "Goodbye\! Thanks for chatting on CheerChat\!\n";
########################################
# This line calls a goto URL script to
# replace the above line if you want...
# (this is what is used on my demo)
#	print "\<form method=\"post\" action=\"/cgi-shl/gotourl.exe\?\" target=\"\_top\"\>Enter a new \<B\>URL\</B\> to go to\: \<INPUT NAME=\"newurl\" VALUE=\"http\://hope\.ml\.org/\" \> \<input type=submit value=\"Leave\"\>\n";
	print "\</CENTER\>\n";
        print "\</body\>\</html\>\n";
	exit 0;

########################################
# subs

sub get_form_data {
	$buffer = "";
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs=split(/&/,$buffer);
	foreach $pair (@pairs)
	{
		@a = split(/=/,$pair);
		$name=$a[0];
		$value=$a[1];
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$value =~ s/~!/ ~!/g;
		$value =~ s/\+/ /g;
		# $value =~ s/\</\&lt\;/g;  # html tag removal (optional)
		# $value =~ s/\>/\&gt\;/g;  # html tag removal (optional)
		$value =~ s/\r//g;
		push (@data,$name);
		push (@data, $value);
	}
	%formdata=@data;
	%formdata;
}
