#!/afs/athena/contrib/perl/p
# $Id: comment,v 1.5 1994/09/20 05:04:10 rei Exp rei $

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 (&badformclient()) {
	  &loserclient();
	}
    else {
	if ($ENV{'REQUEST_METHOD'} eq "POST") {
       	  read(STDIN, $query, $ENV{'CONTENT_LENGTH'});
	  &parse($query);
        } else {
	  if (! $ENV{'SERVER_PROTOCOL'}) {
	    &old($query);
	  }
	  else {
	    &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 {
    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.
Just remember that we can't reply to you if you don't tell us who you
are :-)

<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="eichin@mit.edu"> eichin@mit.edu (Callsign Gateway)
	<option value="erichard@mit.edu"> erichard@mit.edu (Sports Server)
	<option value="mkgray@mit.edu"> mkgray@mit.edu (Web Wanderer and Weather Gateway)
	<option value="os2www@mit.edu"> os2www@mit.edu (OS/2 WWW page)
	<option value="sorokin@mit.edu"> sorokin@mit.edu (Women's resources)
	<option value="rei@mit.edu"> rei@mit.edu (art, "The WWW for Dummies", and anything else)

</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
}
# Removed:
#	<option value="harrisj@mit.edu"> harrisj@mit.edu (Testing this Script)
### 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, $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",
       ($post{'email'} =~ /\S/) ? &prot($post{'email'}) : $nil,
       &prot($post{'url'}),  $post{'to'};
    print SM "X-WebClient: $ENV{'HTTP_USER_AGENT'}\n";
    print SM $post{'body'},"\n";

#Reverse resolve the machine name from the IP address:

@ipaddr = split(/\./, $ENV{'REMOTE_ADDR'});
$packipaddr = pack('C4', @ipaddr);
$realname = (gethostbyaddr($packipaddr,2))[0];
unless ($realname)
{
    $realname = "could not reverse-resolve name"
}



    printf SM "\n-----\nsubmitted by '%s' on %s (%s)\nEmail sent via the WWW comment gateway at %s:%s.\n\nWarning: the sender's name and address may be forged.\n", $post{'email'}, $ENV{'REMOTE_ADDR'}, $realname, $ENV{'SERVER_NAME'}, $ENV{'SERVER_PORT'}

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

sub badformclient {
  $_=$ENV{'HTTP_USER_AGENT'};
  if (/NCSA Mosaic [^\/]*\/2\.([0-9]+)/)
    {
	if ($1 < 2) { 
	  return 1;
	}
  }
  if (/MacMosaic/)
  {
    return 1;
  }
  if (/MacWeb/)
  {
    return 1;
  }
  return 0;
}

sub loserclient {
    print <<"EndOfStuff";
<TITLE>Incompatible Client Warning</TITLE>

<h1>Incompatible Client Warning</h1>

I'm sorry, but your web browsing client is incompatible with this
comment script. It might be a good idea to contact your site
administrator to see if there is a more recent version of your client
program.<P>

To be more specific, your web browser must support the <b>value</b>
tag in Forms to be compatible with this script.  Otherwise, your mail
will bounce. Known incompatible client software is:

<UL>
<LI>Mosaic for X, version 2.1 and earlier
<LI>Mac Mosaic, version 2alpha6 and earlier
<LI>MacWeb, version 1.00ALPHA2 and earlier
</UL>

If your client does support the <B>value</b> tag, and we have
erroneously filtered you out, send mail to webmaster@mit.edu, telling
us your client and version number so we can check it out. Thank
you.<P>

EndOfStuff

}

1;
