#!/usr/local/bin/perl5.00503

$comfile = "/home/toyoda/WWW/.cgi-bin/maplot-bbs.log";
$lastN = 10;

$cgibin="/home/toyoda/WWW/.cgi-bin";
$mapath="/home/toyoda/WWW/maplot";

$hostaddr = $ENV{"REMOTE_ADDR"};
$useragent = $ENV{"HTTP_USER_AGENT"};

$useragent =~ s/ /;/g;
$useragent =~ s/　/;/g;

$time = time;

$spam{"58.98.98.220"} = 1;
$spam{"60.34.10.52"} = 1;
$spam{"60.34.242.160"} = 1;
$spam{"61.45.95.223"} = 1;
$spam{"210.17.155.175"} = 1;
$spam{"210.17.203.126"} = 1;
$spam{"210.17.206.88"} = 1;
$spam{"210.173.225.47"} = 1;
$spam{"218.47.215.59"} = 1;
$spam{"219.73.4.194"} = 1;
$spam{"219.73.71.203"} = 1;
$spam{"219.73.92.168"} = 1;
$spam{"219.78.190.75"} = 1;
$spam{"219.78.190.118"} = 1;
$spam{"219.78.190.141"} = 1;
$spam{"219.78.191.204"} = 1;
$spam{"219.78.191.241"} = 1;
$spam{"219.79.180.108"} = 1;
$spam{"219.111.127.101"} = 1;
$spam{"219.121.77.141"} = 1;
$spam{"219.121.78.71"} = 1;

if ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN, $alldata, $ENV{'CONTENT_LENGTH'});
} else {
    $alldata = $ENV{'QUERY_STRING'};
}

if ($alldata ne "") {
    foreach $data (split(/&/, $alldata)) {
	($key, $val) = split(/=/, $data);
	$val =~ tr/+/ /;
	$val =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("C", hex($1))/eg;
	$param{$key} = $val;
    }
}

$name = $param{"name"};
$comment = $param{"com"};
$compos = $param{"compos"};

$name =~ s/ //g;
$name =~ s/　//g;
$name =~ s/\n//g;

$comment =~ s/ //g;
$comment =~ s/　//g;
$comment =~ s/\n//g;

if (defined($spam{$hostaddr})) {
  print "Content-Type: text/html\n\n";
  exit;
}

if (defined($comment) && $comment ne "") {
    open(TAIL, "tail -10r $comfile |");
    while (defined($line = <TAIL>)) {
	my ($lt, $ha, $name, $cmt) = split(" ", $line);
	if ($comment eq $cmt) {
	    print "Content-Type: text/html\n\n";
	    print "同じコメントの投稿はしばらくできません。\n";
	    close(TAIL);
	    exit;
	}
    }
    close(TAIL);
}

open(COMFILE, ">>$comfile");
flock(COMFILE,2);

if (!($comment eq "")) {
  print COMFILE "$time $hostaddr $name $comment $useragent\n";
}

flock(COMFILE,8);
close(COMFILE);

unless (defined($compos)) { $compos = 0; }

open(FILE, "tail -r $comfile | tail +$compos |");
$Ncom = 0;
$MaxCom = 10;
while (defined($line = <FILE>) && $Ncom < $MaxCom) { # new to old
  chomp($line);
  my ($lt, $ha, $name, $cmt, $ua) = split(" ", $line);
  ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($lt);
  $mon++;
  $disptime = sprintf("%d/%d %02d:%02d", $mon, $mday, $hour, $min);
  
  $comarry[$Ncom] = "$disptime <font color=\"#0000ff\">$name</font><br>$cmt<hr>";
  ++$Ncom;
}
close(FILE);

print "Content-Type: text/html\n\n";

print "感想・要望・雑談BBS<br>\n";

print "<hr>\n";
print "<form method=\"POST\" action=\"http://www.tkl.iis.u-tokyo.ac.jp/~toyoda/cgi-bin/maplot-bbs-3.cgi\">\n";
print "名前:<input type=\"text\" name=\"name\" value=\"\" size=10><br>\n";
print "コメント:<input type=\"text\" name=\"com\" value=\"\" size=10><br>\n";
print "<input type=\"hidden\" name=\"compos\" value=\"0\">\n";
print "<input type=\"submit\" value=\"書き込み\"><br>\n";
print "</form><hr>\n";

for ($i = 0; $i < $Ncom; ++$i) { # new to old
    $str = $comarry[$i];
    print "$str\n";
}
$prev = $compos + $Ncom;
$next = $compos - $Ncom;
if ($next < 0) { $next = 0; }

print "<form method=\"POST\" action=\"http://www.tkl.iis.u-tokyo.ac.jp/~toyoda/cgi-bin/maplot-bbs-3.cgi\">\n";
print "<input type=\"hidden\" name=\"compos\" value=\"$prev\">\n";
print "<input type=\"submit\" value=\"前${MaxCom}\">\n";
print "</form>\n";

print "<form method=\"POST\" action=\"http://www.tkl.iis.u-tokyo.ac.jp/~toyoda/cgi-bin/maplot-bbs-3.cgi\">\n";
print "<input type=\"hidden\" name=\"compos\" value=\"$next\">\n";
print "<input type=\"submit\" value=\"次${MaxCom}\">\n";
print "</form>\n";

exit;
