#!/usr/bin/perl

require 5.0;
use CGI;
require "fmt_time.pm";

$url_board = "index.cgi";
#$maintenance = "10:00";

$q = new CGI;

#$nick = $q->param (-name => 'nick') or $q->cookie (-name => 'nick');
#$email = $q->param (-name => 'email') or $q->cookie (-name => 'email');
#&register unless $nick;
#$cookie_nick = $q->cookie (-name => 'nick', -value => $nick, -expires => '+1m');
#$cookie_email = $q->cookie (-name => 'email', -value => $email, -expires => '+1m');

$chat_file = "chat_message";
$chat_file = $q->param (-name => 'chat') if $q->param (-name => 'chat');
$history = 0;
$history = $q->param (-name => 'hist') if $q->param (-name => 'hist');
$cookie_history = $q->cookie (-name => 'hist', -value => $history, -expires => '+10m');

$sort_mode = $q->cookie (-name => 'sort') if $q->cookie (-name => 'sort');
$sort_mode = $q->param ('sort') if $q->param ('sort');
$sort_mode = "timeN" unless $sort_mode =~ m![PN]$!;
$cookie_sort = $q->cookie (-name => 'sort', -value => $sort_mode, -expires => '+10m');

print $q->header (-type => 'text/html',
				  -pragma => 'no-cache',
				  -cookie => [$cookie_nick, $cookie_email, $cookie_sort]);

### LOAD CHAT
@chat = ();
open CHAT, "<$chat_file";
while ($item = <CHAT>)
{
	push @chat, [ split '\x1', $item ];
}
close CHAT;

### COMPUTE HISTORY
$history = 0 if $history < 0 || $history > $#chat;
$hist_prev = $history - 20;
$hist_prev = 0 if $hist_prev < 0;
$hist_next = $history + 20;
$hist_next = $#chat - 19 if $hist_next > $#chat - 19;
$hist_next = 0 if $hist_next < 0;
$hist_top = 0;
$hist_bottom = $#chat - 19;
$hist_bottom = 0 if $hist_bottom < 0;

###	HEAD
print "<html>\n";
print "<head>\n";
print "<title> ( message ) :.b.o.a.r.d. </title>\n";
print "<meta http-equiv=Content-Type content=text/html; charset=windows-1250>\n";
print "<script language='JavaScript' src='board.js'></script>\n";
print "<style type=text/css>\n";
print "<!--\n";
print "table { font-family: Verdana; font-size: 9pt }\n";
print ".cas { font-size: 80% }\n";
print ".red { color: #FF0000 }\n";
print ".gray { color: #AAAAAA }\n";
print ".hilite { color: #FFFFFF; background-color: #000000 }\n";
print ".gray { color: #AAAAAA }\n";
print ".w { font-size: 6pt; font-weight: bold }\n";
print ".w3 { font-size: 6pt; font-weight: bold; vertical-align: super }\n";
print "-->\n";
print "</style>\n";
print "</head>\n\n";

### PAGE BEGIN
print "<body background=../msg/gfx/back__.gif bgcolor=#FFFFFF link=#000000 vlink=#000000 alink=#000000 onload=\"window.defaultStatus='boarding ( message )';\">\n";
if ($maintenance)
{
	print "<h1>.b.o.a.r.d. maintenance - back until $maintenance</h1></body></html>";
	exit;
}

### TABLE BEGIN
print "<table width=100% border=0>\n";

print "\t<tr>\n";
print "\t\t<td width=90><span class=cas>[<a href='javascript:open_form(\"\", \"$chat_file\");' onmouseover=\"status='add message';return true;\">add message</a>]<br>";
print "[<a href='javascript:open_biig_form(\"\", \"$chat_file\");' onmouseover=\"status='add biiiiiiiiiig message - especially for mln =)';return true;\">add biig one</a>]</span></td>\n";
print "\t\t<td><span class=cas>[<a href='javascript:window.location.reload();' onmouseover=\"status='refresh board';return true;\">refresh board</a>]";
print " ( <a href='/' onmouseover=\"status='( message )';return true;\">message</a> )";
print " [<a href='mailto:moshe\@message.sk?subject=bbc' onmouseover=\"status='beta-board-comments';return true;\">beta-board-comments</a>]</span></td>\n";
print "\t\t<td align=right><span class=cas>\n";

print "\t\t\t[<a href=$url_board?sort=timeN&chat=$chat_file>";
print "<span class=hilite>" if $sort_mode eq "timeN";
print "-sort b";
print "</span>" if $sort_mode eq "timeN";
print "</a><a href=$url_board?sort=timeP&chat=$chat_file>";
print "<span class=hilite>" if $sort_mode eq "timeP";
print "y time+";
print "</span>" if $sort_mode eq "timeP";
print "</a>] \n";

