#!/usr/bin/perl

require "process.cgi";
require "support.cgi";
require "parseform2.lib";

&Parse_form;

$address = "http://www.flwgc.org/chat-bin/chat.cgi"; #PUT PATH TO CHAT HERE

$title = "Front Line Wargame Club Chatroom";

$logfile = "files/a.txt";	#where the login information is stored
$countfile = "files/b.txt";	#stores information used by the counter
$postfile = "files/c.txt";	#contains all the posts used for the main chat screen
$banfile = "files/e.txt";	#list of banned IPs
$whenfile = "files/f.txt";	#file that keeps track of last login times
$ipfile = "files/g.txt";	#list of IPs
$whispers = "whispers";		#folder where whispers are stored

@foul = ('fuck', 'bitch', 'dick', 'shit');	#restricted words

$censor = 0;		#Foul language censoring in regular posts
			#0 means censoring is OFF (default)
			#1 means censoring is ON

$allowhtml = 0;		#whether to allow HTML tags in the chat posts
			#0 means HTML is OFF (default)
			#1 means HTML is ON

$seelist = 2;		#determines what ranks can see the online list
			#see below for list of possible ranks

$basestatus = 0;	#the status someone receives after registering a new nickname
			#0 means newbie (complete with newbie restrictions) (default)
			#1 means regular user
			#2 means power user
			#3 means moderator
			#4 means administrator

#You do not need to modify anything below this line

$login = $formdata {'login'};
$color = $formdata {'color'};
$rank = $formdata {'rank'};
$r = $formdata{'refresh'};

$action = $formdata{'action'};
$post = $formdata {'postdata'};
$conference = $formdata {'conference'};

print "Content-type:text/html\n\n";

print "<HTML><head><title></title>";

if ($action eq 'show')
{
	&printPost;
	&processCounter;
	print "<script language=\"Javascript\">";
	print "parent.counter.location.href=\"$address?action=counter\";";
	print "</script>";
}

if ($action eq 'process')
{
	&processPost;
	print "<script language=\"Javascript\">";
	print "parent.datain.location.href =\"$address?action=get&login=$login&color=$color&rank=$rank&conference=$conference&refresh=$r\";";
	print "parent.whispers.location.href=\"$address?action=whisper&login=$login&refresh=$r\";";
	print "</script>";
	&printPost;
}

if ($action eq 'get')
{
	&getPost;
}

if ($action eq 'whisper')
{
	&printWhispers;
}

if ($action eq 'counter')
{
	&showCount;
}

if ($action eq '')
{
	open (INFILE, "<ban.txt");
	@info = <INFILE>;
	close (INFILE);
	foreach $item (@info)
	{
		if ($item eq $ENV{'REMOTE_ADDR'})
		{
			$exists = 1;
		}
	}
	if ($exists)
	{
		&kicked;
	}
	else
	{
		&start;
	}
}

if ($action eq 'verify')
{
	&verify;
}

if ($action eq 'newoptions')
{
	&optionChange;
	&start;
}

if ($action eq 'options')
{
	&options;
}

if ($action eq 'register')
{
	&registration;
}

if ($action eq 'regprocess')
{
	&regProcess;
	if ($ok)
	{
		&start;
	}
}

print "</body></HTML>";

sub printPost
{
	print << "end HTML";

	<meta HTTP-EQUIV="refresh" CONTENT="$r; URL=$address?action=show&login=$login&refresh=$r">
	<meta HTTP-EQUIV="expires" CONTENT="0">
	<meta HTTT-EQUIV="pragma" CONTENT="no-cache">
	</head>
	<body bgcolor="#000000" text="#FFFFFF">

end HTML

	if ($rank > ($seelist-1))
	{
		open (INFILE, "$countfile");
		@items = <INFILE>;
		close (INFILE);
		print "<div style=\"float:right;float:right; padding-left:3%; padding-bottom:3%;\">Online:<p>";
		foreach $thing (@items)
		{
			$thing =~ s/:.*/ /;
			print "$thing<br>";
		}
		print "</div>";
	}
	open (INFILE, "<$postfile");
	@posts = <INFILE>;
	close (INFILE);
	print "@posts";
}

