File Coverage

blib/lib/Mail/DataFeed/Abusix.pm
Criterion Covered Total %
statement 32 44 72.7
branch 12 14 85.7
condition 12 13 92.3
subroutine 10 12 83.3
pod 1 1 100.0
total 67 84 79.7


line stmt bran cond sub pod time code
1             package Mail::DataFeed::Abusix;
2 1     1   74600 use Moo;
  1         11462  
  1         5  
3 1     1   1495 use v5.20;
  1         4  
4 1     1   7 use strict;
  1         2  
  1         18  
5 1     1   5 use warnings;
  1         2  
  1         31  
6 1     1   5 use feature qw(postderef);
  1         1  
  1         92  
7 1     1   6 no warnings qw(experimental::postderef);
  1         2  
  1         58  
8             # ABSTRACT: Send SMTP transaction data to the Abusix transaction feed
9             our $VERSION = '1.20200331.1'; ## VERSION
10 1     1   7 use Digest::MD5 qw(md5_hex);
  1         1  
  1         61  
11 1     1   541 use IO::Socket;
  1         21764  
  1         5  
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              
28              
29              
30             sub _sockets {
31 0     0   0 my ($self) = @_;
32 0         0 my @sockets;
33 0         0 foreach my $dest ( split ',', $self->feed_dest ) {
34 0         0 my ( $peer_address, $peer_port ) = split(':', $dest, 2);
35 0 0       0 $peer_port = 12211 if !$peer_port;
36 0         0 my $socket = IO::Socket::INET->new(
37             PeerAddr => $peer_address,
38             PeerPort => $peer_port,
39             Proto => 'udp',
40             Type => SOCK_DGRAM,
41             );
42 0         0 push @sockets, $socket;
43             }
44 0         0 return \@sockets;
45             }
46              
47              
48             sub send {
49 0     0 1 0 my ($self) = @_;
50 0         0 my $report = $self->_build_report();
51 0         0 foreach my $socket ($self->sockets->@*) {
52 0         0 $socket->send($report);
53             }
54              
55             }
56              
57             sub _build_report {
58 25     25   49765 my ($self,$args) = @_;
59              
60 25   66     121 my $time = $args->{_time} // time(); # Ability to override time for testing!
61 25         46 my $extended_json = ''; # Reserved for future use, should be empty.
62              
63 25 100 100     379 my $packet = join( "\n",
    100 100        
    100 100        
    100 100        
    100 100        
    100          
64             $self->feed_name,
65             $time,
66             $self->port // '',
67             $self->ip_address // '',
68             $self->reverse_dns // '',
69             $self->helo // '',
70             !defined $self->used_esmtp ? '' : $self->used_esmtp ? 'Y' : 'N',
71             !defined $self->used_tls ? '' : $self->used_tls ? 'Y' : 'N',
72             !defined $self->used_auth ? '' : $self->used_auth ? 'Y' : 'N',
73             $self->mail_from_domain // '',
74             $extended_json,
75             );
76              
77 25         61 $packet = join( "\n",
78             $packet,
79             $self->_checksum($packet),
80             );
81              
82 25         89 return $packet;
83             }
84              
85             sub _checksum {
86 25     25   47 my ($self, $packet) = @_;
87 25         142 my $checksum = md5_hex(join( "\n", $packet, $self->feed_key ));
88 25         78 return $checksum;
89             }
90              
91             1;
92              
93             __END__