#!/afs/athena/contrib/perl/p
# $Id: comment,v 1.2 1994/07/21 18:38:20 nocturne Exp $

require 'ctime.pl';

print("Content-Type: text/html\n\n");

$query = join(' ', @ARGV) if $ARGV[0];
&do_comment($query);

# package comment;

### main part: decide what we're doing...

sub do_comment {
    local($query) = @_;
    $args =~ s/\+/ /g;

    if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $query, $ENV{'CONTENT_LENGTH'});
	do parse($query);
    } else {
	if (! $ENV{'SERVER_PROTOCOL'}) {
	    do old($query);
	} else {
	    do form();
	}
    }
}

### generate a form to be filled out
#
#   for more info, see
#http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html

sub form {
    # local($url) = $main'plexus{'server'}.'/'.@_[0];

    print <<"EndOfForm";
<title>Comments on WWW.MIT.EDU</title>
<h1>Comments on this server</h1>

Thank you for taking some time to comment on our server.  Just fill
out the form below, and click on the "Send..." button.  If you are not sure
who to send to, please send to webmaster@mit.edu.<p>

If you wish to remain anonymous, leave the E-mail address field blank.

<hr>

<FORM method="POST">
Your E-mail address: <input name="email" size=30><p>
Mail to: <select name="to">
	<option value="webmaster@mit.edu" selected> webmaster@mit.edu
	<option value="erichard@mit.edu"> erichard@mit.edu (Sports Server)
	<option value="mkgray@mit.edu"> mkgray@mit.edu (Web Wanderer)
	<option value="os2www@mit.edu"> os2www@mit.edu (OS/2 WWW page)
	<option value="sorokin@mit.edu"> sorokin@mit.edu (Women's resources)
</select><p>
Subject or URL: <input name="url" size=40><p>
<TEXTAREA name="body" rows=9 cols=60></TEXTAREA><p>
<input type="submit" value="Send in the comment">
<input type="reset" value="Naaaaah, just kidding">
</FORM>
<hr>
<ul>
<li><a href="/">SIPB Home Page</a>
<li><a href="services/sis/sports.html">Eric Richard's Sports Server</a>
<li><a href="people/mkgray/web-growth.html">Matthew Gray's Web Wanderer</a>
<li><a href="activities/os2/os2world.html">OS/2 WWW Page</a>
<li><a href="people/sorokin/women/index.html">Jessie Stickgold-Sarah's Women's resources page</a>
</ul>
EndOfForm
}

### remove \000-\039 and \177-\377

sub prot {
    @_[0] =~ s/[\000-\037\177-\377]+//g;
    @_[0];
}

### parsing comments
#
#   the comments are all packed in the form
#      field1=value1&field2=value2&...
#   with the "invalid" characters encoded as %-sequences.

sub parse {
    local(%post, *SM);
    local($tmp,$name);
    local($nil) = 'Anonymous User <nobody>';
    local($sm) = '/usr/lib/sendmail -t -fwebmaster@mit.edu';

    for (split(/&/,@_[0])) {
	if (/=/) {
	    $name = $`;
	    $tmp = $';
	    $tmp =~ s/\+/ /g;
	    $tmp =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;
	    $post{$name} = $tmp;
	}
    }

    open(SM, "|$sm");
    printf SM "From: %s\nSubject: comment -- %s\nTo: %s\n\n",
       ($post{'email'} =~ /\S/) ? &prot($post{'email'}) : $nil,
       &prot($post{'url'}),  $post{'to'};
    print SM $post{'body'},"\n";
    printf SM "\n(submitted by %s on %s)\n", $post{'email'}, $ENV{'REMOTE_ADDR'}
      if ($post{'email'} =~ /\S/);
    close(SM);

    unless ($?) {
	print "<title>Submission Receipt</title>\n";
	if ($post{'to'} eq "webmaster@mit.edu") {
           print "<h1>Thank you for your comments!</h1>\n";
           print "Please continue to let us know what you think is\n";
           print "particularly good or bad about our server.<p>\n";
	}
	else {
	   print "<h1>Your message has been sent!</h1>\n";
	}
        print "Comment as submitted:<p>\n<pre>\n";
	printf "Comment submitted to: %s\n", $post{'to'};
	printf "Your E-mail address: %s\n",
          ($post{'email'} =~ /\S/) ? &prot($post{'email'}) : "(anonymous)";
	printf "Subject:             %s\n\n", &prot($post{'url'});
	printf "Comment body:\n<hr>\n%s\n<hr></pre>\n", $post{'body'};
    } else {
	&main'report_error('internal_error', "sendmail failed with error $?");
    }
}

### compatibility with HTTP/0.9 and earlier
#   (just uses <isindex>)

sub old {
    local($args) = @_;

    print <<"EndOfStuff";
<title>Comments on WWW.MIT.EDU</title>
<h1>Your client is old...</h1>

Your client does not support HTTP/1.0, and therefore can't use the
form support interface.<p>

Please enter your comments in the Search keyword field.<p>

<isindex>
EndOfStuff

    if($args){
	local($date) = &ctime(time); chop($date);
	print "Thanks for your comments!!!\n";
	open(COMM, ">>/var/local/www/comments") ||
	    &error('internal_error',"Can't open comments file!");
	print COMM $ENV{'REMOTE_ADDR'}." $date $args\n";
	close(COMM);
    }
}

1;
