#!/usr/bin/perl 

$admin = "no";

require "config.cgi";
require "cgi-lib.sol";

###########################################
### Who are we dealing with?
###########################################

open (MEMBERS, "$membersfile");

while (<MEMBERS>)

        {

        ($tusername, $tpassword, $tname, $temail, $tweb, $texpiration, 
        $twhoknows) = split (/::/,$_);

        if ($tusername eq $ENV{'REMOTE_USER'})

                {
		$tnamet = $tname;
		$tpasswordt = $tpassword;
		$temailt = $temail;
                }

        }

close (MEMBERS);

$FORM{'where'} = "register";
$FORM{'link'} = $out_link;
$FORM{'time'} = $time;
$FORM{'name'} = $tnamet;
$FORM{'passwd'} = $tpasswordt;
$FORM{'mail'} = $temailt;
$FORM{'room'} = $entry;

######################

$script_name = $ENV{'SCRIPT_NAME'};
$lock_file = "lock2";
&lock;

###############
# Lock File
sub lock {

   $time = time();
   $quit = 0;
   while ($quit != 1) {
      if (-e "$file_dir/$lock_file") {
         if ($time + $lock_time < time()) {
            &parse_form;
         }
         else {
           sleep(1);
         }
      }
      else {
         open(LOCK,">$file_dir/$lock_file");
         close LOCK;
         &parse_form;
      }
   }
}

########################
# Parse Form
# This subroutine is taken from Matt Wright's WWWBoard script
# with a few minor modifications.

sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      # Un-Webify plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;
      $value =~ s/<([^>]|\n)*>//g;

      $FORM{$name} = $value;
   }
   &get_variables;
}

###########
# Get variables from form

sub get_variables {

   if ($FORM{'where'}) {
      $where = "$FORM{'where'}";
   }   

   if ($FORM{'room'}) {
      $room = "$FORM{'room'}";
      $room =~ s/ /_/g;
      $room =~ s/\//_/g;
      $oldroom = $room;
   }

   if ($FORM{'pubroom'}) {
      $pubroom = "$FORM{'pubroom'}";
      $pubroom =~ s/ /_/g;
      $pubroom =~ s/\//_/g;
   }

   if ($FORM{'priroom'}) {
      $priroom = "$FORM{'priroom'}";
      $priroom =~ s/ /_/g;
      $priroom =~ s/\//_/g;
   }

   if ($FORM{'solo'}) {
      $solo = "$FORM{'solo'}";
   }

   if ($FORM{'link'}) {
      $link = "$FORM{'link'}";
      if ($link ne "") {
         $leave_link = $link;
      }
   }

   if ($FORM{'name'}) {
      $name = "$FORM{'name'}";
      $name =~ s/"//g;
      $name =~ s/<//g;
      $name =~ s/>//g;
      $name =~ s/\&//g;
      $name =~ s/\(//g;
      $name =~ s/\)//g;
      $name =~ s/\$/S/g;
      $name =~ s/\*//g;
      $name =~ s/\?//g;
   }

   if ($FORM{'load_time'}) {
      $load_time = "$FORM{'load_time'}";
   }

   if ($FORM{'mail'} =~ /.*\@.*\..*/) {
      $mail = "$FORM{'mail'}";
   }

   if ($FORM{'passwd'}) {
      $passwd = "$FORM{'passwd'}";
   }

   if ($FORM{'nummes'}) {
      $nummes = $FORM{'nummes'};
   }

   if ($FORM{'message'}) {
      $message = $FORM{'message'};
      $message =~ s/\cM//g;
      $message =~ s/\n\n/<p>/g;
      $message =~ s/\n/<br>/g;
      $message =~ s/&lt;/</g;
      $message =~ s/&gt;/>/g;
      $message =~ s/&quot;/"/g;
   }

   ($sec,$min,$hour,$day,$month,$year) = localtime(time);

   if ($sec < 10) {
      $sec = "0$sec";
   }

   if ($min < 10) {
      $min = "0$min";
   }

   if ($hour < 10) {
      $hour = "0$hour";
   }

   $date = "$hour\:$min\:$sec";
   $recorddate = "$date $month/$day/$year";;
   chop($date) if ($date =~ /\n$/);

   $time = time();
   $entry =~ s/ /_/g;
   $entry =~ s/\//_/g;

   &location;
}

