#!/usr/bin/perl  

print qq(Content-type: text/html\n\n);

use CGI;
$query       = new CGI;
$error       = $query->param('error');
$replyto     = $query->param('replyto');
$action      = $query->param('action');
$head     = "parts/header.html";
$foot     = "parts/footer.html";
$admmail          = "www\@mordor.nazgul.com";
$ipaddr	= $ENV{'REMOTE_ADDR'};

$doc_root = "./";

open(HEAD, "$head") || print "$head $!";
open(FOOT, "$foot") || print "$foot $!";

print <HEAD>;


$reply_file  = "./" . $action . "\/archive/" . $replyto . "\.archive";

if ($file) {
	open(FILE, "$file") || print "Can't open $file, $!";
	$query   = new CGI(FILE);
	close(FILE);
}

if ($replyto) {
	open(FILE, "$reply_file") || print "Can't open $reply_file, $!";
	$query   = new CGI(FILE);
	close(FILE);

	$width       = (1); # right offset for table
	$time        = time;
	$action      = $query->param('action');
	$area        = $query->param('action');
	$rname       = $query->param('name');
	$dt          = $query->param('id');
	$r_date      = localtime($dt);
	$area        = $query->param('action');
	$id          = $time;
#	$reply       = $query->param('reply');
	$title       = $query->param('title');
	$body        = $query->param('body');
	$o_thread    = $query->param('o_thread');
} else {
	$width       = (1); # right offset for table
	$time        = time;
	$action      = $query->param('action');
	$area        = $query->param('action');
	$type        = $query->param('type');
	$id          = $query->param('id');
	$id          = $time if (!$id);
	$title       = $query->param('title');
	$url         = $query->param('url');
	$name        = $query->param('name');
	$reply       = $query->param('reply');
	$email       = $query->param('email');
	$quote       = $query->param('quote');
	$author      = $query->param('author');
	$body        = $query->param('body');
	$o_thread    = $id;
	$o_thread    = $query->param('o_thread') if ($error);
}

$head        = "/home/dbb/html/header.html";
$foot        = "/home/dbb/html/footer.html";

open(HEAD, "$head");
open(FOOT, "$foot");

print <HEAD>;
&form;
print <FOOT>;

# subs
#--------------------------------------------------------------

sub form {

	&fix_quote($title,$url,$name,$email,$quote,$author,$body);

	print qq(<H1>communication forum</H1>);
	if ($replyto) {
		print qq(<FONT FACE="Helvetica, Arial" SIZE=4>reply to: <I>$title</I></FONT><BR>);
	} else {
	
	}

	print qq(<FORM ACTION="write.cgi" METHOD=POST>
			 <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=3>
			 <TR>
			 <TD>
			 <B><FONT FACE="Helvetica, Arial">status:</B></FONT>
			 </TD><TD>
			 );
	if ($replyto) {
		print qq(<FONT FACE="Helvetica, Arial">new message/<B>reply</B></FONT>);
	} else {
		print qq(<FONT FACE="Helvetica, Arial"><B>new message</B>/reply</FONT>);
	}
	print qq(
			 </TD></TR>
			 <TR>
			 <TD VALIGN=top><FONT FACE="Helvetica, Arial">
			 <B>name:</B>
			 </FONT></TD><TD>
			 <P><INPUT TYPE="text" NAME="name" VALUE="$name" SIZE=50>
			 </TD></TR>
			 <TR>
			 <TD VALIGN=top><FONT FACE="Helvetica, Arial">
			 <B>email:</B>
			 </FONT></TD><TD>
			 <P><INPUT TYPE="text" NAME="email" VALUE="$email" SIZE=50>
			 </TD></TR>);
#			 <TR>
#			 <TD VALIGN=top><FONT FACE="Helvetica, Arial">
#			 <B>URL (Optional):</B>
#			 </FONT></TD><TD>);
#			 <P><INPUT TYPE="text" NAME="url" VALUE="$url" SIZE=50>
	print qq(</TD></TR>
			 <TR>
			 <TD VALIGN=top><FONT FACE="Helvetica, Arial">
			 <B>Title (of message):</B>
			 </FONT></TD><TD>
			 <P><INPUT TYPE="text" NAME="title" VALUE=");
     if ($replyto) {
          $rtitle = "re:" . $title;
          print qq($rtitle);
     } else {
          print qq($title);
	 }
     print qq(" SIZE=50>
			 </TD></TR>
			 <TR>
			 <TD VALIGN=top>
			 <FONT FACE="Helvetica, Arial"><B>message:</B></FONT>
			 </TD><TD>
			 <P><TEXTAREA NAME="body" ROWS=7 COLS=50 WRAP=hard>);
	$quote = "\nOn " . $r_date . " " . $rname . " wrote: \n";

	if($replyto) {
		print qq($quote);
		@body = split(/\n/, $body);
		foreach $line (@body) {
			$reply_line = "\> " . $line . "\n";
			print qq($reply_line);
		}
	} else {
		print qq($body);
	}
    print qq(</TEXTAREA>
			 </TD></TR>
			 <TR><TD></TD><TD align=center><INPUT TYPE="SUBMIT" VALUE="Post Message"> 
			 <INPUT TYPE="reset" VALUE="Clear Message"></TD></TR>
			 
			 <INPUT TYPE="HIDDEN" NAME="action" VALUE="$action">
			 <INPUT TYPE="HIDDEN" NAME="id" VALUE="$id">
			 <INPUT TYPE="HIDDEN" NAME="o_thread" VALUE="$o_thread">);

			 if ($reply) {
				 print qq( <INPUT TYPE="HIDDEN" NAME="reply" VALUE="$reply">);
			 } else {
				 print qq( <INPUT TYPE="HIDDEN" NAME="reply" VALUE="$replyto">);
			 }
	print qq(</TABLE></P>
			 </FORM>
			 );
}


sub fix_quote {
	foreach(@_) {
            $_ =~ s/"/\&\#168\;/g;
    }
}




























