#!/usr/bin/perl

# Set your bbs name here.
$bbsname = "this bbs";

# This must agree with the filename in bbsapply.pl.

$datafile = "newaccts.txt";
$workfile = "newaccts.tmp";

# Set this to a mail-sending program on your host.
$mailer = "/usr/lib/sendmail";

if ( -f $workfile ) {
  print "Someone else is already processing new accounts.\n";
  print "If this is not true, delete the file $workfile.\n";
  exit(0);
}

if (! -f $datafile ) {
  print "No new account requests are available.\n";
  exit(0);
}

rename($datafile, $workfile) || die "Cannot create work file: $!\n";

$/ = "\n\n";
open(WORKFILE, $workfile);
while (!eof(WORKFILE)) {
  $ent = <WORKFILE>;
  push(@requests, $ent);
}
close(WORKFILE);
$/ = "\n";

$done = 0;
$operator = $ENV{'BBS_USERID'};
if ($operator eq "") {
  $operator = "[cmdline]";
}

$left = @requests;
foreach $ent (@requests) {
  if ($done) {
    push(@deferred, $ent);
  }
  else {    
    print "[$left requests remain]\n\n";
    ($userid, $passwd, $term, $name, $addr, $email) = split(/\n/, $ent);
    print "Userid:     $userid\n";
    print "Password:   $passwd\n";
    print "Termtype:   $term\n";
    print "Realname:   $name\n";
    print "Address:    $addr\n";
    print "Email:      $email\n\n";
    if ($email eq "(none)") {
      $email = "";
    }
    print "(A)pprove, (R)eject, (D)efer, (Q)uit? [A]: ";
    $verdict = &Getline;
    $ans = substr($verdict, 0, 1);
    $ans =~ tr/a-z/A-Z/;
    if ($ans eq "Q") {
      push(@deferred, $ent);
      $done = 1;
    }
    elsif ($ans eq "D") {
      push(@deferred, $ent);
    }
    elsif ($ans eq "R") {
      $execstr = "bin/bbslog -h BBSAPPROVE 1 \"REJECT $userid by $operator\"";
      system($execstr);
      if ($email ne "") {
        &Mailrejection($email);
      }
      print "Request was rejected.\n\n";          
    }
    else {
      $execstr = "bin/bbslog -h BBSAPPROVE 1 \"ACCEPT $userid by $operator\"";
      system($execstr);
      $execstr = "bin/addacct -i \"$userid\" -p \"$passwd\" -u \"$userid\"";
      $execstr .= " -t \"$term\"";
      $execstr .= " -r \"$name\"";
      $execstr .= " -a \"$addr\"";
      if ($email ne "") {
        $execstr .= " -e \"$email\"";
      }
      $rc = system($execstr);
      if ($rc != 0) {
        print "addacct failed\n";
        if ($email ne "") {
            &Mailrejection($email);
        }
        print "Account not added.\n";
      }
      else {
        if ($email ne "") {
            &Mailconfirm($email);
        }
        print "Account was added.\n";
      }
    }
  }
  $left--;
}

$left = @deferred;
if ($left > 0) {
  open(WORKFILE, ">$workfile");
  foreach $ent (@deferred) {
    print WORKFILE "$ent";
  }
  if (open(NEWREQUESTS, "$datafile")) {
    while (<NEWREQUESTS>) {
      print WORKFILE "$_";
    }
    close(NEWREQUESTS);
  }    
  close(WORKFILE);
  rename($workfile, $datafile);
}
else {
  unlink($workfile);
}

sub Mailrejection {
  $addr = $_[0];
  print "Mail rejection to $addr (Y/N)? [N]: ";
  $askmail = &Getline;
  $domail = substr($askmail, 0, 1);
  $domail =~ tr/a-z/A-Z/;
  if ($domail eq "Y") {
    $mailexecstr = "|$mailer $addr";
    print "Enter a one-line message explaining the rejection: \n";
    $explanation = &Getline;
    if ($explanation eq "") {
      $explanation = "(none given)\n";
    }
    if (open(MAILPROC, $mailexecstr)) {
      print MAILPROC "Subject: BBS Account Request\n";
      print MAILPROC "\n";
      print MAILPROC
"Your request for an account \'$userid\' on $bbsname 
was rejected for the following reason:
$explanation
";
      close(MAILPROC);
      wait;
    } 
    else {
      print STDERR "Failed to open mailer process.\n";
      print STDERR "You might need to use a different mailer.\n";
    }
  }
}
  
sub Mailconfirm {
  $addr = $_[0];
  print "Mail confirmation to $addr (Y/N)? [N]: ";
  $askmail = &Getline;
  $domail = substr($askmail, 0, 1);
  $domail =~ tr/a-z/A-Z/;
  if ($domail eq "Y") {
    $mailexecstr = "|$mailer $addr";
    if (open(MAILPROC, $mailexecstr)) {
      print MAILPROC "Subject: BBS Account Request\n";
      print MAILPROC "\n";
      print MAILPROC
"Your request for an account \'$userid\' on $bbsname 
was accepted. The password is as you requested.

";
      close(MAILPROC);
      wait;
    } 
    else {
      print STDERR "Failed to open mailer process.\n";
      print STDERR "You might need to use a different mailer.\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;
}
