package POE::Component::IRC::Plugin::WWW::CPAN; use warnings; use strict; our $VERSION = '0.0101'; use base 'POE::Component::IRC::Plugin::BasePoCoWrap'; use POE::Component::WWW::CPAN; sub _make_default_args { return ( response_event => 'irc_cpan_result', trigger => qr/^cpan\s+(?=\S+)/i, mode => 'all', n => 10, line_length => 350, 's' => 1, ); } sub _make_poco { return POE::Component::WWW::CPAN->spawn( debug => shift->{debug}, ); } sub _make_response_message { my ( $self, $in_ref ) = @_; return $self->_prepare_response( $in_ref ); } sub _make_response_event { my ( $self, $in_ref ) = @_; return { results => $self->_prepare_response( $in_ref ), map { $_ => $in_ref->{"_$_"} } qw( who channel message type ), }; } sub _prepare_response { my ( $self, $in_ref ) = @_; if ( $in_ref->{_command} eq 'query' ) { my $result = $in_ref->{result}{module}[0]; defined $result or return [ 'No matches' ]; eval { $result->{author} = uc substr $result->{author}{link}, 24 if ref $result->{author}; $result->{author} =~ s/\W+//g; for ( @$result{ qw/released description/ } ) { s/^\s+|\s+$//g; s/\s{2,}/ /g; } unless ( defined $result->{version} ) { ( $result->{version} ) = $result->{link} =~ /([\d.]{3,})/; } $result->{link} = "http://search.cpan.org/perldoc?$result->{name}"; }; if ( $@ ) { return [ "Error: $@" ]; } return [ qq|$result->{name} v$result->{version} by $result->{author}| . qq| "$result->{description}" [ $result->{link} ]| ]; } else { my @lines; my @current_line; my $length = 0; foreach ( map $_->{name}, @{ $in_ref->{result}{module} } ) { $length += 1 + length; if ( $length > $self->{line_length} ) { push @lines, join ' ', @current_line; @current_line = (); $length = 0; } else { push @current_line, $_; } } push @lines, join ' ', @current_line; return \@lines; } } sub _make_poco_call { my $self = shift; my $data_ref = shift; my ( $command, $query ) = split ' ', delete $data_ref->{what}, 2; unless ( defined $query ) { $query = $command; $command = 'query'; } $command = lc $command; unless ( $command eq 'search' or $command eq 'query' ) { $command = 'query'; } $self->{poco}->search( { event => '_poco_done', query => $query, mode => $self->{mode}, 's' => $self->{'s'}, n => ($command eq 'query' ? 1 : $self->{n}), _command => $command, map +( "_$_" => $data_ref->{$_} ), keys %$data_ref, } ); } 1; __END__ =head1 NAME POE::Component::IRC::Plugin::WWW::CPAN - access http://search.cpan.org/ from IRC =head1 SYNOPSIS use strict; use warnings; use POE qw(Component::IRC Component::IRC::Plugin::WWW::CPAN); my $irc = POE::Component::IRC->spawn( nick => 'CPAN_bot', server => 'irc.freenode.net', port => 6667, ircname => 'CPAN bot', ); POE::Session->create( package_states => [ main => [ qw(_start irc_001) ], ], ); $poe_kernel->run; sub _start { $irc->yield( register => 'all' ); $irc->plugin_add( 'cpan' => POE::Component::IRC::Plugin::WWW::CPAN->new ); $irc->yield( connect => {} ); } sub irc_001 { $_[KERNEL]->post( $_[SENDER] => join => '#zofbot' ); } Zoffix> cpan_bot, cpan WWW::CPAN WWW::CPAN v0.011 by FERREIRA "CPAN as a web service" [ http://search.cpan.org/perldoc?WWW::CPAN ] cpan_bot, cpan search WWW::CPAN WWW::CPAN POE::Component::WWW::CPAN cpanq App::WWW::CPAN =head1 DESCRIPTION This module is a L plugin which uses L for its base. It provides interface to to L from IRC. It accepts input from public channel events, C messages as well as C (private messages); although that can be configured at will. The plugin has two "modes" of functionality. First mode is displaying information about specified module (this will be the first module found on L matching the input). The second mode, which is referred to as B throughout this documentation will list the names of modules from L which match your input. The C is triggered by starting your search query with C<'search '> word (which must be separated from the query itself by white-space). See the end of SYNOPSYS section above for usage examples. =head1 CONSTRUCTOR =head2 C # plain and simple $irc->plugin_add( 'cpan' => POE::Component::IRC::Plugin::WWW::CPAN->new ); # juicy flavor $irc->plugin_add( 'cpan' => POE::Component::IRC::Plugin::WWW::CPAN->new( mode => 'all', n => 10, s => 1, line_length => 350, auto => 1, response_event => 'irc_cpan_result', banned => [ qr/aol\.com$/i ], addressed => 1, root => [ qr/mah.net$/i ], trigger => qr/^cpan\s+(?=\S+\s+\S+)/i, listen_for_input => [ qw(public notice privmsg) ], eat => 1, debug => 0, ) ); The C method constructs and returns a new C object suitable to be fed to L's C method. The constructor takes a few arguments, but I. The possible arguments/values are as follows: =head3 C ->new( mode => 'all' ); B. Specifies the mode on which the plugin's search will operate. Possible values are C, C, C and C. The same as on L. B C. =head3 C ->new( n => 10 ); B. Specifies how many results to retrieve when the C<'search'> command is issued. As a value takes a positive integer between 1 and 100 (inclusive). B C<10> =head3 C ->new( s => 1 ); B. Applies only to the C<'search'> command. Specifies from which page of results on L to retrive the results. You definitely would want to leave it at its default value. B C<1> =head3 C line_length => 350, B. Applies only to the C<'search'> command. Specifies after how many characters to break up the lines (as for them not to get cut off). The actual number of characters may be less than the number specified as the plugin will not split up the names of modules on several lines. B C<350> =head3 C ->new( auto => 0 ); B. Takes either true or false values, specifies whether or not the plugin should auto respond to requests. When the C argument is set to a true value plugin will respond to the requesting person with the results automatically. When the C argument is set to a false value plugin will not respond and you will have to listen to the events emited by the plugin to retrieve the results (see EMITED EVENTS section and C argument for details). B C<1>. =head3 C ->new( response_event => 'event_name_to_recieve_results' ); B. Takes a scalar string specifying the name of the event to emit when the results of the request are ready. See EMITED EVENTS section for more information. B C =head3 C ->new( banned => [ qr/aol\.com$/i ] ); B. Takes an arrayref of regexes as a value. If the usermask of the person (or thing) making the request matches any of the regexes listed in the C arrayref, plugin will ignore the request. B C<[]> (no bans are set). =head3 C ->new( root => [ qr/\Qjust.me.and.my.friend.net\E$/i ] ); B. As opposed to C argument, the C argument B access only to people whose usermasks match B of the regexen you specify in the arrayref the argument takes as a value. B it is not specified. B as opposed to C specifying an empty arrayref to C argument will restrict access to everyone. =head3 C ->new( trigger => qr/^cpan\s+(?=\S+\s+\S+)/i ); B. Takes a regex as an argument. Messages matching this regex will be considered as requests. See also B option below which is enabled by default. B the trigger will be B from the message, therefore make sure your trigger doesn't match the actual data that needs to be processed. B C. B the optional C<'search'> command that changed plugin's output B the C and is not specifiable. =head3 C ->new( addressed => 1 ); B. Takes either true or false values. When set to a true value all the public messages must be I. In other words, if your bot's nickname is C and your trigger is C you would make the request by saying C. When addressed mode is turned on, the bot's nickname, including any whitespace and common punctuation character will be removed before matching the C (see above). When C argument it set to a false value, public messages will only have to match C regex in order to make a request. Note: this argument has no effect on C and C requests. B C<1> =head3 C ->new( listen_for_input => [ qw(public notice privmsg) ] ); B. Takes an arrayref as a value which can contain any of the three elements, namely C, C and C which indicate which kind of input plugin should respond to. When the arrayref contains C element, plugin will respond to requests sent from messages in public channels (see C argument above for specifics). When the arrayref contains C element plugin will respond to requests sent to it via C messages. When the arrayref contains C element, the plugin will respond to requests sent to it via C (private messages). You can specify any of these. In other words, setting C<( listen_for_input => [ qr(notice privmsg) ] )> will enable functionality only via C and C messages. B C<[ qw(public notice privmsg) ]> =head3 C ->new( eat => 0 ); B. If set to a false value plugin will return a C after responding. If eat is set to a true value, plugin will return a C after responding. See L documentation for more information if you are interested. B: C<1> =head3 C ->new( debug => 1 ); B. Takes either a true or false value. When C argument is set to a true value some debugging information will be printed out. When C argument is set to a false value no debug info will be printed. B C<0>. =head1 EMITED EVENTS =head2 response_event - C The event handler set up to handle the event, name of which you've specified in the C argument to the constructor (it defaults to C) will recieve input every time request is completed. The input will come in a form of a hashref. The keys/values of that hashref are as follows: $VAR1 = { 'who' => 'Zoffix!n=Zoffix@unaffiliated/zoffix', 'type' => 'public', 'channel' => '#zofbot', 'message' => 'cpan_bot, cpan WWW::CPAN', 'results' => [ 'WWW::CPAN v0.011 by FERREIRA "CPAN as a web service" [ http://search.cpan.org/perldoc?WWW::CPAN ]' ] }; =head3 C { 'results' => [ 'WWW::CPAN v0.011 by FERREIRA "CPAN as a web service" [ http://search.cpan.org/perldoc?WWW::CPAN ]' ] } The C key will contain an arrayref of messages which are spoken to the channel/user when C mode is turned on. If you turn off the C mode (see CONSTRUCTOR), this is from where you would fetch the results of query. =head3 C { 'who' => 'Zoffix!Zoffix@i.love.debian.org', } The C key will contain the user mask of the user who sent the request. =head3 C { 'what' => 'WWW::CPAN', } { 'what' => 'search WWW::CPAN', } The C key will contain user's message after stripping the C (see CONSTRUCTOR). =head3 C { 'message' => 'cpan_bot, cpan WWW::CPAN' } The C key will contain the actual message which the user sent; that is before the trigger is stripped. =head3 C { 'type' => 'public', } The C key will contain the "type" of the message the user have sent. This will be either C, C or C. =head3 C { 'channel' => '#zofbot', } The C key will contain the name of the channel where the message originated. This will only make sense if C key contains C. =head1 EXAMPLES The C directory of this distribution contains a sample "cpan_bot", make sure to change in the code or join the correct IRC network/channel. =head1 AUTHOR Zoffix Znet, C<< >> (L, L) =head1 BUGS Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc POE::Component::IRC::Plugin::WWW::CPAN You can also look for information at: =over 4 =item * RT: CPAN's request tracker L =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =item * Search CPAN L =back =head1 COPYRIGHT & LICENSE Copyright 2008 Zoffix Znet, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut