#!/usr/local/bin/perl

# John A. Koppes
# Systems Support Group
# Department of Computer Science
# Iowa State University

# modified by Curtis Clifton and Gary T. Leavens

# -------------------------------------------------------------
# You can run the script from the command line for testing as follows:
# ./email-pl.cgi area=<string> description=<string>
# -------------------------------------------------------------

# todo: seperate die function to print error to web page

# Define fairly-constants

$mailprog = '/usr/sbin/sendmail';
$recipient = 'leavens@cs.iastate.edu';

# where to send the user after processing the output
$FINALURL="../thanks.shtml";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# check the command line for offline debugging:
$buffer=join("&", @ARGV, $buffer) if ($#ARGV > -1);

@pairs = split(/&/, $buffer);

open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";

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 =~ 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;
}

print MAIL "Reply-To: cs541s\@cs.iastate.edu \n";
print MAIL "Subject: Com S 541 feedback: $FORM{area} \n\n";
print MAIL "$FORM{description}";
print MAIL "\n\n\n\n";

close(MAIL);
 

print "Location: $FINALURL\n\n";
