#!/usr/local/bin/perl

print "Content-type: text/html", "\n\n"  ;

require ChatSign ;
require TimeUtilities ;
$data_basedir=&ChatSign::data_basedir ;
$URLdir_executables=&ChatSign::URLdir_executables ;
$HDDdir_executables=&ChatSign::HDDdir_executables ;

$request_method=$ENV{'REQUEST_METHOD'} ;

if ($request_method eq "GET") {
$query_string=$ENV{'QUERY_STRING'} ;
# exit (1) ;
} else {
$size_of_form=$ENV{'CONTENT_LENGTH'} ;
read(STDIN, $query_string, $size_of_form) ;
}

# Split the name-value pairs
@pairs = split(/&/, $query_string);

foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	# Un-Webify plus signs and %-encoding
	$value =~ tr/+/ /;
	# Get rid of all non-alpha/numeric stuff esp " ' ,
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	# $value =~ s/['|"|`|,]//g;
	# Stop people from using subshells to execute commands
	# when using UCB mail (aka mailx).
	$value =~ s/~!/ ~!/g;
	$FORM{$name}=$value ;
}


$website=$FORM{domain};

$sites="$data_basedir/websites.dat" ;
open (SITES,$sites) || die "Cannot open $sites\n" ;
chomp ;
while (<SITES>) {
        chomp ;
        ($site,$password,$default_email)=split(/,/) ;
        $sites{$site}=$site ;
        $default_emails{$site}=$default_email ;
}

# Make sure they have entered a name 

$name=$FORM{requestor} ;
if  ($name=~ /![A-Za-z0-9_+-.]/) { &invalid_name ; }
if  ($name=~ / /) { &invalid_name ; }
unless  ($name) { &invalid_name ; }


$chat_data="$data_basedir/$website/data/$FORM{requestor}" ;

# Now create the blank chat file
open (CREATE,">> $chat_data") ;
chmod ($chat_data, 666) ;

$now=time ;

$status_file="$data_basedir/$website/status.dat" ;
open (FILE, $status_file) ;
@filedata=<FILE> ;
$last_active=@filedata[0] ;
close (FILE) ;

if ($now-$last_active<60) {

$chatrequest="$data_basedir/$website/chats_requested.dat" ;

open (CHATREQUEST,">> $chatrequest") || die "Cannot open $chatrequest\n" ;
print CHATREQUEST "$FORM{requestor},$now,waiting\n" ;
close (CHATREQUEST);

$ENV{'QUERY_STRING'}="requestor=$FORM{requestor}&domain=$FORM{domain}" ;

exec "$HDDdir_executables/requestor_frames.pl" ;

} else {

print <<ENUF;
<HTML>
<TITLE>
ChatSign for $website
</TITLE>
<BODY>
<font face=\"Arial, Helvetica, sans-serif\" size=\"2\">
<CENTER>
<H3>Hello $FORM{requestor}</H3>
Unfortunately there are no operators for <B>$website</B> online at this time.
Please try again later or <A HREF="mailto:$default_emails{$website}">email $website</A>.


</BODY>
</HTML>
ENUF


$chatrequest="$data_basedir/$website/chats_requested.dat" ;

open (CHATREQUEST,">> $chatrequest")  || die "Cannot open $chatrequest\n" ;
print CHATREQUEST "$FORM{requestor},$now,missed\n" ;
close (CHATREQUEST);

($pretty_date, $pretty_time)=&TimeUtilities::make_unixtime_readable($now) ;

$mailprog = '/usr/lib/sendmail';

$recipient=$default_emails{$website} ;
$recipient="andrew\@qld.cc" ;
open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
print MAIL <<ENUF ;
From: missed\@chatsign.com (ChatSign Missed Chats)
To: $recipient 
Subject: You missed a chat with $FORM{requestor}

Dear $recipient         

$FORM{requestor} came to $website for a chat
at $pretty_date $pretty_time (EST Australia) 
but no operators were online.

This email is an automated message which is sent to you
whenever you miss a chat.


Regards
ChatSign


ENUF
close (MAIL) ;

$recipient="missed\@chatsign.com" ;
open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
print MAIL <<ENUF ;
From: missed\@chatsign.com (ChatSign Missed Chats)
To: $recipient
Subject: $website missed a chat with $FORM{requestor}

$FORM{requestor} came to $website for a chat
at $pretty_date $pretty_time (EST Australia)
but no operators were online.

Regards
ChatSign


ENUF
close (MAIL) ;
}

exit (0) ;

sub invalid_name {
print "<HTML><BODY><CENTER><H3>Invalid Name</H3>[$name]</BODY></HTML>" ;
exit (1) ;
}
