+#With spamlist
+if ($spamlist) {
+ #Extract originating ip in spam
+ map {
+ #Open spam file for reading
+ open (my $fh, '<', $_) or die "Can't open < $_: $!";
+
+ #Lookup for X-Originating-IP header
+ while (<$fh>) {
+ if (/X-Originating-IP: (.+)$/) {
+ #Set ip
+ my $ip = ${1};
+
+ #Set blacklist for 80 and 443 dest ports on tcp
+ my %blacklist = ('tcp' => [ 80, 443, 8000, 8080, 8443 ]);
+
+ #Check if v4 ip and not in whitelist
+ #if (is_ipv4($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv4}}) {
+ if (is_ipv4($ip) && not scalar map { my $network = new_ipv4($_); my $netip = new_ipv4($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv4}}) {
+ if (!defined $ip4s{$ip}) {
+ %{$ip4s{$ip}} = ('tcp' => {}, 'udp' => {});
+ }
+ #Add ip tuples in v4 blacklist
+ map { my $proto = $_; map { $ip4s{$ip}{$proto}{$_}=1; } @{$blacklist{$proto}}; } keys %blacklist;
+ } elsif (is_ipv6($ip) && not scalar map { my $network = NetAddr::IP->new($_); my $netip = NetAddr::IP->new($ip); unless ($network->contains($netip)) { (); } } @{$iplist{ipv6}}) {
+ if (!defined $ip6s{$ip}) {
+ %{$ip6s{$ip}} = ('tcp' => {}, 'udp' => {});
+ }
+ #Add ip tuples in v6 blacklist
+ map { my $proto = $_; map { $ip6s{$ip}{$proto}{$_}=1; } @{$blacklist{$proto}}; } keys %blacklist;
+ }
+ }
+ }
+
+ #Close spam file
+ close $fh or die "Can't close fh: $!";
+ } glob('/var/spool/mail/*/.Junk/{new,cur,tmp}/*');
+}
+