##########
# Where are they coming from?

sub location {

   if ($ENV{'HTTP_REFERER'} =~ /http:\/\/([^\/]+)/) {
      if (!($1 =~ /$valid_referer/)) {
         &unlock;
         exit;
      }
   }


   if ($name eq "link") {
   }
   elsif ($solo ne "") {
      if ($solo eq "$return_to_chat") {
         &update;   # Update the reload time in the visitors file
         &reload;   # Reload the chat page with the latest information
         &unlock;   # Unlock the script
      }
      else {   
         &privmsg;  # Send the 1 on 1 message
         &update;   # Update the reload time in the visitors file
         &reload;   # Reload the chat page with the latest information
         &unlock;   # Unlock the script
      }
   }
   elsif ($where eq "register") {
      &create;   # Maintain files (create, delete, and shorten)
      &register;  # Add their information to the visitors file
      &reload;    # Reload the chat page with the latest information
      &unlock;    # Unlock the script
   }
   elsif ($where eq "$one_on_one_text") {
      &create;    # Maintain files (create, delete, and shorten)
      &one_on_one; # Print the one on one page
      &unlock;     # Unlock the script
   }
   elsif ($where eq "$post_text") {
      &create;  # Maintain files (create, delete, and shorten)
      &post;     # Add their message to the chat file
      &update;   # Update the reload time in the visitors file
      &reload;   # Reload the chat page with the latest information
      &unlock;   # Unlock the script
   }

   elsif ($where eq "$reload_text") {
      &snag;     # Get the information in the message field
      &create;  # Maintain files (create, delete, and shorten)
      &update;   # Update the reload time in the visitors file
      &reload;   # Reload the chat page with the latest information
      &unlock;   # Unlock the script
   }
   elsif ($where eq "$go_there") {
      if ($priroom ne "") {
         $room = "$priroom";
         &create;  # Maintain files (create, delete, and shorten)
         &change;   # Remove the person from the old room
         &register; # Add their information to the visitors file
         &reload;   # Reload the chat page with the latest information
         &unlock;   # Unlock the script
      }
      elsif ($pubroom ne "") {
         $room = "$pubroom";
         &create;  # Maintain files (create, delete, and shorten)
         &change;   # Remove the person from the old room
         &register; # Add their information to the visitors file
         &reload;   # Reload the chat page with the latest information
         &unlock;   # Unlock the script
      }
      else {
         &create;  # Maintain files (create, delete, and shorten)
         &update;   # Update the reload time in the visitors file
         &reload;   # Reload the chat page with the latest information
         &unlock;   # Unlock the script
      }
   }
   elsif ($where eq "$leave_text") {
      &leave;    # Remove person's name from visitors file and send them away
      &unlock;   # Unlock the script
   }
   else {
      &countv; # Count the number of visitors
      &unlock;  # Unlock the script
   }
   exit;
}
########################
# Create files if they don't exist
sub create {
   open(INDEX,">$file_dir/index.html");
   print INDEX "$index_html\n";
   close(INDEX);

   open(INDEXB,">$vis_dir/index.html");
   print INDEXB "$index_html\n";
   close(INDEXB);

   &createv;
}

sub createv {
   if (-e "$file_dir/vis$room") {
      open (VISITORS,"$file_dir/vis$room");
      @visitors = <VISITORS>;
      close (VISITORS);

      if ($visitors[0] =~ /\<!--begin--\>/) {
         foreach $visitors_line (@visitors) {
            ($bogus,$load,$bogus2) = split(/:/,$visitors_line);
            if ($visitors_line =~ /\<!--begin--\>/) {
               push (@visitors_new,"<!--:$time:--><!--begin-->\n");
            }
            elsif ($time - $load > $visitors_time) {
            }

            else {
               push (@visitors_new,"$visitors_line");
            }
         }
         open(VISITORS,">$file_dir/vis$room");
         foreach $visitors_line(@visitors_new) {
            print VISITORS "$visitors_line";
         }
         close (VISITORS);
         undef @visitors_new;
      }

      else {
         open (VISITORS,">$file_dir/vis$room");
         print VISITORS "<!--:$time:--><!--begin-->";
         close (VISITORS);
      }
   }
   else {
      open (VISITORS,">$file_dir/vis$room");
      print VISITORS "<!--:$time:--><!--begin-->";
      close (VISITORS);
   }
   opendir(FILES,$file_dir);
   @filelist =  grep(!/^\.\.?$/, readdir (FILES));
   closedir(FILES);
   foreach $filelist (@filelist) {
      if (-A "$file_dir/$filelist" > $chat_time / 86400) {
         unlink("$file_dir/$filelist");
      }
   }

   &createc;

}

