#!/moira/bin/perl -Tw

# $Id: access.gen 3956 2010-01-05 20:56:56Z zacheiss $

use DBI;

# The following exit codes are defined and MUST BE CONSISTENT withh the
# error codes the library uses:
$MR_DBMS_ERR = 47836421;
$MR_OCONFIG = 47836460;
%users = ();

$outfile = '/moira/dcm/access.out';

$dbh = DBI->connect("dbi:Oracle:moira", "moira", "moira")
    || exit $MR_DBMS_ERR;

# Get the list of valid MIT.EDU user e-mail addresses
$sth = $dbh->prepare("SELECT login FROM users WHERE status != 3");

$sth->execute;

umask 022;
open(OUT, ">$outfile") || exit $MR_OCONFIG;

while(($login) = $sth->fetchrow_array) {
    $login =~ s/\0//g;
    $users{$login} = $login;
    
    print OUT "From:$login\@MIT.EDU RELAY\n";
}

# Get all the valid MIT.EDU mailing list addresses
$sth = $dbh->prepare("SELECT name FROM list WHERE active !=0 " .
		     "AND maillist = 1");

$sth->execute;

while(($name) = $sth->fetchrow_array) {
    $name =~ s/\0//g;
    
    # Ensure we do not re-print an entry that may be a personal user group
    # and was already handled by the user e-mail addresses
    if(!$users{$name}) { 
	print OUT "From:$name\@MIT.EDU RELAY\n";
    }
}

close(OUT);
exit;
