#!/opt/perl/bin/perl
#################################
# ChatPro Script Version 1.1
# by Mike Wheeler
# mwheeler@gladstone.uoregon.edu
#
# Copyright 1996, All Rights Reserved
#
# This script and others available at 
# http://gladstone.uoregon.edu/~mwheeler/cgi
#
# This script has parts (form parsing) borrowed from and a lot 
# learned from the great scripts at Matt's Script Archive at 
# http://worldwidemart.com/scripts/
#################################
#
# Define Variables
# The first thing you need to do is define the variables to make this 
# script work for you. There are A LOT of variables, but don't worry,
# it's not that bad. You only NEED to set a few of them. The rest are to
# customize the look and language of the script.
#
# A couple guidelines, if you use a quotation mark (") at symbol (@)
# or dollar sign ($) in any of these variables, you MUST but a 
# backslash (\) immediatley before that character.
#
##################
# Required Changes
#
# These are variables you must set for the script to run on your server.

$valid_referer = "www.auburn.edu";
# This is the domain from which the script will run.
# The part immediately following http:// and before any slashes (/)
# This insures the script will only accept forms sent from this
# domain. It fills a big security hole pointed out by Matt Wright.

$file_dir = "/home/duc4/collear/public_html/chatpro/files"; 
# This is the directory path to the chat and visitor files that
# will be made for each room. These files are deleted after haven't
# been used for a set amount of time so make this a directory
# exclusively for these files. This directory must be set to chmod 777

$vis_dir = "/home/duc4/collear/public_html/chatpro/visitors";
# This is the directory path to the visitor files that
# will be made for each person. These files are deleted after they haven't
# been used for a set amount of time so make this a directory
# exclusively for these files. This directory must be set to chmod 777
#
# That is all you NEED to change for the script to run on your server.
# Remember you MUST set the two directories' permissions to chmod 777
# and the script itself MUST be set to chmod 755
##############
# The Look
#
# These are variables to control the look of the script. You'll
# want to change some of them, but many are fine as they are.

$page_title = "AU Study"; 
# This is the title of the chat page

$title_graphic = "http://austudy.auburn.edu/images/austudy10.gif"; 
# Make it blank if you don't have a title graphic. If you do, it should be 
# about 270 pixels wide and 30 pixels tall

$background = "http://www.auburn.edu/~collear/paper.jpg";
# This is the url of a background texture for all the pages this script
# produces. Leave it blank if you don't want a background texture.

$bgcolor = "ccccff";
# This is the background color for all the pages this script produces.
# Remember to use hex numbers (ffffff is white). Some browsers will
# accept literal color names such as "red".

$text_color = "000000";
$link_color = "0000ff";
$vlink_color = "660099";
# This is the text color, the link color and the viewed link color for
# all the pages this script produces. 

$oldcolor = "444444";
# This is the color of the text for messages a person has already read

$one_color = "cc0000";
# This is the color of the text for one on one messages. This is also
# the color of the large message on the registration page telling people
# to reload

$new_message = "<img src=\"http://www.auburn.edu/~collear/new.gif\">";
# This is what goes before the name for new messages. A simple "NEW:"
# will suffice if you don't have a graphic you want to use. If you do
# have a graphic put an image tag in here for example
# <img src=\"new.gif\"> Be sure to put a backslash before the quotation
# marks.
#
##################
# The Feel
#
# These are variables to control the feel of the script. You'll
# want to change some of them, but many are fine as they are.

$schedule_file = "http://www.auburn.edu/~collear/chatpro/schedule";
# If you have a file detailing scheduled chats put the url of the schedule 
# file between the quotes. Otherwise leave them empty. If you put something
# in here a link will be added to this file from the registration page.

$help_file = "http://www.cabinessence.com/cgi/chatpro/help.html";
# This is the name of the help file for the script. You can leave it set
# to my help page, or change it to one of your own, or delete the variable
# and a link won't show up at all.

$public_rooms = 
"<option>Lobby<option>Cheerleaders<option>Cheer 
Camp<option>Study_Hall<option>Tichenor<option>Architecture<option>Business 
Building<option>Whatever<option>Cafeteria<option>Procrastination"; 
# These are the names of the public rooms that anyone can enter. 
# Each room must be preceeded by <option>

