#!/usr/bin/perl
# Define Variables
$mailprog = '/usr/sbin/sendmail';
# tested on $mailprog = '/usr/lib/sendmail';
$date = `/bin/date`; chop ($date);
$file = '/home/smgames/public_html/echess/index.html';
$sendURL ='http://smiliegames.com/cgi-bin/chess-new2.pl';
$viewURL ='http://smiliegames.com/cgi-bin/chess-new2.pl';
$boundary ='----=_NextPart_000_0015_01BD5374.A51E9AC0';
$feedback='sales@smilie.com';
######################
# Necessary Fields in HTML Form:   
# recipient = specifies who mail is sent to
# username = specifies the remote users email address for replies
# realname = specifies the remote users real identity
# subject = specifies what you want the subject of your mail to be
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    # Stop people from using subshells to execute commands
    # Not a big deal when using sendmail, but very important
    # when using UCB mail (aka mailx).
    # $value =~ s/~!/ ~!/g;
    # Uncomment for debugging purposes
    # print "Setting $name to $value<P>";
    $FORM{$name} = $value;
}
# name value pairs have now been formed
#what is the method GET or POST
$method=$ENV{'REQUEST_METHOD'};
#which event - for reading or sending?
$event=$FORM{'event'};
if ($event eq 'send')

{

#send mail with chess attachment

 $FORM{'event'}='read';

# Print the Initial Output Heading



open (MAIL, "|$mailprog $FORM{'to'}") || die "Can't open $mailprog!\n";

print MAIL "From: $FORM{'from'}\r\n";

print MAIL "Reply-To: $FORM{'from'}\r\n";

print MAIL "To: $FORM{'to'}\r\n";

print MAIL "Subject: $FORM{'subject'}\r\n";

print MAIL "Content-type: multipart/mixed; boundary=\"$boundary\"\r\n";

print MAIL "MIME-Version: 1.0\r\n\r\n";

print MAIL "--$boundary\r\n";

print MAIL "Content-type: text/plain; charset=us-ascii\r\n\r\n";

print MAIL "$FORM{'message'}\r\n*********\r\n";

print MAIL "You have been sent a move using :)SMILIE's eChess\r\n\n";

print MAIL "Open this attachment using a browser such as Netscape Navigator or InternetExplorer\r\n";

print MAIL "Javascript MUST be enabled to play.\r\n";

print MAIL "If you have problems email:$feedback\r\n\r\n";

print MAIL "THANK YOU FOR PLAYING :)SMILIE'S eCHESS\r\n";

print MAIL "--$boundary\r\n";

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

print MAIL "Content-disposition: attachment; filename=\"chess.html\"\r\n\r\n";

&output_form_data_send(MAIL);

print MAIL "--$boundary--\r\n\r\n";

close(MAIL);

print "Location: http://www.smiliegames.com/echess/sent.html\n\n";

exit;

} 

else

{

#read page with form data

 $FORM{'event'}='send';

# Print the Initial Output Heading

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

#now output chess page

open(CHESS,$file);

@lines=<CHESS>;

foreach $line (@lines)

{

	if ($line =~ /<servlet/ )

	{

		print "<!-- start -->\n";

		if ($method =~ /GET/ )

		{

			print "<script language=\"javascript\">\n";

			print "<!-- >\n";

			print "if (compatible())displayForm(\'$sendURL\');\n";

			print "\/\/ -->\n";

			print "<\/script>\n";

		}

		else

		{

		&output_form_data_view();

		}

	}

# outputForm();

	elsif ($line=~ /<\/servlet>/)

	{

		print "<!-- end -->\n";

	}

	else

	{

		print $line;

	}

}

close(CHESS);

print "</body></html>";

}

#print "Content-type: text/html\n\n";
#print "<HTML><HEAD>";
#print "<TITLE>CGI Test</TITLE>";
#print "</HEAD>";
#print "<BODY><H2>I just wrote a web page using Perldinner3!</H2>";
#print "</BODY></HTML>";

sub output_form_data_send

{

	local($out,$arg2) = @_;

	print $out "<form action=\"$viewURL\" method=post>\r\n";

	foreach $name (keys %FORM)

	{

		$value=$FORM{$name};

		print $out "<input type=hidden name=\"$name\" value=\"$value\">\r\n";

	}

	if ($event eq "send")

	{

		print  $out "<center><input type=submit value=\"Click here to play\"></center>\r\n";

	}

	print $out "</form>\r\n";

}



sub output_form_data_view

{

	print "<form name=\"chessform\" action=\"$sendURL\" method=post>\n";

	foreach $name (keys %FORM)

	{

		$value=$FORM{$name};

		print "<input type=hidden name=\"$name\" value=\"$value\">\n";

	}

	print "</form>\n";

}
