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

filter: Statementstmt 16.8% 17 / 101 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 33.3% 5 / 15 Podpod 16.6% 1 / 6 totaltotal 13.7% 23 / 167 CC 22 SCAR 45.2
CC
22
Cov
12%
_parse_input
210.0
_xkcd_done
42.0
new
20.0
CRAP 91.4
↑ Summary Build »
1 package POE::Component::IRC::Plugin::WWW::XKCD::AsText;
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 = '0.003';
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use POE qw(Component::WWW::XKCD::AsText);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POE::Component::IRC::Plugin qw(:ALL);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 0 ▶ my $package = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
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} || [] }
Branch: 1 branch
BranchTrueFalseTotal
$args{"listen_for_input"} or [] 0 0 0
35 };
36  
37 0 return bless \%args, $package;
38 }
39  
40 sub _start {
41 0 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _start: not called CC=1 SCAR=6.9
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 sub PCI_register {
53 0 ▶ my ( $self, $irc ) = splice @_, 0, 2;
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
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 sub _shutdown {
75 0 ▶ my ($kernel, $self) = @_[ KERNEL, OBJECT ];
Subroutine _shutdown: not called CC=1 SCAR=6.9
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 sub PCI_unregister {
83 0 ▶ my $self = shift;
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
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 sub S_public {
94 0 ▶ my ( $self, $irc ) = splice @_, 0, 2;
Subroutine S_public: not called CC=1 SCAR=6.9
Pod undocumented
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 sub S_notice {
102 0 ▶ my ( $self, $irc ) = splice @_, 0, 2;
Subroutine S_notice: not called CC=1 SCAR=6.9
Pod undocumented
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 sub S_msg {
110 0 ▶ my ( $self, $irc ) = splice @_, 0, 2;
Subroutine S_msg: not called CC=1 SCAR=6.9
Pod undocumented
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 sub _parse_input {
118 0 ▶ my ( $self, $irc, $who, $channel, $message, $type ) = @_;
Subroutine _parse_input: not called CC=14 SCAR=53.5
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} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"banned"} or [] 0 0 0
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 sub _xkcd_done {
168 0 ▶ my ( $kernel, $self, $in_ref ) = @_[ KERNEL, OBJECT, ARG0 ];
Subroutine _xkcd_done: not called CC=6 SCAR=37.4
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 1 1;
211  
212 __END__
213  
214 =encoding utf8
215  
216 =head1 NAME
217  
218 POE::Component::IRC::Plugin::WWW::XKCD::AsText - read http://xkcd.com comics on IRC
219  
220 =head1 SYNOPSIS
221  
222 use strict;
223 use warnings;
224  
225 use POE qw(Component::IRC Component::IRC::Plugin::WWW::XKCD::AsText);
226  
227 my $irc = POE::Component::IRC->spawn(
228 nick => 'XKCD',
229 server => 'irc.freenode.net',
230 port => 6667,
231 ircname => 'XKCD Bot',
232 );
233  
234 POE::Session->create(
235 package_states => [
236 main => [ qw(_start irc_001) ],
237 ],
238 );
239  
240 $poe_kernel->run;
241  
242 sub _start {
243 $irc->yield( register => 'all' );
244  
245 $irc->plugin_add(
246 'XKCD' =>
247 POE::Component::IRC::Plugin::WWW::XKCD::AsText->new
248 );
249  
250 $irc->yield( connect => {} );
251 }
252  
253 sub irc_001 {
254 $_[KERNEL]->post( $_[SENDER] => join => '#zofbot' );
255 }
256  
257 <Zoffix_> XKCDBot, xkcd 1
258 <XKCDBot> [[A boy sits in a barrel which is floating in an ocean.]]
259 <XKCDBot>
260 <XKCDBot> Boy: I wonder where I'll float next?
261 <XKCDBot>
262 <XKCDBot> [[The barrel drifts into the distance. Nothing else can be seen.]]
263 <XKCDBot>
264 <XKCDBot> {{Alt: Don't we all.}}
265  
266 =head1 DESCRIPTION
267  
268 This module is a L<POE::Component::IRC> plugin which uses
269 L<POE::Component::IRC::Plugin> for its base. It provides interface to
270 read L<http://xkcd.com> comics' transcriptions on IRC.
271 It accepts input from public channel events, C</notice> messages as well
272 as C</msg> (private messages); although that can be configured at will.
273  
274 =head1 CONSTRUCTOR
275  
276 =head2 new
277  
278 # plain and simple
279 $irc->plugin_add(
280 'XKCD' => POE::Component::IRC::Plugin::WWW::XKCD::AsText->new
281 );
282  
283 # juicy flavor
284 $irc->plugin_add(
285 'XKCD' =>
286 POE::Component::IRC::Plugin::WWW::XKCD::AsText->new(
287 auto => 1,
288 response_event => 'irc_xkcd',
289 banned => [ qr/aol\.com$/i ],
290 addressed => 1,
291 trigger => qr/^xkcd\s+(?=\S)/i,
292 listen_for_input => [ qw(public notice privmsg) ],
293 eat => 1,
294 debug => 0,
295 )
296 );
297  
298 The C<new()> method constructs and returns a new
299 C<POE::Component::IRC::Plugin::WWW::XKCD::AsText> object suitable to be
300 fed to L<POE::Component::IRC>'s C<plugin_add> method. The constructor
301 takes a few arguments, but I<all of them are optional>. The possible
302 arguments/values are as follows:
303  
304 =head3 auto
305  
306 ->new( auto => 0 );
307  
308 B<Optional>. Takes either true or false values, specifies whether or not
309 the plugin should auto respond to requests. When the C<auto>
310 argument is set to a true value plugin will respond to the person requesting
311 a comic with the results automatically. When the C<auto> argument
312 is set to a false value plugin will not respond and you will have to
313 listen to the events emited by the plugin to retrieve the results (see
314 EMITED EVENTS section and C<response_event> argument for details).
315 B<Defaults to:> C<1>.
316  
317 =head3 response_event
318  
319 ->new( response_event => 'event_name_to_recieve_results' );
320  
321 B<Optional>. Takes a scalar string specifying the name of the event
322 to emit when the comic's text is retrieved. See EMITED EVENTS
323 section for more information. B<Defaults to:> C<irc_xkcd>
324  
325 =head3 banned
326  
327 ->new( banned => [ qr/aol\.com$/i ] );
328  
329 B<Optional>. Takes an arrayref of regexes as a value. If the usermask
330 of the person (or thing) requesting the comic matches any of
331 the regexes listed in the C<banned> arrayref, plugin will ignore the
332 request. B<Defaults to:> C<[]> (no bans are set).
333  
334 =head3 trigger
335  
336 ->new( trigger => qr/^xkcd\s+(?=\S)/i );
337  
338 B<Optional>. Takes a regex as an argument. Messages matching this
339 regex will be considered as requests for a comic. See also
340 B<addressed> option below which is enabled by default. B<Note:> the
341 trigger will be B<removed> from the message, therefore make sure your
342 trigger doesn't match the actual data which is ment to be a comic ID number.
343 B<Defaults to:> C<qr/^xkcd\s+(?=\S)/i>
344  
345 =head3 addressed
346  
347 ->new( addressed => 1 );
348  
349 B<Optional>. Takes either true or false values. When set to a true value
350 all the public messages must be I<addressed to the bot>. In other words,
351 if your bot's nickname is C<XKCDBot> and your trigger is C<qr/^xkcd\s+/>
352 you would request the comic by saying C<XKCDBot, xkcd 222>.
353 When addressed mode is turned on, the bot's nickname, including any
354 whitespace and common punctuation character will be removed before
355 matching the C<trigger> (see above). When C<addressed> argument it set
356 to a false value, public messages will only have to match C<trigger> regex
357 in order to request the comic. Note: this argument has no effect on
358 C</notice> and C</msg> requests. B<Defaults to:> C<1>
359  
360 =head3 listen_for_input
361  
362 ->new( listen_for_input => [ qw(public notice privmsg) ] );
363  
364 B<Optional>. Takes an arrayref as a value which can contain any of the
365 three elements, namely C<public>, C<notice> and C<privmsg> which indicate
366 which kind of input plugin should respond to. When the arrayref contains
367 C<public> element, plugin will respond to requests sent from messages
368 in public channels (see C<addressed> argument above for specifics). When
369 the arrayref contains C<notice> element plugin will respond to
370 requests sent to it via C</notice> messages. When the arrayref contains
371 C<privmsg> element, the plugin will respond to requests sent
372 to it via C</msg> (private messages). You can specify any of these. In
373 other words, setting C<( listen_for_input => [ qr(notice privmsg) ] )>
374 will enable comics only via C</notice> and C</msg> messages.
375 B<Defaults to:> C<[ qw(public notice privmsg) ]>
376  
377 =head3 eat
378  
379 ->new( eat => 0 );
380  
381 B<Optional>. If set to a false value plugin will return a
382 C<PCI_EAT_NONE> after
383 responding. If eat is set to a true value, plugin will return a
384 C<PCI_EAT_ALL> after responding. See L<POE::Component::IRC::Plugin>
385 documentation for more information if you are interested. B<Defaults to>:
386 C<1>
387  
388 =head3 debug
389  
390 ->new( debug => 1 );
391  
392 B<Optional>. Takes either a true or false value. When C<debug> argument
393 is set to a true value some debugging information will be printed out.
394 When C<debug> argument is set to a false value no debug info will be
395 printed. B<Defaults to:> C<0>.
396  
397 =head1 EMITED EVENTS
398  
399 =head2 response_event
400  
401 $VAR1 = {
402 'who' => 'Zoffix!n=Zoffix@unaffiliated/zoffix',
403 'type' => 'public',
404 'channel' => '#zofbot',
405 'message' => 'XKCDBot, xkcd 333',
406 'id' => '333',
407 'text' => q|comic's text here|,
408 };
409  
410 The event handler set up to handle the event, name of which you've
411 specified in the C<response_event> argument to the constructor
412 (it defaults to C<irc_xkcd>) will recieve input
413 every time comic request is completed. The input will come in the form
414 of a hashref in C<ARG0>. The keys/values of that hashref are as follows:
415  
416 =head3 who
417  
418 { 'who' => 'Zoffix!n=Zoffix@unaffiliated/zoffix' }
419  
420 The C<who> key will contain the usermask of the user who requested the
421 comic.
422  
423 =head3
424  
425 { 'type' => 'public' }
426  
427 The C<type> key will contain the "type" of the message sent by the
428 requestor. The possible values are: C<public>, C<notice> and C<privmsg>
429 indicating that request was requested in public channel, via C</notice>
430 and via C</msg> (private message) respectively.
431  
432 =head3 channel
433  
434 { 'channel' => '#zofbot' }
435  
436 The C<channel> key will contain the name of the channel from which the
437 request
438 came from. This will only make sense when C<type> key (see above) contains
439 C<public>.
440  
441 =head3 message
442  
443 { 'message' => 'XKCDBot, kxcd 333', }
444  
445 The C<message> key will contain the message which the user has
446 sent to request the comic.
447  
448 =head3 id
449  
450 { 'id' => '333' }
451  
452 The C<term> key will contain the ID of the comic being retrieved.
453  
454 =head3 text
455  
456 { 'text' => 'comic text goes here' }
457  
458 The C<text> key will contain the text of the comic or an error message.
459 You can differentiate the errors by checking if C<error> key is set to
460 a true value.
461  
462 =head3 error
463  
464 { 'error' => '1' }
465  
466 When C<error> key is present it is an indication that an error occured
467 during the retrieval of the comic (including non-existant comic IDs). The
468 actual error message will be present in C<text> key.
469  
470 =head1 AUTHOR
471  
472 Zoffix Znet, C<< <zoffix at cpan.org> >>
473 (L<http://zoffix.com>, L<http://haslayout.net>)
474  
475 =head1 BUGS
476  
477 Please report any bugs or feature requests to C<bug-poe-component-irc-plugin-www-xkcd-astext at rt.cpan.org>, or through
478 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Component-IRC-Plugin-WWW-XKCD-AsText>. I will be notified, and then you'll
479 automatically be notified of progress on your bug as I make changes.
480  
481 =head1 SUPPORT
482  
483 You can find documentation for this module with the perldoc command.
484  
485 perldoc POE::Component::IRC::Plugin::WWW::XKCD::AsText
486  
487 You can also look for information at:
488  
489 =over 4
490  
491 =item * RT: CPAN's request tracker
492  
493 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=POE-Component-IRC-Plugin-WWW-XKCD-AsText>
494  
495 =item * AnnoCPAN: Annotated CPAN documentation
496  
497 L<http://annocpan.org/dist/POE-Component-IRC-Plugin-WWW-XKCD-AsText>
498  
499 =item * CPAN Ratings
500  
501 L<http://cpanratings.perl.org/d/POE-Component-IRC-Plugin-WWW-XKCD-AsText>
502  
503 =item * Search CPAN
504  
505 L<http://search.cpan.org/dist/POE-Component-IRC-Plugin-WWW-XKCD-AsText>
506  
507 =back
508  
509 =head1 COPYRIGHT & LICENSE
510  
511 Copyright 2008 Zoffix Znet, all rights reserved.
512  
513 This program is free software; you can redistribute it and/or modify it
514 under the same terms as Perl itself.
515  
516 =cut
517  
↑ Summary Build »