
#
#  The initializer
#

require "flush.pl";

$NEXT_SERIAL = "$ENV{SERVER_VAR}/lib/next_serial_number";
$newmail = 0;

while ($mail = shift)
{
    if($mail =~ /^-mail$/)
    {
	$newmail = 1;
    }
}

#
# Check for any mail
#

if ($ENV{SERVER_EMAIL_READY} && ($newmail == 1)) {
  chomp($date = `date`); 
  chomp($startNumber = `cat $NEXT_SERIAL`);
  $number = $startNumber;

    #
    # There are messages to process. All mail from
    # users in the BAD_LIST will be placed in a 
    # separate folder. The name of this folder will
    # be their user name (e.g., Mail/root,
    # Mail/MAILOR-DAEMON)
    #
  $DOMAIN = "";
  if ($ENV{"SERVER_POP_SERVER"} =~ /^(\w+)\.(\S+)\s*$/) {
      $DOMAIN = "\@$2";
  }
    @BAD_LIST = ("MAILER-DAEMON", "root", "Operator", "postmaster", "Majordomo", "$ENV{SERVER_USER}$DOMAIN");

    foreach (@BAD_LIST) {
      s/\s*//g;
      $NONE = system("pick -from $_ >/dev/null 2>&1");
      unless ($NONE) {
        print "Placing mail from \"$_\" in \"$ENV{MAIL_FOLDER}/$_\"\n";
	&flush(STDOUT);
	@FILES = `pick -from $_`; 
	chomp (@FILES);
	system("mkdir $ENV{MAIL_FOLDER}/$_") unless (-e "$ENV{MAIL_FOLDER}/$_");
        system("refile @FILES +$_ > /dev/null 2>&1"); 
      }
    }

    #
    # Remove any mail not sent to neos
    # 
    ($mailname, $stuff) = split /@/, $ENV{SERVER_EMAIL};
    $NONE = system("pick -not -lbrace -to $mailname -or -cc $mailname -rbrace >/dev/null 2>&1");
    unless ($NONE) {
        print "Placing spam in \"$ENV{MAIL_FOLDER}/spam\"\n";
	&flush(STDOUT);
	@FILES = `pick -not -lbrace -to $ENV{SERVER_EMAIL} -or -cc $ENV{SERVER_EMAIL} -rbrace`; 
	chomp (@FILES);
	system("mkdir $ENV{MAIL_FOLDER}/spam") unless (-e "$ENV{MAIL_FOLDER}/spam");
        system("refile @FILES +spam > /dev/null 2>&1"); 
      }


    #
    # See if there's anything left after removing
    # the bad people. 
    #

    $NONE = system("pick -lbrace -cc $mailname -and -not -to $mailname -rbrace >/dev/null 2>&1");
    unless ($NONE) {
      print "Placing cc mail in \"$ENV{MAIL_FOLDER}/cc\"\n"; &flush(STDOUT);
      system("mkdir $ENV{MAIL_FOLDER}/cc") unless (-e "$ENV{MAIL_FOLDER}/cc");  
      system("refile `pick -lbrace -cc $mailname -and -not -to $mailname -rbrace 2>&1` +cc > /dev/null 2>&1"); 
    }
    $notdone=1;
    while ($notdone) {
      $tmpfile = "$ENV{SERVER_TMP}/$ENV{SERVER_USER}-initializer-$$";
      system("show +jobs first -noshowproc -noheader > $tmpfile 2>&1"); 
      if ($? != 0) { 
        $notdone=0;
      } else {
	$SOME = 1;
	system("mkdir $ENV{SERVER_JOBS}/job.$number");
	system("mv $tmpfile $ENV{SERVER_JOBS}/job.$number/job.received");
	$password = &generate_password();
        $interface = "mail"; &log;
        system("rmm +jobs first") and die "initializer.pl: rmm call failed";

        # Increment job number

	$number++;
      } 
    } 
  unlink("$tmpfile") or die "initializer.pl: Could not remove $tmpfile"; 
  sleep 1;		    
}

#
# Check for WEB submissions
#
if ((-f "$ENV{SERVER_SPOOL}/WEB/MAIL")&&($ENV{SERVER_WWW_READY})) {
  chdir("$ENV{SERVER_SPOOL}/WEB");
  unless ($startNumber) { 
    chomp($startNumber = `cat $NEXT_SERIAL`);
    $number = $startNumber;
  }
  $SOME = 1;
  chomp($date = `date`);

  unlink("MAIL");
  chomp(@WEBLIST = `ls -c -r websub.* 2>/dev/null`);
  foreach (@WEBLIST) {
      if(/number$/) {
	  next; #be careful not to treat the number files as submissions  
      }
      #only process the job if it has been completely written
      if(-f "DONE.$_") {
	  unlink("DONE.$_");
	  mkdir("$ENV{SERVER_JOBS}/job.$number", 0755);
	  rename("$_", "$ENV{SERVER_JOBS}/job.$number/job.received");
	  $password = &generate_password();
	  $interface = "web"; &log;
	  open(HANDLE, ">>$ENV{SERVER_JOBS}/job.$number/job.out") or die "Could not open job.out for job $number";
	  print HANDLE "Welcome to $ENV{SERVER_NAME}!\n\n";
	  close(HANDLE);
      
	  # Output for cgi-script
      
	  $numberFile = "$_.number";
	  open(HANDLE, ">$numberFile");
	  print HANDLE "$number\n";
	  close HANDLE;
	  system("chmod a+rw $numberFile");
	  
	  # Increment job number
      
	  $number += 1;
      }			     
  }
}

