#!/usr/bin/perl -- -*-perl-*-

# ------------------------------------------------------------
# Form-mail.pl, by Reuven M. Lerner (reuven@the-tech.mit.edu).
#
# Last updated: March 14, 1994
#
# Form-mail provides a mechanism by which users of a World-
# Wide Web browser may submit comments to the webmasters
# (or anyone else) at a site.  It should be compatible with
# any CGI-compatible HTTP server.
# 
# Please read the README file that came with this distribution
# for further details.
# ------------------------------------------------------------

# ------------------------------------------------------------
# This package is Copyright 1994 by The Tech. 

# Form-mail is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.

# Form-mail is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Form-mail; see the file COPYING.  If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# ------------------------------------------------------------

# 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 = 'www@dcs.exeter.ac.uk';

# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";

# 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 feedback are blank, then give a "blank form" response
&blank_response if $FORM{'changes'} eq "\n";

# Now send mail to $maintainer

# open (MAIL, "|$mailprog $FORM{'recipient'}") || die "Can't open $mailprog!\n";
# print MAIL "Subject: $FORM{'subject'}\n\n";
# print MAIL "$FORM{'sentfrom'} sent the following\n";
# print MAIL  "------------------------------------------------------------\n";
# print MAIL "$FORM{'changes'}";
# print MAIL "\n------------------------------------------------------------\n";
# close (MAIL);

# Confirm request sent
# Print a title and initial heading
print "<Head><Title>Thank you</Title></Head>";
print "<Body><H1>Thank you</H1>";
print "The message now been sent to $FORM{'recipient'}.<P>";

print "<P>Uids are $< and $>\n";

open(DEBUG, ">/tmp/gsw.debug") || die "Can't open /tmp/gsw.debug\n";
print DEBUG "Env is ",%ENV,"\n";
close(DEBUG);
#system "echo Hello from cgiscript > /tmp/gsw";
#system "/usr/bin/echo", "The environment is ", @ENV;
system "DISPLAY=csx0:0; export DISPLAY; /usr/local/bin/xrsh $ENV{'REMOTE_HOST'} /usr/local/bin/gspreview /home/gordon/finance/accounts/equip.ps";
#system "source /tmp/gsw.stdenv; printenv > /tmp/gsw; /usr/local/bin/gspreview";
#system "/usr/local/bin/gspreview > /tmp/gsw";
#system "echo $ENV{'DISPLAY'} > /tmp/gsw";

#system "/usr/openwin/bin/xcalc","-display","csx0:0";
#system "/usr/local/bin/gspreview","-display","csx0:0";

print "Ran gspreview with error message $! and number $?\n";

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response
{
    # Print a title and initial heading
    print "<Head><Title>Error</Title></Head>";
    print "<Body><H1>Error</H1>";
    print "Your changes appear to be blank, and thus were not sent ";
    print "to our the section maintainer. If you wish to try again, ";
    print "go back to the form on the previous page.<P>";
    exit;
}
