#!/usr/bin/perl -w

if($^O eq 'MSWin32') {

  $execListAcct = "listacct.exe";
  $execListmlists = "listmlists.exe";
  $execGetlist = "getlist.exe";
  $execListSub = "listsubscribers.exe";

  $inputListAcct = "listacct.tmp";
  $inputListmlists = "listmlists.tmp";
  $inputGetlist = "getlist.tmp";
  $tempListSub = "listsub.tmp";

} else {

  $execListAcct = "./listacct";
  $execListmlists = "./listmlists";
  $execGetlist = "./getlist";
  $execListSub = "./listsubscribers";

  $inputListAcct = "/tmp/listacct.tmp";
  $inputListmlists = "/tmp/listmlists.tmp";
  $inputGetlist = "/tmp/getlist.tmp";
  $tempListSub = "/tmp/listsub.tmp";

}

sub CreateList($$$);
sub ListSubscr($$$$$$);

# The CLI.pm module can be downloaded from <http://www.stalker.com/CGPerl>
use cli;

print "Input Password of Postmaster:\n";
while(<STDIN>) {
  chomp;             
  my ($password) = split;

   $CGProPostmasterLogin= "postmaster";
   $CGProPostmasterPassword = $password;
   $CGProServerAddress = "localhost";

   my $cli = new CGP::CLI( { PeerAddr => $CGProServerAddress,
                           PeerPort => 106,
                           login    => $CGProPostmasterLogin,
                           password => $CGProPostmasterPassword } )
   || return "$CGProPostmasterLogin can't login to CGPro: ".$CGP::ERR_STRING;

print "Starting\n";

my $execStr="$execListAcct -i SMTP-Address,Name>$inputListAcct";
system($execStr)==0 or die "Can't execute $execStr\n" ; 
die "Can't find $inputListAcct\n" unless -e $inputListAcct;

my $execStr1="$execListmlists>$inputListmlists";
system($execStr1)==0 or die "Can't execute $execStr1\n" ; 
die "Can't find $inputListmlists\n" unless -e $inputListmlists;

open INFILE1, "< $inputListmlists" or die "Can't open $inputListmlists: $!\n";

while(<INFILE1>) {
  chomp;
  my ($idList) = split ;

  my $execStr2="$execGetlist $idList ->$inputGetlist";
 system($execStr2)==0 or die "Can't execute $execStr2\n" ; 
 die "Can't find $inputGetlist\n" unless -e $inputGetlist;

 open INFILE2, "< $inputGetlist" or die "Can't open $inputGetlist: $!\n";
 while(<INFILE2>) {
   chomp;
   my ($idGetlist1,$idGetlist2) = split / /;

   $idGetlist1 =~ /\w+/g;
   $idGetlist2 =~ /\[+/g;

   if ($idGetlist1 eq 'List-Owners:' ) {
     $idGetlist2 =~ s/\[+//;
     $idGetlist2 =~ s/<+//;
     $idGetlist2 =~ /(\w+)/;
     $owners = $1;
   }

   if ($idGetlist1 eq 'SMTP-Address:' ) {
     $idGetlist2 =~ s/\[+//;
     $idGetlist2 =~ /(\w+)/;
     $addresses = $1;
   }

 }
 close INFILE2;
 unlink $inputGetlist;

   CreateList($cli,$owners,$addresses);
   
   if ($addresses ne 'All') {print "Subscribe...\n";}
   ListSubscr($cli,$addresses,$idList,$execListSub,$tempListSub,$inputListAcct);

}

   $cli->Logout();

close INFILE1;
unlink $inputListmlists;
unlink $inputListAcct;

print "\nPress <Enter> to continue...";
<>;

   last;

}

exit;
#-----------------------
sub CreateList($$$) {
   my ($cli,$owners,$addresses)=@_;

   if ($addresses ne 'All') { 
     $cli->CreateList($addresses,$owners); 
     print "Create list: ",$addresses," ",$owners,"\n";
   }

}
#---------------------
sub ListSubscr($$$$$$) {
  my ($cli,$addresses,$idList,$execListSub,$tempListSub,$inputListAcct)=@_;

  my $execStr2="$execListSub $idList>$tempListSub";
  system($execStr2)==0 or die "Can't execute $execStr2\n" ; 
  die "Can't find $tempListSub\n" unless -e $tempListSub;

  open INFILE3, "< $tempListSub" or die "Can't open $tempListSub: $!\n";

  while(<INFILE3>) {
    chomp;
   my ($idSublist) = split / /;
   if ($idSublist eq 'There') {return $idSublist;}
  
   open INFILE4, "< $inputListAcct" or die "Can't open $inputListAcct : $!\n";

   $check = 0;
   while(<INFILE4>) {
      chomp;
      my ($idAcct1,$idAcct2)= split /\;/;

      if ($idSublist =~ /\<+/) {
        $idSublist =~ s/\<+//;
        $idSublist =~ s/\>+//;

       $cli->List($addresses,"subscribe",$idSublist);

       $check = 1;
      }
      if ($idAcct1 =~ /$idSublist+/) {

        $subscribe = $idSublist;
        $subscribe =~ s/$idSublist+/\<$idSublist\>/;
        $cli->List($addresses,"subscribe",$idAcct2,$subscribe);

       $check = 1;
      }

   }
   close INFILE4;
   if ($check eq 0) {
      $idSublist =~ /\w([\@\.]+)/g;
        $subscribe = $idSublist;
        $subscribe =~ s/$idSublist+/\<$idSublist\>/;
        $cli->List($addresses,"subscribe",$subscribe);
   }
   $check = 0;
  }
  close INFILE3;
  unlink $tempListSub;

}