#!/usr/bin/perl

$infofile = "etc/newuser.info";
$datafile = "newaccts.txt";

$linect = 0;
if (open(INFO, $infofile)) {
  while (<INFO>) {
    print "$_";
    $linect++;
    if ($linect == 23) {
      print "[Press RETURN]: ";
      <STDIN>;
      $linect = 0;
    }
  }
  close(INFO);
  if ($linect > 20) {
    print "[Press RETURN]: ";
    <STDIN>;
  }
  print "Apply for an account (Y/N)? [Y]: ";
  $answer = &Getline;
  if ($answer =~ /^[Nn]/) {
    exit(0);
  }
  print "\n\n";
}

promptuserid:
print 
"Enter your userid. This must be 2-12 characters long, must begin with a
letter, end with a letter or number, and may have hyphens or underscores 
as long as they're not adjacent. Spaces are NOT allowed.

Userid: ";

$userid = &Getline;
print "\n";

if (length($userid) < 2 || length($userid) > 12) {
  print "A userid must have between 2 and 12 characters.\n";
  goto promptuserid;
}

# Man, this is ugly.
if (!($userid=~/^[A-Za-z][A-Za-z0-9-_]*[A-Za-z0-9]$/)||($userid=~/[-_]{2}/)) {
  print "Illegal userid syntax.\n";
  goto promptuserid;
}

$dupe = 0;
$userid_allcaps = $userid;
$userid_allcaps =~ tr/a-z/A-Z/;
if (open(PASSWDS, "etc/passwds")) {
  while (<PASSWDS>) {
    ($id_allcaps, $rest) = split(/[ :]/);
    $id_allcaps =~ tr/a-z/A-Z/;
    if ($userid_allcaps eq $id_allcaps) {
      $dupe = 1;
      last;
    }
  }
  close(PASSWDS);
}
else {
  die "Cannot open etc/passwds: $!\n";
}

if ($dupe) {
  print "That userid is already taken. Please choose another.\n";
  goto promptuserid;
}  

promptpass:
print 
"Enter a password, 1-8 characters. This should be a temporary password
that you will change after you log in. It is case-sensitive and will NOT be
recorded in unencrypted form after your account is created.
[NOTE: It will echo to the screen].

Password: ";

$password = &Getline;
print "\n";

if (length($password) < 1) {
  print "Null passwords are not acceptable. Please enter a password.\n";
  goto promptpass;
}

print
"Enter your terminal type, if you know it. If not don't enter anything and
vt100 will be assumed.

Termtype: ";

$termtype = &Getline;
print "\n";

if ($termtype eq "") {
    $termtype = "vt100";
}

promptname:
print
"Enter your real name, first and last. THIS IS REQUIRED.

Real name: ";

$realname = &Getline;
print "\n";

if ($realname eq "") {
  print "You must enter your name.\n";
  goto promptname;
}

promptaddr:
print
"Enter your postal address. City and state or country is sufficient.

Address: ";

$address = &Getline;
print "\n";

if ($address eq "") {
  print "You must enter your address.\n";
  goto promptaddr;
}

print
"Enter your e-mail address if you have one.

Email: ";

$email = &Getline;
print "\n";

if ($email eq "") {
  $email = '(none)';
}

print
"You have entered the following:
Userid:       $userid
Password:     $password
Termtype:     $termtype
Real name:    $realname
Address:      $address
Email:        $email

Send new account request (Y/N)? [Y]: ";

$answer = &Getline;
print "\n";

if ($answer =~ /^[Nn]/) {
    print "Request NOT sent.\n";
}
else {
    open(HANDLE, ">>$datafile") || die("Cannot open newaccts file!\n");
    print HANDLE 
          "$userid\n$password\n$termtype\n$realname\n$address\n$email\n\n";
    close(HANDLE);
    print "Request sent.\n";
    $login = $ENV{'BBS_USERID'};
    $fromhost = $ENV{'BBS_FROMHOST'};
    if ($login eq "") {
      $userid = "[cmdline]";
    }
    if ($fromhost eq "") {
      $fromhost = "localhost";
    }
    $execstr = "bin/bbslog -h BBSAPPLY 1 \"APPLY for $userid by $login from $fromhost\"";
    system($execstr);
    if ($email ne '(none)') {
      print "If approved mail will be sent to $email.\n";
    }
    else {
      print "Check back in a couple of days to see if you're approved.\n";
    }
}

sub Getline {
    local($char_in);
    local($line_out);
    local($c);
   
    do {
      $char_in = getc;
      $c = ord($char_in);    
      if ($c == 0) {
        exit(0);
      }
      elsif ($c == 8 || $c == 127) {
        chop($line_out);
      }
      elsif ($c >= 32) {
        $line_out .= $char_in;
      } 
    } while ($c != 10);

    $Getline_return = $line_out;
}

