File Coverage

blib/lib/Mail/Milter/Authentication/Handler/AbusixDataFeed.pm
Criterion Covered Total %
statement 65 70 92.8
branch 17 20 85.0
condition 4 6 66.6
subroutine 12 13 92.3
pod 0 6 0.0
total 98 115 85.2


line stmt bran cond sub pod time code
1             package Mail::Milter::Authentication::Handler::AbusixDataFeed;
2 1     1   3880 use strict;
  1         3  
  1         30  
3 1     1   5 use warnings;
  1         2  
  1         85  
4 1     1   10 use Mail::Milter::Authentication 2.201811;
  1         33  
  1         32  
5 1     1   5 use base 'Mail::Milter::Authentication::Handler';
  1         3  
  1         127  
6             # ABSTRACT: Send data to Abusix
7             our $VERSION = '2.20181204'; ## VERSION
8              
9 1     1   9 use English qw{ -no_match_vars };
  1         2  
  1         7  
10 1     1   406 use Sys::Syslog qw{:standard :macros};
  1         3  
  1         348  
11 1     1   479 use Mail::DataFeed::Abusix;
  1         41355  
  1         762  
12              
13             sub default_config {
14             return {
15 1     1 0 4028 'feed_name' => 'name_of_feed',
16             'feed_dest' => 'server:port',
17             'feed_key' => 'this_is_a_secret',
18             'listening_port' => '25',
19             };
20             }
21              
22             sub helo_callback {
23 6     6 0 105929 my ( $self, $helo_host ) = @_;
24 6         24 $self->{'helo_name'} = $helo_host;
25 6         19 return;
26             }
27              
28             sub envfrom_callback {
29 6     6 0 5444 my ($self, $from) = @_;
30 6         28 my $config = $self->handler_config();
31              
32             $self->{ 'abusix_feed' } = Mail::DataFeed::Abusix->new(
33             'feed_name' => $config->{ 'feed_name' },
34             'feed_dest' => $config->{ 'feed_dest' },
35 6         442 'feed_key' => $config->{ 'feed_key' },
36             );
37 6         2526 $self->{ 'abusix_feed' }->mail_from_domain( $self->get_domain_from( $from ) );
38 6         1060 $self->{ 'abusix_feed' }->helo( $self->{ 'helo_name' } );
39 6         29 $self->{ 'abusix_feed' }->port( $config->{ 'listening_port' } );
40 6         33 $self->{ 'abusix_feed' }->ip_address( $self->ip_address() );
41              
42 6         128 delete $self->{ 'first_received' };
43              
44 6         27 my $resolver = $self->get_object('resolver');
45 6         1084 my @rdns;
46 6         25 my $packet = $resolver->query( $self->ip_address(), 'PTR' );
47 6 100       1962 if ($packet) {
48 1         7 foreach my $rr ( $packet->answer ) {
49             # We do not consider CNAMES here
50 1 50       11 if ( $rr->type eq "PTR" ) {
51 1         35 my $rdatastr = $rr->rdatastr;
52 1         129 $rdatastr =~ s/\r//g;
53 1         3 $rdatastr =~ s/\n//g;
54 1         5 push @rdns, $rdatastr;
55             }
56             }
57             }
58 6         41 $self->{ 'abusix_feed' }->reverse_dns( join( ',', @rdns ) );
59              
60 6         47 return;
61             }
62              
63             sub header_callback {
64 22     22 0 29451 my ( $self, $header, $value ) = @_;
65 22 100       83 return if defined $self->{ 'first_received' };
66 10 100       40 return if lc $header ne 'received';
67 4         17 my $protocol = Mail::Milter::Authentication::Config::get_config()->{'protocol'};
68 4 50       47 return if $protocol ne 'smtp';
69              
70 4         13 $self->{ 'first_received' } = $value;
71 4         14 return;
72             }
73              
74             sub eoh_callback {
75 6     6 0 6439 my ( $self ) = @_;
76 6 100       24 if ( $self->is_handler_loaded('TLS') ) {
77 4   100     71 $self->{ 'abusix_feed' }->used_tls( $self->is_encrypted() || 0 ); # Explicit 0 over undef if TLS handler is loaded
78             }
79 6         171 $self->{ 'abusix_feed' }->used_auth( $self->is_authenticated() );
80 6 100       134 if ( defined $self->{ 'first_received' } ) {
81 4         21 my $used_smtp = $self->{ 'first_received' } =~ / with SMTP/;
82 4         17 my $used_esmtp = $self->{ 'first_received' } =~ / with ESMTP/;
83 4         76 warn " VALUES SMTP $used_smtp ESMTP $used_esmtp";
84 4 50 50     39 if ( $used_smtp xor $used_esmtp ) {
85             # Filters line noise!
86 4 100       26 $self->{ 'abusix_feed' }->used_esmtp( 1 ) if $used_esmtp;
87 4 100       22 $self->{ 'abusix_feed' }->used_esmtp( 0 ) if $used_smtp;
88             }
89             }
90 6         36 $self->{ 'abusix_feed' }->send();
91 6         617 return;
92             }
93              
94              
95             sub close_callback {
96 0     0 0   my ( $self ) = @_;
97              
98 0           delete $self->{'helo_name'};
99 0           delete $self->{'abusix_feed'};
100 0           delete $self->{ 'first_received' };
101 0           return;
102             }
103              
104             1;
105              
106             __END__