#!/usr/local/bin/perl
# Forums v2.0.0, a bbs program for Linux and other *nix written in Perl.

require "ctime.pl";

# ------------- tuneable parameters go here....configure for your system

$HOME="/home/shaggy/forums/vox";
$MODFILE="$HOME/modfile"; # the file to replace moderated posts with. 
$DELFILE="$HOME/delfile";
$EDITOR="$HOME/pico";   # the default editor.  if a user sets their env var
			# BBS_EDIT it will use it instead.
$MAXAGE=10; # time in days a thread will live.
$PAGER="$HOME/less -eCmw";
$WELCOMEPAGE="$HOME/welcome"; # file shown at login
$GOODBYEPAGE="$HOME/goodbye"; # file shown at logout
$CHMOD=0664; # certain files are group writeable. use 0666 for world writeable.
	     # forums -needs- these files to be group or world writeable.
$GROUP="500";	 # this is the group who has the write permission. use the 
		 # guid number, not the name.  see $SETGROUP below.
$SETGROUP=1; 	 # 1 = force all user's posts to $GROUP's group's ownership. 
		 # 0 = do not. doing this is a good idea -- if a user with the 
		 # wrong group uses forums, they will screw things up.  if 
		 # files are world writeable this is not a problem -- set to 0.	

$SYSADMIN="shaggy";  # person who gets mail notification of errors and such.

$THREADED=1;     # 1= threads  0= no threads.

#---------------end of tuneable parameters---------------------------



if ($ENV{"BBS_EDIT"} ne "")
{
	$EDITOR=$ENV{"BBS_EDIT"};
}

$MAXAGE=$MAXAGE*86400; # convert to seconds.
&read_whoami;
&read_list_of_forums;
&register_in_wholist;

if ($SETGROUP)
{
	$uid=(getpwnam($whoiam))[2];
}

$a=$ENV{"HOME"}."/.jfile";
if (-e $a)
{
	&update_jfile;
}
else
{
	&make_new_user_jfile;
} 


&welcome();
unless (fork)
{
	exec("clear");
}
wait;

# here is the main routine of the forums program. it is a loop that sits
# in the lobby waiting for input. it has a handy help command. 
while (1)
{
	if (-s "$HOME/who/.$whoiam")
	{
		print "\nMessage(s) waiting\n";
	}
	print "lobby>";
	$a=<STDIN>;
	chop $a;
	$b=substr($a,0,1);
	if ($b eq "r")
	{
		 &read_a_forum($a); 
	}
	elsif ($b eq "n")
	{
		&new_scan;
	}
	elsif ($b eq "l")
	{
		if ($THREADED) { &list_forums_threaded; }
		else { &list_forums_unthreaded; }
	}
	elsif ($b eq "j")
	{
		&join_forum($a);
	}
	elsif ($b eq "u")
	{
		&unjoin_forum($a);
	}
	elsif ($b eq "w")
	{
		&who_is_on;
	}
	elsif ($b eq "m")
	{
		&message_user($a);
	}
	elsif ($b eq "v")
	{
		&view_messages();
	}
	elsif ($b eq "s")
	{
		&show_jlist();
	}
	elsif ($b eq "h" || $b eq "?")
	{
		&help_the_poor_fool;
	}
	elsif ($b eq "c")
	{
		unless (fork)
		{
			exec("clear");
		}
		wait;
	}
	elsif ($b eq "k")
	{
		&catch_all_forums();
	}
	elsif ($b eq "d")
	{
		unless (fork)
		{
			print "system time is : ".`date +%A" "%B" "%e" "%r" "%Y" "%Z`;
		}
		wait;
	}
	elsif ($b eq "a")
	{
		print "this will mail the system administrator.\n";
		print "typical messages will be errors encountered, suggestions, or the like.\n";
		print "type a subject, and a message followed by a \".\" on a line by itself to end.\n";
		unless (fork)
		{
			exec ("mail $SYSADMIN");
		}
		wait;
	}
	elsif ($b eq 'p')
	{
		unless (fork)
		{
			exec ("passwd");
		}
		wait;
	}
	elsif ($b eq "e")
	{
		$sigfile=$ENV{"HOME"}."/.forumsig";
		unless (fork)
		{
			exec ("$EDITOR $sigfile");
		}
		wait;
	}
	elsif ($b eq "q")
	{
		if (-s "$HOME/who/.$whoiam")
		{
			print "\nMessages waiting...read now or quit? (R/q)";
			$answer=<STDIN>;
			chop($answer);
			if ($answer ne "q")
			{
				&view_messages();
				&unregister_in_wholist;
			}
			else
			{
				&unregister_in_wholist;
				last;
			}
		}
		else
		{
			&unregister_in_wholist;
			# &clean_up_wholist;
			last;
		}
	}
	else
	{
		print "\ni don't understand that. type ? or h for help.\n"
	}
}	

&goodbye;


sub help_the_poor_fool
{
	unless (fork)
	{
		exec("clear");
	}
	wait;
	print "r)ead a forum\n";
	print "n)ewscan all joined forums\n";
	print "l)ist forums\n";
	print "s)how joined forums.\n";
	print "j)oin a forum\n";
	print "u)njoin a forum\n";
	print "k)atch up all joined forums (ie: mark all ";
	if ($THREADED) { print "posts in all threads as read).\n"; }
	else { print "posts as read).\n"; }
	print "w)ho is online with you\n";
	print "m)essage another user\n";
	print "v)iew messages\n";
	print "c)lear the screen\n";
	print "d)ate and time\n";
	print "a)lert the administrator to problems via mail\n";
	print "p)assword -- change yours.\n";
	print "e)dit your sig\n";
	print "h)elp (also \"?\")\n";
	print "q)uit forums\n";
	print "\n";
}

sub read_list_of_forums
{
	local($listline,@fields);
	unless (open(LIST,"$HOME/forums.list"))
	{
		 die "Cannot open list of forums\n";
        }

  	$x=0;
	while ($listline=<LIST>)
	{
		if ((substr($listline,0,1) eq "#") || ($listline eq "\n"))
		{
			 next;
		}
		chop($listline);
		@fields=split(/\s+/,$listline);
		$list_of_forums[$x]=$fields[0];
		$list_of_privs[$x]=$fields[1];
		$list_of_moderators[$x]=$fields[2];
		$x++;	
	}
	close (LIST);
}

sub list_forums_threaded
{
	local ($forum,$x);
	unless (fork)
	{
		exec("clear");
	}
	wait;
	print "Forum 	 			  ";
	print "number of live threads\n";
	print "------                            ----------------------\n";
	$x=0;
	foreach $forum (@list_of_forums)
	{
		&check_living($forum);
		unless (open (LIVING,"$HOME/$forum/$forum.living.list"))
		{
			$lc=100;
			while ($lc--)
			{
				if (open(LIVING,"$HOME/$forum/$forum.living.list"))
				{
					last;
				}
			}
			if ($lc==0)
			{
				print "Unable to open list of live forums. Listing aborted.\n";
				print "Try again.  If problem persists, report error to sys admin.\n";
				return;
			}
		}
		&lock_file(LIVING);
		$countliving=0;
		while (<LIVING>)
		{
			if ($_!=0)
			{
				$countliving++;
			}
		}
		&unlock_file(LIVING);
		close (LIVING);
		print "$forum";
		$_=$forum;
		$count=tr/a-z//;
		if ($count<8)
		{
			print "\t";
			print "	     			    $countliving\n";
		}
		elsif ($count>=16 && $count<24)
		{
			print "			    $countliving\n";
		}
		elsif ($count>=24)
		{
			print "		    $countliving\n";
		}	
		else
		{
			print "	     			    $countliving\n";
		}
		$x++;
		if ($x == 22)
		{
			print "Press any key to continue\n";
			$x=<STDIN>;
			$x=0;
		}
	}
	print "\n";
}

sub list_forums_unthreaded
{
	local ($forum,$x,$numposts,$count);
	unless (fork)
	{
		exec("clear");
	}
	wait;
	print "Forum 	 			  ";
	print "   number of posts\n";
	print "------                            ----------------------\n";
	$x=0;
	foreach $forum (@list_of_forums)
	{
		unless (open(TEMP,"$HOME/$forum/$forum.1.0"))
		{
			$lc=100;
			while ($lc--)
			{
				if (open(TEMP,"$HOME/$forum/$forum.1.0"))
				{
					last;
				}
			}
			if ($lc==0)
			{
				print "Unable to open necessary file. Listing aborted.\n";
				print "Try again.  If problem persists, report error to sys admin.\n";
				return;
			}
		}
		&lock_file(TEMP);
		$numposts=<TEMP>;
		chop $numposts;
		close(TEMP);
		&unlock_file(TEMP);
		if ($numposts eq "") { $numposts=0; }
		print "$forum";
		$_=$forum;
		$count=tr/a-z//;
		if ($count<8)
		{
			print "\t";
			print "	     			    $numposts\n";
		}
		elsif ($count>=16 && $count<24)
		{
			print "			    $numposts\n";
		}
		elsif ($count>=24)
		{
			print "		    $numposts\n";
		}	
		else
		{
			print "	     			    $numposts\n";
		}
		$x++;
		if ($x == 22)
		{
			print "Press any key to continue\n";
			$x=<STDIN>;
			$x=0;
		}
	}
	print "\n";
}

