Fixing up numbers and totals
[clearscm.git] / maps / bin / add2nulllist.pl
index 27d694e..82a3714 100755 (executable)
@@ -4,7 +4,8 @@ use warnings;
 
 use FindBin;
 
-use lib $FindBin::Bin, '/opt/clearscm/lib';
+use lib "$FindBin::Bin/../lib";
+use lib "$FindBin::Bin/../../lib";
 
 use MAPS;
 use MAPSLog;
@@ -14,89 +15,114 @@ use Display;
 # Highly specialized!
 my $userid = $ENV{USER};
 my $Userid;
-my $type = "null";
+my $type = 'null';
 
-sub GetItems {
-  my $filename = shift;
+die "TODO: Test this script";
+
+sub GetItems($) {
+  my ($filename) = @_;
 
   my @items;
 
-  open FILE, $filename
+  open my $file, '<', $filename
     or error "Unable to open $filename - $!", 1;
 
-  while (<FILE>) {
+  while (<$file>) {
     my @fields = split;
     my %item;
 
-    my @address = split /\@/, $fields [0];
+    my @address = split /\@/, $fields[0];
 
     $item{pattern}   = $address[0];
     $item{domain}    = $address[1];
     $item{comment}   = $fields[1] ? $fields[1] : '';
-    $itme{hit_count} = $fields[2] ? $fields[2] : 0;
+    $item{hit_count} = $fields[2] ? $fields[2] : 0;
+    $item{retention} = $fields[3];
 
     push @items, \%item;
   } # while
 
+  close $file;
+
   return @items;
 } # GetItems
 
-sub Add2List {
-  my @items = @_;
+sub Add2List(@) {
+  my (@items) = @_;
 
-  my $sender  = "";
-  my $nextseq = MAPSDB::GetNextSequenceNo $userid, $type;
+  my $item;
 
-  foreach (@items) {
-    my %item = %{$_};
+  my $item->{sequence} = GetNextSequenceNo(
+    userid => $userid,
+    type   => $type,
+  );
 
-    my $pattern   = $item{pattern};
-    my $domain    = $item{domain};
-    my $comment   = $item{comment};
-    my $hit_count = $item{hit_count};
+  $item->{userid} = $userid;
+  $item->{type}   = $type;
 
-    display_nolf "Adding $pattern\@$domain ($comment) to null list ($nextseq)...";
+  for $item (@items) {
+    display_nolf "Adding $item->{pattern}\@$item->{domain} ($item->{comment}) to null list ($item->{sequence})...";
 
-    last if ((!defined $pattern || $pattern eq '') &&
-             (!defined $domain  || $domain  eq ''));
+    last unless $item->{pattern} or $item->{domain};
 
-    $sender = lc ("$pattern\@$domain");
+    $item->{sender} = CheckEmail $item->{pattern}, $item->{domain};
 
-    if (OnNulllist $sender) {
-      display " Already on list";
-    } else {
-      Add2Nulllist $sender, $userid, $comment, $hit_count;
-      display " done";
+    my ($status, $rule) = OnNulllist($item->{sender}, $userid);
 
-      # Now remove this entry from the other lists (if present)
-      foreach my $otherlist ("white", "black") {
-        my $sth = FindList $otherlist, $sender;
-        my ($sequence, $count);
+    if ($status) {
+      display ' Already on list';
+    } else {
+      my ($message, $msg) = Add2Nulllist(%$item);
 
-        ($_, $_, $_, $_, $_, $sequence) = GetList $sth;
+      display ' done';
 
-        if ($sequence) {
-          $count = DeleteList $otherlist, $sequence;
+      # Now remove this entry from the other lists (if present)
+      for my $otherlist ('white', 'black') {
+        FindList(
+          userid => $item->{userid},
+          type   => $otherlist,
+          sender => $item->{sender}
+        );
+
+        my $seq = GetList;
+
+        if ($seq->{sequence}) {
+          my $count = DeleteList(
+            userid   => $item->{userid},
+            type     => $otherlist,
+            sequence => $seq->{sequence},
+          );
+
+          display "Removed $item->{sender} from ${Userid}'s " . ucfirst $otherlist . ' list'
+            if $count > 0;
+
+          ResequenceList(
+            userid => $item->{userid},
+            type   => $otherlist,
+          );
         } # if
-      } # foreach
+      } # for
     } # if
-    $nextseq++;
+
+    $item->{sequence}++;
   } # while
+
+  return;
 } # Add2List
 
 # Main
 my $filename;
 
-if ($ARGV [0]) {
+if ($ARGV[0]) {
   $filename = $ARGV[0];
 } else {
   error "Must specify a filename of addresses to null list", 1;
 } # if
 
-SetContext $userid;
+SetContext($userid);
 
 $Userid = ucfirst $userid;
 
-Add2List (GetItems $filename);
+Add2List(GetItems ($filename));
 
 exit;