$entry = "Lobby";
#This is the name of the room you want all people to enter when they 
#first join the chat.

$allow_private = "1";
# If you want to allow users to create private rooms leave this set
# to "1" if you don't want private rooms change it to "0" (zero)

$out_link = "http://www.auburn.edu/~collear/uca.html";
# This is the url where you want people sent when they leave the chat page. 
# Probably your own main page.

$visitors_time = 600; 
# This is the time in seconds which people will remain in the visitors list
# since their last reload.

$chat_time = 900;  
# This is the time in seconds which messages will remain in the chat file.

$old_time = 120;
# This is the time in seconds that messages which have been read will be
# shown if someone has the number of messages set to "New"

$lock_time = 5; 
# This is the time in seconds that the script will wait if the lock file 
#exists before running the script. If the lock file remains for a few seconds,
# it is probably a bogus file leftover from a previous error.

#That's it! The rest of the variables are for changing the language of the script.
#
######################
# Language variables
#
# The following variables are provided for converting this script for 
# non-english speaking people. Feel free to reword what I say anyway
# you please.
######################
#
# Signup Page

$reg_page = "Registration Page";
# This is the subtitle for the page shown directly under the $script_title
# $title_graphic

$you_must = "You must hit \"RELOAD\" on the chat page to see the latest conversation.";
# This is the big red message that is displayed telling them to reload.

$instruct = "Click <b>POST</b> or <b>RELOAD</b> to see the latest conversation in a room. Type a message and click <b>POST</b> to send it. Click <b>LEAVE</b> to leave the script.";
# These are the BRIEF instructions that are shown on the signup page.

$no_vis = "There are no people in the $entry room right now."; 
$one_vis = "There is one person in the $entry room right now.";
# These are the messages when there are no visitors and one visitor
# in the room set by the $entry variable.

$mult_vis_a = "There are";
$mult_vis_b = "people in the $entry room right now.";
# These are the messages displayed before and after the number when there
# are multiple people in the room specified by $entry.

$no_vis_tot = "There are no people chatting anywhere inside.";
$one_vis_tot = "There is one person in the entire place right now.";
# These are the messages when there are no visitors and one visitor
# in the room set by the $entry variable.

$mult_vis_tot_a = "There are";
$mult_vis_tot_b = "people chatting total right now.";
# These are the messages displayed before and after the number when there
# are multiple people in the room specified by $entry.


$enter_name = "You must enter your name below, but e-mail address is optional. The password insures the \"one to one\" messages are not read by other people (choose any password).";
# This is the message telling them to enter their name and that e-mail is 
# optional

$name_blank = "Name:";
$passwd_blank = "Password:";
$mail_blank = "E-Mail:";
# These are the messages before the name, password and e-mail parts 
# of the form

$enter_chat = "Enter Chat";
# This is the text on the button to enter the chat.

$sched_note_a = "This room has";
$sched_note_b = "scheduled";
$sched_note_c = "meeting times.";
# These three form a message alerting users of the scheduled times if you
# filled in the $schedule_file variable above. The first one is before the 
# link the second one IS the linked text and the thrid is after the link.

$help_a = "Want some";
$help_b = "HELP";
$help_c = "using this chat page?";
# These three form a message offering help with the page to users if you
# filled in the $help_file variable above. The first one is before the link
# the second one IS the linked text and the thrid is after the link.

$script_by = "This script was written by";
# This is the message that tells people Mike Wheeler wrote the script.
# (This same message appears at the end of the chat files as well)
#
# End entry room variables
#####################
# Main Chat Page variables

$message_text = "Messages:";
# This is the text just before the text field where they enter their message.
# It also serves as a link down to the message area.

$one_on_one_text = "1 on 1";
$post_text = "Post";
$reload_text = "Reload";
$clear_text = "Clear";
$leave_text = "Leave";
# These are the names of the five buttons on the control panel.

$num_mesgs = "# of mesgs";
# This is the text before the radio buttons for selecting the number
# of messages to display