sub read_whoami
{
# this uses the program `whoami` to find out the user's name and 
# then reads userpriv.dat it leaves us with @privcats, @privcats being which 
# categories we are able to read.

	local(@privs,@line,$privlevel,$priv,@privline);
	$whoiam=`whoami`;
	chop $whoiam;
	unless (fork)
	{
		exec("clear");
	}
	wait;
	unless (open(USERPRIV,"$HOME/userpriv.dat"))
	{
		print "Trying to open $HOME/userpriv.dat with no luck....\n";
		die "cannot open user priviledges file\n";
	}
	while ($a=<USERPRIV>)
	{
		if ((substr($a,0,1) eq "#") || ($a eq "\n"))
		{
			next;
		}
		chop ($a);
		if ($a ne "CATEGORIES")
		{
			next;
		}		
		while ($a=<USERPRIV>)
		{
			chop($a);
			if ($a eq "USERS") {last;}
			@line=split(/\s+/,$a);
			$privlevel=shift(@line); shift(@line);
			$privs[$privlevel]=pack("I*",split(/,/,$line[0]));
		}
		$found=0;
		while ($a=<USERPRIV>)
		{
			chop($a);
			@line=split(/\s+/,$a);
			if ($line[0] eq $whoiam)
			{
				shift(@line); shift(@line);
				@privline=split(/,/,$line[0]);
				$x=0;
				foreach $priv (@privline)
	 			{
					@temp=unpack("I*",$privs[$priv]);
					foreach $thing (@temp)
					{
						$privcats[$x++]=$thing;
					}
				}	
				$found = 1;	
				last;
			}
		}
	}
	if (!$found)
	{
		@temp=unpack("I*",$privs[0]);
		$x=0;
		foreach $thing (@temp)
		{
			$privcats[$x++]=$thing;
		}
	}
		
}

sub register_in_wholist
{
	unless (fork)
	{
		exec ("touch $HOME/who/$whoiam");
	}
	wait;	
	chmod ($CHMOD,"$HOME/who/$whoiam");
}

sub unregister_in_wholist
{
	unlink("$HOME/who/$whoiam") || print "Unable to unregister in who list.\n";
	&clean_up_wholist;
}

