Merged MAPS2.0
[clearscm.git] / maps / bin / add2nulllist.pl
index 8ae2c78..82a3714 100755 (executable)
@@ -4,7 +4,8 @@ use warnings;
 
 use FindBin;
 
-use lib "$FindBin::Bin/../lib", '/opt/clearscm/lib';
+use lib "$FindBin::Bin/../lib";
+use lib "$FindBin::Bin/../../lib";
 
 use MAPS;
 use MAPSLog;
@@ -14,7 +15,9 @@ use Display;
 # Highly specialized!
 my $userid = $ENV{USER};
 my $Userid;
-my $type = "null";
+my $type = 'null';
+
+die "TODO: Test this script";
 
 sub GetItems($) {
   my ($filename) = @_;
@@ -34,6 +37,7 @@ sub GetItems($) {
     $item{domain}    = $address[1];
     $item{comment}   = $fields[1] ? $fields[1] : '';
     $item{hit_count} = $fields[2] ? $fields[2] : 0;
+    $item{retention} = $fields[3];
 
     push @items, \%item;
   } # while
@@ -44,46 +48,63 @@ sub GetItems($) {
 } # GetItems
 
 sub Add2List(@) {
-  my @items = @_;
+  my (@items) = @_;
+
+  my $item;
 
-  my $sender  = '';
-  my $nextseq = GetNextSequenceNo($userid, $type);
+  my $item->{sequence} = GetNextSequenceNo(
+    userid => $userid,
+    type   => $type,
+  );
 
-  for (@items) {
-    my %item = %{$_};
+  $item->{userid} = $userid;
+  $item->{type}   = $type;
 
-    my $pattern   = $item{pattern};
-    my $domain    = $item{domain};
-    my $comment   = $item{comment};
-    my $hit_count = $item{hit_count};
+  for $item (@items) {
+    display_nolf "Adding $item->{pattern}\@$item->{domain} ($item->{comment}) to null list ($item->{sequence})...";
 
-    display_nolf "Adding $pattern\@$domain ($comment) to null list ($nextseq)...";
+    last unless $item->{pattern} or $item->{domain};
 
-    last if ((!defined $pattern || $pattern eq '') &&
-             (!defined $domain  || $domain  eq ''));
+    $item->{sender} = CheckEmail $item->{pattern}, $item->{domain};
 
-    $sender = lc ("$pattern\@$domain");
+    my ($status, $rule) = OnNulllist($item->{sender}, $userid);
 
-    if (OnNulllist($sender)) {
+    if ($status) {
       display ' Already on list';
     } else {
-      Add2Nulllist($sender, $userid, $comment, $hit_count);
+      my ($message, $msg) = Add2Nulllist(%$item);
+
       display ' done';
 
       # Now remove this entry from the other lists (if present)
       for my $otherlist ('white', 'black') {
-        my $sth = FindList($otherlist, $sender);
-        my ($sequence, $count);
-
-        ($_, $_, $_, $_, $_, $sequence) = GetList($sth);
-
-        if ($sequence) {
-          $count = DeleteList($otherlist, $sequence);
+        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
       } # for
     } # if
 
-    $nextseq++;
+    $item->{sequence}++;
   } # while
 
   return;