#!/usr/local/bin/perl -w

use strict;
use CGI qw(:standard);
require hnet;

my $output = new CGI;

my ($sub, $nexturl, $to, $user, $referer, $remote_addr, $remote_ident);

my $file = param('file');
my $num = param('num');

#####

my ($new, @message, %mes);

if ($file and $num) {

	my $list = "revolution";

	chdir "/storm/lib/logs/$list" || &end("Couldn't chdir! $!");

	my $i = 0;

	opendir(D,".");
	my @dirs = readdir(D);
	closedir(D);

	open(F, "$file") or &end("Can't open $file! $!");
	LOOP:
	while (<F>) {
		if (/^\={30}/) {
			$new = 1;
			next LOOP;
		} elsif ($new == 0) { next LOOP; }
		elsif ($new == 4) { 
			if (/^\s*From:\s+(.*)/) { 
				$mes{'From'} = $1; 
			} else { push(@message, $_); }
			next LOOP;
		}
		elsif (/^Date:\s+(.*)/ && ( $new == 1 ) ) {
			if ($i < $num) {
				$i++;
				$new = 0;
				next LOOP;
			} elsif ( $i > $num ) {
				last LOOP;
			}
			$mes{'Date'} = $1;
			$new = 2;
			$i++;
		} elsif ($new == 1) {
			$new = 0;
			next LOOP;
	#	} elsif (/^Subject:\s+(.*)/) {
	#		$mes{'Subject'} = $1;
		} elsif (/^From:\s+(.*)/) {
			$mes{'From'} = $1;
		} elsif (/^\s*$/) {
			$new = 4;
			next LOOP;
		}
	}
	close F;
}
#####

if (param('sub')) {$sub = param('sub'); } else { $sub = ""; }
$sub =~ s/"/&quot;/g;
if (param('nexturl')) {$nexturl = param('nexturl'); } else { $nexturl = ""; }

if (exists $ENV{'REMOTE_HOST'}) { $remote_addr = $ENV{'REMOTE_HOST'}; }
elsif (exists $ENV{'REMOTE_ADDR'}) { $remote_addr = $ENV{'REMOTE_ADDR'}; }
else { $remote_addr = ""; }

if (exists $ENV{'REMOTE_IDENT'}) { $remote_ident = $ENV{'REMOTE_IDENT'}; }
else { $remote_ident = ""; }

if (exists $ENV{'REMOTE_USER'}) { $user = $ENV{'REMOTE_USER'}; }
else { $user = $remote_ident . "\@" . $remote_addr; }
if (exists $ENV{'HTTP_REFERER'}) { $referer = $ENV{'HTTP_REFERER'}; }
else { $referer = ""; }

if (param('to')) { $to = param('to'); }
else { $to = ""; }

print header;
print <<EOP;
<HTML><HEAD><title>Thank You for Taking the Time to Respond</title></head>
<BODY BGCOLOR="#FFFFFF" BACKGROUND="../PAPER01M.GIF">
<FONT SIZE="+2">Thank You For Taking the Time to Participate!</font>
<UL>
<LI>We're glad to hear from you.  We require a valid e-mail address for
messages, so please make sure to fill in the From: line below.
<LI>After you have typed your message in the space provided, press the
<B>send</b> button at the bottom of the page.
</ul>
EOP

if (not param('quote') and param('ent')) {
	my $self = self_url;
	$self =~ s/&/&amp;/g;
	print <<HTML;
<p>You may <A HREF="$self&amp;quote=1">quote</a> the original message if you
wish.
<p>
HTML
}

print <<EOP;
<form method=POST action="mail2.cgi">
<input type=hidden name="host" value="">
<input type=hidden name="ref" value="$referer">
<input type=hidden name="to" value="$to">
<input type=hidden name="nexturl" value="$nexturl">
<input type=hidden name="from" value="$user">
<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>
<TR><TD><b>To:</b></td><TD><b>$to</b></td></tr>
<TR><TD><b>From (e-mail address required):</b></td><TD><input size=45 name="from_usr" value="$user"></td></tr>
<TR><TD><b>Subject:</b></td><TD><input size=45 name="subject" value="$sub"></td></tr>
<tr><td valign=top><b>Your Message:</b></td><td>
<textarea name="body" value="" rows=10 cols=80>
EOP

if (param('quote')) {
	print "On $mes{'Date'}, $mes{'From'} wrote:\n\n";
	foreach (@message) {
		print "> $_";
	}
}

print <<EOP;
</textarea></td></tr>
</table>
<input type=submit value="    Send     ">     
<input type=reset value="  Start Over    ">
</form>
</body></html>
EOP
