perlnomic‎ > ‎

taskmaster.pl

#!/usr/bin/perl

BEGIN {
    use Safe;
    require "$ENV{HOME}/perlnomic/saveload.pl";
    require "$ENV{HOME}/perlnomic/outmail.pl";
    require "$ENV{HOME}/perlnomic/inmail.pl";
}

sub debugmessage {
    warn $_[0];
    $mail{anti}{message} .= "Warning: $_[0]\n";
}

sub sortnum (@) {
    return sort {$a<=>$b;} @_;
}

# Create a safe compartment
$nomic = new Safe;

$nomic->share('%data');
$nomic->share('@rules');
$nomic->share('$rules');
$nomic->share('@mail');
$nomic->share('%mail');
$nomic->share('@args');
$nomic->share('$message');
$nomic->share('*sortnum');

# Load various data
%data = %{loadval("<$ENV{HOME}/perlnomic/perlnomic.dat")};

open RULES, "<$ENV{HOME}/perlnomic/rules.pl";
while (<RULES>) {
    $rules .= $_;
    if (/^##/) {
        push @rules, $rule if $rule;
        $rule = '';
    }
    else {
        $rule .= $_;
    }
}
push @rules, $rule if $rule;
close RULES;

# Read data
@inmail = @{readmail("<$ENV{HOME}/Mail/perlnomic")};

# Delete old messages
unlink "$ENV{HOME}/Mail/perlnomic";

# Seed the RNG - defaults to timer, probably a bad idea
srand;

# Give the time to these programs
$data{date} = scalar localtime;
$data{date2} = [localtime];

# Debug
do "$ENV{HOME}/perlnomic/perlnomic.debug.pl";
die "Debug script failed: $@" if $@;

# Load in the rules
foreach $rule (@rules) {
    $nomic->reval($rule);
    debugmessage "Rules (loading) are buggy: $@" if $@;
}

# Call a setup function
$nomic->reval('setup();');
debugmessage "Rules (setup) are buggy: $@" if $@;

# Give it the messages
foreach $message (@inmail) {
    $_ = $message;
    $nomic->reval('mail($_);');
    debugmessage "Rules (mail) are buggy: $@" if $@;
};

# Call a main function
$nomic->reval('main();');
debugmessage "Rules (main) are buggy: $@" if $@;

# Copy from mail hash to mail array
foreach $key (keys %mail) {
    push @mail, $mail{$key};
}

outmail(@mail);

open OUT, ">$ENV{HOME}/perlnomic/perlnomic.dat";
saveval(\%data, OUT);
close OUT;


open OUT, ">$ENV{HOME}/perlnomic/rules.pl";
print OUT $rules;
close OUT;