blib/lib/Bot/BasicBot/Pluggable/Module/Notify.pm

filter: Statementstmt 68.7% 97 / 141 Branchbran 47.4% 37 / 78 Conditioncond 34.2% 13 / 38 MC/DCmcdc 4.5% 1 / 22 Subroutinesub 87.5% 14 / 16 Podpod 100% 4 / 4 totaltotal 55.5% 166 / 299 CC 99 SCAR 39.9
CC
99
Cov
57%
told
174.1
told
174.1
_load_notification_file
19.4
CRAP 53.9
↑ Summary
1 package Bot::BasicBot::Pluggable::Module::Notify;
2  
3 0 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine BEGIN: called CC=1 SCAR=0.4
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 38 our $VERSION = '1.00';
7  
8 #----------------------------------------------------------------------------
9  
10 #############################################################################
11 # Library Modules #
12 #############################################################################
13  
14 8 ▶use base qw(Bot::BasicBot::Pluggable::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use List::MoreUtils qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use MIME::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #############################################################################
22 # Variables #
23 #############################################################################
24  
25 14 my (%settings,%emails);
26 14 my $load_time = 0;
27  
28 12 my %defaults = (
29 smtp => '',
30 replyto => 'no-reply@example.com',
31 from => 'no-reply@example.com',
32 active => 15,
33 );
34
35 #----------------------------------------------------------------------------
36  
37 #############################################################################
38 # Public Methods #
39 #############################################################################
40  
41 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=14.0
Pod documented
Pod documented
42 0 my $self = shift;
43  
44 0 my $file = $self->store->get( 'notify', 'notifications' );
45 0 ▶ unless($file) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($file) 0 0 0
unless ($file) 6 62 68
46 0 $file = $0;
47 0 $file =~ s/\.pl$/.csv/;
48 }
49  
50 0 $self->store->set( 'notify', 'notifications', $file );
51 }
52
53 2 ▶sub help {
Subroutine help: called CC=2 SCAR=9.2
Pod documented
54 2 ▶ my $active = $settings{active} || $defaults{active};
Condition33%2/6
$settings{'active'} || $defaults{'active'}
l!l&&r!l&&!r
000
$settings{'active'} || $defaults{'active'}
l!l&&r!l&&!r
204
MC/DC0%0/2
$settings{'active'} || $defaults{'active'}
$settings{'active'} $defaults{'active'}
MC/DC0%0/2
$settings{'active'} || $defaults{'active'}
$settings{'active'} $defaults{'active'}
Truth table0%0/3
A || B
A= $settings{'active'}B= $defaults{'active'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $settings{'active'}B= $defaults{'active'}
ABresult
1X1
011
000
55 2 return "if you have been away for more than $active minutes, and someone posts a channel message, identifying you, this will email you the message.";
56 }
57
58 0 ▶sub told {
Subroutine told: not called CC=17 SCAR=51.6
Pod documented
59 2 my ( $self, $mess ) = @_;
60 1 my $body = $mess->{body};
61  
62 1 ▶ return 0 unless defined $body;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $body 1 1 2
unless defined $body 243 29 272
63 2 ▶ return 0 unless($self->_load_notification_file());
Branch: 2 branches
BranchTrueFalseTotal
unless $self->_load_notification_file 0 0 0
unless $self->_load_notification_file 2 27 29
64 0 ▶ return 0 if($body =~ m!^\s*/?who(is|was)?\s+!); # ignore who requests
Branch: 2 branches
BranchTrueFalseTotal
if $body =~ m[^\s*/?who(is|was)?\s+] 0 0 0
if $body =~ m[^\s*/?who(is|was)?\s+] 0 27 27
65  
66 0 my (@words) = split(/[^@\w\+\-]+/,$body);
67 0 my $data = $self->bot->channel_data( $mess->{channel} );
68 0 my %users = map { $_ => 0 } keys %$data; # get users in channel
69  
70 # get identities
71 0 my $pocoirc = $self->bot->pocoirc( $mess->{channel} );
72 0 my @nicks = $pocoirc->nicks();
73 0 my %nicks = map { $_ => $pocoirc->nick_info($_) } @nicks;
74 0 $self->{nicks} = \%nicks;
75  
76 0 ▶ my $sender = $self->_match_user($mess->{who}, $self->{nicks}) || '';
Condition16%1/6
$self->_match_user($mess->{'who'}, $self->{'nicks'}) || ''
l!l
000
$self->_match_user($mess->{'who'}, $self->{'nicks'}) || ''
l!l
008
MC/DC0%0/1
$self->_match_user($mess->{'who'}, $self->{'nicks'}) || ''
$self->_match_user($mess->{'who'}, $self->{'nicks'})
MC/DC0%0/1
$self->_match_user($mess->{'who'}, $self->{'nicks'}) || ''
$self->_match_user($mess->{'who'}, $self->{'nicks'})
Truth table0%0/2
A
A= $self->_match_user($mess->{'who'}, $self->{'nicks'})
Aresult
11
00
Truth table50%1/2
A
A= $self->_match_user($mess->{'who'}, $self->{'nicks'})
Aresult
11
00
77 0 my @recipients = grep { $_ ne $sender } keys %users;
78  
79 0 my $prev = '';
80 0 for my $word (@words) {
81 0 ▶ next if($word =~ /(\-\-|\+\+)$/); # ignore karma messages
Branch: 2 branches
BranchTrueFalseTotal
if $word =~ /(\-\-|\+\+)$/ 0 0 0
if $word =~ /(\-\-|\+\+)$/ 0 27 27
82 0 ▶ next if($prev eq 'seen'); # ignore seen messages
Branch: 1 branch
BranchTrueFalseTotal
if $prev eq "seen" 0 0 0
83 0 $prev = $word;
84  
85 0 my $nick = '';
86 0 ▶ if($word =~ /@?(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /\@?(\w+)/) 0 0 0
87 0 my $user = $1;
88 0 ▶ $nick = $self->_match_user($user, $self->{nicks}) || '';
Condition40%2/5
$self->_match_user($user, $self->{'nicks'}) || ''
l!l
00
$self->_match_user($user, $self->{'nicks'}) || ''
l!l
260
MC/DC0%0/1
$self->_match_user($user, $self->{'nicks'}) || ''
$self->_match_user($user, $self->{'nicks'})
MC/DC100%1/1
$self->_match_user($user, $self->{'nicks'}) || ''
$self->_match_user($user, $self->{'nicks'})
Truth table0%0/2
A
A= $self->_match_user($user, $self->{'nicks'})
Aresult
11
00
Truth table100%2/2
A
A= $self->_match_user($user, $self->{'nicks'})
Aresult
11
00
89 }
90  
91 0 ▶ next if($nick && $users{$nick}); # don't send repeated mails
Branch: 1 branch
BranchTrueFalseTotal
if $nick and $users{$nick} 0 0 0
Condition0%0/3
$nick and $users{$nick}
!ll&&!rl&&r
000
MC/DC0%0/2
$nick and $users{$nick}
$nick $users{$nick}
Truth table0%0/3
A and B
A= $nickB= $users{$nick}
ABresult
0X0
100
111
92  
93 0 ▶ if($word eq '@all') {
Branch: 3 branches
BranchTrueFalseTotal
if ($word eq '@all') { } 0 0 0
elsif ($word eq '@here') { } 0 0 0
elsif ($nick and $emails{$nick}) { } 0 0 0
94 0 $self->_send_email(1,$mess,@recipients);
95 0 return 1; # we only send 1 email per user
96 } elsif($word eq '@here') {
97 0 my @users = []; # filter based on seen in the last hour
98 0 $self->_send_email(2,$mess,@recipients);
99 0 return 1; # we only send 1 email per user
100 ▶ } elsif($nick && $emails{$nick}) {
Condition0%0/3
$nick and $emails{$nick}
!ll&&!rl&&r
000
MC/DC0%0/2
$nick and $emails{$nick}
$nick $emails{$nick}
Truth table0%0/3
A and B
A= $nickB= $emails{$nick}
ABresult
0X0
100
111
101 0 $self->_send_email(1,$mess,$nick);
102 0 $users{$nick} = 1; # we only send 1 email per user
103 0 @recipients = grep { $_ ne $nick } @recipients; # don't send repeated mails
104 }
105 }
106
107 0 ▶ return 1 if(any { $_ == 1 } values %users);
Subroutine __ANON__: called CC=1 SCAR=3.5
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ == 1; } , values %users) 0 0 0
108 0 return 0;
109 }
110  
111 #############################################################################
112 # Private Methods #
113 #############################################################################
114  
115 7 ▶sub _send_email {
Subroutine _send_email: called CC=9 SCAR=22.9
116 12 my ($self,$type,$mess,@users) = @_;
117  
118 my $subject = sprintf "IRC: %s sent you a message",
119 5 $mess->{who};
120 my $body = sprintf "Hi,\n\n%s sent the following message in channel %s at %s %s:\n\n%s\n\n",
121 $mess->{who},
122 $mess->{channel},
123 DateTime->now->ymd, DateTime->now->hms,
124 5 $mess->{body};
125  
126 5 my $data = $self->bot->channel_data( $mess->{channel} );
127 5 my %channel = map { $_ => 1 } keys %$data; # get users in channel
128  
129 5 for my $user (@users) {
130 25 my $nick = $self->_match_user($user, $self->{nicks});
131 5 ▶ next unless($nick);
Branch: 1 branch
BranchTrueFalseTotal
unless $nick 0 5 5
132  
133 # if user is in channel, they must be inactive for at least 15 minues
134 # if the user is not in the channel, send them an email, even if they
135 # were recently active, as they have likely just left.
136  
137 5 ▶ if($channel{$user}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($channel{$user}) 0 5 5
138 5 my $seen = $self->store->get( 'Seen', "seen_$user");
139 25 ▶ if($seen && $seen->{'time'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen and $seen->{"time"}) 0 5 5
Condition66%2/3
$seen and $seen->{"time"}
!ll&&!rl&&r
320
MC/DC0%0/2
$seen and $seen->{"time"}
$seen $seen->{"time"}
Truth table0%0/3
A and B
A= $seenB= $seen->{"time"}
ABresult
0X0
100
111
140 5 my $time = time - $seen->{'time'};
141 5 ▶ next if($time < $settings{active} * 60);
Branch: 1 branch
BranchTrueFalseTotal
if $time < $settings{"active"} * 60 0 9 9
142 5 ▶ next if($time > 3600 && $type == 2);
Branch: 1 branch
BranchTrueFalseTotal
if $time > 3600 and $type == 2 0 9 9
Condition66%2/3
$time > 3600 and $type == 2
!ll&&!rl&&r
180
MC/DC0%0/2
$time > 3600 and $type == 2
$time > 3600 $type == 2
Truth table0%0/3
A and B
A= $time > 3600B= $type == 2
ABresult
0X0
100
111
143 }
144 }
145  
146 $self->_sendmail(
147 to => $emails{$nick}{email},
148 25 subject => $subject,
149 body => $body
150 );
151 }
152 }
153  
154 4 ▶sub _load_notification_file {
Subroutine _load_notification_file: called CC=15 SCAR=29.6
155 9 my $self = shift;
156  
157 9 ▶ my $fn = $self->store->get( 'notify', 'notifications' ) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fn = $self->store->get("notify", "notifications") or return 0 15 8 23
158 13 ▶ return 0 unless(-r $fn); # file must be readable
Branch: 1 branch
BranchTrueFalseTotal
unless -r $fn 8 9 17
159  
160 13 my $mod = (stat($fn))[9];
161 13 ▶ return 1 if($mod <= $load_time && keys %emails); # don't reload if not modified
Branch: 1 branch
BranchTrueFalseTotal
if $mod <= $load_time and keys %emails 2 15 17
Condition66%2/3
$mod <= $load_time and keys %emails
!ll&&!rl&&r
1610
MC/DC0%0/2
$mod <= $load_time and keys %emails
$mod <= $load_time keys %emails
Truth table66%2/3
A and B
A= $mod <= $load_timeB= keys %emails
ABresult
0X0
100
111
162  
163 29 ▶ my $fh = IO::File->new($fn,'r') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($fn, "r") or return 0 1 6 7
164 13 (%settings,%emails) = ();
165 23 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1 10 11
166 23 s/\s+$//;
167 17 ▶ next if(/^#/ || /^$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ or /^$/ 1 1 2
Condition66%2/3
/^#/ or /^$/
l!l&&r!l&&!r
501
MC/DC0%0/2
/^#/ or /^$/
/^#/ /^$/
Truth table0%0/3
A or B
A= /^#/B= /^$/
ABresult
1X1
011
000
168 17 my ($nick,$ident,$email) = split(/,/,$_,3);
169
170 10 ▶ if($nick eq 'CONFIG') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nick eq "CONFIG") 0 0 0
171 2 $settings{$ident} = $email;
172 1 next;
173 }
174  
175 1 $emails{$nick}{email} = $email;
176 9 ▶ $emails{$nick}{ident} = $ident if($ident);
Branch: 1 branch
BranchTrueFalseTotal
if $ident 0 0 0
177 }
178  
179 1 $fh->close;
180 1 $load_time = $mod;
181  
182 1 for my $key (keys %defaults) {
183 4 ▶ $settings{$key} ||= $defaults{$key};
Condition0%0/3
$settings{$key} ||= $defaults{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$settings{$key} ||= $defaults{$key}
$settings{$key} $defaults{$key}
Truth table0%0/3
A ||= B
A= $settings{$key}B= $defaults{$key}
ABresult
1X1
011
000
184 }
185  
186 2 ▶ return 0 unless($settings{smtp});
Branch: 1 branch
BranchTrueFalseTotal
unless $settings{"smtp"} 0 0 0
187 1 ▶ return 1 if(keys %emails);
Branch: 1 branch
BranchTrueFalseTotal
if keys %emails 0 0 0
188 0 return 0;
189 }
190  
191 6 ▶sub _match_user {
Subroutine _match_user: called CC=9 SCAR=22.5
192 6 my ($self,$user,$nicks) = @_;
193  
194 # matches a known user
195 6 ▶ return $user if($emails{$user});
Branch: 1 branch
BranchTrueFalseTotal
if $emails{$user} 6 0 6
196  
197 # see if idents match
198 6 for my $ident (keys %emails) {
199 6 ▶ next unless($emails{$ident}{ident});
Branch: 1 branch
BranchTrueFalseTotal
unless $emails{$ident}{"ident"} 0 6 6
200  
201 6 for my $nick (keys %$nicks) {
202 2 ▶ next unless($user eq $nick);
Branch: 1 branch
BranchTrueFalseTotal
unless $user eq $nick 4 2 6
203  
204 2 ▶ return $ident if($nicks->{$nick}->{Real} =~ /\Q$emails{$ident}{ident}\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $nicks->{$nick}{"Real"} =~ /\Q$emails{$ident}{"ident"}\E/ 2 0 2
205 2 ▶ return $ident if($nicks->{$nick}->{User} =~ /\Q$emails{$ident}{ident}\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $nicks->{$nick}{"User"} =~ /\Q$emails{$ident}{"ident"}\E/ 8 2 10
206 8 ▶ return $ident if($nicks->{$nick}->{Userhost} =~ /\Q$emails{$ident}{ident}\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $nicks->{$nick}{"Userhost"} =~ /\Q$emails{$ident}{"ident"}\E/ 0 8 8
207 }
208 }
209  
210 8 return;
211 }
212  
213 33 ▶sub _sendmail {
Subroutine _sendmail: called CC=2 SCAR=6.9
214 41 my ($self,%hash) = @_;
215  
216 41 MIME::Lite->send('smtp', $settings{smtp}, Timeout=>60);
217  
218 my $mail = MIME::Lite->new(
219 'Reply-To' => $settings{replyto},
220 'From' => $settings{from},
221  
222 'Subject' => $hash{subject},
223 'To' => $hash{to},
224 'Data' => $hash{body}
225 20 );
226  
227 56 eval { $mail->send };
228 276 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 17 24 41
229 29 print "MailError: eval=[$@]\n";
230 29 return;
231 }
232  
233 29 return 1;
234 }
235  
236
237 12 1;
238
239 __END__
↑ Summary