print "\t\t\t[<a href=$url_board?sort=nickN&chat=$chat_file>";
print "<span class=hilite>" if $sort_mode eq "nickN";
print "-sort b";
print "</span>" if $sort_mode eq "nickN";
print "</a><a href=$url_board?sort=nickP&chat=$chat_file>";
print "<span class=hilite>" if $sort_mode eq "nickP";
print "y nick+";
print "</span>" if $sort_mode eq "nickP";
print "</a>] \n";

print "\t\t\t[<a href=$url_board?sort=subjN&chat=$chat_file>";
print "<span class=hilite>" if $sort_mode eq "subjN";
print "-sort b";
print "</span>" if $sort_mode eq "subjN";
print "</a><a href=$url_board?sort=subjP&chat=$chat_file>";
print "<span class=hilite>" if $sort_mode eq "subjP";
print "y subject+";
print "</span>" if $sort_mode eq "subjP";
print "</a>]\n";

print "\t\t\t<br>";

print "\t\t\t[ ";
print "<a href='$url_board?hist=$hist_top&chat=$chat_file'>&lt;&lt;$hist_top</a>" if $hist_top < $history;
print "<span class=gray>&lt;&lt;</span>" if $hist_top >= $history;
print " <a href='$url_board?hist=$hist_prev&chat=$chat_file'>&lt;$hist_prev</a>" if $hist_prev < $history;
print " <span class=gray>&lt;</span>" if $hist_prev >= $history;
print " history ";
print "<a href='$url_board?hist=$hist_next&chat=$chat_file'>$hist_next&gt;</a>" if $hist_next > $history;
print "<span class=gray>&gt;</span>" if $hist_next <= $history;
print " <a href='$url_board?hist=$hist_bottom&chat=$chat_file'>$hist_bottom&gt;&gt;</a>" if $hist_bottom > $history;
print " <span class=gray>&gt;&gt;</span>" if $hist_bottom <= $history;
print " ]";

print "\t\t</span></td></tr>\n";

print "\t<tr>\n";
print "\t\t<td></td>\n";
print "\t\t<td valign=top nowrap colspan=2>\n";
print "\t\t\t<hr noshade size=1>\n";
print "\t\t</td>\n";
print "\t</tr>\n";

### TABLE CONTENT
@chat = sort by_nick_p @chat if $sort_mode eq "nickP";
@chat = sort by_nick_n @chat if $sort_mode eq "nickN";
@chat = sort by_time_p @chat if $sort_mode eq "timeP";
@chat = sort by_time_n @chat if $sort_mode eq "timeN";
@chat = sort by_subject_p @chat if $sort_mode eq "subjP";
@chat = sort by_subject_n @chat if $sort_mode eq "subjN";