sub createc {
   if (-e "$file_dir/chat$room") {

      open(MAIN,"$file_dir/chat$room");
      @main = <MAIN>;
      close(MAIN);

      if ($main[0] =~ /\<!--begin--\>/) {

         foreach $main_line (@main) {
            ($bogus,$entry,$bogus2) = split(/:/,$main_line);
            if ($main_line =~ /\<!--begin--\>/) {
               push (@main_new, "<!--:$time:--><!--begin-->\n");
            }
            elsif ($time - $entry > $chat_time) {
            }

            else {
               push (@main_new,"$main_line")
            }

         }
         open(MAIN,">$file_dir/chat$room");
         foreach $main_line(@main_new) {
            print MAIN "$main_line";
         }
         close (MAIN);
         undef @main_new;
      }
      else {
         open (MAIN,">$file_dir/chat$room");
         print MAIN "<!--:$time:--><!--begin-->\n";
         print MAIN "<!--:$time:-->The chat file somehow became corrupted, but it has been fixed and all is fine now.<hr noshade size=1>\n";
         close (MAIN);
      }

   }
   else {
      open (CHAT,">$file_dir/chat$room");
      print CHAT "<!--:$time:--><!--begin-->";
      close (CHAT);
   }
   &createone;
}

sub createone {
   $vname = $name;
   $vname =~ s/ /_/g;
   $vname =~ s/\//_/g;
   if (-e "$vis_dir/$vname") {
   }
   else {
      $password = crypt($passwd,MW);
      open (VFILE,">$vis_dir/$vname");
      print VFILE "<!--:$time:--><!--begin--><!--:$password:-->\n";
      close (VFILE)
   }
   &countv;
}
sub countv {
   opendir(VFILES,$vis_dir);
   @vfilelist =  grep(!/^\.\.?$/, readdir (VFILES));
   closedir(VFILES);
   foreach $vfilelist (@vfilelist) {
      if (-A "$vis_dir/$vfilelist" > $visitors_time / 86400) {
         unlink("$vis_dir/$vfilelist");
      }
   }

}

###########
# Register
sub register {

   $login = "$date";
   
   open (VISITORS,"$file_dir/vis$room") || die $!;
   @visitors = <VISITORS>;
   close (VISITORS);

   foreach $visitors_line (@visitors) {
      if ($visitors_line =~ /<!--$name-->/) {
      }
      elsif ($visitors_line =~ /\<!--begin--\>/) {
         push (@visitors_new,"<!--:$time:--><!--begin-->\n");
         if ($mail eq "") {
            push (@visitors_new,"<!--:$time:--><tr><td><!--$name--><b>$name</b></td> <td> <br></td></tr>\n");
         }
         else {
            push (@visitors_new,"<!--:$time:--><tr><td><!--$name--><a href=\"mailto:$mail\"><b>$name</b></a></td> <td><br> </td></tr>\n");
         }
      }
      else {
         push (@visitors_new,"$visitors_line");
      }
   }
   open(VISITORS,">$file_dir/vis$room") || die $!;
   foreach $visitors_line(@visitors_new) {
      print VISITORS "$visitors_line";
   }
   close(VISITORS);
   undef @visitors_new;

   $vname = $name;
   $vname =~ s/ /_/g;
   $vname =~ s/\//_/g;
   open(VFILE,"$vis_dir/$vname");
   @vfile = <VFILE>;
   close(VFILE);

   open(VFILE,">$vis_dir/$vname");
   foreach $vfile_line (@vfile) {
      if ($vfile_line =~ /\<!--begin--\>/) {
         push (@vfile_new,"$vfile[0]","<!--:$time:-->$name, $reload_remind<p>\n");
      }
      else {
         push (@vfile_new,"$vfile_line");
      }
   }
   open(VFILE,">$vis_dir/$vname");
   foreach $vfile_line(@vfile_new) {
      print VFILE "$vfile_line";
   }
   close (VFILE);
   undef @vfile_new;

   open(MAIN,"$file_dir/chat$room") || die $!;
   @main = <MAIN>;
   close(MAIN);

   foreach $main_line (@main) {
      if ($main_line =~ /\<!--begin--\>/) {
         push 
(@main_new,"<!--:$time:--><!--begin-->\n","<!--:$time:--><b>$name</b> $joined_chat $date<p>\n");
      }

      else {
        push (@main_new,"$main_line");
      }
   }
   open(MAIN,">$file_dir/chat$room") || die $!;
   foreach $main_line(@main_new) {
      print MAIN "$main_line";
   }
   close(MAIN);
   undef @main_new;
}
##########
# Modify chat page

