#! /srvtools/http/bin/perl
#############################################################################
#  
#   File:          PostMessage.pl
#
#   Authors:       Paul Coddington and Jungmin Kim
#
#   Version:       9-May-96
#
#   Input:         path name, file name, 
#                  title, message, (current message for feed back)
#                  hidden date, hidden name, hidden title, hidden message
#                  ( message to be responded ),
#                  user information
#          
#   Output:        HTML text with fill-out forms
#
#   Description:   Create fill-out forms to edit message.
#                  passed message (to be responded), and made message are
#                  passed to main function.
#                  passed message: path/filename
#                  new message:    path/(new file)
#               
#   Usage:         "http://.../WeBBoard/post:path:file:user"
#                  user is in the form "username:email_address".
#
#   5-Mar-95       Initial version
#   9-May-96       Changed user interface to reduce size (try to fit on 
#                  one page) and also to make it more obvious what the 
#                  3 choices are, by including the subject title.
#
#   To Do:         Somehow crop subject title so it doesn't take up 2
#                  more than one line?
#                  Tidy up the code by using a subroutine to convert
#                  special characters.
#
#############################################################################

sub PostMessage{
    local($path, $file, $title, $message, 
	  $hdate, $hname,  $htitle, $hmessage, $method, $user,
	  $initial, $hdate2, $hname2,  $htitle2, $hmessage2,
	  $subdir) = @_;

    # ****************************************************** modified
    local($pname, $pemail, $html_title, $html_message, 
	  $html_title2, $html_message2, $temp, $temp2,
	  $response, $comment, $newpost, $pathname );

    if ($method eq "" || $method eq "response") {
	$response = "CHECKED";
	$comment = $newpost = "";
    }
    elsif ($method eq "comment") {
	$comment = "CHECKED";
	$response = $newpost = "";
    }
    else {
	$newpost = "CHECKED";
	$response = $comment = "";
    }

    $html_title = $htitle;
    $html_message = $hmessage;
    $html_title2 = $htitle2;
    $html_message2 = $hmessage2;

    $temp = $html_title;	# original title of message to be responded
    $temp =~ s/\<P\>/ /ig;
    $temp =~ s/\<BR\>/ /ig;
    $temp2 = $html_title2;		
    $temp2 =~ s/\<P\>/ /ig;
    $temp2 =~ s/\<BR\>/ /ig;

    #### replace HTML reserved characters
    $htitle =~ s/&/&amp;/g;
    $htitle =~ s/>/&gt;/g;
    $htitle =~ s/</&lt;/g;
    $htitle =~ s/\"/&quot;/g;
    $hmessage =~ s/&/&amp;/g;
    $hmessage =~ s/>/&gt;/g;
    $hmessage =~ s/</&lt;/g;
    $hmessage =~ s/\"/&quot;/g;
    $htitle2 =~ s/&/&amp;/g;
    $htitle2 =~ s/>/&gt;/g;
    $htitle2 =~ s/</&lt;/g;
    $htitle2 =~ s/\"/&quot;/g;
    $hmessage2 =~ s/&/&amp;/g;
    $hmessage2 =~ s/>/&gt;/g;
    $hmessage2 =~ s/</&lt;/g;
    $hmessage2 =~ s/\"/&quot;/g;
    $title =~ s/&/&amp;/g;
    $title =~ s/>/&gt;/g;
    $title =~ s/</&lt;/g;
    $title =~ s/\"/&quot;/g;
    $message =~ s/&/&amp;/g;
    $message =~ s/>/&gt;/g;
    $message =~ s/</&lt;/g;
    $message =~ s/\"/&quot;/g;

# THIS COULD BE A BUG -- use different delimiter (other than colon)?
# It's currently not used is OK for the moment. (PDC)
    #### decode user information to get email address
    $_ = $user;
    s/\+/ /g;   
    # This isn't used anymore
    # ($pname, $pemail) = split(/:/, $_);

# Print out the HTML page
    print "Content-type\: text/html\n\n";
    print "\n";
    print "<HTML>\n";
    print "<HEAD>\n";
    print "<TITLE> Post a Message </TITLE>\n";
    print "</HEAD>\n";
    print "<BODY>\n";
    # print "<H1> Post a message </H1>\n";

    $pathname = $conference{$path};
    # print "<H2> to ${pathname} forum</H2>";
    # print "<IMG SRC=\"$gif/pointer.xbm\" ALIGN=\"left\" ALT=\"\">";
    # print "<P>\n";
    # print "<HR>\n";

    print "<A NAME=\"PostMessage\">\n";
    print "<FORM METHOD=\"POST\" ";
    print "ACTION=\"$action\/$subdir\:preview\:$path\:$file\:$user\">\n";
    print "<FONT SIZE=\"+1\"><B>Choose one of the following\:</B></FONT>\n";
    print "<BR>\n";
    print "<INPUT TYPE=\"radio\" NAME=\"method\" VALUE=\"newpost\" $newpost>";
    print "<FONT color=\"#AA0000\"><b> Create a new topic for the forum:</b>\n";
    print "</FONT>\n";
    print "<B>${pathname}</B>\n";
    print "<BR>\n";
    print "<INPUT TYPE=\"radio\" NAME=\"method\" VALUE=\"response\" $response>";
    print "<FONT color=\"00AA00\"><b> Respond to this topic: </b></FONT>";
if ($file eq $initial) { # Clicked on a topic
    print "$file. <A HREF=\"#$file\">$temp</A>\n";
}
else { # Clicked on a comment or response 
    print "$initial. <A HREF=\"#$initial\">$temp2</A>\n";
    print "<BR>\n";
    print "<INPUT TYPE=\"radio\" NAME=\"method\" VALUE=\"comment\" $comment>";
    print "<FONT color=\"0000AA\"><b> Comment on this response: </b></FONT>";
    print "$file. <A HREF=\"#$file\">$temp</A>\n";
}
#     print "<BR>\n";
#     print "<B>or <A HREF=\"$action/$subdir\:read\:$path\:$file\:$user#$file\">\n";
#    print "Cancel this post and return to the forum</A></B>\n";
    print "<P>\n";

    #### hidden fields to be passed

    print "<INPUT TYPE=\"hidden\" NAME=\"hdate\" VALUE=\"$hdate\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"hname\" VALUE=\"$hname\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"htitle\" VALUE=\"$htitle\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"hmessage\" VALUE=\"$hmessage\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"hdate2\" VALUE=\"$hdate2\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"hname2\" VALUE=\"$hname2\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"htitle2\" VALUE=\"$htitle2\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"hmessage2\" VALUE=\"$hmessage2\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"initial\" VALUE=\"$initial\">\n";


    #### generate fill-out forms
    
    print "<P>\n";
    print "<B>Type in a subject title for your message\: </B><BR>\n";
    print "<INPUT SIZE=72 TYPE=\"text\" NAME=\"title\" VALUE=\"$title\">\n";
    print "<P>\n";
    print "<B>Type in your message, ";
    print "then click on the \n";
    print "<INPUT TYPE=\"submit\" VALUE=\"SUBMIT\"> \n";
    print "button\:</B>\n";
    print "<TEXTAREA NAME=\"message\" ROWS=6 COLS=72>${message}</TEXTAREA><BR>\n";
    print "<P>\n";

    print "</FORM>";

if ($file ne $initial) {  # For a comment

    #### Include the initial (topic) message 
    print "<P><HR>\n";
    print "<A NAME=\"$initial\">\n";
    print "<H3>The current topic is\: </H3>\n";
    print "$initial <B>$html_title2</B>\n";
    print "from $hname2\n";
    print "on $hdate2.\n";
    print "<P>\n";
    print "$html_message2\n";
    print "<P>\n";

    # Help users get back to the top
    print "<HR>\n";
    print "<B><A HREF=\"#PostMessage\">Go back to the top of this page</A> \n";
    print "to add your response or comment.</B>\n";
    print "<BR><HR>\n";

    #### Include the (comment) message you're replying to
    print "<P>\n";
    print "<A NAME=\"$file\">\n";
    print "<H3>The current comment on this topic is\: </H3>\n";
    print "$file <B>$html_title</B>\n";
    print "from $hname\n";
    print "on $hdate.\n";
    print "<P>\n";
    print "$html_message\n";
    print "<P>\n";
    
}
else { # For a topic

    #### Include the initial (topic) message 
    print "<P><HR>\n";
    print "<A NAME=\"$file\">\n";
    print "<H3>The current topic is\: </H3>\n";
    print "$file <B>$html_title</B>\n";
    print "from <I><B>$hname</B></I>\n";
    print "on $hdate.\n";
    print "<P>\n";
    print "$html_message\n";
    print "<P>\n";
}

    # Help users get back to the top
    print "<HR>\n";
    print "<B><A HREF=\"#PostMessage\">Go back to the top of this page</A> \n";
    print "to add your response or comment.</B>\n";
    print "<BR><HR>\n";
    print "</BODY>\n";
    print "</HTML>\n";
}
1;