for ($i = $history; $i < $history + 20 && $i <= $#chat; $i++)
{
	($nick, $email, $www, $time, $subject, $msg) = @{ $chat[$i] };
	$msubject = "(message board) RE: $subject";
	$msubject =~ s! !%20!gi;
	$date = &get_date ($time);
	$time = &get_time ($time);

	print "\t<tr>\n";
	print "\t\t<td width=90 valign=top rowspan=2>\n";
	print "\t\t<a href=mailto:$email?subject=$msubject>$nick</a> \n" if $email;
	print "\t\t$nick \n" unless $email;
  print "\t\t<a href=\"javascript:open_www('$www')\" onmouseover=\"status='w3 -> $www';return true;\"><span class=w>w</span><span class=w3>3</span></a>\n" if $www;
  print "\t\t<br>\n";
	print "\t\t<span class=cas>$date<br>$time</span>\n";
	print "\t\t</td>\n";
	print "\t\t<td valign=top><a href=\"javascript:open_form('$subject', '$chat_file')\" onmouseover=\"status='reply';return true;\"><b>$subject</b></a></td>\n";
	print "\t</tr>\n";
	print "\t<tr>\n";
	print "\t\t<td valign=top colspan=2>\n";
	print "\t\t$msg\n";
	print "\t</td>\n";
	print "\t</tr>\n";
	print "\t<tr>\n";
	print "\t\t<td></td>\n";
	print "\t\t<td valign=top nowrap colspan=2>\n";
	print "\t\t\t<hr noshade size=1>\n";
	print "\t\t</td>\n";
	print "\t</tr>\n";

	$row++;
}

### HISTORY ON BOTTOM
print "\t<tr>\n";
print "\t\t<td width=90>&nbsp;</td>\n";
print "\t\t<td>&nbsp;</td>\n";
print "\t\t<td align=right><span class=cas>\n";
print "\t\t\t[ ";
print "<a href='$url_board?hist=$hist_top&chat=$chat_file'>&lt;&lt;$hist_top</a>" if $hist_top < $history;
print "<span class=gray>&lt;&lt;</span>" if $hist_top >= $history;
print " <a href='$url_board?hist=$hist_prev&chat=$chat_file'>&lt;$hist_prev</a>" if $hist_prev < $history;
print " <span class=gray>&lt;</span>" if $hist_prev >= $history;
print " history ";
print "<a href='$url_board?hist=$hist_next&chat=$chat_file'>$hist_next&gt;</a>" if $hist_next > $history;
print "<span class=gray>&gt;</span>" if $hist_next <= $history;
print " <a href='$url_board?hist=$hist_bottom&chat=$chat_file'>$hist_bottom&gt;&gt;</a>" if $hist_bottom > $history;
print " <span class=gray>&gt;&gt;</span>" if $hist_bottom <= $history;
print " ]";
print "\t\t</span></td></tr>\n";

###	TABLE END
print "</table>\n";
print "</body>\n";
print "</html>\n\n";


sub
by_nick_p
{
	lc @{$a}[0] cmp lc @{$b}[0] || @{$b}[3] <=> @{$a}[3];
}

sub
by_nick_n
{
	lc @{$b}[0] cmp lc @{$a}[0] || @{$b}[3] <=> @{$a}[3];
}

sub
by_time_p
{
	@{$a}[3] <=> @{$b}[3];
}

sub
by_time_n
{
	@{$b}[3] <=> @{$a}[3];
}

sub
by_subject_p
{
	$sa = lc @{$a}[4];
	$sb = lc @{$b}[4];
	$sa =~ s!^\s+(.+)\s+$!$1!gi;
	$sb =~ s!^\s+(.+)\s+$!$1!gi;
	$sa =~ s!^re(\[\d+\])?:\s+!!gi;
	$sb =~ s!^re(\[\d+\])?:\s+!!gi;

	$sa cmp $sb || @{$b}[3] <=> @{$a}[3];
}

sub
by_subject_n
{
	$sa = lc @{$a}[4];
	$sb = lc @{$b}[4];
	$sa =~ s!^\s+(.+)\s+$!$1!gi;
	$sb =~ s!^\s+(.+)\s+$!$1!gi;
	$sa =~ s!^re(\[\d+\])?:\s+!!gi;
	$sb =~ s!^re(\[\d+\])?:\s+!!gi;

	$sb cmp $sa || @{$b}[3] <=> @{$a}[3];
}

sub
register
{
	print $q->header (-type => 'text/html',
					  -pragma => 'no-cache');

	print "<html>\n";
	print "<head>\n";
	print "<title>m.e.s.s.a.g.e.:.b.o.a.r.d.:.r.e.g.i.s.t.r.a.t.i.o.n</title>\n";
	print "<meta http-equiv='Content-Type' content='text/html; charset=windows-1250'>\n";
	print "<style type='text/css'>\n";
	print "<!--\n";
	print "table {  font-family: verdana; font-size: 75%; font-weight: bold}\n";
	print "-->\n";
	print "</style></head>\n";
	print "\n";
	print "<body bgcolor='#FFFFFF'>\n";
	print "<form method='post' action='$url_board'>\n";
	print "\t<table border='0' width='361'>\n";
	print "\t\t<tr>\n";
	print "\t\t\t<td width='69'>nick</td>\n";
	print "\t\t\t<td valign='top' width='290'>\n";
	print "\t\t\t\t<input type='text' name='nick' size='40'>\n";
	print "\t\t\t</td>\n";
	print "\t\t</tr>\n";
	print "\t\t<tr>\n";
	print "\t\t\t<td width='69'>e-mail\n";
	print "\t\t\t</td>\n";
	print "\t\t\t<td valign='top' width='290'>\n";
	print "\t\t\t\t<input type='text' name='email' size='40'>\n";
	print "\t\t\t\t\n";
	print "\t\t\t</td>\n";
	print "\t\t</tr>\n";
	print "\t\t<tr>\n";
	print "\t\t\t<td valign='top' width='69'>&nbsp;</td>\n";
	print "\t\t\t<td valign='top' width='290'>\n";
	print "\t\t\t\t<div align='right'>\n";
	print "\t\t\t\t\t<input type='reset' name='Reset' value='Reset'>\n";
	print "\t\t\t\t\t<input type='submit' name='Submit' value='Submit'>\n";
	print "\t\t\t</div>\n";
	print "\t\t\t</td>\n";
	print "\t\t</tr>\n";
	print "\t</table>\n";
	print "\t\t\t\t\n";
	print "</form>\n";
	print "\n";
	print "</body>\n";
	print "</html>\n";

	exit 1;
}