sub post {
   if ($message ne "") {
      open(MAIN,"$file_dir/chat$room") || die $!;
      @main = <MAIN>;
      close(MAIN);

      foreach $main_line (@main) {
         if ($main_line =~ /\<!--begin--\>/) {
            push (@main_new,"<!--:$time:--><!--begin-->\n","<!--:$time:--><b>$name</b> $date<br>$message<p>\n");
         }

         else {
            push (@main_new,"$main_line");
         }
      }
      open(MAIN,">$file_dir/chat$room") || die $!;
      foreach $main_line(@main_new) {
         print MAIN "$main_line";
      }
      close(MAIN);
      undef @main_new;
   }
}
#########
# Snag information when person hits reload

sub snag {
   $mess = $message;
}
##########
# Update Visitor's log

sub update {
   open (VISITORS,"$file_dir/vis$room") || die $!;
   @visitors = <VISITORS>;
   close (VISITORS);

   foreach $visitors_line (@visitors) {
      if ($visitors_line =~ /<!--$name-->/) {
      }
      elsif ($visitors_line =~ /\<!--begin--\>/) {
         push (@visitors_new,"<!--:$time:--><!--begin-->\n");
         if ($mail eq "") {
            push (@visitors_new,"<!--:$time:--><tr><td><!--$name--><b>$name</b></td> <td>$date<br></td></tr>\n");
         }
         else {
            push (@visitors_new,"<!--:$time:--><tr><td><!--$name--><a href=\"mailto:$mail\"><b>$name</a></b></td> <td>$date<br></td></tr>\n");
         }
      }
      else {
         push (@visitors_new,"$visitors_line");
      }
   }
   open(VISITORS,">$file_dir/vis$room") || die $!;
   foreach $visitors_line(@visitors_new) {
      print VISITORS "$visitors_line";
   }
   close(VISITORS);
   undef @visitors_new;
}

