#!/bin/perl -- -*-perl-*-

# Define fairly-constants
$mailprog = '/usr/lib/sendmail';
#--------------------------------------------
$recipient = 'Distribution@vul-soft.demon.co.uk';
#--------------------------------------------
print "Content-type: text/html\n\n";
print "<Head><Title>Thank you</Title></Head>";
#---------------------------------------------------------------------------------
print "<Body><CENTER><H3>REQUEST TO BE INCLUDED ON THE RETAIL LIST</H3><br><hr>";
#--------------------------------------------------------------------------------
# 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;
    # 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;
}
# If the comments are blank, then give a "blank form" response
&blank_response unless $FORM{'shopname'};
# Now send mail to $recipient
#-------------------------------------------------------------------------------------
open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
print MAIL "This Shop wants to be added to your Retail list, better check if they do stock your titles!\n\n";
print MAIL  "------------------------------------------------------------\n";
print MAIL "Shop Name: $FORM{'shopname'}\n";
print MAIL "Contact Name: $FORM{'contname'}\n";
print MAIL "Address: $FORM{'add1'}\n";
print MAIL "Address: $FORM{'add2'}\n";
print MAIL "Address: $FORM{'add3'}\n";
print MAIL "Country: $FORM{'country'}\n";
print MAIL "Tel: $FORM{'tel'}\n";
print MAIL "Fax: $FORM{'fax'}\n";
print MAIL "Email: $FORM{'email'}\n";
print MAIL "Who Supplies Them: $FORM{'who'}\n";

print MAIL "\n------------------------------------------------------------\n";
print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n";
print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n";
print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
close (MAIL);
#-------------------------------------------------------------------------------------

# Make the person feel good for writing to us
print "Vulcan Conformation<br><hr>";
print "Thank you for your details, we will process the information swiftly.<br><hr>";
print "<A HREF=\"/\">Main Vulcan Page</A><br>";

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response
{
    print "Your shop name was blank and no request was sent to Vulcan.<br><hr>";
    print "<A HREF=\"/\">Main Vulcan Page</A><br>";
    exit;
}