sub getPost
{
	print << "end HTML";
	
	</head>
	<body bgcolor="#000000" text="#FFFFFF" onLoad="self.focus();document.enter.postdata.focus()">

	
<form action="$address?action=process&login=$login&color=$color&rank=$rank&refresh=$r" method=POST name="enter" target="display">
	
	<input type="text" name="postdata" size="40">
	<input type="submit" value="Post!">
	<input type="button" value="Options" onClick="parent.location.href ='$address?action=options&login=$login&color=$color&rank=$rank&conference=$conference&refresh=$r'">
	<input type="button" value="Help" onClick="window.open('faq.html')">
	<font size=2>Conference:</font>
	<input type="text" name="conference" value="$conference" size="15"></form></body></HTML>

end HTML

}

sub printWhispers
{
	print << "end HTML";

	<meta HTTP-EQUIV="refresh" CONTENT="$r; URL=$address?action=whisper&login=$login&refresh=$r">
	<meta HTTP-EQUIV="expires" CONTENT="0">
	<meta HTTT-EQUIV="pragma" CONTENT="no-cache">
	</head>
	<body bgcolor="#000000" text="#FFFFFF">

end HTML

	open (INFILE, "$whispers/$login.txt");
	@items = <INFILE>;
	close (INFILE);
	print (@items);	
}

sub processPost
{
	@it = split (/ /, $post);
	if (($it[0] eq '/w') || ($it[0] eq '/msg') || ($it[0] eq '/n'))
	{
		open (INFILE, "<$logfile");
		@handles = <INFILE>;
		close (INFILE);
		$exists = 0;
		foreach $handle (@handles)
		{
			@check = split (/:/, $handle);
			if ($check[0] eq $it[1])
			{
				$exists = 1;
			}
		}
		if ($exists == 1)
		{
			shift (@it);
			$target = shift (@it);
			if ($post =~ /http:\/\/.*/)
			{
				foreach $thing (@it)
				{
					if ($thing =~ /http:\/\/.*/)
					{
						$thing = "<a href=\"$thing\" target=\"_blank\">$thing</a>";
					}
				}
			}
			$neww = "<b><i>$login: <font color=\"$color\">@it</font></i></b><br>";
			$newself = "<b>$login -> $target: <font color=\"$color\">@it</font></b><br>";
			&whispers($target, $neww);
			&whispers($login, $newself);
		}
		else
		{
			&whispers($login, "Error: $it[1] does not exist<br>");
		}
	}
	elsif ($it[0] eq '/nick')
	{
		&nickChange;
	}
	elsif ($it[0] eq '/clear')
	{
		&clear;
	}
	elsif ($it[0] eq '/promote')
	{
		&promote;
	}
	elsif ($it[0] eq '/cls')
	{
		&clearScreen;
	}
	elsif ($it[0] eq '/kick')
	{
		&kick;
	}
	elsif ($it[0] eq '/protect')
	{
		&protected;
	}
	elsif ($it[0] eq '/ip')
	{
		&showip;
	}
	elsif ($it[0] eq '/ban')
	{
		&ban;
	}
	elsif ($conference)
	{
		if ($it[0] ne '>')
		{
			@targets = split (/ /, $conference);
			$neww = "<b><i>$login: <font color=\"$color\">@it</font></i></b><br>";
			$newself = "<b>$login -> @targets: <font color=\"$color\">@it</font></b><br>";
			foreach $target (@targets)
			{
				&whispers($target, $neww);
			}
			&whispers($login, $newself);
		}
		else
		{
			$post =~ s/^> //;
			if (($post =~ /^\//) && !($post =~ /^\/me /) && !($post =~ /^\/a /) && !($post =~ /^\/sys /))
			{
				&processPost;
			}
			else
			{
				&makePost;
			}
		}
	}
	else
	{
		if ($rank == 0)
		{
			foreach $thing (@foul)
			{
				if ($post =~ /$thing/)
				{
					&kicked;
				}
			}
		}
		if (@it)
		{
			&makePost;
		}
	}
}
