#!/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 ;

$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 ;
}

# Make sure they have entered an email address

$email_address=$FORM{requestor} ;
unless  ($email_address=~ /\@/) { &invalid_email_address ; }
unless  ($email_address=~ /(.*)\@(.*)\.(.*)/) { &invalid_email_address ; }
if  ($email_address=~ /![A-Za-z0-9_+-.]/) { &invalid_email_address ; }
if  ($email_address=~ / /) { &invalid_email_address ; }
unless  ($email_address) { &invalid_email_address ; }

# Work out who we are dealing with

# Make sure it's from a legit site

# First get the name of the refering site

$website=$ENV{'HTTP_REFERER'} ;
$referer=$website ;

$website=~ s/http\:\/\///g ;
($firstbit,$lastbit)=split(/\//,$website) ;
($website,$rest)=split(/\:/,$firstbit) ;
$website=~s /\.$// ;
$website=~tr /[A-Z]/[a-z]/ ;      

# Check they have the logo on their page

# Now get a list of legit sites
$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 ;
}

unless ($sites{$website} eq $website) {
print <<ENUF;
<HTML>
<SCRIPT Language="JavaScript">
function make_and_focus(address)
{
chatWindow=window.open('',address,'scrollbars=auto,resizable=yes,width=600,height=600')
chatWindow.focus(address) ;
return (true) ;
}
</SCRIPT>
<TITLE>
ChatSign
</TITLE>
<BODY>
<font face=\"Arial, Helvetica, sans-serif\" size=\"2\">
<CENTER>
<H3>Hello $FORM{requestor}</H3>

You have been directed to the live chat page
from an unlicenced website. [$website]

The licenced chat sites who might be able to 
help you are listed below -

ENUF
print "<TABLE BORDER=0>" ;
foreach $site (sort keys(%sites)) {
	print "<TR><TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\"><A HREF=\"http://$site\">$site</A></TD></TD>" ;
}
print "</TABLE>" ;

print <<ENUF;
</BODY>
</HTML>
ENUF
exit (0) ;
}


$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) ;

$requestor_stripped=$FORM{requestor} ;
$requestor_stripped=~ s/\W//g ;


if ($now-$last_active<60) {
print <<ENUF;
<HTML>
<META HTTP-EQUIV="Refresh" CONTENT="60; URL=$referer">
<HEAD>
<SCRIPT Language="JavaScript">
function make_and_focus(address)
{
chatWindow=window.open('',address,'scrollbars=no,resizable=yes,width=600,height=500')
chatWindow.focus(address) ;
return (true) ;
}
</SCRIPT>
<TITLE>
ChatSign - Welcome $FORM{requestor}
</TITLE>
</HEAD>
<BODY>
<font face=\"Arial, Helvetica, sans-serif\" size=\"2\">
<CENTER>
<H3>Hello and Welcome to <B>$website</B></H3>

<P>To use ChatSign you need a browser that runs <A HREF="$URLdir_executables/javascript.pl">JavaScript</A>.

<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from non JS Browsers

document.write("Your browser does.<P>Your request to chat has been logged and one of our trained operators is about to pick up your request and respond.<P>Please enter a name for yourself during the chat and click on the <B>Chat</B> button below to commence chatting.") ;


document.write("<FORM onSubmit=\\"return make_and_focus(\'$requestor_stripped\')\\" TARGET=\'$requestor_stripped\' ACTION=$URLdir_executables/requestor_frames.pl>") ;

document.write("<INPUT TYPE=text NAME=requestor VALUE=$FORM{requestor}>") ;
document.write("<INPUT TYPE=hidden NAME=requestor VALUE=$FORM{requestor}>") ;
document.write("<INPUT TYPE=hidden NAME=domain VALUE=$website>") ;
document.write("<INPUT TYPE=SUBMIT VALUE=Chat></FORM>") ;
//-->
</SCRIPT>

<!--
<FORM onSubmit="return make_and_focus(\'$requestor_stripped\')" TARGET=\'$requestor_stripped\' ACTION="$URLdir_executables/requestor_frames.pl">
<INPUT TYPE=text NAME=requestor VALUE=$FORM{requestor}>
<INPUT TYPE=hidden NAME=requestor VALUE=$FORM{requestor}>
<INPUT TYPE=hidden NAME=domain VALUE=$website>
<INPUT TYPE=SUBMIT VALUE=Chat></FORM>
-->

<HR NOSHADE>
<P><P>
<H3>About ChatSign</H3>
</CENTER>
<TABLE><TR><TD>
<IMG SRC="http://www.chatsign.com/img/small_cs_logo.gif" WIDTH=115 HEIGHT=40>
</TD><TD>
<font face=\"Arial, Helvetica, sans-serif\" size=\"2\">
<UL>
<LI>ChatSign is way of Website owners chatting live one-on-one with people such as your self who are interested in their site.
<LI>ChatSign is <FONT COLOR=red>free</FONT> for web site users 
<LI>ChatSign requires no special software.
<LI>Please go to <A HREF="http://www.ChatSign.com/">www.ChatSign.com</A> to learn more about what ChatSign is.
<LI>This page will automatically return to $website shortly.
</UL>
</TD></TR>
</TABLE>
</BODY>
</HTML>
ENUF

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

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

} 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} ;
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_email_address {
# print "<HTML><BODY><CENTER><H3>Invalid Email Address</H3>$email_address does not appear to be a valid email address</BODY></HTML>" ;
# exit (1) ;
}
