Alert/Input/IMAP.pm

filter: Statementstmt 25.7% 25 / 97 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 42.8% 6 / 14 Podpod 0.0% 0 / 5 totaltotal 15.0% 31 / 206 CC 32 SCAR 42.8
CC
32
Cov
15%
open
182.0
get
56.0
close
56.0
CRAP 72.5
« Alert/TemplateGrp.pm ↑ Summary Alert/InputGrp.pm »
1 package Transform::Alert::Input::IMAP;
2  
3 2 our $VERSION = '1.00'; # VERSION
4 # ABSTRACT: Transform alerts from IMAP 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 ArrayRef InstanceOf Maybe);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Mail::IMAPClient;
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 parsed_folder => (
18 is => 'ro',
19 isa => Str,
20 predicate => 1,
21 );
22  
23 has _conn => (
24 is => 'rw',
25 isa => Maybe[InstanceOf['Mail::IMAPClient']],
26 lazy => 1,
27 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
28 0 my $self = shift;
29 0 ▶ Mail::IMAPClient->new( %{$self->connopts} ) || do {
Branch: 1 branch
BranchTrueFalseTotal
"Mail::IMAPClient"->new(%{$self->connopts;}) or do { $self->log->error('IMAP New/Connect/Login failed: ' . $@); return } 0 0 0
30 0 $self->log->error('IMAP New/Connect/Login failed: '.$@);
31 0 return;
32 };
33 },
34 2 predicate => 1,
35 clearer => 1,
36 );
37 2 has _list => (
38 is => 'rw',
39 isa => ArrayRef[Str],
40 predicate => 1,
41 clearer => 1,
42 );
43  
44 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
45 0 my ($orig, $self) = (shift, shift);
46 0 my $hash = shift;
47 0 ▶ $hash = { $hash, @_ } unless ref $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash 0 0 0
48  
49 0 ▶ $hash->{parsed_folder} = delete $hash->{connopts}{parsedfolder} if exists $hash->{connopts}{parsedfolder};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$hash{"connopts"}{"parsedfolder"} 0 0 0
50  
51 0 $orig->($self, $hash);
52 2 };
53  
54 0 ▶sub open {
Subroutine open: not called CC=13 SCAR=52.0
Pod undocumented
55 0 my $self = shift;
56 my $imap = $self->_conn ||
57 # maybe+default+error still creates an undef attr, which would pass an 'exists' check on predicate
58 0 ▶ do { $self->_clear_conn; return; };
Condition0%0/3
$self->_conn || do { $self->_clear_conn; return }
l!l&&r!l&&!r
000
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 }
ABresult
1X1
011
000
59  
60 # figure out which state it's in
61 0 ▶ unless ($imap->IsSelected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($imap->IsSelected) 0 0 0
62 0 ▶ if (not $imap->IsConnected) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $imap->IsConnected) { } 0 0 0
elsif (not $imap->IsAuthenticated) { } 0 0 0
63 0 ▶ $imap->connect || do { $self->log->error('IMAP Connect/Login failed: '.$imap->LastError); return; };
Branch: 1 branch
BranchTrueFalseTotal
$imap->connect or do { $self->log->error('IMAP Connect/Login failed: ' . $imap->LastError); return } 0 0 0
64 }
65 elsif (not $imap->IsAuthenticated) {
66 0 ▶ $imap->login || do { $self->log->error('IMAP Login failed: ' .$imap->LastError); return; };
Branch: 1 branch
BranchTrueFalseTotal
$imap->login or do { $self->log->error('IMAP Login failed: ' . $imap->LastError); return } 0 0 0
67 }
68  
69 # might not have a folder set (or might have a specific Folder option)
70 0 ▶ if (!$imap->IsSelected || $self->connopts->{folder}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $imap->IsSelected or $self->connopts->{"folder"}) 0 0 0
Condition0%0/3
not $imap->IsSelected or $self->connopts->{"folder"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $imap->IsSelected or $self->connopts->{"folder"}
not $imap->IsSelected $self->connopts->{"folder"}
Truth table0%0/3
not(A) or B
A= not $imap->IsSelectedB= $self->connopts->{"folder"}
ABresult
1X1
011
000
71 0 ▶ $imap->select($self->connopts->{folder} || 'Inbox') || do { $self->log->error('IMAP Select failed: '.$imap->LastError); return; };
Branch: 1 branch
BranchTrueFalseTotal
$imap->select($self->connopts->{"folder"} || "Inbox") or do { $self->log->error('IMAP Select failed: ' . $imap->LastError); return } 0 0 0
Condition0%0/2
$self->connopts->{'folder'} || 'Inbox'
l!l
00
MC/DC0%0/1
$self->connopts->{'folder'} || 'Inbox'
$self->connopts->{'folder'}
Truth table0%0/2
A
A= $self->connopts->{'folder'}
Aresult
11
00
72 }
73 }
74  
75 0 ▶ my $msgs = ($self->has_parsed_folder ? $imap->messages : $imap->unseen) || do {
Branch: 1 branch
BranchTrueFalseTotal
$self->has_parsed_folder ? : 0 0 0
Condition0%0/3
($self->has_parsed_folder ? $imap->messages : $imap->unseen) || do { $self->log->error('IMAP Messages failed: ' . $imap->LastError); return }
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->has_parsed_folder ? $imap->messages : $imap->unseen) || do { $self->log->error('IMAP Messages failed: ' . $imap->LastError); return }
($self->has_parsed_folder ? $imap->messages : $imap->unseen) do { $self->log->error('IMAP Messages failed: ' . $imap->LastError); return }
Truth table0%0/3
A || B
A= ($self->has_parsed_folder ? $imap->messages : $imap->unseen)B= do { $self->log->error('IMAP Messages failed: ' . $imap->LastError); return }
ABresult
1X1
011
000
76 0 $self->log->error('IMAP Messages failed: '.$imap->LastError);
77 0 return;
78 };
79 0 $self->_list($msgs);
80  
81 0 return 1;
82 }
83  
84 0 ▶sub opened {
Subroutine opened: not called CC=3 SCAR=24.8
Pod undocumented
85 0 my $self = shift;
86 0 ▶ $self->_has_conn and $self->_conn and $self->_conn->IsSelected;
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_conn and $self->_conn and $self->_conn->IsSelected 0 0 0
Condition0%0/3
$self->_has_conn and $self->_conn
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_has_conn and $self->_conn
$self->_has_conn $self->_conn
Truth table0%0/3
A and B
A= $self->_has_connB= $self->_conn
ABresult
0X0
100
111
87 }
88  
89 0 ▶sub get {
Subroutine get: not called CC=7 SCAR=40.3
Pod undocumented
90 0 my $self = shift;
91 0 my $uid = shift @{$self->_list};
92 my $imap = $self->_conn ||
93 # maybe+default+error still creates an undef attr, which would pass an 'exists' check on predicate
94 0 ▶ do { $self->_clear_conn; return; };
Condition0%0/3
$self->_conn || do { $self->_clear_conn; return }
l!l&&r!l&&!r
000
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 }
ABresult
1X1
011
000
95  
96 0 ▶ my $msg = $imap->message_string($uid) || do { $self->log->error('Error grabbing IMAP message '.$uid.': '.$imap->LastError); return; };
Condition0%0/3
$imap->message_string($uid) || do { $self->log->error('Error grabbing IMAP message ' . $uid . ': ' . $imap->LastError); return }
l!l&&r!l&&!r
000
MC/DC0%0/2
$imap->message_string($uid) || do { $self->log->error('Error grabbing IMAP message ' . $uid . ': ' . $imap->LastError); return }
$imap->message_string($uid) do { $self->log->error('Error grabbing IMAP message ' . $uid . ': ' . $imap->LastError); return }
Truth table0%0/3
A || B
A= $imap->message_string($uid)B= do { $self->log->error('Error grabbing IMAP message ' . $uid . ': ' . $imap->LastError); return }
ABresult
1X1
011
000
97 0 $msg =~ s/\r//g;
98 0 my $pmsg = Email::MIME->new($msg);
99 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 }
l!l&&r!l&&!r
000
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 }
ABresult
1X1
011
000
100 0 ▶ my $part = first { $_ && $_->content_type =~ /^text\/plain/ } $pmsg->parts;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ and $_->content_type =~ m[^text/plain] 0 0 0
101 0 ▶ $part ? $part->body_str : $pmsg->body_raw;
Branch: 1 branch
BranchTrueFalseTotal
$part ? : 0 0 0
102 };
103 0 $body =~ s/\r//g;
104 0 my $hash = {
105 $pmsg->header_obj->header_pairs,
106 BODY => $body,
107 };
108  
109 # Move message
110 0 ▶ if ($self->has_parsed_folder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_parsed_folder) 0 0 0
111 0 ▶ $imap->move( $self->parsed_folder, $uid ) || do { $self->log->error('Error moving IMAP message '.$uid.': '.$imap->LastError); return; };
Branch: 1 branch
BranchTrueFalseTotal
$imap->move($self->parsed_folder, $uid) or do { $self->log->error('Error moving IMAP message ' . $uid . ': ' . $imap->LastError); return } 0 0 0
112 }
113 # (if not, message_string will auto-set the Seen flag.)
114  
115 0 return (\$msg, $hash);
116 }
117  
118 0 ▶sub eof {
Subroutine eof: not called CC=2 SCAR=17.9
Pod undocumented
119 0 my $self = shift;
120 0 ▶ return not ($self->_has_list and @{$self->_list});
Condition0%0/3
$self->_has_list && @{$self->_list;}
!ll&&!rl&&r
000
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;}
ABresult
0X0
100
111
121 }
122  
123 0 ▶sub close {
Subroutine close: not called CC=7 SCAR=40.3
Pod undocumented
124 0 my $self = shift;
125 0 ▶ my $imap = $self->_conn || do {
Condition0%0/3
$self->_conn || do { $self->_clear_list; $self->_clear_conn; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_conn || do { $self->_clear_list; $self->_clear_conn; return }
$self->_conn do { $self->_clear_list; $self->_clear_conn; return }
Truth table0%0/3
A || B
A= $self->_connB= do { $self->_clear_list; $self->_clear_conn; return }
ABresult
1X1
011
000
126 # maybe+default+error still creates an undef attr, which would pass an 'exists' check on predicate
127 0 $self->_clear_list;
128 0 $self->_clear_conn;
129 0 return;
130 };
131  
132 0 $self->_clear_list;
133 0 my $is_valid = $self->opened;
134  
135 # valid connection
136 0 ▶ if ($is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_valid) 0 0 0
137 0 ▶ $imap->close || $self->log->warn('Error closing IMAP: ' .$imap->LastError);
Branch: 1 branch
BranchTrueFalseTotal
$imap->close or $self->log->warn('Error closing IMAP: ' . $imap->LastError) 0 0 0
138 }
139 # open or valid connection
140 0 ▶ if ($imap->IsConnected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imap->IsConnected) 0 0 0
141 0 ▶ $imap->logout || $self->log->warn('Error logging out of IMAP: '.$imap->LastError);
Branch: 1 branch
BranchTrueFalseTotal
$imap->logout or $self->log->warn('Error logging out of IMAP: ' . $imap->LastError) 0 0 0
142 }
143 # invalid connection
144 0 ▶ $self->_clear_conn unless ($is_valid);
Branch: 1 branch
BranchTrueFalseTotal
unless $is_valid 0 0 0
145  
146 0 return 1;
147 }
148  
149 2 42;
150  
151 __END__
« Alert/TemplateGrp.pm ↑ Summary Alert/InputGrp.pm »