Plugin/AlarmClock.pm

filter: Statementstmt 11.4% 17 / 148 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 29.4% 5 / 17 Podpod 0.0% 0 / 2 totaltotal 8.9% 22 / 246 CC 36 SCAR 48.5
CC
36
Cov
8%
_make_response_message
306.0
_seconds_to_readable_time
110.0
_alarm_rang
30.0
CRAP 128.0
↑ Summary Plugin/Thanks.pm »
1 package POE::Component::IRC::Plugin::AlarmClock;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.001003'; # VERSION
7  
8 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use base 'POE::Component::IRC::Plugin::BaseWrap';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub _make_default_args {
Subroutine _make_default_args: not called CC=1 SCAR=6.9
13 return (
14 0 trigger => qr/^alarm\s+(?=\S+)/i,
15 response_event => 'irc_alarm_clock',
16 max_alarms => 5,
17 _store => {},
18 );
19 }
20  
21 0 ▶sub PCI_register {
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
22 0 my ( $self, $irc ) = splice @_, 0, 2;
23  
24 0 $self->{irc} = $irc;
25  
26 0 $irc->plugin_register( $self, 'SERVER', qw(public notice msg) );
27  
28 0 $self->{session_id} = POE::Session->create(
29 object_states => [
30 $self => [ qw(
31 _start
32 _shutdown
33 _alarm_rang
34 _alarm_set
35 _alarm_delete
36 )
37 ],
38 ],
39 )->ID;
40  
41 0 return 1;
42 }
43  
44 0 ▶sub _alarm_delete {
Subroutine _alarm_delete: not called CC=1 SCAR=6.9
45 0 my ( $kernel, $timer_id ) = @_[KERNEL, ARG0];
46 0 $kernel->alarm_remove( $timer_id );
47 }
48  
49 0 ▶sub _alarm_set {
Subroutine _alarm_set: not called CC=3 SCAR=24.8
50 0 my ( $kernel, $self, $alarm_id ) = @_[KERNEL, OBJECT, ARG0];
51  
52 0 my $store = $self->{_store};
53 0 my $alarm_store = $store->{alarms}{ $alarm_id };
54  
55 my $timer_id = $kernel->alarm_set(
56 0 _alarm_rang => $alarm_store->{time} => $alarm_id
57 );
58  
59 0 ▶ unless ( $timer_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timer_id) 0 0 0
60 0 my $who = $alarm_store->{in_ref}{who};
61 0 my $nick = ( split /!/, $who )[0];
62  
63 0 $self->{irc}->yield( privmsg => $nick =>
64 "Failed to set the alarm ($!)"
65 );
66 0 delete $store->{alarms}{ $alarm_id };
67 0 delete $store->{users}{ $who }{ $alarm_id };
68 0 keys %{ $store->{users}{ $who } }
69 0 ▶ or delete $store->{users}{ $who };
Branch: 1 branch
BranchTrueFalseTotal
keys %{$$store{"users"}{$who};} or delete $store->{"users"}{$who} 0 0 0
70 }
71  
72 0 $alarm_store->{timer_id} = $timer_id;
73 }
74  
75 0 ▶sub _alarm_rang {
Subroutine _alarm_rang: not called CC=5 SCAR=34.0
76 0 my ( $self, $alarm_id ) = @_[OBJECT, ARG0];
77  
78 0 my $store = $self->{_store};
79  
80 0 my $alarm_ref = delete $store->{alarms}{$alarm_id};
81 my $message = (
82 delete $store->{users}{ $alarm_ref->{user} }{$alarm_id}
83 0 )->{message};
84  
85 0 keys %{ $store->{users}{ $alarm_ref->{user} } }
86 0 ▶ or delete $store->{users}{ $alarm_ref->{user} };
Branch: 1 branch
BranchTrueFalseTotal
keys %{$store->{"users"}{$alarm_ref->{"user"}};} or delete $store->{"users"}{$alarm_ref->{"user"}} 0 0 0
87  
88 $self->{debug}
89 0 ▶ and carp "Alarm rang: user => $alarm_ref->{user}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and carp(qq[Alarm rang: user => $alarm_ref->{"user"}]) 0 0 0
90  
91 0 my $in_ref = $alarm_ref->{in_ref};
92 0 my $user = ( split /!/, $in_ref->{who} )[0];
93  
94 0 my $type = $in_ref->{type};
95 0 my $where = $user;
96 0 ▶ if ( $type eq 'public' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "public") 0 0 0
97 0 $type = 'privmsg';
98 0 $where = $in_ref->{channel};
99 }
100  
101 0 delete @{ $alarm_ref->{in_ref} }{ qw(rang list del set) };
102  
103 0 my $out_message = "$user, alarm rang $message";
104 $self->{irc}->yield( $type => $where => $out_message )
105 0 ▶ if $self->{auto};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto"} 0 0 0
106  
107 $self->{irc}->send_event(
108 $self->{response_event} => {
109 rang => 1,
110 out => $out_message,
111 0 map { $_ => $alarm_ref->{in_ref}{ $_ } }
112 qw( who type channel ),
113 }
114 );
115 }
116  
117 0 ▶sub _start {
Subroutine _start: not called CC=1 SCAR=6.9
118 0 my ( $kernel, $self ) = @_[KERNEL, OBJECT];
119 0 $self->{session_id} = $_[SESSION]->ID;
120 0 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
121 }
122  
123 0 ▶sub _shutdown {
Subroutine _shutdown: not called CC=1 SCAR=6.9
124 0 my ( $kernel, $self ) = @_[KERNEL, OBJECT];
125 0 $kernel->alarm_remove_all;
126 0 $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ );
127 0 undef;
128 }
129  
130 0 ▶sub PCI_unregister {
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
131 0 my $self = shift;
132  
133 0 delete $self->{irc};
134 0 $poe_kernel->call( $self->{session_id} => '_shutdown' );
135  
136 0 return 1;
137 }
138  
139 0 ▶sub _make_response_message {
Subroutine _make_response_message: not called CC=17 SCAR=57.2
140 0 my ( $self, $in_ref ) = @_;
141  
142 0 delete @$in_ref{ qw(rang list del set) };
143  
144 0 my $in = $in_ref->{what};
145 0 my $store = $self->{_store};
146 0 ▶ if ( my ( $time, $message ) = $in
Branch: 3 branches
BranchTrueFalseTotal
if (() = $in =~ /^(?:set|start)\s+(\d+[smh]?)\s*(.+)?/xi) { } 0 0 0
elsif ($in =~ /^(list|show)\b/i) { } 0 0 0
elsif (my($id) = $in =~ /^(?:del(?:ete)?|rem(?:ove)?)\s+(\d)/i) { } 0 0 0
147 =~ /^(?:set|start)\s+(\d+[smh]?)\s*(.+)?/xi
148 ) {
149 0 ▶ $message = ''
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
150 unless defined $message;
151  
152 0 $in_ref->{set} = 1;
153  
154 0 my $user = $in_ref->{who};
155  
156 0 ▶ if ( keys %{ $store->{users}{$user} } >= $self->{max_alarms} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$store{"users"}{$user};} >= $self->{"max_alarms"}) 0 0 0
157 0 return "Sorry but you may not set any more alarms."
158 . " Clear your old ones or wait for them to ring";
159 }
160  
161 0 my %existing_alarm_numbers = map { $_->{num} => 1 }
162 0 values %{ $store->{users}{$user} };
163 0 my $alarm_num = 0;
164 0 while ( 1 ) {
165 last
166 0 ▶ unless exists $existing_alarm_numbers{ $alarm_num };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $existing_alarm_numbers{$alarm_num} 0 0 0
167  
168 0 $alarm_num++;
169 }
170  
171 0 my $alarm_id = rand() . time() . rand();
172 0 $store->{users}{$user}{$alarm_id}
173 = {
174 time => time + $self->_make_offset( $time ),
175 message => $message,
176 num => $alarm_num,
177 };
178  
179 0 @{ $store->{alarms}{ $alarm_id } }{ qw(
180 user
181 id
182 time
183 in_ref
184 )
185 } = (
186 $user,
187 $alarm_id,
188 $store->{users}{$user}{$alarm_id}{time},
189 0 $in_ref,
190 );
191  
192 0 $poe_kernel->post( $self->{session_id} => _alarm_set => $alarm_id );
193  
194 0 return "Alarm will ring in "
195 . $self->_seconds_to_readable_time( $self->_make_offset( $time ) );
196 }
197 elsif ( $in =~ /^(list|show)\b/i) {
198 $self->{debug}
199 0 ▶ and carp "Alarm LIST from $in_ref->{who}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and carp(qq[Alarm LIST from $in_ref->{"who"}]) 0 0 0
200  
201 # we will always reply to this command via /notice
202 0 @$in_ref{ qw(list type) } = ( 1, 'notice' );
203  
204 return "You don't have any alarms set"
205 0 ▶ unless keys %{ $store->{users}{ $in_ref->{who} } || {} };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$store->{"users"}{$in_ref->{"who"}} or {};} 0 0 0
Condition0%0/2
$store->{'users'}{$in_ref->{'who'}} || {}
l!l
00
MC/DC0%0/1
$store->{'users'}{$in_ref->{'who'}} || {}
$store->{'users'}{$in_ref->{'who'}}
Truth table0%0/2
A
A= $store->{'users'}{$in_ref->{'who'}}
Aresult
11
00
206  
207 0 my @alarms = sort { $a->{time} <=> $b->{time} }
208 0 values %{ $store->{users}{ $in_ref->{who} } };
209  
210 0 my @out_messages;
211 0 for ( @alarms ) {
212 my $readable_time
213 0 = $self->_seconds_to_readable_time( $_->{time} - time() );
214 0 push @out_messages,
215 "[ $_->{num} - $readable_time - $_->{message} ]";
216 }
217  
218 $self->{debug}
219 0 ▶ and carp "About to list these alarms: @out_messages";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and carp("About to list these alarms: @out_messages") 0 0 0
220  
221 0 return join q| |, @out_messages;
222 }
223 elsif ( my ( $id ) = $in =~ /^(?:del(?:ete)?|rem(?:ove)?)\s+(\d)/i) {
224 $self->{debug}
225 0 ▶ and carp "Alarm REMOVE from $in_ref->{who}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"debug"} and carp(qq[Alarm REMOVE from $in_ref->{"who"}]) 0 0 0
226  
227 # we will always reply to this command via /notice
228 0 @$in_ref{ qw(del type) } = ( 1, 'notice' );
229  
230 return "You don't have any alarms set"
231 0 ▶ unless keys %{ $store->{users}{ $in_ref->{who} } || {} };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$store->{"users"}{$in_ref->{"who"}} or {};} 0 0 0
Condition0%0/2
$store->{'users'}{$in_ref->{'who'}} || {}
l!l
00
MC/DC0%0/1
$store->{'users'}{$in_ref->{'who'}} || {}
$store->{'users'}{$in_ref->{'who'}}
Truth table0%0/2
A
A= $store->{'users'}{$in_ref->{'who'}}
Aresult
11
00
232  
233  
234 0 my $user_store = $store->{users}{ $in_ref->{who} };
235  
236 my $alarm_to_delete = (
237 0 grep { $user_store->{ $_ }{num} == $id }
238 keys %$user_store
239 )[0];
240  
241 0 ▶ $alarm_to_delete
Branch: 1 branch
BranchTrueFalseTotal
$alarm_to_delete or return "Alarm ID $id does not exist; use `list` command" . ' to show your alarms' 0 0 0
242 or return "Alarm ID $id does not exist; use `list` command"
243 . " to show your alarms";
244  
245 my $poe_timer_id = (
246 delete $store->{alarms}{ $alarm_to_delete }
247 0 )->{timer_id};
248  
249 $poe_kernel->call(
250 0 $self->{session_id} => _alarm_delete => $poe_timer_id
251 );
252  
253 0 my $alarm_info = delete $user_store->{ $alarm_to_delete };
254  
255 keys %$user_store
256 0 ▶ or delete $store->{users}{ $in_ref->{who} };
Branch: 1 branch
BranchTrueFalseTotal
keys %$user_store or delete $store->{"users"}{$in_ref->{"who"}} 0 0 0
257  
258 return "Deleted alarm $alarm_info->{num} [$alarm_info->{message}]"
259 . " which would have rang in "
260 0 . $self->_seconds_to_readable_time( $alarm_info->{time} - time );
261 }
262 else {
263 0 $in_ref->{invalid} = 1;
264 0 return "Invalid command in alarm plugin";
265 }
266 }
267  
268 0 ▶sub _seconds_to_readable_time {
Subroutine _seconds_to_readable_time: not called CC=10 SCAR=47.0
269 0 my ( $self, $seconds ) = @_;
270  
271 0 my %result = map { $_ => 0 } qw(days hours minutes seconds);
272 0 $result{minutes} = int( $seconds / 60 );
273 0 $seconds -= $result{minutes} * 60;
274  
275 0 ▶ if ( $result{minutes} and $result{minutes} >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result{"minutes"} and $result{"minutes"} >= 60) 0 0 0
Condition0%0/3
$result{"minutes"} and $result{"minutes"} >= 60
!ll&&!rl&&r
000
MC/DC0%0/2
$result{"minutes"} and $result{"minutes"} >= 60
$result{"minutes"} $result{"minutes"} >= 60
Truth table0%0/3
A and B
A= $result{"minutes"}B= $result{"minutes"} >= 60
ABresult
0X0
100
111
276 0 $result{hours} = int( $result{minutes} / 60 );
277 0 $result{minutes} -= $result{hours} * 60;
278 }
279  
280 0 ▶ if ( $result{hours} and $result{hours} >= 24 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result{"hours"} and $result{"hours"} >= 24) 0 0 0
Condition0%0/3
$result{"hours"} and $result{"hours"} >= 24
!ll&&!rl&&r
000
MC/DC0%0/2
$result{"hours"} and $result{"hours"} >= 24
$result{"hours"} $result{"hours"} >= 24
Truth table0%0/3
A and B
A= $result{"hours"}B= $result{"hours"} >= 24
ABresult
0X0
100
111
281 0 $result{days} = int( $result{hours} / 24 );
282 0 $result{hours} -= $result{days} * 24;
283 }
284  
285 0 $result{seconds} = $seconds;
286  
287 0 my @responses;
288  
289 $result{days}
290 0 ▶ and push @responses, "$result{days} day(s)";
Branch: 1 branch
BranchTrueFalseTotal
$result{"days"} and push @responses, qq[$result{"days"} day(s)] 0 0 0
291  
292 $result{hours}
293 0 ▶ and push @responses, "$result{hours} hour(s)";
Branch: 1 branch
BranchTrueFalseTotal
$result{"hours"} and push @responses, qq[$result{"hours"} hour(s)] 0 0 0
294  
295 $result{minutes}
296 0 ▶ and push @responses, "$result{minutes} minute(s)";
Branch: 1 branch
BranchTrueFalseTotal
$result{"minutes"} and push @responses, qq[$result{"minutes"} minute(s)] 0 0 0
297  
298 $result{seconds}
299 0 ▶ and push @responses, "$result{seconds} second(s)";
Branch: 1 branch
BranchTrueFalseTotal
$result{"seconds"} and push @responses, qq[$result{"seconds"} second(s)] 0 0 0
300  
301 0 ▶ @responses == 1
Branch: 1 branch
BranchTrueFalseTotal
@responses == 1 and return $responses[0] 0 0 0
302 and return $responses[0];
303  
304 0 my $last = pop @responses;
305 0 return join( q|, |, @responses ) . " and $last";
306 }
307  
308 0 ▶sub _make_offset {
Subroutine _make_offset: not called CC=5 SCAR=34.0
309 0 my ( $self, $time ) = @_;
310 0 ▶ return $time
Branch: 1 branch
BranchTrueFalseTotal
if not $time =~ /\D/ or $time =~ s/s//i 0 0 0
Condition0%0/3
not $time =~ /\D/ or $time =~ s/s//i
l!l&&r!l&&!r
000
MC/DC0%0/2
not $time =~ /\D/ or $time =~ s/s//i
not $time =~ /\D/ $time =~ s/s//i
Truth table0%0/3
not(A) or B
A= not $time =~ /\D/B= $time =~ s/s//i
ABresult
1X1
011
000
311 if $time !~ /\D/ or $time =~ s/s//i;
312  
313 0 ▶ if ( $time =~ s/h//i ) { return $time * 3600; }
Branch: 2 branches
BranchTrueFalseTotal
if ($time =~ s/h//i) { } 0 0 0
elsif ($time =~ s/m//i) { } 0 0 0
314 0 elsif ( $time =~ s/m//i ) { return $time * 60; }
315  
316 0 croak 'We should never get here; please email to zoffix@cpan.org';
317 }
318  
319 0 ▶sub _message_into_response_event { 'out' }
Subroutine _message_into_response_event: not called CC=1 SCAR=6.9
320  
321 1 1;
322 __END__
↑ Summary Plugin/Thanks.pm »