#!/opt/bin/perl
# -- -*-perl-*-

# ------------------------------------------------------------

# Define fairly-constants

# This should match the mail program on your system.
$mailprog = '/usr/lib/sendmail';

# This should be set to the username or alias that runs your
# WWW server.
$recipient = "fundesa\@mail.auburn.edu";

# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";
# Print a title and initial heading
print "<Head><Title>Thank you</Title></Head>";
print "<Body><H1>Thank you</H1>";

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


{
# Now send mail to $recipient

open (MAIL, "|$mailprog -f $FORM{'email'} $recipient") || die "Can't open 
$mailprog!\n"; 
print MAIL "To: $recipient\n";
print MAIL "From: $FORM{'email'}\n";
print MAIL "Subject: $FORM{'subject'}\n\n";
print MAIL "$FORM{'email'} ($FORM{'name'}) sent the following\n"; 
print MAIL "------------------------------------------------\n\n";
print MAIL "Favorite part: $FORM{'favorite'}\n"; 
print MAIL "Comments: $FORM{'comments'}\n"; 
print MAIL "------------------------------------------------\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 "<p>Your comments have been registered.";
print "<p>Thank you for writing.";

$FORM{'retdesc'} =~ tr/_/ /;

if ($FORM{'returl'} ne "")
  { 
    print "<A HREF=\"$FORM{'returl'}\"> $FORM{'retdesc'}</A>.</p>";
  }
else
  {
    print "<A HREF=\"/users/$username\"> go back.</A></p>";
  }
}

# ------------------------------------------------------------