############
# Reload
sub reload {
   open(MAIN,"$file_dir/chat$room") || die $!;
   @main = <MAIN>;
   close(MAIN);

   open (VISITORS,"$file_dir/vis$room") || die $!;
   @visitors = <VISITORS>;
   close (VISITORS);

   $vname = $name;
   $vname =~ s/ /_/g;
   $vname =~ s/\//_/g;
   open (VFILE,"$vis_dir/$vname");
   @vfile = <VFILE>;
   close (VFILE);

   shift(@main);
   shift(@vfile);
   @unchat = @main;
   push (@unchat,@vfile);
   @schat = sort(@unchat);
   @chat = reverse(@schat);

   print "Content-type: text/html\n\n";
   print "<html><head>

<script language=\"JavaScript\">
function openWindow(url) {
        popUpWin = window.open(url,'demoWin','toolbar=0,location=yes,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=450,height=300');
        if (navigator.appName == 'Netscape') {
                popUpWin.focus();
        }
}
</script>

<title>$page_title</title></head>\n";
   print "<body bgcolor=$bgcolor background=\"$background\" text=$text_color link=$link_color vlink=$vlink_color>\n";
   print "<center><form method=POST action=\"$script_name\">\n";
   print "<table border=0><tr valign=top>\n";
   print "<td><table cellpadding=0><tr>\n";
   print "<td><input type=submit name=\"where\" value=\"$post_text\"></td>\n";
   print "</tr><tr>\n";
   print "<td><input type=submit name=\"where\" value=\"$reload_text\"></td>\n";
   print "</tr><tr>\n";
   print "<td><input type=submit name=\"where\" value=\"$one_on_one_text\"></td>\n";
   print "</tr><tr>\n";
   print "<td><input type=reset value=\"$clear_text\"></td>\n";
   print "</tr><tr>\n";
   print "<td><input type=submit name=\"where\" value=\"$leave_text\"><br></td></tr>\n";
   print "</tr></table></td><td valign=bottom><center>\n";
   print "<font size=4 color=$link_color>$page_title";

if ($name eq "$admin_name") {$admin = "yes"}
   if ($admin eq "yes")
   {print " (admin. version) ";}
   print "</font></center>\n";
   print "<input type=hidden name=\"load_time\" value=\"$time\">\n";
   print "<input type=hidden name=\"room\" value=\"$room\">\n";
   print "<input type=hidden name=\"mail\" value=\"$mail\">\n";
   print "<input type=hidden name=\"name\" value=\"$name\">\n";
   print "<input type=hidden name=\"link\" value=\"$leave_link\">\n";
   print "<input type=hidden name=\"passwd\" value=\"$passwd\">\n";
   print "<table width=100%><tr align=center>\n";
   if ($help_file ne "") {
      print "<td width=*><a href=\"$help_file\">$help_b</a></td>\n";
   }
   print "</tr></table><textarea COLS=50 ROWS=5 name=\"message\" wrap>\n";
   print "$mess</textarea></center></td>\n";

$nummes = "40";

   print "</table></td></tr></table>\n";
   print "<table border=0 width=100%><tr valign=top>\n";
   print "<td width=140><table cellpadding=0 border=1>\n";
   print "<tr><td valign=bottom><b><font color=800000>$people</font></b>\n";
   print "</td><td valign=bottom><font color=$link_color>$last</font><br></td></tr><tr></tr>@visitors\n";
   print "</table>\n";
   print "<table border=0><tr><td>\n";
   print "<font size=4>$change_rooms</font>\n";
   print "$currently_in<br><b>$room</b>\n";
   print "</td></tr><tr>\n";
   print "<td>$pub_room_title<br>\n";
   print "<select size=6 name=\"pubroom\">$public_rooms</select><br></td></tr>\n";
   print "<tr><td>\n";
   print "<input type=submit name=\"where\" value=\"$go_there\"></td></tr>\n";
if ($name eq "$admin_name") {$admin = "yes"}
   if ($admin eq "yes")
	{print "
	<br>
	<tr><td>\n
	<br>\n
	</form>
<font size=4>$admin_functions:</font><br>
<b>
<li><a href=\"javascript:openWindow('$monitor_url')\">$mon_button</a><br>
<li><a href=\"javascript:openWindow('$records_url')\">$rec_button</a><br>
<li><a href=\"javascript:openWindow('$clear_url')\">$clear_button</a><br>
<li><a href=\"javascript:openWindow('$archive_url')\">$archive_button</a>
</b>
	</td></tr>\n";}

print "
</table></td>\n";
   print "<td width=100%>\n";

#start
print "<table border=1 width=100% bgcolor=$message_area_color><tr><td>";

   if ($nummes eq "10") {
      @ten = @chat[(0 .. 10)];
      foreach $one_of_ten (@ten) {
         ($bogus,$mes_time,$bogus2) = split(/:/,$one_of_ten);
         if ($mes_time > $load_time) {
            print "<font color=$newcolor>$one_of_ten</font>";
         }
         else {
            print "<font color=$oldcolor>$one_of_ten</font>";
         }
      }
   }
   elsif ($nummes eq "20" || $nummes eq "") {
      @twenty = @chat[(0 .. 20)];
      foreach $one_of_twenty (@twenty) {
         ($bogus,$mes_time,$bogus2) = split(/:/,$one_of_twenty);
         if ($mes_time > $load_time) {
            print "<font color=$newcolor>$one_of_twenty</font>";
         }
         else {
            print "<font color=$oldcolor>$one_of_twenty</font>";
         }
      }

   }
   elsif ($nummes eq "30") {
      @thirty = @chat[(0 .. 30)];
      foreach $one_of_thirty (@thirty) {
         ($bogus,$mes_time,$bogus2) = split(/:/,$one_of_thirty);
         if ($mes_time > $load_time) {
            print "<font color=$newcolor>$one_of_thirty</font>";
         }
         else {
            print "<font color=$oldcolor>$one_of_thirty</font>";
         }
      }

   }
   elsif ($nummes eq "40") {
      @forty = @chat[(0 .. 40)];
      foreach $one_of_forty (@forty) {
         ($bogus,$mes_time,$bogus2) = split(/:/,$one_of_forty);
         if ($mes_time > $load_time) {
            print "<font color=$newcolor>$one_of_forty</font>";
         }
         else {
            print "<font color=$oldcolor>$one_of_forty</font>";
         }
      }
   }
   elsif ($nummes eq "new") {
      @new = @chat[(0 .. 30)];
      foreach $one_of_new (@new) {
         ($bogus,$mes_time,$bogus2) = split(/:/,$one_of_new);
         if ($mes_time > $load_time) {
            print "<font color=$newcolor>$one_of_new</font>";
         }
         elsif ($mes_time + $old_time > $load_time) {
            print "<font color=$oldcolor>$one_of_new</font>";
         }
      }
   }

#finish
print "</td></tr></table>";

   print "</td></tr></table>\n";
   print "</body> </html>\n";


###################################################################
#                    Edit the Results Data File.                  #
###################################################################

# Use cgi-lib.sol to create a lock file.  This lock file will prevent two 
# people from accessing the resuls database at the same time.

&GetFileLock ("$lock_file");

# Open the results data file for "appending" (>>)

open (RECORDS, ">>$records_file") || &CgiDie ("I am sorry, but I
        was not able to open the results data file in the Edit the
        Results Data File routine.  The value I have is $records_file.
        Would you check the path and the permission.");

if ($message ne "") {

$hostname = $ENV{'REMOTE_HOST'};
print RECORDS "<tr>\n";
print RECORDS "<td>$name\n";
print RECORDS "<td>$message\n";
print RECORDS "<td>$room\n";
print RECORDS "<td>$recorddate\n";
print RECORDS "<td>$hostname\n";
close (RECORDS);

}

}
##############
# Change rooms
sub change {
   $load_time = "0";
   open(MAIN,"$file_dir/chat$oldroom") || die $!;
   @main = <MAIN>;
   close(MAIN);

   foreach $main_line (@main) {
      if ($main_line =~ /\<!--begin--\>/) {
         push 
(@main_new,"<!--:$time:--><!--begin-->\n","<!--:$time:--><b>$name</b> $left_room $date<br><p>\n");
      }
      else {
         push (@main_new,"$main_line");
      }
   }
   open(MAIN,">$file_dir/chat$oldroom") || die $!;
   foreach $main_line(@main_new) {
      print MAIN "$main_line";
   }
   close(MAIN);
   undef @main_new;

   open (VISITORS,"$file_dir/vis$oldroom") || die $!;
   @visitors = <VISITORS>;
   close (VISITORS);

   foreach $visitors_line (@visitors) {
      if ($visitors_line =~ /<!--$name-->/) {
      }
      else {
         push (@visitors_new,"$visitors_line");
      }
   }
   open(VISITORS,">$file_dir/vis$oldroom") || die $!;
   foreach $visitors_line(@visitors_new) {
      print VISITORS "$visitors_line";
   }
   close(VISITORS);
   undef @visitors_new;
}
###############
# One on one
sub one_on_one {

   $vname = $name;
   $vname =~ s/ /_/g;
   $vname =~ s/\//_/g;

   opendir(VFILES,$vis_dir);
   @vfilelist =  grep(!/^\.\.?$/, readdir (VFILES));
   closedir(VFILES);
   @vfilelist = sort(@vfilelist);
   print "Content-type: text/html\n\n";
   print "<html><head>

<script language=\"JavaScript\">
function openWindow(url) {
        popUpWin = window.open(url,'demoWin','toolbar=0,location=yes,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=450,height=300');
        if (navigator.appName == 'Netscape') {
                popUpWin.focus();
        }
}
</script>

<title>$one_on_one_page</title></head>\n";
   print "<body bgcolor=$bgcolor background=\"$background\" text=$text_color link=$link_color vlink=$vlink_color>\n";
   print "<font size=4 color=$link_color>$page_title</font>\n";
   print "<font size=4>$one_on_one_page</font>\n";
   print "$one_on_one_instruct\n";
   print "<form method=post action=\"$script_name\">\n";
   print "<input type=hidden name=\"load_time\" value=\"$load_time\">\n";
   print "<input type=hidden name=\"room\" value=\"$room\">\n";
   print "<input type=hidden name=\"mail\" value=\"$mail\">\n";
   print "<input type=hidden name=\"name\" value=\"$name\">\n";
   print "<input type=hidden name=\"link\" value=\"$leave_link\">\n";
   print "<input type=hidden name=\"nummes\" value=\"$nummes\">\n";
   print "<textarea cols=50 rows=4 name=\"message\" wrap>$message</textarea><br>\n";
   print "<font size=4>$one_on_one_to</font size=4>\n";
   foreach $vfilelist (@vfilelist) {
      if ($vfilelist ne "index.html" && $vfilelist ne "$vname") {
         print "<input type=submit name=\"solo\" value=\"$vfilelist\">\n";
      }
   }
   print "<br>--------------------<br>\n";
   print "<input type=submit name=\"solo\" value=\"$return_to_chat\">\n";
   open (VFILE,"$vis_dir/$vname") || die $!;
   @vfile = <VFILE>;
   close (VFILE);
   print "<hr noshade size=1></center>@vfile\n";
   print "</form>
</body></html>\n";
}

##############
# Send the private message
sub privmsg {
   open (VFILE,"$vis_dir/$solo") || die $!;
   @vfile = <VFILE>;
   close (VFILE);
   if ($message ne "") {
      foreach $vfile_line (@vfile) {
         if ($vfile_line =~ /\<!--begin--\>/) {
            push (@vfile_new,"$vfile_line","<!--:$time:--><font color=$one_color>$one_on_one_text: <b>$name</b> $date<br> $message</font><p>\n");
         }
         else {
            push (@vfile_new,"$vfile_line");
         }
      }
      open(VFILE,">$vis_dir/$solo") || die $!;
      foreach $vfile_line(@vfile_new) {
         print VFILE "$vfile_line";
      }
      close(VFILE);
      undef @vfile_new;
   }

   $vname = $name;
   $vname =~ s/ /_/g;
   $vname =~ s/\//_/g;
   open (VFILE,"$vis_dir/$vname") || die $!;
   @vfile = <VFILE>;
   close (VFILE);
   if ($message ne "") {
      foreach $vfile_line (@vfile) {
         if ($vfile_line =~ /\<!--begin--\>/) {
            push (@vfile_new,"$vfile_line","<!--:$time:--><font color=$one_color>$one_on_one_text: <b>To: $solo</b> $date<br>$message</font><p>\n");
         }
         else {
            push (@vfile_new,"$vfile_line");
         }
      }
      open(VFILE,">$vis_dir/$vname") || die $!;
      foreach $vfile_line(@vfile_new) {
         print VFILE "$vfile_line";
      }
      close(VFILE);
      undef @vfile_new;
   }
}
###############
# Leave
sub leave {
   open(MAIN,"$file_dir/chat$room") || die $!;
   @main = <MAIN>;
   close(MAIN);

   foreach $main_line (@main) {
      if ($main_line =~ /\<!--begin--\>/) {
         push (@main_new,"<!--:$time:--><!--begin-->\n","<!--:$time:--><b>$name</b> $left_chat $date<br><p>\n");
      }

      else {
         push (@main_new,"$main_line");
      }
   }
   open(MAIN,">$file_dir/chat$room") || die $!;
   foreach $main_line(@main_new) {
      print MAIN "$main_line";
   }
   close(MAIN);
   undef @main_new;

   open (VISITORS,"$file_dir/vis$room") || die $!;
   @visitors = <VISITORS>;
   close (VISITORS);

   foreach $visitors_line (@visitors) {
      if ($visitors_line =~ /<!--$name-->/) {
      }
      else {
         push (@visitors_new,"$visitors_line");
      }
   }
   open(VISITORS,">$file_dir/vis$room") || die $!;
   foreach $visitors_line(@visitors_new) {
      print VISITORS "$visitors_line";
   }
   close(VISITORS);
   undef @visitors_new;

   print "Location: $leave_link\n\n";
   $vname = $name;
   $vname =~ s/ /_/g;
   $vname =~ s/\//_/g;
   unlink("$vis_dir/$vname");
}

###########
# Unlock
sub unlock {
   unlink("$file_dir/$lock_file");
   $quit = 1;
}