$nummes_new = "New";
# This is the text next to the radio button to only display new
# messages

$people = "People";
$reload_time_text = "Reload";
#These are the headers of the visitors table.

$change_rooms = "Change Rooms";
# This is the text displayed as the title of the room change area.

$currently_in = "You are currently in:";
# This is the prefix to the name of their current room down in the
# room changing area.

$pub_room_title = "Public Rooms:";
$pri_room_title = "Private Rooms:";
# These are the prefixes to selecting public and private rooms

$go_there = "Go There";
#This is the name of the button for changing rooms

$joined_chat = "joined the chat at";
$left_chat = "left the chat at";
$left_room = "left the room at";
# These are the public messages displayed when someone joins the chat
# leaves the chat or leaves a room (respectively). When a person enters
# a new room the $joined_chat one is used. These will all be preceeded
# by the person's name in bold and followed by the time.

#
# End main page variables
###################
# One on one page variables
$one_on_one_page = "One on One Messages";
# This is the title of the one on one page

$one_on_one_instruct = "Type your one on one message in this field and then click on the person's name to send it.";
#This is the instructions for sending a one on one message

$one_on_one_to = "Select the person to send your message to:";
#This is the text above the buttons with everyone's name

$return_to_chat = "Return to Chat";
#This is the button to return to the chat

$reload_remind = "you must hit the reload button to see the latest conversation.";
# This is the one on one message sent when someone joins reminding
# them to reload. It will be preceeded by their name and a comma

#
# End one on one page variables
####################
#Miscellaneous variables.

$index_html = "Nice try, but it's not that easy to see the names of chat files.";
# This is the html page that will be returned if the go to the file or
# visitors directory in hopes of seeing file names which would include private 
# room names.

$wrong_name = "The name you requested is already in use, or you didn't enter a name, or the password you entered is incorrect, or you didn't enter a password, please try again.";
# This variable is for the page if they enter a name which is taken,
# don't enter a name, enter the wrong password, or don't enter a
# password.
#
# End Miscellaneous language variables
######################
#
# That is it for variables you can set.
#
# You MUST get my permission by e-mail (mwheeler@gladstone.uoregon.edu) 
# if you want to make changes on anything below.
###############################################
$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) = localtime(time);

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

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

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

   $date = "$hour\:$min\:$sec";
   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/)) {
         &login;
         &unlock;
         exit;
      }
   }


   if ($name eq "link") {
         &name_taken; # Tell them the name was taken and to try again
   }
   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") {
      &check_name;
      &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
      &login;   # Print the login page
      &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>\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");
      }
   }

}

############
# Login

