blib/lib/POE/Component/IRC/Plugin/WWW/XKCD/AsText.pm

filter: Statementstmt 16.8% 17 / 101 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 33.3% 5 / 15 Podpod 16.6% 1 / 6 totaltotal 13.6% 23 / 169 CC 22 SCAR 45.2
CC
22
Cov
12%
_parse_input
210.0
_xkcd_done
42.0
new
20.0
CRAP 91.4
↑ Summary
1 package POE::Component::IRC::Plugin::WWW::XKCD::AsText;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.004';
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use POE qw(Component::WWW::XKCD::AsText);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use POE::Component::IRC::Plugin qw(:ALL);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
13 0 my $package = shift;
14 0 ▶ croak "Even number of arguments must be specified"
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 0 0
15 if @_ & 1;
16 0 my %args = @_;
17 0 $args{ lc $_ } = delete $args{ $_ } for keys %args;
18  
19 # fill in the defaults
20 0 %args = (
21 debug => 0,
22 auto => 1,
23 response_event => 'irc_xkcd',
24 banned => [],
25 addressed => 1,
26 eat => 1,
27 trigger => qr/^xkcd\s+(?=\S)/i,
28 listen_for_input => [ qw(public notice privmsg) ],
29  
30 %args,
31 );
32  
33 $args{listen_for_input} = {
34 0 ▶ map { $_ => 1 } @{ $args{listen_for_input} || [] }
Condition0%0/2
$args{'listen_for_input'} || []
l!l
00
MC/DC0%0/1
$args{'listen_for_input'} || []
$args{'listen_for_input'}
Truth table0%0/2
A
A= $args{'listen_for_input'}
Aresult
11
00
35 };
36  
37 0 return bless \%args, $package;
38 }
39  
40 0 ▶sub _start {
Subroutine _start: not called CC=1 SCAR=6.9
41 0 my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
42 0 $self->{_session_id} = $_[SESSION]->ID();
43 0 $kernel->refcount_increment( $self->{_session_id}, __PACKAGE__ );
44
45 $self->{poco} = POE::Component::WWW::XKCD::AsText->spawn(
46 debug => $self->{debug},
47 0 );
48  
49 0 undef;
50 }
51  
52 0 ▶sub PCI_register {
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
53 0 my ( $self, $irc ) = splice @_, 0, 2;
54
55 0 $self->{irc} = $irc;
56
57 0 $irc->plugin_register( $self, 'SERVER', qw(public notice msg) );
58
59 0 $self->{_session_id} = POE::Session->create(
60 object_states => [
61 $self => [
62 qw(
63 _start
64 _shutdown
65 _xkcd_done
66 )
67 ],
68 ],
69 )->ID;
70  
71 0 return 1;
72 }
73  
74 0 ▶sub _shutdown {
Subroutine _shutdown: not called CC=1 SCAR=6.9
75 0 my ($kernel, $self) = @_[ KERNEL, OBJECT ];
76 0 $self->{poco}->shutdown;
77 0 $kernel->alarm_remove_all();
78 0 $kernel->refcount_decrement( $self->{_session_id}, __PACKAGE__ );
79 0 undef;
80 }
81  
82 0 ▶sub PCI_unregister {
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my $self = shift;
84
85 # Plugin is dying make sure our POE session does as well.
86 0 $poe_kernel->call( $self->{_session_id} => '_shutdown' );
87
88 0 delete $self->{irc};
89
90 0 return 1;
91 }
92  
93 0 ▶sub S_public {
Subroutine S_public: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my ( $self, $irc ) = splice @_, 0, 2;
95 0 my $who = ${ $_[0] };
96 0 my $channel = ${ $_[1] }->[0];
97 0 my $message = ${ $_[2] };
98 0 return $self->_parse_input( $irc, $who, $channel, $message, 'public' );
99 }
100  
101 0 ▶sub S_notice {
Subroutine S_notice: not called CC=1 SCAR=6.9
Pod undocumented
102 0 my ( $self, $irc ) = splice @_, 0, 2;
103 0 my $who = ${ $_[0] };
104 0 my $channel = ${ $_[1] }->[0];
105 0 my $message = ${ $_[2] };
106 0 return $self->_parse_input( $irc, $who, $channel, $message, 'notice' );
107 }
108  
109 0 ▶sub S_msg {
Subroutine S_msg: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my ( $self, $irc ) = splice @_, 0, 2;
111 0 my $who = ${ $_[0] };
112 0 my $channel = ${ $_[1] }->[0];
113 0 my $message = ${ $_[2] };
114 0 return $self->_parse_input( $irc, $who, $channel, $message, 'privmsg' );
115 }
116  
117 0 ▶sub _parse_input {
Subroutine _parse_input: not called CC=14 SCAR=53.5
118 0 my ( $self, $irc, $who, $channel, $message, $type ) = @_;
119  
120 warn "Got input: [ who => $who, channel => $channel, "
121 . "mesage => $message ]"
122 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
123  
124 return PCI_EAT_NONE
125 0 ▶ unless exists $self->{listen_for_input}{ $type };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"listen_for_input"}{$type} 0 0 0
126  
127 0 my $what;
128 0 ▶ if ( $self->{addressed} and $type eq 'public' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'addressed'} and $type eq 'public') { } 0 0 0
Condition0%0/3
$self->{"addressed"} and $type eq "public"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"addressed"} and $type eq "public"
$self->{"addressed"} $type eq "public"
Truth table0%0/3
A and B
A= $self->{"addressed"}B= $type eq "public"
ABresult
0X0
100
111
129 0 my $my_nick = $irc->nick_name();
130 0 ($what) = $message =~ m/^\s*\Q$my_nick\E[\:\,\;\.]?\s*(.*)$/i;
131 }
132 else {
133 0 $what = $message;
134 }
135  
136 0 ▶ return PCI_EAT_NONE
Branch: 1 branch
BranchTrueFalseTotal
unless defined $what and $what =~ s/$self->{"trigger"}// 0 0 0
Condition0%0/3
defined $what and $what =~ s/$self->{"trigger"}//
!ll&&!rl&&r
000
MC/DC0%0/2
defined $what and $what =~ s/$self->{"trigger"}//
defined $what $what =~ s/$self->{"trigger"}//
Truth table0%0/3
A and B
A= defined $whatB= $what =~ s/$self->{"trigger"}//
ABresult
0X0
100
111
137 unless defined $what and $what =~ s/$self->{trigger}//;
138  
139 0 $what =~ s/^\s+|\s+$//;
140  
141 0 ▶ return PCI_EAT_NONE
Branch: 1 branch
BranchTrueFalseTotal
unless length $what and not $what =~ /\D/ 0 0 0
Condition0%0/3
length $what and not $what =~ /\D/
!ll&&!rl&&r
000
MC/DC0%0/2
length $what and not $what =~ /\D/
length $what not $what =~ /\D/
Truth table0%0/3
A and not(B)
A= length $whatB= not $what =~ /\D/
ABresult
0X0
100
111
142 unless length $what and $what !~ /\D/;
143  
144 warn "Matched trigger: [ who => $who, channel => $channel, "
145 . "what => $what ]"
146 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
147  
148 0 ▶ foreach my $ban_re ( @{ $self->{banned} || [] } ) {
Condition0%0/2
$self->{'banned'} || []
l!l
00
MC/DC0%0/1
$self->{'banned'} || []
$self->{'banned'}
Truth table0%0/2
A
A= $self->{'banned'}
Aresult
11
00
149 0 ▶ return PCI_EAT_NONE
Branch: 1 branch
BranchTrueFalseTotal
if $who =~ /$ban_re/ 0 0 0
150 if $who =~ /$ban_re/;
151 }
152  
153 $self->{poco}->retrieve( {
154 session => $self->{_session_id},
155 0 event => '_xkcd_done',
156 id => $what,
157 _who => $who,
158 _channel => $channel,
159 _message => $message,
160 _type => $type,
161 }
162 );
163  
164 0 ▶ return $self->{eat} ? PCI_EAT_ALL : PCI_EAT_NONE;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'eat'} ? : 0 0 0
165 }
166  
167 0 ▶sub _xkcd_done {
Subroutine _xkcd_done: not called CC=6 SCAR=37.4
168 0 my ( $kernel, $self, $in_ref ) = @_[ KERNEL, OBJECT, ARG0 ];
169  
170 0 my $response_message;
171  
172 0 ▶ if ( exists $in_ref->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $in_ref->{'error'}) { } 0 0 0
173 0 $response_message = [ $in_ref->{error} ];
174 }
175 else {
176 0 $response_message = $in_ref->{text};
177 0 $response_message =~ s/\n\s*\n/\n \n/g;
178 0 $response_message = [ split /\n/, $response_message ];
179 }
180
181 $self->{irc}->_send_event( $self->{response_event} => {
182 text => $in_ref->{text},
183 id => $in_ref->{id},
184 0 map { $_ => $in_ref->{"_$_"} }
185 qw( who channel message type ),
186 }
187 );
188  
189 0 ▶ if ( $self->{auto} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auto"}) 0 0 0
190 my $response_type = $in_ref->{_type} eq 'public'
191 ? 'privmsg'
192 0 ▶ : $in_ref->{_type};
Branch: 1 branch
BranchTrueFalseTotal
$in_ref->{'_type'} eq 'public' ? : 0 0 0
193  
194 my $where = $in_ref->{_type} eq 'public'
195 ? $in_ref->{_channel}
196 0 ▶ : (split /!/, $in_ref->{_who})[0];
Branch: 1 branch
BranchTrueFalseTotal
$in_ref->{'_type'} eq 'public' ? : 0 0 0
197  
198 0 for ( @$response_message ) {
199 $kernel->post( $self->{irc} =>
200 0 $response_type =>
201 $where =>
202 $_
203 );
204 }
205 }
206  
207 0 undef;
208 }
209  
210 2 1;
211  
212 __END__
↑ Summary