#!/opt/perl/bin/perl
########################################
# Thank you for downloading QuikChat...
# This script was built and tested under
# Perl 5.003 for Win32, but should work
# on any platform.  PLEASE DO NOT
# REDISTRIBUTE THIS SCRIPT!  See
# README.TXT for more information.
# This script may be obtained from Matt
# Hahnfeld's Web Site at:
#
# http://www.cs.hope.edu/~hahnfld/
#
# Of course, it is freeware!
# THIS IS VERSION 2.1 RELEASED 4/1/97

########################################
#	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';

# here is where the script is located
# with a virtual path (web browser accessed)

	$thescript='http://www.auburn.edu/cgi-bin/quikchat.pl';
	$thelogoffscript='http://www.auburn.edu/cgi-bin/quiklogoff.pl';

########################################
# 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 "Pragma: no-cache\nContent-type: text/html\n\n";
	print "\<html\>\<title\>\</title\>\<BODY BGCOLOR\=\#000080 TEXT\=\#FFFFFF\>\n";
	print "\<CENTER\>\<TABLE CELLSPACING=0 CELLPADDING=0\>\n";
	print "\<TR\>\n";
	print "\<TD\>\n";
	print "\<nobr\>\<FORM ACTION=\"$thescript\" METHOD=\"POST\"\>Your message\:\n\<input name=username type=hidden value=\"$formdata{'username'}\"\>\n";
	print "\<input type=text name=message size=35\>\n";
	print "\<input type=submit value=\"Post This\"\>";
	print "\</form\>\</nobr\>\n";
	print "\</TD\>\n";
	print "\<TD\>\n";
	print "\<nobr\>\<FORM ACTION=\"$thelogoffscript\" METHOD=\"POST\"\>\<input name=username type=hidden value=\"$formdata{'username'}\"\>\n";
	print "\<input type=hidden name=message value=\"Bye\! I just logged off\!\"\>\n";
	print " \<input type=submit value=\"Logoff\"\>";
	print "\</form\>\</nobr\>\n";
	print "\</TD\>\n";
	print "\</TR\>\n";
	print "\</TABLE\>\</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;
}