#
# Check for Kestrel submissions
#

if (-f "$ENV{SERVER_SPOOL}/KESTREL/MAIL") {
  chdir("$ENV{SERVER_SPOOL}/KESTREL");
  unless ($startNumber) { 
    chomp($startNumber = `cat $NEXT_SERIAL`);
    $number = $startNumber;
  }
  $SOME = 1;
  chomp($date = `date`);

  unlink("MAIL");
  chomp(@KESTRELLIST = `ls -c -r kestrel.* 2>/dev/null`);
  foreach (@KESTRELLIST) {
      if(/number$/) {
	  next; #be careful not to treat the number files as submissions  
      }

      #only process the job if it has been completely written
      if(-f "DONE.$_") {

	  unlink("DONE.$_"); 
	  mkdir("$ENV{SERVER_JOBS}/job.$number", 0755);
	  rename("$_", "$ENV{SERVER_JOBS}/job.$number/job.received");
	  $password = &generate_password();
	  $interface = "kestrel"; &log;
	  open(HANDLE, ">>$ENV{SERVER_JOBS}/job.$number/job.out");
	  print HANDLE "Welcome to $ENV{SERVER_NAME}!\n\n";
	  close(HANDLE);
	  
	  # Output for kestrel server
	  
	  $numberFile = "$_.number";
	  open(HANDLE, ">$numberFile");
	  print HANDLE "$number\n";
	  close HANDLE;

	  # Increment job number

	  $number += 1;
      }
  }
}

#
# Check for SOCKET submissions
#

if (-f "$ENV{SERVER_SPOOL}/SOCKET/MAIL") {
  chop($date = `date`);
  chdir("$ENV{SERVER_SPOOL}/SOCKET");
  system("rm -f $ENV{SERVER_SPOOL}/SOCKET/MAIL");
  unless ($startNumber) { 
    chop($startNumber = `cat $NEXT_SERIAL`);
    $number = $startNumber;
  }
  $SOME = 1;
  chop(@SOCKETLIST = `ls -c -r socksub.* 2>/dev/null`);
  foreach (@SOCKETLIST) {
      if(/number$/) {
	  next; #be careful not to treat the number files as submissions  
      }
      #only process the job if it has been completely written
      if(-f "DONE.$_") {

	  system("rm -f DONE.$_; mkdir $ENV{SERVER_JOBS}/job.$number");
	  rename("$_", "$ENV{SERVER_JOBS}/job.$number/job.received");
	  $password = &generate_password();
	  $interface = "sockets"; &log;
	  open(HANDLE, ">>$ENV{SERVER_JOBS}/job.$number/job.out");
	  print HANDLE "Welcome to $ENV{SERVER_NAME}!\n\n";
	  close(HANDLE);

	  # Output for submission tool 
	  
	  $numberFile = "$_.number";
	  open(HANDLE, ">$numberFile");
	  print HANDLE "$number\n";
	  close HANDLE;
	  
	  # Increment job number
	  
	  $number += 1;
      }
  }
}

#
# If there were any submissions then we will
# call the extractor.  It's possible that only
# e-mail submissions came, and they were all
# from the BAD_LIST.  In this case, we wouldn't
# call the extractor.

if ($SOME) {
  system("echo $number > $NEXT_SERIAL");
  $number_received = $number - $startNumber;
  $stopNumber = $number - 1;

  #
  # Fork a parser for each job.
  #

  for ($i=$startNumber;$i<=$stopNumber;$i++) {
    unless (fork) {
      exec("$^X $ENV{SERVER_BIN}/parser.pl $i");
    }
  }
}

#
# Logging
#

sub log {
  local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $year += 1900;
  $mon  += 1;
  if($mon < 10) {
      $mon = "0".$mon;
  }
  if($mday < 10) {
      $mday = "0".$mday;
  }
  if($min < 10) {
      $min = "0".$min;
  }
  if($sec < 10) {
      $sec = "0".$sec;
  }
  $startDate = "$mon/$mday/$year";
  $startTime = "$hour:$min:$sec";
  system("echo $startDate $startTime > $ENV{SERVER_JOBS}/job.$number/job.start");
  system("echo $interface > $ENV{SERVER_JOBS}/job.$number/job.interface");
  system("echo $password > $ENV{SERVER_JOBS}/job.$number/job.password");
}


#
# Generating a job password
#

sub generate_password {
    my $pass_length;
    my $passwd;
    my $r;
    my $i;

    $pass_length = 8;
    srand;
    $passwd = "";
    $i = 32;
    while (length($passwd) < $pass_length) {
        $r = rand(26) % 26;
        $i=int($r) + 65;
        if(int(rand(2)) % 2) {
            $i = $i + 32;       
        }
        $passwd = $passwd . pack("c",$i);
    }
    return $passwd;
}



