#!/usr/local/bin/perl

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

$request_method=$ENV{'REQUEST_METHOD'} ;

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

if ($request_method eq "GET") {
$query_string=$ENV{'QUERY_STRING'}
} 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 ;
}

$now=time ;

print <<ENUF;
<HTML>
<SCRIPT Language="JavaScript">
function make_and_focus(address) 
{
chatWindow=window.open('',address,'scrollbars=auto,resizable=yes,width=600,height=500')
chatWindow.focus(address) ;
return (true) ;
}
</SCRIPT>
<META HTTP-EQUIV="Refresh" CONTENT="30; URL=$URLdir_executables/operator_chats_waiting.pl?operator=$FORM{operator}">
<BODY>
<CENTER>
<TABLE BORDER=8 CELLSPACING=0 CELLPADDING=2>
<TR BGCOLOR=#eaeaea>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Requestor</TD>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Domain</TD>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Time Since Requested</TD>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Status</TD>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">Chat</TD></TR>
ENUF

$websites="$data_basedir/websites.dat" ;

open (PASSWD,$websites) || die "Cannot open $websites file\n" ;
while (<PASSWD>) {
	chomp ;
	($domain,$password,$default_email_address,$operators)=split(/,/) ;
	foreach $operator (split(/:/,$operators)) {
	if ($operator eq $FORM{operator}) {
		$FORM{domain}=$domain ;

# From here to !!!!!!!!!!! is the loop per domain

$chatrequest="$data_basedir/$FORM{domain}/chats_requested.dat" ;
open (CHATREQUEST,$chatrequest) || die "Cannot open $chatrequest\n" ;
while (<CHATREQUEST>) {
unless (/#/) {
	chomp ;
	($requestor,$time,$status)=split(/,/) ;
	$elapsed=$now-$time ;
	($pretty_elapsed)=&TimeUtilities::display_seconds_as_hhmmss($elapsed) ;
	if ($status eq "waiting") { 
		$sound++ ;
	} 

$domain_stripped="$FORM{domain}$requestor" ;
$domain_stripped=~ s/\W//g ;

	print <<ENUF;
<TR>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$requestor</TD>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\"><A HREF=\"http://$FORM{domain}\">$FORM{domain}</A></TD>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$pretty_elapsed (hours:mins:secs)</TD>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">$status</TD>
<TD><font face=\"Arial, Helvetica, sans-serif\" size=\"2\"><FORM onSubmit=\"return make_and_focus(\'$domain_stripped\')\" TARGET=\'$domain_stripped\' METHOD=POST ACTION=$URLdir_executables/domain_chat_frames.pl>
<INPUT TYPE=hidden NAME=requestor VALUE=$requestor>
<INPUT TYPE=hidden NAME=domain VALUE=$FORM{domain}>
<SCRIPT Language="JavaScript">
<!-- Hide
document.write("<INPUT TYPE=SUBMIT VALUE=Chat>")
//-->
</SCRIPT>
</FORM></TD></TR>
ENUF
}
}

close (CHATREQUEST);


# Now update data file to show domain still logged in

$status_file="$data_basedir/$FORM{domain}/status.dat" ;
open (STATUS,"> $status_file") || die "Cannot open $status_file\n" ;
print STATUS "$now\n\n" ;
close (STATUS) ;


# From here to !!!!!!!!!!! is the loop per domain
} # Loop here foreach operator
} # Loop if operator eq FROM{operator}
} # Loop while websites

if ($sound) { 
	print "<EMBED SRC=\"\/warn.wav\" AUTOSTART=TRUE HIDDEN=TRUE>" ; 
}

print <<ENUF;
</TABLE>
</BODY>
</HTML>
ENUF

exit (0) ;
