#! /usr/local/bin/perl
#############################################################################
#  
#   File: vz-answer.pl
#
#   Author:  Paul Coddington
#
#   Version: 0.1  
#
#   Date: August 15, 1995
#
#   Input: CGI input ('QUERY_STRING') 
#          QUERY_STRING: name    
#                        email 
#                        answer 
#                        filename 
#
#   Output: Add "answer" to the end of the list in "filename"
#
#
#############################################################################
#  
#   MAIN FUNCTION
#
#############################################################################

%in = ();
&ParseArg(*in);			# parse arguments passed by http server
&AddAnswer();


#############################################################################
#  
#   SUBROUTINES
#
#############################################################################


sub AddAnswer {          

  $name = $in{'name'};
  $email = $in{'email'};
  $answer = $in{'answer'};
  $filename = $in{'filename'};
  
  if (!open(QUESTFILE, $filename) ) {
	&PrintHeader;
 	print "cannot open $filename";
 	return 0;
  }
  close(QUESTFILE);

#
# Do an "in place" edit on the answers file, i.e. copy $filename
# to $filename~ and create a new $filename with the answer appended
#
  $^I = "~"; 
  @ARGV = ($filename);
  $end = 0;
  while (<>) {    
    if (/<\/DL>/) {  # end of list of answers
      &Append;
      $end = 1;
    }
    if ((/<ADDRESS>/) && !($end)) {  # This is the first post
      print "<DL>\n";
      &Append;
      print "</DL>\n";
    }
    print;
  }

  &PrintHeader;
  if (!open(QUESTFILE, $filename) ) {
	&PrintHeader;
 	print "cannot open $filename";
 	return 0;
  }
  while (<QUESTFILE>) {    
    print;
  }
  
  return 1;

}



sub Append {

  # Add the new answer to the file 
  print "<P>\n";
  print "<DT><B>\n From ";
  if (email eq "") {
     print "$name ";
  } 
  else {
     print "<A HREF=\"mailto:$email\">$name</A>";
  } 
  $date = `date '+at %H:%M:%S on %m/%d/%y'`;
  chop $date;
  print " $date: \n";
  print "</B><P>\n";
  print "<DD>\n $answer \n";
}



##########################################################################

# Standard CGI interface stuff

sub PrintHeader {                      # Print out the Header for HTML
  print "Content-type: text/html\n\n";
  print "\n";
}


sub ParseArg {                        # reads the passed arguments
    local ($i, $loc, $key, $val);
    local (*in) = @_;

    if ($ENV{'REQUEST_METHOD'} eq "GET") {                      # Read in text
       $in = $ENV{'QUERY_STRING'};
    } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
      for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
        $in .=getc;
      }
    }

    @in = split(/&/,$in);

    foreach $i (0 .. $#in) {
      $in[$i] =~ s/\+/ /g;                          # Convert plus's to spaces
      $in[$i] =~ s/%(..)/pack("c",hex($1))/ge;
                                # Convert %XX from hex numbers to alphanumeric
      $loc = index($in[$i], "=");                  # Split onto key and value.
      $key = substr($in[$i],0,$loc);
      $val = substr($in[$i],$loc+1);
      $in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
      $in{$key} .= $val;
   }
}
1;
