Large MAPS update
[clearscm.git] / maps / bin / add2nulllist.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use FindBin;
6
7 use lib "$FindBin::Bin/../lib", '/opt/clearscm/lib';
8
9 use MAPS;
10 use MAPSLog;
11 use MAPSWeb;
12 use Display;
13
14 # Highly specialized!
15 my $userid = $ENV{USER};
16 my $Userid;
17 my $type = "null";
18
19 sub GetItems($) {
20   my ($filename) = @_;
21
22   my @items;
23
24   open my $file, '<', $filename
25     or error "Unable to open $filename - $!", 1;
26
27   while (<$file>) {
28     my @fields = split;
29     my %item;
30
31     my @address = split /\@/, $fields[0];
32
33     $item{pattern}   = $address[0];
34     $item{domain}    = $address[1];
35     $item{comment}   = $fields[1] ? $fields[1] : '';
36     $item{hit_count} = $fields[2] ? $fields[2] : 0;
37
38     push @items, \%item;
39   } # while
40
41   close $file;
42
43   return @items;
44 } # GetItems
45
46 sub Add2List(@) {
47   my @items = @_;
48
49   my $sender  = '';
50   my $nextseq = GetNextSequenceNo($userid, $type);
51
52   for (@items) {
53     my %item = %{$_};
54
55     my $pattern   = $item{pattern};
56     my $domain    = $item{domain};
57     my $comment   = $item{comment};
58     my $hit_count = $item{hit_count};
59
60     display_nolf "Adding $pattern\@$domain ($comment) to null list ($nextseq)...";
61
62     last if ((!defined $pattern || $pattern eq '') &&
63              (!defined $domain  || $domain  eq ''));
64
65     $sender = lc ("$pattern\@$domain");
66
67     if (OnNulllist($sender)) {
68       display ' Already on list';
69     } else {
70       Add2Nulllist($sender, $userid, $comment, $hit_count);
71       display ' done';
72
73       # Now remove this entry from the other lists (if present)
74       for my $otherlist ('white', 'black') {
75         my $sth = FindList($otherlist, $sender);
76         my ($sequence, $count);
77
78         ($_, $_, $_, $_, $_, $sequence) = GetList($sth);
79
80         if ($sequence) {
81           $count = DeleteList($otherlist, $sequence);
82         } # if
83       } # for
84     } # if
85
86     $nextseq++;
87   } # while
88
89   return;
90 } # Add2List
91
92 # Main
93 my $filename;
94
95 if ($ARGV[0]) {
96   $filename = $ARGV[0];
97 } else {
98   error "Must specify a filename of addresses to null list", 1;
99 } # if
100
101 SetContext($userid);
102
103 $Userid = ucfirst $userid;
104
105 Add2List(GetItems ($filename));
106
107 exit;