sub who_is_on
{
	local($date,@time,$x,$y);
	unless (fork)
	{
		exec("clear");
	}
	wait;
	$x=0;
	foreach $thing (<$HOME/who/*>)
	{
		$date=(stat($thing))[9];
		$time[$x++]=&ctime($date);
	}
	$x=0;
	print "who is on now           On Since\n";
	print "-------------           --------\n";
	$y=0;
	foreach $thing (<$HOME/who/*>)
	{
		@thing=split(/\//,$thing);
		print "$thing[@thing-1]			$time[$x++]";
		$y++;
		if ($y==22)
		{
			print "Press <Return> to continue...";
			$y=0;
			<STDIN>;
		}
	}
	print "\n";
}
	
sub message_user
{
	local($username,$user,$thing,@thing,$lockcounter,$filelocked);
	
	$username=$_[0];
	$username=~/(\w+)\W+(\w+).*/;
	$user=$2;
	if ($user eq "")
	{
		print "Message for who? ";
		$user=<STDIN>;
		chop($user);
		if ($user eq "")
		{
			return;
		}
	}

	$a=0;
	foreach $thing (<$HOME/who/*>)
	{
		@thing=split(/\//,$thing);
		if ($thing[@thing-1] eq $user) 
		{
			$a=1;
			last;
		}
	}      
	if ($a!=1) 
	{
		print "$user is not online right now\n";
		return;
	}
	else
	{
		$filelocked=0;
		unless (open(MSGFILE,">>$HOME/who/.$user"))
		{
			$lockcounter=100;
			while ($lockcounter--) 
			{
				if (open(MSGFILE,">>$HOME/who/.$user")) {last;}
			}
			if ($lockcounter==0)
			{
				print "error trying to open message file.\n please report the error to the sys admin.\n";
				return;
			}
		}
		&lock_file(MSGFILE);
		$filelocked=1;
		if ($SETGROUP)
		{
			chown($uid,$GROUP,"$HOME/who/.$user");
		}
		chmod ($CHMOD,"$HOME/who/.$user");

		print MSGFILE "Message from $whoiam\n";
		print MSGFILE "---------------------------------------\n";
		print "Type up to 4 lines. a blank line means you are done.\n";
		for ($x=0;$x<4;$x++)
		{
			$a=<STDIN>;
			if ($a ne "\n")
			{
				print MSGFILE $a;	
			}	
			else
			{
				last;
			}
		}
		print MSGFILE "---------------------------------------\n";
		if ($filelocked) {&unlock_file(MSGFILE);}
		close (MSGFILE);
	}
}	

sub view_messages
{
	local ($z);
	if (-e "$HOME/who/.$whoiam")
	{
		if (-z "$HOME/who/.$whoiam")
		{
			print "No Messages.\n";
			unlink ("$HOME/who/.$whoiam");
			return;
		}
	}
	else
	{
		print "No Messages.\n";
		return;
	}
	unless (open(MSGFILE,"$HOME/who/.$whoiam"))
	{
		$lc=100;
		while($lc--)
		{
			if (open (MSGFILE,"$HOME/who/.$whoiam"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Error: Unable to open message file!!\n";
			return;
		}
	}
	&lock_file(MSGFILE);
	$z=0;
	while ($a=<MSGFILE>)
	{
		print $a;
		$z++;
		if ($z==22)
		{
			print "Press <Return> to continue...";
			<STDIN>;
			$z=0;
		}
	}
	&unlock_file(MSGFILE);
	close (MSGFILE);	
	unlink("$HOME/who/.$whoiam") || print "Unable to remove .whoiam file\n";
}

sub make_new_user_jfile
{ 
	local($forum,$lock_counter,$flocked);
	
	$a=$ENV{"HOME"}."/.jfile";
	unless (fork)
	{	
		exec("touch $a");
	}
	wait;
	$flocked=0;
	unless (open (JFILE,">$a"))
	{
		$lock_counter=100;
		while ($lock_counter--)
		{
			if (open(JFILE,">$a"))
			{
				last;
			}
		}
		if ($lock_counter==0)
		{
			die "Could not create joined file for user $whoiam\n";
		}
	}
	&lock_file(JFILE);
	$flocked=1;
 	foreach $forum (@list_of_forums)
	{
		print JFILE "$forum -1\n";
	}
	if ($flocked) {&unlock_file(JFILE);}
	close (JFILE);
}	

sub show_jlist
{
	local($x);
	print "\nJoined Forums\n";
	print "-------------\n";
	$jfilename=$ENV{"HOME"}."/.jfile";
	unless (open (FILE,$jfilename))
	{
		$lock_counter=100;
		while ($lockcounter--)
		{
			if (open (FILE,$jfilename)) {last;}
		}
		if ($lockcounter==0)
		{
			print "unable to open list of joined files! report error to sysadmin.\n";
			return;
		}
	}
	$x=0;
	while (<FILE>) 
	{
		@list=split(/\s+/,$_);	
		if (@list[1] != -1)
		{
			$temp=@list[0];
			print "$temp\n";
			$x++;
			if ($x==22)
			{
				print "Press <return> to continue...";
				<STDIN>;
				$x=0;
			}
		}
	}
	&unlock_file(FILE);
	close(FILE);
	print "\n";
}

sub join_forum
{
	local($forum,@cline,$a,$b,@a,$c,$x);
	
	$cline=$_[0];
	$cline =~ /(\w+)\W+(\w+).*/;
	$forum=$2;
	if ($forum eq "")
	{
		print "Forum to join? ";
		$forumname=<STDIN>;
		chop($forum);
		$forumname =~ /(\w+).*/;
		$forum=$1;
		if ($forum eq "") {return;}
	}

	$a=$ENV{"HOME"}."/.jfile";
	unless (open(JFILE,"$a"))
	{
		print "Error : Could not open jfile. please report to sysadmin.\n";
		return;   
	}

	$b=$ENV{"HOME"}."/.temp-jfile";
	unless (open(TJFILE,">$b"))
	{
		print "Error : Could not open temporary jfile\nPlease report error to Admin.\n";
		close(JFILE);
		return;
	}

	$joined=0;
	while ($c=<JFILE>)
	{	
		@a=split(/\s+/,$c);
		if ($a[0] eq $forum)
		{
		    	if ($a[1] == -1)
			{
				$x=0;
				while ($list_of_forums[$x] ne $forum) {$x++;}
				for ($y=0;$y<@privcats;$y++)
				{
					if ($list_of_privs[$x]==$privcats[$y])
					{
						last;
					}
				}	
				if ($y<@privcats)
				{
					&check_living($forum);
					unless (open (LIVEFILE,"$HOME/$forum/$forum.living.list"))
					{
						$lc=100;
						while ($lc--)
						{
							if (open (LIVEFILE,"$HOME/$forum/$forum.living.list"))
							{
								last;
							}
						}
						if ($lc==0)
						{
							print "Unable to open living list. join abortd.\n";
							close(TJFILE);
							close(JFILE);
							return;
						}
					}
					&lock_file(LIVEFILE);
					print TJFILE "$forum ";
					while (<LIVEFILE>)
					{
						chop();
						unless ($_==0)
						{
							print TJFILE "$_ 0 ";
						}
					}
					print TJFILE "\n";
					&unlock_file(LIVEFILE);
					close (LIVEFILE);
					print "Joined $forum\n";
					$joined=1;
				}
				else
				{
					print "You are not priviledged to join that forum.\n";
					print TJFILE "$forum -1\n";
					$joined=1;
				}
			}
			else
			{
				print TJFILE "@a\n";
				print "Already joined to forum $forum\n";
				$joined=1;
			}
		}
		else
		{
			print TJFILE "@a\n";      
   		}
	}	
	if (!$joined) {print "Unable to join forum $forum....perhaps you mispelled it?\n";}
	close (JFILE);
	close (TJFILE);
	unlink $a;
	unless (rename ($b,$a))
	{
		print "Unable to rename temporary file...join aborted.\n";
		print "Please report error to admin.\n";
	}
}
 
sub unjoin_forum
{
	local($forum,$cline);
	
	$cline=$_[0];
	$cline=~/(\w+)\W+(\w+).*/;
	$forum=$2;
	if ($forum eq "")
	{
		print "Forum to unjoin? ";
		$forum=<STDIN>;
		chop ($forum);	
		if ($forum eq "") {return;}
	}

	$a=$ENV{"HOME"}."/.jfile";
	unless (open(JFILE,"$a"))
	{
		print "Error : Could not open jfile. please report this error to the sysadmin.\n";   
		return;
	}

	$b=$ENV{"HOME"}."/.temp-jfile";
	unless (open(TJFILE,">$b"))
	{
		print "Error : Could not open temporary file\n";
		print "Please report error to admin.\n";
		close(JFILE);
		return;
	}

	while ($c=<JFILE>)
	{	
		@a=split(/\s+/,$c);
		if ($a[0] eq $forum)
		{
				print TJFILE "$forum -1\n";
		}
		else
		{
			print TJFILE "@a\n";      
   		}
	}	
	
	close (JFILE);
	close (TJFILE);
	unlink $a;
	unless (rename ($b,$a))
	{
		print "Error: could not join forum. please report this error to the sysadmin.\n";
		return;
	}
}


sub catch_all_forums
{
	local ($forum,$lc);
	
	$jfile=$ENV{"HOME"}."/.jfile";
	unless (open(JFILE,"$jfile"))
	{
		print "Error : Unable to open jfile to catch forums.\nplease report this error to the sysadmin.\n";
		return;
	}
	$tjfile=$ENV{"HOME"}."/.temp-jfile";
	unless (open(TJFILE,">$tjfile"))
	{
		print "Error : Unable to open temporary file in \"catch_all_forums\".\nplease report this error to the sysadmin.\n";		
		close(JFILE);
		return;
	}
	foreach $forum (@list_of_forums)
	{
		print TJFILE $forum." ";
		&check_living($forum);	
		unless (open (LIVINGLIST,"$HOME/$forum/$forum.living.list"))
		{
			$lc=100;
			while ($lc--)
			{
				if (open (LIVINGLIST,"$HOME/$forum/$forum.living.list")) {last;}
			}
			if ($lc==0)
			{
				print "Unable to open living list.  Please report error to sysadmin.\n";
				return;
			}
		}
		&lock_file(LIVINGLIST); 
		$temp2=<JFILE>;
		@temp=split(/\s+/,$temp2);
		if ($temp[1] eq "-1")
		{
			print TJFILE "-1\n";
		}
		else
		{
			if ($THREADED)
			{
				while ($temp=<LIVINGLIST>)
				{
					if ($temp==0) {last;}
					chop $temp;
					print TJFILE $temp." ";
					open (TEMP,"$HOME/$forum/$forum.$temp.0");
					$var=<TEMP>;
					chop $var;
					print TJFILE $var." ";
					close TEMP;
				}
			}
			else
			{
				open (TEMP,"$HOME/$forum/$forum.1.0");
				$var=<TEMP>;
				chop ($var);
				print TJFILE "1 ".$var;
			}
			print TJFILE "\n";
		}
		&unlock_file(LIVINGLIST);
		close (LIVINGLIST);
	}			
	close JFILE;
	close TJFILE;
	if (rename ($tjfile,$jfile))
	{
		print "Caught up all joined forums.\n";
	}
	else
	{
		print "Error! could not catch all forums...please report error to sysadmin.\n";
	}
}

sub read_a_forum
{
#	This func is long and ugly, almost 700 lines. it does a lot of sanity 
#	checking, assigns some important variables, and proceeds to handle the 
#	reading, complete with read menu prompt.  while reading, it assigns a 
#	lot of things to holding variables, so they can be checked against and 
#	changed back and used. it is complicated because users can change 
#	threads and posts which makes it hard to keep track of what you have 
#	read already, and where you are. re-writing this is the next thing
#	on my list. 
	
	local($newscanning,$postalt,$forum,$quit,$z,$z2,$firsttime,$continue,$post,$thread,$b,$prompt);

	$a=$_[0];
	@argline=split(/\s/,$_[0]);
	$forum=$argline[1];
	$newscanning=$argline[2];

	if ($forum eq "")
	{
		print "Which forum do you want to read? ";
		$forum=<STDIN>;
		chop($forum);		
		if ($forum eq "") {return;}
	}
	
	unless (chdir("$HOME/$forum"))
	{
		print "Unable to read forum $forum...perhaps you mispelled it?\n";
		return;
	}
	
	$jfile=$ENV{"HOME"}."/.jfile";
	unless (open(JFILE,"$jfile"))
	{
		print "Error : Unable to open jfile in \"read_a_forum\"\nPlease report error to sysadmin.\n";
		return;
	}
	$tjfile=$ENV{"HOME"}."/.temp-jfile";
	unless (open(TJFILE,">$tjfile"))
	{
		print "Error : Unable to open temporary file in \"read_a_forum\"\nPlease report error to sysadmin.\n";
		close(JFILE);
		return;
	}		
	
	&prepare_to_read_forum($forum);
	$notjoined=0;

	if ($a[1] == -1)
	{	
		$notjoined=1;
		$thingx=0;
		while ($list_of_forums[$thingx] ne $forum) {$thingx++;}
		for ($thingy=0;$thingy<@privcats;$thingy++)
		{
			if ($list_of_privs[$thingx]==$privcats[$thingy])
			{
				last;
			}
		}	
		if ($thingy==@privcats)
		{
			print "You are not priviledged to read that forum.\n";
			print TJFILE "$forum -1\n";
			&finish_reading_forum;
			return;
		}
		else
		{
			$a[1]="";
		}
	}

	if (-e "$HOME/$forum/$forum.0.0")
	{
		unless (open (FFILE,"$HOME/$forum/$forum.0.0"))
		{
			$lc=100;
			while ($lc--)
			{
				if (open(FFILE,"$HOME/$forum/$forum.0.0"))
				{
					last;
				}
			}
			if ($lc==0)
			{
				print "Unable to open 0.0 file...please report error to sysadmin.\n";
				close(TJFILE);
				close(JFILE);
				return;
			}
		}
	}
	else
	{
		&make_00_file($forum);
		unless (open(FFILE,"$HOME/$forum/$forum.0.0"))
		{
			$lc=100;
			while ($lc--)
			{
				if (open(FFILE,"$HOME/$forum/$forum.0.0"))
				{
					last;
				}
			}
			if ($lc==0)
			{
				print "Unable to open 0.0 file...please report error to sysadmin.\n";
				close(TJFILE);
				close(JFILE);
				return;
			}
		}
	}		
	&lock_file(FFILE);
	$number_of_threads=<FFILE>;
	&unlock_file(FFILE);
	close (FFILE);
	chop ($number_of_threads);
	$quit=0;
		
	if ($number_of_threads == 0)
	{
		if ($THREADED)
		{
			print "No threads in forum -$forum-. start one? (y/N) ";
		}
		else
		{
			print "No posts in forum -$forum-.  post? (y/N) ";
		}
		$prompt=<STDIN>;
		chop ($prompt);
		if (($prompt eq "y") || ($prompt eq "Y"))
		{
			if (&write_new_thread($forum,$number_of_threads))
			{
				$number_of_threads++;	
			}
		}
	}
	
	shift(@a);
	print TJFILE "$forum ";
	$index=0;
	$bindex=0;
	if ($a[$index]=="") 
	{
		$a[$index]=1;
		$a[$index+1]=0;
	}

	$movethread=0;
	$lastgoodthread=1;
	$lastgoodindex=1;
	$nolivingthreads=1;
	$Katch=0;
	$skip=0;
	$havereadone=0;
	if ($a[$index] eq "") {$threadstart=1;}
	else {$threadstart=$a[$index];}
	for ($thread=$threadstart;$thread<=$number_of_threads;$thread++)
	{
		if ($thread<$a[$index])
		{
			next;
		}

		$dontwrite=0;
		if ($THREADED)
		{
			$mtime=(stat("$HOME/$forum/$forum.$thread.0"))[9];
			if ((time()-$mtime)>$MAXAGE) 
			{
				$index+=2;
				if ($thread<$number_of_threads || $havereadone)
				{
					next;
				}
				else
				{
					$dontwrite=1; 
					$threadsaver=$thread;
					$indexsaver=$index;
					$index=$lastgoodindex;
					$thread=$lastgoodthread;
					$bindex-=2;
					if ($bindex<0) {$bindex=0;}
				}
			}	
			else
			{
				$lastgoodthread=$thread;
				$lastgoodindex=$index;
				$nolivingthreads=0;
			}
		}
		else
		{
			$lastgoodthread=$thread;
			$lastgoodindex=$index;
			$nolivingthreads=0;
		}
				
		unless (open (THREAD,"$HOME/$forum/$forum.$thread.0")) 
		{
			$lc=100;
			while ($lc--)
			{
				if (open(THREAD,"$HOME/$forum/$forum.$thread.0"))
				{
					last;
				}
			}
			if ($lc==0)
			{
				print "Unable to open .0 file...please report error to sysadmin.\n";
				close(JFILE);
				close(TJFILE);
				return;
			}
		}
		&lock_file(THREAD);
		$num_posts=<THREAD>;
		&unlock_file(THREAD);
		close(THREAD);
		chop ($num_posts);
		if (!$dontwrite)
		{
			$b[$bindex]=$thread;
		}

		if (!$movethread) {$firsttime=1;}
		else {$firsttime=0;}
		$moveindex=0;
		if ($a[$index+1]>$num_posts) {$a[$index+1]=$num_posts;}
		if ($a[$index+1]!=$num_posts)
		{
			$start=$a[$index+1]+1;
		}
		else
		{
			$start=$a[$index+1];
		}
		if ($start eq "") {$start=1;}
		if ($nolivingthreads) {$start=$num_posts;}
		$movepost=0;
		for ($post=$start;$post<=$num_posts;$post++)
		{
			$postalt=$post;
			if (($a[$index+1] != $num_posts) || ($thread==$number_of_threads) || $dontwrite || $movethread || $movepost) 
			{
				$movepost=0;
				if ($post eq "" || $post==0) 
				{
					$post=1;
					$postalt=1;
				}
				if ((($a[$index+1]!=$num_posts) || (!$firsttime)) && !$nolivingthreads)
				{
					unless (fork)
					{	
						exec("clear");
					}
					wait;
					unless (fork)
					{
						exec("$PAGER $HOME/$forum/$forum.$thread.$post"); 
					}
					wait;
					$firsttime=0;
					$havereadone=1;
				}
				$continue=1;
				while ($continue)
				{
					$continue=0;
					if ($nolivingthreads)
					{
						print "\n$forum: No living threads> ";
						if (!$newscanning)
						{
							$prompt=<STDIN>;
							chop($prompt);
						}
						else
						{
							$prompt="";
						}	
					}
					elsif ($firsttime && $newscanning)
					{
						if (!$havereadone)
						{
							print "$forum: No new posts.\n";
						}
						$prompt="";
					}
					else
					{
						print "\n$forum:";
						if ($THREADED)
						{
							print" Thread $thread of $number_of_threads --";
						}
						print " Post $post of $num_posts> ";
						$prompt=<STDIN>;
						chop($prompt);
					}
					if (($prompt eq "+") || ($prompt eq ""))	
					{
						# next post;
					}
					elsif (substr($prompt,0,1) eq "+")
					{
						$prompt=~s/\D+//;
						$post+=$prompt-1;
						if ($post>=$num_posts)
						{
							print "Can't move past last post ($num_posts)\n";
							print "Press 'a' to see last post\n";
							$post=$num_posts;	
							$continue=1;
						}
						$firsttime=0;
						$movepost=1;
					}
					elsif ($prompt eq "-" || substr($prompt,0,1) eq "-")
					{
						$movepost=1;
						if ($prompt eq "-")
						{
							if ($post==$num_posts && $firsttime)
							{		
								$delta=1;
							}	
							else
							{
								$delta=2;
							}
						}
						else
						{
							$prompt =~ s/\D+//;
							$delta = $prompt;
							$delta+=1;
						}
						$post-=$delta;
						if ($post<0) 
						{
							print "\nCan't move back past post 1.\n";
							print "Press 'a' to see 1st post.\n";
							$post=1;
							$continue=1;
						}
						$firsttime=0;
					}
					elsif ($prompt eq "a")
					{
						$firsttime=0;
						$post--;
					}
					elsif ($prompt=~/^[1-9]/)
					{
						$movepost=1;
						$prompt =~ s/\D+//g;
						if ($prompt>$num_posts)
						{
							print "\nLast post is $num_posts\n";
							$continue=1;
						}
						elsif ($prompt<1)
						{
							print "\nFirst post is 1\n";
							$continue=1;
						}
						else
						{
							$b[$bindex]=$thread;
							$post=$prompt-1;
						}
						$firsttime=0;
					}
					elsif ($prompt eq "<" && $THREADED)
					{ 	
						if ($bindex==0) 
						{
							print "\nAlready at first thread\n";
							$continue=1;
						}
						else
						{
							$b[$bindex]=$thread;
							if ($b[$bindex+1] eq "" || $b[$bindex+1]<$post) {$b[$bindex+1]=$post;}
							if ($b[$bindex+1] eq "") {$b[$bindex+1] = 0;}
							$bindex-=2;
							if ($bindex<0) { $bindex=0;}
							$thread=$b[$bindex];
							while ($a[$index]!=$thread){$index-=2;if ($index<0) { $index=0;last;}}
							if ($thread eq "")
							{
								$thread=$a[$index];
								if ($thread eq "")
								{
									$thread = 1;
								}
							}
							$post=$b[$bindex+1];
							if ($post eq "") {$post=0;}
							$postalt=$post;
							$continue=1;	
							$movethread=1;
							$moveindex=1;
							$firsttime=0;
							$dontwrite=0;
							unless (open (NUMPOSTS,"$HOME/$forum/$forum.$thread.0"))
							{
								$lc=100;
								while ($lc--)
								{
									if (open(NUMPOSTS,"$HOME/$forum/$forum.$thread.0"))
									{
										last;
									}
								}
								if ($lc==0)
								{
									print "Unable to open NUMPOSTS. Please report error to sysadmin.\n";
									close(JFILE);
									close(TJFILE);
									return;
								}
							}
							&lock_file(NUMPOSTS);
							$num_posts=<NUMPOSTS>;
							&unlock_file(NUMPOSTS);
							close (NUMPOSTS);
							chop ($num_posts);
						}
					}	
					elsif ($prompt eq ">" && $THREADED)
					{
						if ($thread==$number_of_threads)
						{
							print "\nAlready at last thread.\n";
							$continue=1;
						}
						else
						{
							$b[$bindex]=$thread;
							if ($b[$bindex+1] eq "" || $b[$bindex+1]<$post) {$b[$bindex+1]=$post;}
							if ($b[$bindex+1] eq "") {$b[$bindex+1]=0;}
							$holdingindex=$index;
							$index+=2;
							$bindex+=2;
							if ($b[$bindex] eq "")
							{
								if ($a[$index] eq "")
								{
									$tempthread=$thread+1;
									$temppost=0;
								}
								else
								{
									$tempthread=$a[$index];
									$temppost=$a[$index+1];
									if ($temppost eq "") {$temppost=0;}
								}
								$mtime=(stat("$HOME/$forum/$forum.$tempthread.0"))[9];
								while ((time()-$mtime)>$MAXAGE) 
								{
									if ($tempthread<=$number_of_threads)
									{
										$index+=2;
										if ($a[$index] ne "") { $tempthread=$a[$index];}
										else { $tempthread++;}
									}
									else
									{
										print "No more living threads.\n";	
										last;
									}
									$mtime=(stat("$HOME/$forum/$forum.$tempthread.0"))[9];
								}
								if ($tempthread<=$number_of_threads)
								{
									$thread=$tempthread;
									$post=$temppost;
									$b[$bindex]=$thread;
									$b[$bindex+1]=$post;
								}
								else
								{
									$bindex-=2;
									if ($bindex<0) {$bindex=0;}
									$index=$holdingindex;
								}
							}
							else
							{
								$thread=$b[$bindex];
								if ($b[$bindex+1] eq "") { $b[$bindex+1]=0;}
								$post=$b[$bindex+1];				
								$index=0;
								while ($a[$index]!=$thread && $index<@a){$index+=2;}
								if ($index==@a) {$index-=2;if ($index<0) {$index=0;}}
							}
							$postalt=$post;
							$movethread=1;
							$moveindex=1;
							$firsttime=0;
							$continue=1;
							$dontwrite=0;
							unless (open (NUMPOSTS,"$HOME/$forum/$forum.$thread.0"))
							{
								$lc=100;
								while ($lc--)
								{
									if (open(NUMPOSTS,"$HOME/$forum/$forum.$thread.0"))
									{
										last;
									}
								}
								if ($lc==0)
								{
									print "Unable to open NUMPOSTS. Please report error to sysadmin.\n";
									close(JFILE);
									close(TJFILE);
									return;
								}
							}
							&lock_file(NUMPOSTS);
							$num_posts=<NUMPOSTS>;
							&unlock_file(NUMPOSTS);
							close (NUMPOSTS);
							chop ($num_posts);
						}
					}
					elsif ((substr($prompt,0,1) eq "t") && ($THREADED))
					{
						$prompt =~ s/t\ //;
						if ($prompt<=0)
						{
							print "\nFirst thread is thread 1\n";
							$continue=1;
						}
						elsif ($prompt>$number_of_threads)
						{
							print "\nLast thread is $number_of_threads\n";
							$continue=1;
						}
						elsif ((time()-(stat("$HOME/$forum/$forum.$prompt.0"))[9])>$MAXAGE)
						{
							print "\nNot a living thread.\n";
							$continue=1;
						}
						elsif ($thread>$prompt)
						{
							if ($b[$bindex+1] eq "" || $b[$bindex+1]<$post) {$b[$bindex+1]=$post;}
							if ($b[$bindex+1] eq "") {$b[$bindex+1]=0;}
							$holdindex=$bindex;
							$bindex=0;
							while (($b[$bindex]!=$prompt) && ($bindex<$holdindex))
							{
								$bindex+=2;
							}
							if ($bindex==$holdindex)
							{
								print "\nNot a living thread.\n";
								$continue=1;
							}
							else
							{
								$thread=$prompt;
								while ($a[$index]!=$thread && $index>=0) { $index-=2;}
								if ($index<0) {$index=0;}
								$post=$b[$bindex+1];
								if ($post eq "") {$post=0;}
								$postalt=$post;
								unless (open (NUMPOSTS,"$HOME/$forum/$forum.$thread.0"))
								{
									$lc=100;
									while ($lc--)
									{
										if (open(NUMPOSTS,"$HOME/$forum/$forum.$thread.0"))
										{
											last;
										}
									}
									if ($lc==0)
									{
										print "Unable to open NUMPOSTS. Please report error to sysadmin.\n";
										close(JFILE);
										close(TJFILE);
										return;
									}
								}
								&lock_file(NUMPOSTS);
								$num_posts=<NUMPOSTS>;
								&unlock_file(NUMPOSTS);
								close(NUMPOSTS);
								chop ($num_posts);
								$continue=1;
								$moveindex=1;
								$movethread=1;
								$firsttime=0;
							}
						}
						elsif ($prompt>$thread)
						{
							$z2=$bindex;
							$b[$bindex]=$thread;
							if ($b[$bindex+1] eq "" || $b[$bindex+1]<$post) {$b[$bindex+1]=$post;}
							if ($b[$bindex+1] eq "") {$b[$bindex+1]=0;}
							for ($z=$index+2;$z<@a;$z+=2)
							{
								$mtime=(stat("$HOME/$forum/$forum.$a[$z].0"))[9];
								if ($z2>=@b)
								{
									if (time()-$mtime<$MAXAGE)
									{
										$b[$z2]=$a[$z];
										$b[$z2+1]=$a[$z+1];
										$z2+=2;
									}
								}
								else
								{
									if (time()-$mtime<$MAXAGE) 
									{
										$z2+=2;
										if ($z2>=@b) {$b[$z2]=$a[$z];$b[$z2+1]=$a[$z+1];}
									}
								}
								if ($a[$z]==$prompt)
								{
									last;
								}
							}
							if ($a[$z]==$prompt)
							{
								$bindex=$z2;
								$index=$z;
								$post=$b[$z2+1];
								if ($post eq "") {$post=$a[$z+1];}
								if ($post eq "") {$post=0;}
								$postalt=$post;
								$thread=$prompt;
								open (NUMPOSTS,"$HOME/$forum/$forum.$thread.0");
								$num_posts=<NUMPOSTS>;
								chop ($num_posts);
								close(NUMPOSTS);
								$moveindex=1;
								$movethread=1;
								$continue = 1;
								$firsttime=0;
							}
							else
							{
								while ($b[$z2] ne "" && $b[$z2] != $prompt) {$z2+=2;}
								&check_living($forum);
								unless (open (LIVING,"$HOME/$forum/$forum.living.list"))
								{
									$lc=100;
									while ($lc--)
									{
										if (open(LIVING,"$HOME/$forum/$forum.living.list"))
										{
											last;
										}
									}
									if ($lc==0)
									{					
										print "Unable to open needed file! please report error to sysadmin.\n";
										close(TJFILE);
										close(JFILE);
										return;
									}
								}
								&lock_file(LIVING);
								$holdaz=$a[$z-2];
								while (<LIVING>)
								{
									chop($_);
									if ($_<=$holdaz)
									{
										next;
									}
									$b[$z2]=$_;
									$b[$z2+1]=0;
									if ($_==$prompt) {last;}
									$z2+=2;
								}
								&unlock_file(LIVING);
								close(LIVING);
								if ($_!=$prompt)
								{
									if ($prompt>$number_of_threads)
									{
										print "\nNo such thread.\n";
									}
									else
									{
										print "\nNot a living thread.\n";
									}
									$continue=1;
									$bindex=$z2;
								}								
								else
								{
									$index=$z-2;
									$bindex=$z2;
									$thread=$prompt;
									$post=0;
									$postalt=$post;				
									unless (open (NUMPOSTS,"$HOME/$forum/$forum.$thread.0"))
									{
										$lc=100;
										while ($lc--)
										{
											if (open(NUMPOSTS,"$HOME/$forum/$forum.$thread.0"))
											{
												last;
											}
										}
										if ($lc==0)
										{
											print "Unable to open NUMPOSTS. Please report error to sysadmin.\n";
											close(JFILE);
											close(TJFILE);			
											return;
										}
									}
									&lock_file(NUMPOSTS);
									$num_posts=<NUMPOSTS>;
									&unlock_file(NUMPOSTS);
									close(NUMPOSTS);
									chop ($num_posts);
									$moveindex=1;
									$movethread=1;
									$firsttime=0;
									$continue = 1;
								}		
							}
						}		
						else 
						{
							$continue=1;
						}
					}
					elsif ($prompt eq "f")
					{
						if ($temp=&follow_post($forum,$thread,$post,$num_posts+1))
						{
							$num_posts=$temp;
						}
						$continue=1;
					}
					elsif ($prompt eq "p")
					{
						if ($temp=&post_to_current_thread($forum,$thread,$num_posts))
						{
							$num_posts=$temp;
						}
						$continue=1;
					}
					elsif ($prompt eq "w" && $THREADED)
					{			
						if ($temp=&write_new_thread($forum,$number_of_threads))
						{
							$number_of_threads=$temp;
							if ($nolivingthreads)
							{
								$nolivingthreads=0;
								$thread=$number_of_threads;
								$num_posts=1;
								$post=1;
							}
						}
						$continue=1;
					}
					elsif ($prompt eq "m")
					{
						&moderate_post($forum,$thread,$post);
						$continue=1;
					}
					elsif ($prompt eq "h" || $prompt eq "?")
					{
						&help_read();
						$continue=1;
					}
					elsif ($prompt eq "c")
					{
						unless (fork)
						{
							exec("clear");
						}
						wait;
						$continue=1;
					}
					elsif ($prompt eq "d")
					{
						&delete_post($forum,$thread,$post);
						$continue=1;
					}
					elsif (substr($prompt,0,1) eq "l")
					{
						if ($THREADED)
						{
							&list_threads($forum);
						}
						else
						{
							$prompt=~s/\D+//;
							&list_posts($forum,$prompt);
						}
						$continue=1;
					}
					elsif ($prompt eq "k")
					{
						$post=$num_posts;
						$postalt=$post;
						$continue=1;
					}	
					elsif ($prompt eq "K" && $THREADED)
					{
						$post=$num_posts+1;
						$Katch=1;
					}
					elsif ($prompt eq "s")
					{
						$skip=1;
					}
					elsif ($prompt eq "q")
					{
						$quit=1;
					}
					else
					{
						$continue=1;
					}
				} # close while ($continue)
			} # close if
			if ($quit || $Katch || $skip)
			{
				$postalt=$post;
				$post=$num_posts+1;
			}
		} # close for posts
		if (!$dontwrite) 
		{
			$b[$bindex+1]=$postalt;
		}
		else
		{
			$thread=$threadsaver;
			$index=$indexsaver-2;
		}
		$index+=2;
		$bindex=0;
		while ($b[$bindex] != $thread && $b[$bindex] ne "") {$bindex+=2;}
		if ($b[$bindex] ne "") {$bindex+=2;}
		if ($quit || $Katch || $skip) {last;}
	} #close for threads 
	if ($Katch)
	{
		&check_living($forum);
		$counter=0;
		unless (open (LIVINGLIST,"$HOME/$forum/$forum.living.list"))
		{
			$lc=100;
			while ($lc--)
			{
				if (open(LIVINGLIST,"$HOME/$forum/$forum.living.list"))
				{
					last;
				}
			}
			if ($lc==0)
			{
				print "Unable to open LIVINGLIST.\nKatch aborted.\nreport error to sysadmin.\n";
				close(JFILE);
				close(TJFILE);
				return;
			}
		}
		&lock_file(LIVINGLIST);
	 	while ($live[$counter]=<LIVINGLIST>)
		{
			unless ($live[$counter]==0)
			{
				$counter++;
			}
		}
		&unlock_file(LIVINGLIST);
		close(LIVINGLIST);
		$z=0;
		foreach $live (@live)
		{
			chop ($live);
			unless (open (NUMPOSTS,"$HOME/$forum/$forum.$live.0"))
			{
				$lc=100;
				while ($lc--)
				{
					if (open (NUMPOSTS,"$HOME/$forum/$forum.$live.0"))
					{ 
						last;
					}
				}
				if ($lc==0)
				{
					print "Unable to open NUMPOSTS.\nKatch aborted.\nreport error to sysadmin.\n";
					close(JFILE);
					close(TJFILE);
					return;
				}
			}
			&lock_file(NUMPOSTS);
			$b[$z]=$live;
			$b[$z+1]=<NUMPOSTS>;
			&unlock_file(NUMPOSTS);
			close (NUMPOSTS);
			chop ($b[$z+1]);
			$z+=2;
		}
		print TJFILE "@b\n";
		for ($cnt=0;$cnt<@b;$cnt+=2)
                {
                        $b[$cnt]="";
                        $b[$cnt+1]="";
                }
	}
	else
	{
		$bindex=0;
		for ($z=0;$z<@a;$z+=2)
		{
			if ($b[$bindex] != $a[$z])
			{
				$mtime=(stat("$HOME/$forum/$forum.$a[$z].0"))[9];
				if ((time()-$mtime)<$MAXAGE)
				{ 
	 				$b[$bindex]=$a[$z];
					$b[$bindex+1]=$a[$z+1];
					$bindex+=2;
				}
			}
			else
			{
				$bindex+=2;
			}
		}
		if ($notjoined)
		{
			print TJFILE "-1\n";
		}
		else
		{
			print TJFILE "@b\n";		
		}
		for ($cnt=0;$cnt<@b;$cnt++)
		{
			$b[$cnt]="";
		}
	}
	print "\n";
	&finish_reading_forum;
}

sub list_threads
{
	local($mtime,$numlines,$numposts,$form,$thread,$counter);

	$form=$_[0];
	$thread=1;
	$numlines=0;

	if (-e "$HOME/$form/$form.0.0")
	{
		unless (open (FFILE,"$HOME/$form/$form.0.0"))
		{
			$lc=100;
			while ($lc--)
			{
				if (open(FFILE,"$HOME/$form/$form.0.0"))
				{
					last;
				}
			}
			if ($lc==0)
			{
				print "Unable to list threads. please report error to sysadmin.\n";
				return;
			}
		}
	}
	else
	{
      	        &make_00_file($form);
		unless (open (FFILE,"$HOME/$form/$form.0.0"))
		{
			$lc=100;
			while ($lc--)
			{
				if (open(FFILE,"$HOME/$form/$form.0.0"))
				{
					last;
				}
			}
			if ($lc==0)
			{
				print "Unable to list threads. please report error to sysadmin.\n";
				return;
			}
		}
	}
	&lock_file(FFILE);
        $numthreads=<FFILE>;
	&unlock_file(FFILE);
        close (FFILE);	
	chop($numthreads);
	
	unless (fork)
	{
		exec("clear");
	}
	wait;
	while ($thread<=$numthreads)
	{
		$mtime=(stat("$HOME/$form/$form.$thread.0"))[9];
		if ((time()-$mtime)<$MAXAGE)
		{
			print "-----------------------------------\n";
			unless (open(FILE,"$HOME/$form/$form.$thread.1"))
			{
				$lc=100;
				while ($lc--)
				{
					if (open (FILE,"$HOME/$form/$form.$thread.1"))
					{
						last;
					}
				}
				if ($lc==0)
				{
					print "Unable to open necessay file for list...list aborted.\n report error to sysadmin.\n";
					return;
				}
			}
			&lock_file(FILE); 
			for ($counter=0;$counter<4;$counter++)
			{
				$info=<FILE>;
				print $info;
			}
			if ($form eq "moderated")
			{
				$info=<FILE>;
				print $info;
				$info=<FILE>;
				print $info;
			}
			&unlock_file(FILE);
			close(FILE);
			unless (open(TEMP,"$HOME/$form/$form.$thread.0"))
			{
				$lc=100;
				while ($lc--)
				{
					if (open (TEMP,"$HOME/$form/$form.$thread.0"))
					{
						last;
					}
				}
				if ($lc==0)
				{
					print "Unable to open necessay file for list...list aborted.\n report error to sysadmin.\n";
					return;
				}
			}
			&lock_file(TEMP); 
			$thingamabob=<TEMP>;
			&unlock_file(TEMP);
			close(TEMP);
			if ($form ne "moderated") {print "Number of posts: $thingamabob\n";}
			print "\n";
			$numlines+=7;
			if ($numlines>20)
			{
				print "Press enter to continue...";
				<STDIN>;
				$numlines=0;
				unless (fork)
				{
					exec("clear");
				}
				wait;
			}
		}
		$thread++;
	}
}	

sub list_posts
{
	local ($poster,$subj,$counter,$lines,$start);
	unless (open(TEMP,"$HOME/$forum/$forum.1.0"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open (TEMP,"$HOME/$forum/$forum.1.0"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to open necessay file for list...list aborted.\n report error to sysadmin.\n";
			return;
		}
	}
	&lock_file(TEMP); 
	$listnumposts=<TEMP>;
	&unlock_file(TEMP);
	close(TEMP);
	
	$start=$_[1];
	if ($start eq "") { $start = 1; }

	$lines=20;
	print "\nListing of posts and posters.\n";
	print "-----------------------------\n";
	for ($counter=$start;$counter<=$listnumposts;$counter++)
	{
		open (TEMP,"$HOME/$forum/$forum.1.$counter");
		$poster=<TEMP>;$poster=<TEMP>;
		$subj=<TEMP>;$subj=<TEMP>;
		close (TEMP);
		chop $poster; chop $subj;
		print $counter.") ".$poster."     ".$subj."\n";
		if ($lines==0)
		{
			print "Press <return> to continue listing...";
			$lines=23;
			<STDIN>;
			print "\n";
		}
	}
}

sub prepare_to_read_forum
{
	while ($a=<JFILE>)
	{
		chop $a;
		@a=split(/\s+/,$a);
		if ($a[0] eq $_[0])
		{
			return;
		}
		elsif (&is_in_flist($a[0]))
		{
			print TJFILE "@a\n";
		}
	}
}

sub is_in_flist
{
	local ($form,$b,@b);
	
	$form=$_[0];
		
	unless (open(FLIST,"$HOME/forums.list"))
	{
		print "Error : Cannot open list of forums\n";
		print "Try again, and contact the sysadmin if problem persists.\n";
		return;
	}

	while ($b=<FLIST>)
	{
		@b=split(/\s+/,$b);
		if ($form eq $b[0])
		{
			return 1;
		}
 	}
	return 0;
}

sub make_00_file
{
	local ($form);
	
	$form=$_[0];
	unless (open (FFILE,">$HOME/$form/$form.0.0"))
	{
		$lc=100;
		while ($lc--)
		{
			if (-e "$HOME/$form/$form.0.0")
			{
				return;
			}
			if (open(FFILE,">$HOME/$form/$form.0.0"))	
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Error creating 0.0 file. report error to sysadmin.\n";
			return;
		}
	} 
	&lock_file(FFILE);
	print FFILE "0\n";
	&unlock_file(FFILE);
    	close FFILE;
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/$form/$form.0.0");
	}
	chmod ($CHMOD,"$HOME/$form/$form.0.0");
}

sub make_x0_file
{
	local ($form,$thred);
	
	$form=$_[0];
	$thred=$_[1];
	
	unless (open (FFILE,">$HOME/$form/$form.$thred.0"))
	{
		$lc=100;
		while ($lc--)
		{
			if (-e "$HOME/$form/$form.$thred.0")
			{
				return;
			}
			if (open(FFILE,">$HOME/$form/$form.$thred.0"))	
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Error creating .0 file. report error to sysadmin.\n";
			return;
		}
	}
	&lock_file(FFILE);
	print FFILE "0\n";
	&unlock_file(FFILE);
	close(FFILE);
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/$form/$form.$thred.0");
	}
	chmod ($CHMOD,"$HOME/$form/$form.$thred.0");
}

sub write_new_thread
{
	$c=$_[1]+1;	
	
	unless (open(FFILE1,">$HOME/$_[0]/$_[0].$c.1"))
	{
		$lc=100;
		while ($lc--)
		{
			$c++;
			if (open (FFILE1,">$HOME/$_[0]/$_[0].$c.1"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to start new thread. contact sysadmin.\n";
			return 0;
		}
	}
	&lock_file(FFILE1);

	print FFILE1 "Forum: $_[0] ";	
	if ($THREADED) {print FFILE1 "Thread: $c ";}
	print FFILE1 "Post: 1\n";
	print FFILE1 "Post by: $whoiam\n";	
	print FFILE1 `date`;
	print "Subject? ";
	$d=<STDIN>;
	print FFILE1 "Subject: $d";
	print FFILE1 "\n\n";
	&unlock_file(FFILE1);
	close(FFILE1);	

	unless (fork)
	{
		exec("$EDITOR ~/.temp.new.thread");
	}
	wait;
	if (-z $ENV{"HOME"}."/.temp.new.thread") 
	{
		print "Empty posts are not accepted.\n";
		unlink ("$HOME/$_[0]/$_[0].$c.1");
		return 0;
	}
	unless (-e $ENV{"HOME"}."/.temp.new.thread")
	{
		print "Posting aborted.\n";
		unlink ("$HOME/$forum/$forum.$c.1");
		return 0;
	}

	unless (fork)
	{
		exec("cat ~/.temp.new.thread >> $HOME/$_[0]/$_[0].$c.1");
	}
	wait;
	unless (fork)
	{
		exec("rm ~/.temp.new.thread");
	}
	wait;
	
	$sig=$ENV{"HOME"}."/.forumsig";
	if (-e $sig)
	{
		unless (fork)
		{
			exec("cat ~/.forumsig >> $HOME/$_[0]/$_[0].$c.1");
		}
		wait; 
	}
	
	unless (open(FFILE2,">$HOME/$_[0]/$_[0].$c.0"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open(FFILE2,">$HOME/$_[0]/$_[0].$c.0"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to lock .0 file...report error to admin.\n";
			return 0;
		}
	}
	&lock_file(FFILE2);
	print FFILE2 "1\n";
	&unlock_file(FFILE2);
	close (FFILE2);
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/$_[0]/$_[0].$c.0");
	}
	chmod ($CHMOD,"$HOME/$_[0]/$_[0].$c.0");

	unless (open (FFILE,"+<$HOME/$_[0]/$_[0].0.0"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open (FFILE,"+<$HOME/$_[0]/$_[0].0.0"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to enter new thread in list! new thread aborted.\n";
			print "Please report this error to the syadmin. thank you.\n";
			return 0;
		}
	}
	&lock_file(FFILE);
	$um=<FFILE>;
	if ($um<$c)
	{
		seek(FFILE,0,0);
		print FFILE "$c\n";
	}
	&unlock_file(FFILE);
	close(FFILE);
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/$_[0]/$_[0].0.0");
	}
	chmod($CHMOD,"$HOME/$_[0]/$_[0].0.0");

	return $c;
}

sub finish_reading_forum
{
	local ($c,@c);

	while ($c=<JFILE>)
	{
		@c=split(/\s+/,$c);
		if (&is_in_flist($c[0]))
		{
			print TJFILE "@c\n";
		}
	}
	close(JFILE);
	close(TJFILE);
	rename($tjfile,$jfile);
}

sub post_to_current_thread
{
	local ($fforum,$tthread,$ppost);
	$fforum=$_[0];
	$tthread=$_[1];
	$ppost=$_[2];

	$ppost++;
	unless (open(FILE1,">$HOME/$fforum/$fforum.$tthread.$ppost"))
	{
		$lc=100;
		while ($lc--)
		{
			$post++;
			if (open(FILE1,">$HOME/$fforum/$fforum.$tthread.$ppost"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to lock post file...try again.\n if problem persists, report to sysadmin.\n";
			return 0;
		}
	}
	&lock_file(FILE1);

	unless (open (FILE2,"$HOME/$fforum/$fforum.$tthread.1"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open(FILE2,"$HOME/$fforum/$fforum.$tthread.1"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to lock .1 file...try again.\n if problem persists, report to sysadmin.\n";
			&unlock_file(FILE1);
			close(FILE1);
			return 0;
		}
	}
	&lock_file(FILE2);
	
	print FILE1 "Forum: $fforum ";
	if ($THREADED) {print FILE1 "Thread: $tthread  ";}
	print FILE1 "Post: $ppost\n";
	print FILE1 "Post by: $whoiam\n";
	print FILE1 `date`;
	if (!$THREADED) 
	{
		print "Subject? ";
		$subject=<STDIN>;
		chop $subject;
		print FILE1 "Subject: $subject\n";
	}	
	else
	{
		$d=<FILE2>; $d=<FILE2>; $d=<FILE2>; $d=<FILE2>;
		print FILE1 $d;
	}
	&unlock_file(FILE2);
	close (FILE2);
	print FILE1 "\n\n";
	
	unless (fork)
	{
		exec("$EDITOR ~/.temp.new.post");
	}
	wait;
	if (-z $ENV{"HOME"}."/.temp.new.post")
	{
		print "Empty posts not accepted.\n";
		&unlock_file(FILE1);
		close(FILE1);
		unlink ("$HOME/$fforum/$fforum.$tthread.$ppost");
		return 0;
	}
	unless (-e $ENV{"HOME"}."/.temp.new.post")
	{
		print "Posting aborted.\n";
		&unlock_file(FILE1);
		close(FILE1);
		unlink ("$HOME/$fforum/$fforum.$tthread.$ppost");
		return 0;
	}
	

	$tempfilename=$ENV{"HOME"}."/.temp.new.post";
	open (FILE3,$tempfilename);
	while (<FILE3>)
	{
		print FILE1;
	}
	close(FILE3);
	unlink($tempfilename);
	$sigfile=$ENV{"HOME"}."/.forumsig";
	if (-e $sigfile)
	{
		open (FILE3,$sigfile);
		while (<FILE3>)
		{
			print FILE1;
		}
		close (FILE3);
	}
	&unlock_file(FILE1);
	close(FILE1);	
	
 	unless (open(FILE,"+<$HOME/$fforum/$fforum.$tthread.0"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open(FILE,"+<$HOME/$fforum/$fforum.$tthread.0"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to lock .0 file...try again.\n if problem persists, report to sysadmin.\n";
			return 0;
		}
	}
	&lock_file(FILE);
	$um=<FILE>;
	if ($um<$ppost)
	{
		seek(FILE,0,SEEK_SET);
		print FILE "$ppost\n";
	}
	&unlock_file(FILE);
	close (FILE);
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/$fforum/$fforum.$tthread.0");
	}
	chmod ($CHMOD,"$HOME/$fforum/$fforum.$tthread.0");
	
	return $ppost;
}
	
sub help_read
{
	unless (fork)
	{
		exec("clear");
	}
	wait;
	print "<enter>) move to next post in current thread.\n";
	print "+/-) forward/back one post in current thread.\n";
	print "+/- n) move n posts forward or back.\n"; 
	print "n) move to post number n (don't type a literal \"n\", type a number).\n";
	print "a)gain -- see the current post again.\n";
	if ($THREADED)
	{
		print ">/<) foreward/back one thread.\n";
		print "t n) move to (t)hread n.\n";
		print "w)rite a subject post for a new thread.\n";
	}
	print "p)ost ";
	if ($THREADED) { print "to current thread.\n";}
	else { print "\n"; }
	print "f)ollow current post.\n";
	print "m)oderate current post (must be moderator).\n";
	print "d)elete current post (you must be its owner).\n";
	if ($THREADED) { print "l)ist living threads.\n";}
	else { print "l)ist posts\n"; }
	print "k)atch all posts";
	if ($THREADED) 
	{
		print " in current thread (mark as read).\n";
		print "K)atch all posts in all threads in current forum (mark as read).\n";
	}
	else { print " (mark as read) \n"; }
	print "s)kip this forum (in a newscan, go to next forum).\n";
	print "c)lear the screen.\n";
	print "?/h)elp.\n";
	print "q)uit to lobby.\n";
	print "\n";
}	

sub new_scan
{
	local ($forum,$jfile);

	print "\n";
  
	$jfile=$ENV{"HOME"}."/.jfile";
	foreach $forum (@list_of_forums)
	{
		unless (open (JFILE,"$jfile"))
		{
		 	print "Unable to open needed file! newscan aborted.\n";
			return;
		}
		while ($e=<JFILE>)
		{
			@e=split(/\s+/,$e);
			if ($e[0] eq $forum) {last};
		}
		if ($e[0] eq $forum)
		{
			if ($e[1] != -1) 
			{
				close (JFILE);
				&read_a_forum("r $forum 1");
			}
			else
			{
				close (JFILE);
			}
		}
		else
		{
			print JFILE "$forum -1";
			close (JFILE);
		}
	}
	print "\n";
}

sub check_living
{
	local($f,$mtime,$xx,$numthreads,$tfile);
	$f=$_[0];

	if (!(-e "$HOME/$f/$f.0.0"))
	{
		&make_00_file;
	}
	unless (open(FILE,"$HOME/$f/$f.0.0"))  
	{
		$lc=100;
		while ($lc--)
		{
			if (open (FILE,"$HOME/$f/$f.0.0"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Cannot open 0.0 file. report error to sysadmin.\n";
			<STDIN>;
			return;
		}
	}
	&lock_file(FILE);
	$numthreads=<FILE>;
	&unlock_file(FILE);
	close (FILE);
	chop($numthreads);
	
	$tfile=$ENV{"HOME"}."/.temp.103";
	unless (open (FILE,"+>$HOME/$f/$f.living.list"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open(FILE,"+>$HOME/$f/$f.living.list"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to open living list. report error to sysadmin.\n";
			<STDIN>;
			return;
		}
	}
	&lock_file(FILE);

	$tfile=$ENV{"HOME"}."/.temp.forums.file";
	unless (open (FILE2,">$tfile"))
	{
		print "Unable to make temp file....check_living aborted.\nplease report error to sysadmin.\n";
		close(FILE);
		<STDIN>;
		return;
	}

	while (<FILE>)
	{
		$xx=$_;
		$mtime=(stat("$HOME/$f/$f.$xx.0"))[9];
		if (((time()-$mtime)<$MAXAGE) || (!THREADED))
		{
			if ($xx!=0) {print FILE2 "$xx\n";}
		}
	}
	&unlock_file(FILE);
	close (FILE);
	if ($numthreads==0)
	{
		print FILE2 "0\n";
		$xx=1;
	}
	else
	{	
		if ($xx == 0) { $xx=1; }
		for (;$xx<=$numthreads;$xx++)
		{
			$mtime=(stat("$HOME/$f/$f.$xx.0"))[9];
			if (((time()-$mtime)<$MAXAGE) || (!THREADED)) # thread is alive or we are not threading.
			{
				print FILE2 "$xx\n";
			}
		}
	}	
	close (FILE2);
	unlink("$HOME/$f/$f.living.list");
	unless (rename($tfile,"$HOME/$f/$f.living.list"))
	{
		print "Problem creating living list file.\n";
		return;
	}
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/$f/$f.living.list");
	}
	chmod ($CHMOD,"$HOME/$f/$f.living.list");
}
	
sub follow_post
{
	local ($fforum,$tthread,$ppost,$nnewpost,$tfile,$subj,$poster,@poster);
	$fforum=$_[0];
	$tthread=$_[1];
	$ppost=$_[2];
	$nnewpost=$_[3];

	$tfile=$ENV{"HOME"}."/.temp.follow.post";
	unless (open (TFILE,">$tfile"))
	{
		print "Unable to make temp file....followup aborted.\nplease report error to sysadmin.\n";
		return 0;
	}
	
	unless (open(FILE,"$HOME/$fforum/$fforum.$tthread.$ppost"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open(FILE,"$HOME/$fforum/$fforum.$tthread.$ppost"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to follow. try again or report error to sysadmin.\n";
			close(TFILE);
			return 0;
		}
	}
	&lock_file(FILE);
	$poster=<FILE>; $poster=<FILE>;$subj=<FILE>;$subj=<FILE>;
	@poster=split(/:/,$poster);
	chop($poster[1]);
	print TFILE "Earlier,$poster[1] wrote:\n";
	$poster=<FILE>;
	while (<FILE>)
	{
		print TFILE "> $_";
	}
	print TFILE "\n";
	&unlock_file(FILE);
	close (FILE);
	close (TFILE);

	$tfilesize=(-s $tfile);
	
	unless (fork)
	{
		exec("$EDITOR $tfile");
	}
	wait;

	unless (-e $tfile)
	{
		print "Posting aborted.\n";
		return 0;
	}
	if ((-s $tfile) == $tfilesize)
	{
		print "Posting Aborted\n";
		return 0;
	}

	while (-e "$HOME/$fforum/$fforum.$tthread.$nnewpost") {$nnewpost++;}
	unless (open (FILE,">$HOME/$fforum/$fforum.$tthread.$nnewpost"))
	{
		$lc=100;
		while ($lc--)
		{
			if (-e "$HOME/$fforum/$fforum.$tthread.$nnewpost") {$nnewpost++;}
			if (open(FILE,">$HOME/$fforum/$fforum.$tthread.$nnewpost"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to follow. try again or report error to sysadmin.\n";
			return;
		}
	}
	&lock_file(FILE);
	print FILE "Forum: $fforum Thread: $tthread  Post: $nnewpost\n";
	print FILE "Post by: $whoiam\n";
	print FILE "Date: ".`date`;
	@subj=split(/:/,$subj);
	if ($subj[1] eq " re ")
	{
		print FILE "Subject: re :$subj[2]";
	}
	else 
	{
		print FILE "Subject: re :$subj[1]";
	}
	print FILE "\n\n";
	
	open (TFILE,$tfile);
	while (<TFILE>)
	{
		print FILE;
	}
	$sigfile=$ENV{"HOME"}."/.forumsig";
	if (-e $sigfile)
	{
		open (SIG,$sigfile);
		while (<SIG>)
		{
			print FILE;
		}
		close (SIG);
	}
	close(TFILE);
	unlink($tfile);
	&unlock_file(FILE);
	close(FILE);

	unless (open (FILE2,"+<$HOME/$fforum/$fforum.$tthread.0")) 
	{
		$lc=100;
		while ($lc--)
		{
			if (open(FILE2,"+<$HOME/$fforum/$fforum.$tthread.0"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to follow. try again or report error to sysadmin.\n";
			return 0;
		}
	}
	&lock_file(FILE2);
	$um=<FILE2>;
	chop($um);
	if ($um<$nnewpost)
	{
		seek(FILE2,0,SEEK_SET);
		print FILE2 "$nnewpost\n";
	}
	&unlock_file(FILE2);
	close (FILE2);
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/$fforum/$fforum.$tthread.0");
	}
	chmod ($CHMOD,"$HOME/$fforum/$fforum.$tthread.0");
	return $nnewpost;
}

sub moderate_post
{
	local ($tthread,$ppost,$fforum,$x,$match,@moderators,$mod);
	$fforum=$_[0];
	$tthread=$_[1];
	$ppost=$_[2];
	
	$x=0;
	while ($fforum ne $list_of_forums[$x]) {$x++;}

	@moderators=split(/,/,$list_of_moderators[$x]);
	$match=0;
	foreach $mod (@moderators)
	{
		if ($whoiam eq $mod)
		{
			$match=1;
		}
	}
	unless ($match)
	{
		print "You do not have moderator priviledges for this forum.\n";
		return;
	}

	if (!(-e "$HOME/moderated/moderated.0.0"))
	{
		&make_00_file("moderated");
	}

	unless (open (FILE,"+<$HOME/moderated/moderated.0.0"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open (FILE,"+<$HOME/moderated/moderated.0.0"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to open 0.0 file....report error to sysadmin.\n";
			return;
		}
	}
	&lock_file(FILE);
	if ($THREADED) {$numthreads=<FILE>;
	chop($numthreads);
	$numthreads++;
	seek(FILE,0,SEEK_SET);}
	else
	{
		$numthreads=1;
	}
	print FILE "$numthreads\n";
	&unlock_file(FILE);
	close(FILE);
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/moderated/moderated.0.0");
	}
	chmod($CHMOD,"$HOME/moderated/moderated.0.0");

	unless (open (FILE1,"+<$HOME/moderated/moderated.$numthreads.0"))
	{
		$lc=100;
		while ($lc--)
		{
			if (open (FILE1,">$HOME/moderated/moderated.$numthreads.0"))
			{
				last;
			}
		}
		if ($lc==0)
		{
			print "Unable to open .0 file....report error to sysadmin.\n";
			return;
		}
	}
	&lock_file(FILE1);
	if ($THREADED)
	{
		print FILE1 "1\n";
		$np=1;
	}
	else
	{
		$np=<FILE1>;
		chop $np;
		$np++;
		seek(FILE1,0,SEEK_SET);
		print FILE1 $np;
	}
	&unlock_file(FILE1);
	close (FILE1);
	if ($SETGROUP)
	{
		chown($uid,$GROUP,"$HOME/moderated/moderated.$numthreads.0");
	}
	chmod ($CHMOD,"$HOME/moderated/moderated.$numthreads.0");

	unless (open (NFILE,">$HOME/moderated/moderated.$numthreads.$np"))
	{
		print "Problem moving file...report error to administrator.\n";
		return;
	}
	if ($THREADED) {print NFILE "Moderated thread # $numthreads\n";}
	print NFILE "Moved from the $fforum forum by $whoiam on ".`date`."\n";
	open (OLDFILE,"$HOME/$fforum/$fforum.$tthread.$ppost");
	while (<OLDFILE>)
	{
		print NFILE $_;
	}	
	close (OLDFILE);
	close (NFILE);
	unless(fork)
	{
		exec("cp $MODFILE $HOME/$fforum/$fforum.$tthread.$ppost");
	}
	wait;
}		

sub welcome
{
	if (-s $WELCOMEPAGE)
	{
		unless (fork)
		{
			exec("$PAGER $WELCOMEPAGE");
		}
		wait;
	}
}
	

sub goodbye
{
	if (-s $GOODBYEPAGE)
	{
		unless (fork)
		{
			exec("$PAGER $GOODBYEPAGE");
		}
		wait;	
	}
}

sub lock_file
{
	local($ffile,$LOCK_EX);
	$LOCK_EX=2;
	
	$ffile=$_[0];
	flock($ffile,$LOCK_EX);
	seek($ffile,0,SEEK_SET);
}	

sub unlock_file
{
	local($ffile,$LOCK_UN);
	$LOCK_UN=8;

	$ffile=$_[0];
	flock($ffile,$LOCK_UN);
}

sub clean_up_wholist
{
	local (@wholist1,@wholist2,$who1,$who2,$ok);

	@wholist1=`who`;
	@wholist2=(<$HOME/who/*>);
	
	foreach $who2 (@wholist2)
	{
		$ok=0;
		foreach $who1 (@wholist1)
		{
			@splitwho1=split(/\s+/,$who1);
			@splitwho2=split(/\//,$who2);
			if ($splitwho2[@splitwho2-1] eq $splitwho1[0]) { $ok=1;last;}
		}
		if (!$ok)
		{
			unlink("$HOME/who/$splitwho2[@splitwho2-1]");
		}
	} 
}

sub update_jfile
{
	local($a,$c,@b,$x);

	$a=$ENV{"HOME"}."/.jfile";
	unless (open (JFILE,"$a"))
	{
		print "error updating jfile.\n";
		return;
	}
	$c=$ENV{"HOME"}."/.tjfile";
	unless (open(TJFILE,">$c"))
	{
		print "error updating jfile.\n";
		close (JFILE);
		return;
	}

	$x=0;
	while ($x<@list_of_forums)
	{
		while (<JFILE>)
		{
			@b=split (/\s+/);
			if ($b[0] eq $list_of_forums[$x])
			{
				last;
			}
		}
		if ($b[0] eq $list_of_forums[$x])
		{
			print TJFILE "@b\n";
		}
		else
		{
			print TJFILE "$list_of_forums[$x] -1\n";
		}
		seek(JFILE,0,0);
		$x++;
	}

	close (JFILE);
	close (TJFILE);
	unless (unlink ($a))
	{
		print "Problem updating jfile...\n";
		return;
	}
	unless (rename($c,$a))
	{
		print "Problem updating jfile...unable to rename tjfile.\n";
		return;
	}
}

sub delete_post
{
	local ($f,$t,$p);
	$f=$_[0];
	$t=$_[1];
	$p=$_[2];
	
	unless (-O "$HOME/$f/$f.$t.$p")
	{
		print "\nUnable to delete post $f.$t.$p.\n";
		print "If you are its rightful owner, report the problem to the admin.\n";
		print "Otherwise, shame on you! you should know better.\n\n";
		return;
	}
	unless (open(OLDFILE,">$HOME/$f/$f.$t.$p"))
	{
		print "\nHaving a problem deleting your post....please report the error\n";
		print "to tha admin with the 'a' command from the lobby.\n\n";
		return;
	}
	unless (open (DELFILE,$DELFILE))
	{
		print "\nunable to delete post.  this error should not occur.\n";
		print "Please notify the admin with the 'a' command from the lobby\n\n";
		close(OLDFILE);
		return;
	}

	while (<DELFILE>)
	{
		print OLDFILE;
	}

	close (DELFILE);
	close (OLDFILE);
}
	
