| 1 | package Transform::Alert::Input::POP3; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | our $VERSION = '1.00'; # VERSION | |||||||||||||||||||
| 4 | # ABSTRACT: Transform alerts from POP3 messages | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use sanity; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use MooX::Types::MooseLike::Base qw(Str Int ArrayRef HashRef InstanceOf Maybe); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | use Net::POP3; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use Email::MIME; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 2 | ▶ | use List::AllUtils 'first'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | with 'Transform::Alert::Input'; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # Stolen from connopts | ||||||||||||||||||||
| 17 | 2 | has username => ( | |||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||
| 19 | isa => Str, | ||||||||||||||||||||
| 20 | required => 1, | ||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||
| 22 | 2 | has password => ( | |||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||
| 24 | isa => Str, | ||||||||||||||||||||
| 25 | required => 1, | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | has _conn => ( | ||||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||
| 30 | isa => Maybe[InstanceOf['Net::POP3']], | ||||||||||||||||||||
| 31 | lazy => 1, | ||||||||||||||||||||
| 32 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||
| 34 | 0 | ▶ | Net::POP3->new( %{$self->connopts} ) || do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 0 | $self->log->error('POP3 New failed: '.$@); | |||||||||||||||||||
| 36 | 0 | return; | |||||||||||||||||||
| 37 | }; | ||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||
| 39 | 2 | predicate => 1, | |||||||||||||||||||
| 40 | clearer => 1, | ||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||
| 42 | 2 | has _list => ( | |||||||||||||||||||
| 43 | is => 'rw', | ||||||||||||||||||||
| 44 | isa => ArrayRef[Int], | ||||||||||||||||||||
| 45 | predicate => 1, | ||||||||||||||||||||
| 46 | clearer => 1, | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 50 | 0 | my ($orig, $self) = (shift, shift); | |||||||||||||||||||
| 51 | 0 | my $hash = shift; | |||||||||||||||||||
| 52 | 0 | ▶ | $hash = { $hash, @_ } unless ref $hash; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | $hash->{username} = delete $hash->{connopts}{username}; | |||||||||||||||||||
| 55 | 0 | $hash->{password} = delete $hash->{connopts}{password}; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # Net::POP3 is a bit picky about its case-sensitivity | ||||||||||||||||||||
| 58 | 0 | foreach my $keyword (qw{ Host ResvPort Timeout Debug }) { | |||||||||||||||||||
| 59 | 0 | ▶ | $hash->{connopts}{$keyword} = delete $hash->{connopts}{lc $keyword} if (exists $hash->{connopts}{lc $keyword}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | $orig->($self, $hash); | |||||||||||||||||||
| 63 | 2 | }; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | sub open { | ||||||||||||||||||
|
Subroutine
open: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 66 | 0 | my $self = shift; | |||||||||||||||||||
| 67 | my $pop = $self->_conn || | ||||||||||||||||||||
| 68 | # maybe+default+error still creates an undef attr, which would pass an 'exists' check on predicate | ||||||||||||||||||||
| 69 | 0 | ▶ | do { $self->_clear_conn; return; }; | ||||||||||||||||||
|
Condition0%0/3
$self->_conn || do {
$self->_clear_conn;
return
}
MC/DC0%0/2
$self->_conn || do {
$self->_clear_conn;
return
}
$self->_conn
do {
$self->_clear_conn;
return
}
Truth table0%0/3
A || B
A= $self->_connB= do {
$self->_clear_conn;
return
}
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | ▶ | unless ( $pop->login($self->username, $self->password) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | $self->log->error('POP3 Login failed: '.$pop->message); | |||||||||||||||||||
| 73 | 0 | return; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | my $msgnums = $pop->list; | |||||||||||||||||||
| 77 | $self->_list([ | ||||||||||||||||||||
| 78 | 0 | sort { $a <=> $b } keys %$msgnums | |||||||||||||||||||
| 79 | ]); | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | return 1; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | ▶ | sub opened { | ||||||||||||||||||
|
Subroutine
opened: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 85 | 0 | my $self = shift; | |||||||||||||||||||
| 86 | 0 | ▶ | return $self->_has_conn && $self->_conn->opened; | ||||||||||||||||||
|
Condition0%0/3
$self->_has_conn && $self->_conn->opened
MC/DC0%0/2
$self->_has_conn && $self->_conn->opened
$self->_has_conn
$self->_conn->opened
Truth table0%0/3
A && B
A= $self->_has_connB= $self->_conn->opened
| |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||
| 91 | 0 | my $num = shift @{$self->_list}; | |||||||||||||||||||
| 92 | 0 | my $pop = $self->_conn; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | my $amsg = $pop->get($num); | |||||||||||||||||||
| 95 | 0 | ▶ | unless ($amsg) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | $self->log->error('Error grabbing POP3 message #'.$num.': '.$pop->message); | |||||||||||||||||||
| 97 | 0 | return; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 0 | $pop->delete($num); | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | my $msg = join '', @$amsg; | |||||||||||||||||||
| 102 | 0 | $msg =~ s/\r//g; | |||||||||||||||||||
| 103 | 0 | my $pmsg = Email::MIME->new($msg); | |||||||||||||||||||
| 104 | 0 | ▶ | my $body = eval { $pmsg->body_str } || do { | ||||||||||||||||||
|
Condition0%0/3
eval {
do {
$pmsg->body_str
}
} || do {
my $part = &first(sub {
$_ and $_->content_type =~ m[^text/plain];
}
, $pmsg->parts);
$part ? $part->body_str : $pmsg->body_raw
}
MC/DC0%0/2
eval {
do {
$pmsg->body_str
}
} || do {
my $part = &first(sub {
$_ and $_->content_type =~ m[^text/plain];
}
, $pmsg->parts);
$part ? $part->body_str : $pmsg->body_raw
}
eval {
do {
$pmsg->body_str
}
}
do {
my $part = &first(sub {
$_ and $_->content_type =~ m[^text/plain];
}
, $pmsg->parts);
$part ? $part->body_str : $pmsg->body_raw
}
Truth table0%0/3
A || B
A= eval {
do {
$pmsg->body_str
}
}B= do {
my $part = &first(sub {
$_ and $_->content_type =~ m[^text/plain];
}
, $pmsg->parts);
$part ? $part->body_str : $pmsg->body_raw
}
| |||||||||||||||||||||
| 105 | 0 | ▶ | my $part = first { $_ && $_->content_type =~ /^text\/plain/ } $pmsg->parts; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 0 | ▶ | $part ? $part->body_str : $pmsg->body_raw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||
| 108 | 0 | $body =~ s/\r//g; | |||||||||||||||||||
| 109 | 0 | my $hash = { | |||||||||||||||||||
| 110 | $pmsg->header_obj->header_pairs, | ||||||||||||||||||||
| 111 | BODY => $body, | ||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 0 | return (\$msg, $hash); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 0 | ▶ | sub eof { | ||||||||||||||||||
|
Subroutine
eof: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||||||||
| 119 | 0 | ▶ | return not ($self->_has_list and @{$self->_list}); | ||||||||||||||||||
|
Condition0%0/3
$self->_has_list && @{$self->_list;}
MC/DC0%0/2
$self->_has_list && @{$self->_list;}
$self->_has_list
@{$self->_list;}
Truth table0%0/3
A && B
A= $self->_has_listB= @{$self->_list;}
| |||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | ▶ | sub close { | ||||||||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||
| 124 | 0 | my $pop = $self->_conn; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | ▶ | $pop->quit if $self->opened; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 0 | $self->_clear_list; | |||||||||||||||||||
| 128 | 0 | $self->_clear_conn; | |||||||||||||||||||
| 129 | 0 | return 1; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 2 | 42; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||