package User;
#Forum user module.
###########################################
#
# this package contains the routines for creating,
# setting, storing, and retrieving forum users
# version 0.1 11/27/2004
# version 0.2 11/29/2004 added beginning of get_user function
# version 0.3 12/02/2004 changed output to Dumper but ran
#                        into parsing problems when inputting the same data
# version 0.4 12/06/2004 changed output form back to label:data format
#                        and wrote existing get_user routine.
#
# Ben Martin
#
###########################################
use strict;
use warnings;
use Data::Dumper;

  my $userfile = "./userlist.dat";

  # creates a new user object to be stored in the users list file
  sub new{
    my $class = shift;
    my $new_user = { username => "",
                     password  => "",
                     email   => "",
                   };
    bless($new_user, $class);
    return $new_user;
  }

  # Invoke this with $u->set_userinfo('name', 'passwd', 'email@address')
  sub set_userinfo{
    my $self = shift;
    my ($username, $password, $email) = @_;
    $self->{username} = $username;
    $self->{password}  = $password;
    $self->{email}   = $email;
  }

  # Dumps the set info to a data fuile for later retrieval
  sub save_userinfo{
    my $self = shift;
    open USERDATA, ">> $userfile";
    print USERDATA "name:" . $self->{username} . "\n";
    print USERDATA "pass:" . $self->{password} . "\n";
    print USERDATA "mail:" . $self->{email} . "\n";
    print USERDATA "---";
  }
  
  # This subroutine can find a user by name.
  # Call this subroutine using $u->get_user($username)
  sub get_user{
    my $self = shift;
    my ($search_term) = @_;
    open (USERS, "< $userfile") or die "Oops - couldn't read from file: '$userfile'";
    my $username = "";
    my $password = "";
    my $email = "";
    my $line = "";
    my @user_data = ();
    while($line = USERS){
      if($line =~ "name"){
        $username = split(/:/ , $line);
      }
      elsif($line =~ "pass"){
        $password = split(/:/ , $line);
      }
      elsif($line =~ "email"){
        $email = split(/:/ , $line);
      }
      else{
        @user_data = ($username, $password, $email)
        if($username = $search_term){
          return @userdata;
        }
        else{
          @userdata = ();
        }
      }
    }
  }

  # if the user already exists or the password and confirm lines don't match
  # $check_user is set to true and the webpage displays a warning telling
  # the user what went wrong
  sub check_user{
    my $user=@_;
    my check_user = 0;
    if get_user($username) =
  }
1;