sub login {
   opendir(VFILES,$vis_dir);
   @vfilelist =  grep(!/^\.\.?$/, readdir (VFILES));
   closedir(VFILES);
   $vtotal = @vfilelist;
   $vtotal = $vtotal - 1;

   $entry =~ s/ /_/g;
   $entry =~ s/\//_/g;
   open (VISITORS,"$file_dir/vis$entry");
   @visitors = <VISITORS>;
   close (VISITORS);

   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$entry");
   foreach $visitors_line(@visitors_new) {
      print VISITORS "$visitors_line";
   }
   close (VISITORS);
   $vnum=@visitors_new;
   $vnum=$vnum - 1;
   undef @visitors_new;

   print "Content-type: text/html\n\n";
   print "<html><head><title>$page_title Login</title></head>\n";
   print "<body bgcolor=$bgcolor background=\"$background\" text=$text_color link=$link_color vlink=$vlink_color><ul><center>\n";
   if ($title_graphic eq "") {
      print "<H1>$page_title</h1>\n";
   }
   else {
      print "<h1><img src=\"$title_graphic\" alt=\"$page_title\"></h1>\n";
   }
   print "<h2>$reg_page</h2><p></center>\n";
   print "<b><font size=4 color=$one_color><center>$you_must</font></b></center><p>\n";
   print "$instruct<p>\n";
   if ($vnum < 1) {
      print "$no_vis<p>\n";
   }
   elsif ($vnum==1) {
      print "$one_vis<P>\n";
   }
   else {
      print "$mult_vis_a $vnum $mult_vis_b<P>\n";
   }
   if ($vtotal < 1) {
      print "$no_vis_tot<p>\n";
   }
   elsif ($vtotal==1) {
      print "$one_vis_tot<P>\n";
   }
   else {
      print "$mult_vis_tot_a $vtotal $mult_vis_tot_b<P>\n";
   }

   print "$enter_name<br>\n";
   print "<form method=POST action=\"$script_name\"><input type=hidden name=\"where\" value=\"register\">\n";
   print "<input type=hidden name=\"link\" value=\"$out_link\">\n";
   print "<input type=hidden name=\"time\" value=\"$time\">\n";
   print "<table><tr><td>$name_blank</td>\n";
   print "<td><input type=text size=30 name=\"name\" maxlength=25><br></td></tr>\n";
   print "<tr><td>$passwd_blank</td>\n";
   print "<td><input type=password name=\"passwd\" size=30></td></tr>\n";
   print "<tr><td>$mail_blank</td>\n";
   print "<td><input type=text size=30 name=\"mail\"><br></td></tr>\n";
   print "</table><input type=hidden name=\"room\" value=\"$entry\">\n";
   print "<input type=submit value=\"$enter_chat\"></form>\n";
   if ($schedule_file ne "") {
      print "$sched_note_a <a href=\"$schedule_file\">$sched_note_b</a> $sched_note_c<p>\n";
   }
   if ($help_file ne "") {
      print "$help_a <a href=\"$help_file\"> $help_b</a> $help_c<p>\n";
   }   
   print "$script_by <a href=\"http://gladstone.uoregon.edu/~mwheeler/cgi\">Mike Wheeler</a>.<p></body> </html>\n";
}
############
# Check Name

sub check_name {
   if ($passwd eq "") {
      &name_taken;
   }
   else {
      $vname = $name;
      $vname =~ s/ /_/g;
      $vname =~ s/\//_/g;
      opendir(VFILES,$vis_dir);
      @vfilelist =  grep(!/^\.\.?$/, readdir (VFILES));
      closedir(VFILES);

      foreach $vfilelist_line (@vfilelist) {
         if ($vfilelist_line eq $vname) {
            open (VSAME,"$vis_dir/$vname") || die $!;
            @vsame = <VSAME>;
            close (VSAME);
            ($bogus,$bogus2,$bogus3,$test,$bogus4) = split(/:/,$vsame[0]);
            $password = crypt($passwd,MW);
            if ($password ne $test) {
               &name_taken;
           } 
         }
      }
   }
}

