File Coverage

blib/lib/Mail/DataFeed/Abusix.pm
Criterion Covered Total %
statement 34 46 73.9
branch 12 14 85.7
condition 12 13 92.3
subroutine 11 13 84.6
pod 1 1 100.0
total 70 87 80.4


line stmt bran cond sub pod time code
1             package Mail::DataFeed::Abusix;
2 1     1   74735 use Moo;
  1         11859  
  1         7  
3 1     1   1503 use v5.20;
  1         4  
4 1     1   10 use strict;
  1         1  
  1         19  
5 1     1   4 use warnings;
  1         3  
  1         27  
6 1     1   6 use feature qw(postderef);
  1         2  
  1         89  
7 1     1   5 no warnings qw(experimental::postderef);
  1         2  
  1         59  
8             # ABSTRACT: Send SMTP transaction data to the Abusix transaction feed
9             our $VERSION = '2.20210112'; ## VERSION
10 1     1   7 use Digest::MD5 qw(md5_hex);
  1         2  
  1         75  
11 1     1   556 use IO::Socket;
  1         22377  
  1         4  
12              
13             has feed_name => ( is => 'ro', required => 1 );
14             has feed_key => ( is => 'ro', required => 1);
15             has feed_dest => ( is => 'ro', required => 1);
16              
17             has sockets => ( is => 'ro', lazy => 1, builder => '_sockets' );
18              
19             has port => ( is => 'rw' );
20             has ip_address => ( is => 'rw' );
21             has reverse_dns => ( is => 'rw' );
22             has helo => ( is => 'rw' );
23             has used_esmtp => ( is => 'rw', default => undef );
24             has used_tls => ( is => 'rw', default => undef );
25             has used_auth => ( is => 'rw', default => undef );
26             has mail_from_domain => ( is => 'rw' );
27             has time => ( is => 'rw', lazy => 1, builder => '_build_time' );
28              
29              
30              
31             sub _sockets {
32 0     0   0 my ($self) = @_;
33 0         0 my @sockets;
34 0         0 foreach my $dest ( split ',', $self->feed_dest ) {
35 0         0 my ( $peer_address, $peer_port ) = split(':', $dest, 2);
36 0 0       0 $peer_port = 12211 if !$peer_port;
37 0         0 my $socket = IO::Socket::INET->new(
38             PeerAddr => $peer_address,
39             PeerPort => $peer_port,
40             Proto => 'udp',
41             Type => SOCK_DGRAM,
42             );
43 0         0 push @sockets, $socket;
44             }
45 0         0 return \@sockets;
46             }
47              
48              
49             sub send {
50 0     0 1 0 my ($self) = @_;
51 0         0 my $report = $self->_build_report();
52 0         0 foreach my $socket ($self->sockets->@*) {
53 0         0 $socket->send($report);
54             }
55              
56             }
57              
58             sub _build_time {
59 1     1   13 my ($self) = @_;
60 1         7 return time;
61             }
62              
63             sub _build_report {
64 25     25   49868 my ($self,$args) = @_;
65              
66 25   66     658 my $time = $args->{_time} // $self->time; # Ability to override time for testing!
67 25         180 my $extended_json = ''; # Reserved for future use, should be empty.
68              
69 25 100 100     419 my $packet = join( "\n",
    100 100        
    100 100        
    100 100        
    100 100        
    100          
70             $self->feed_name,
71             $time,
72             $self->port // '',
73             $self->ip_address // '',
74             $self->reverse_dns // '',
75             $self->helo // '',
76             !defined $self->used_esmtp ? '' : $self->used_esmtp ? 'Y' : 'N',
77             !defined $self->used_tls ? '' : $self->used_tls ? 'Y' : 'N',
78             !defined $self->used_auth ? '' : $self->used_auth ? 'Y' : 'N',
79             $self->mail_from_domain // '',
80             $extended_json,
81             );
82              
83 25         65 $packet = join( "\n",
84             $packet,
85             $self->_checksum($packet),
86             );
87              
88 25         75 return $packet;
89             }
90              
91             sub _checksum {
92 25     25   64 my ($self, $packet) = @_;
93 25         137 my $checksum = md5_hex(join( "\n", $packet, $self->feed_key ));
94 25         74 return $checksum;
95             }
96              
97             1;
98              
99             __END__