#!/usr/bin/perl

require <ctime.pl>;

###########################################################################
# 
# dumpacct.pl
# Ray Rocker
# October 1, 1994
#
# Quick and dirty report generator for EBBS 3.0 accounts.
# For each account, writes 13 lines:
#  1. Userid
#  2. Username
#  3. Last login time
#  4. Last login host
#  5. Permission bits above and below the default (Basic-1 thru Basic-4)
#  6. Flags, by name
#  7. Terminal type
#  8. Realname
#  9. Address
# 10. Email address
# 11. Protocol
# 12. Editor
# 13. Blank line (separator)
#
# This code is public domain. Hack/enhance/distribute it as you please.
# I don't make any guarantees about it, etc etc.
#
###########################################################################

# You must have BBSHOME set for this script!!!!
$bbshome = $ENV{"BBSHOME"};

# Read in permstrs file.
$permstrfile = "$bbshome/etc/permstrs";
$numpermstrs = 0;
if (open(PERMFILE, $permstrfile)) {
  while (<PERMFILE>) {
    if (/^[#\s]/) { next; }
    chop;
    $permstrs[$numpermstrs++] = $_;
  }
  close(PERMFILE);
}

# Set up names of flags.
$numflgstrs = 6;
@flgstrs = ("Cloak", "Exempt", "Disabled", "Shared", "NoPage", "NoOverride");

# Open password file. Die if this fails.
$passfile = "$bbshome/etc/passwds";
open(PASS, $passfile) || die "Cannot open $passfile: $!\n";

# Loop through password file...
while (<PASS>) {
  if (/^[#\s]/) { next; }   # Skip comments and blank lines.
  ($rawid, $passwd, $perms, $flags, $rawname) = split(/:/);
  ($userid) = split(/ /, $rawid);
  ($username) = split(/ /, $rawname);
  print("$userid\n$username\n");
  
# Get the last login info.
  $lastlogfile = "$bbshome/home/$userid/lastlogin";
  if (open(LASTLOG, $lastlogfile)) {
    ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, 
     $mtime, $ctime, $blksize, $blocks) = stat(LASTLOG);
    print(&ctime($mtime));
    $lasthost = <LASTLOG>;
    close(LASTLOG);
    print("$lasthost\n");
  }
  else {
    print("<none>\n<none>\n");
  }

# Print the permissions in a neat way.
  $mask = hex($perms);
  if ($mask == 15) {
    print("<default>");
  }
  else {
    for ($i=0; $i<32; $i++) {
      if ($mask & (1<<$i)) {
        if ($i<$numpermstrs) { if ($i>4) { print("$permstrs[$i] "); } }
        else { print("<permbit-$i> "); }
      }
      else { if ($i<4) { print("!$permstrs[$i] "); } }
    }
  }
  print("\n");

# Print the flags in a neat way.
  $mask = hex($flags);
  if ($mask == 0) {
    print("<none>");
  }
  else {
    for ($i=0; $i<16; $i++) {
      if ($mask & (1<<$i)) {
        if ($i<$numflgstrs) { print("$flgstrs[$i] "); }
        else { print("<flag-$i> "); }
      }
    }
  }
  print("\n");

# Set some defaults, then read the profile file.
  $terminal = "vt100";
  $realname = "<no realname>";
  $address = "<no address>";
  $email = "<no email>";
  $protocol = "<no protocol>";
  $editor = "builtin";  
  if (open(PROFILE, "$bbshome/home/$userid/profile")) {
    while (<PROFILE>) {
      if (/^[#\s]/) { next; }
      ($tag, $value) = split(/=/);
      chop($value);
      if ($tag eq "TERM") { $terminal = $value; }
      elsif ($tag eq "NAME") { $realname = $value; }
      elsif ($tag eq "ADDR") { $address = $value; }
      elsif ($tag eq "MAIL") { $email = $value; }
      elsif ($tag eq "PROT") { $protocol = $value; }
      elsif ($tag eq "EDIT") { $editor = $value; }
    }
    close(PROFILE);
  }
  print("$terminal\n$realname\n$address\n$email\n$protocol\n$editor\n");
  print("\n");
}

close(PASS);