###########
# Name taken
sub name_taken {
   print "Content-type: text/html\n\n";
   print "<html><head><title>$page_title Login</title></head>\n";
   print "<body bgcolor=$bgcolor background=\"$background\" text=$text_color link=$link_color vlink=$vlink_color><ul><center>\n";
   if ($title_graphic eq "") {
      print "<H1>$page_title</h1>\n";
   }
   else {
      print "<h1><img src=\"$title_graphic\" alt=\"$page_title\"></h1>\n";
   }
   print "<h2>$reg_page</h2><p></center>\n";
   print "$wrong_name<p>\n";
   print "$enter_name<br>\n";
   print "<form method=POST action=\"$script_name\"><input type=hidden name=\"where\" value=\"register\">\n";
   print "<input type=hidden name=\"room\" value=\"$room\">\n";
   print "<input type=hidden name=\"link\" value=\"$link\">\n";
   print "<table><tr><td>$name_blank</td>\n";
   print "<td><input type=text size=30 name=\"name\" maxlength=25><br></td></tr>\n";
   print "<tr><td>$passwd_blank</td>\n";
   print "<td><input type=password name=\"passwd\" size=30><br></td></tr>\n";
   print "<tr><td>$mail_blank</td>\n";
   print "<td><input type=text size=30 name=\"mail\"><br></td></tr>\n";
   print "</table>\n";
   print "<input type=submit value=\"$enter_chat\"></form>\n";
   print "</body></html>\n";
   &unlock;
   exit;
}
###########
# 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<hr>\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<hr>\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<hr>\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><title>$page_title</title></head>\n";
   print "<body bgcolor=$bgcolor background=\"$background\" text=$text_color link=$link_color vlink=$vlink_color>\n";
   print "<form method=POST action=\"$script_name\">\n";
   print "<center><table border=5><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><center>\n";

   if ($title_graphic eq "") {
      print "<H3>$page_title</h3></center>\n";
   }
   else {
      print "<img src=\"$title_graphic\" border=0 alt=\"$page_title\"><br>\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 "<table width=100%><tr align=center>\n";
   print "<td><b><a href=\"#messages\">\n";
   print "$message_text</a></b></td><td width=33%></td>\n";
   if ($help_file ne "") {
      print "<td width=33%><a href=\"$help_file\">$help_b</a></td>\n";
   }
   print "</tr></table><textarea COLS=50 ROWS=4 name=\"message\" wrap>\n";
   print "$mess</textarea></center></td>\n";
   print "<td><table>\n";
   print "<tr align=center><td>$num_mesgs</td></tr>\n";
   if ($nummes eq "") {
      $nummes = "20";
   }
   print "<tr><td><input type=radio name=\"nummes\" value=\"10\""; 
   if ($nummes eq "10") {
      print " checked";
   }
   print ">10</td></tr>\n";
   print "<tr><td><input type=radio name=\"nummes\" value=\"20\"";
   if ($nummes eq "20") {
      print " checked";
   }
   print ">20</td></tr>\n";
   print "<tr><td><input type=radio name=\"nummes\" value=\"30\"";
   if ($nummes eq "30") {
      print " checked";
   }
   print ">30</td></tr>\n";
   print "<tr><td><input type=radio name=\"nummes\" value=\"40\"";
   if ($nummes eq "40") {
      print " checked";
   }
   print ">40</td></tr>\n";
   print "<tr><td><input type=radio name=\"nummes\" value=\"new\"";
   if ($nummes eq "new") {
      print " checked";
   }
   print ">$nummes_new<br></td></tr>\n";
   print "</table></td></tr></table><hr>\n";
   print "<table border=5><tr valign=top>\n";
   print "<td width=140><hr><table cellpadding=0>\n";
   print "<tr><td><b>$people</b>\n";
   print "</td> <td>$reload_time_text<br></td></tr><tr></tr>@visitors\n";
   print "</table><hr>\n";
   print "<table><tr><td>\n";
   print "<h3>$change_rooms</h3>\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";
   if ($allow_private eq "1") {
      print "$pri_room_title<br>\n";
      print "<input type=text size=15 name=\"priroom\"><br></td></tr><tr><td>\n";
   }
   print "<input type=submit name=\"where\" value=\"$go_there\"></td></tr></table><hr></td>\n";
   print "<td><a name=\"messages\"></a><hr>\n";

   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 "$new_message $one_of_ten";
         }
         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 "$new_message $one_of_twenty";
         }
         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 "$new_message $one_of_thirty";
         }
         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 "$new_message $one_of_forty";
         }
         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 "$new_message $one_of_new";
         }
         elsif ($mes_time + $old_time > $load_time) {
            print "<font color=$oldcolor>$one_of_new</font>";
         }
      }
   }
   print "</td></tr></table><p><hr>\n";
   print "</center></form>$script_by\n";
   print " <a href=\"http://gladstone.uoregon.edu/~mwheeler/cgi\">\n";
   print "Mike Wheeler</a>.<p> </body> </html>\n";
}

##############
# 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><hr>\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><title>$one_on_one_page</title></head>\n";
   print "<body bgcolor=$bgcolor background=\"$background\" text=$text_color link=$link_color vlink=$vlink_color><center>\n";
   if ($title_graphic ne "") {
      print "<img src=\"$title_graphic\" alt=\"page_title\">\n";
   }
   else {
      print "<h2>$page_title</h2>\n";
   }
   print "<h3>$one_on_one_page</h3>\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 "<h3>$one_on_one_to</h3>\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></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><hr>\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><hr>\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><hr>\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;
}
