package POE::Component::IRC::Plugin::WWW::OhNoRobotCom::Search; use warnings; use strict; our $VERSION = '0.001'; use POE::Component::WWW::OhNoRobotCom::Search; use base 'POE::Component::IRC::Plugin::BasePoCoWrap'; sub _make_default_args { return ( response_event => 'irc_ohnorobot_results', trigger => qr/^comics?\s+(?=\S+)/i, obj_args => { }, max_results => 3, ); } sub _make_poco { my $self = shift; return POE::Component::WWW::OhNoRobotCom::Search->spawn( %{ $self->{obj_args} || {} } ); } sub _make_response_message { my ( $self, $in_ref ) = @_; my @out = exists $in_ref->{error} ? $in_ref->{error} : sort keys %{ $in_ref->{results} }; @out = splice @out, 0, $self->{max_results}; return [ join ' ', @out ]; } sub _make_response_event { my $self = shift; my $in_ref = shift; return { ( exists $in_ref->{error} ? ( error => $in_ref->{error} ) : ( results => $in_ref->{results} ) ), term => $in_ref->{term}, map { $_ => $in_ref->{"_$_"} } qw( who channel message type ), } } sub _make_poco_call { my ( $self, $data_ref ) = @_; $self->{poco}->search({ event => '_poco_done', term => delete $data_ref->{what}, map( +( exists $self->{$_} ? ( $_ => $self->{$_} ) : () ), qw(max_results comic_id include lucky) ), map +( "_$_" => $data_ref->{$_} ), keys %$data_ref, } ); } 1; __END__ =head1 NAME POE::Component::IRC::Plugin::WWW::OhNoRobotCom::Search - search http://ohnorobot.com/ website from IRC =head1 SYNOPSIS use strict; use warnings; use POE qw(Component::IRC Component::IRC::Plugin::WWW::OhNoRobotCom::Search); my $irc = POE::Component::IRC->spawn( nick => 'OhNoRobotComBot', server => 'irc.freenode.net', port => 6667, ircname => 'Bot for searching ohnorobot.com', ); POE::Session->create( package_states => [ main => [ qw(_start irc_001) ], ], ); $poe_kernel->run; sub _start { $irc->yield( register => 'all' ); $irc->plugin_add( 'OhNoRobot' => POE::Component::IRC::Plugin::WWW::OhNoRobotCom::Search->new( comic_id => 56 # XKCD comics ) ); $irc->yield( connect => {} ); } sub irc_001 { $_[KERNEL]->post( $_[SENDER] => join => '#zofbot' ); } OhNoRobotComBot, comic foo http://xkcd.com/233/ http://xkcd.com/240/ http://xkcd.com/261/ OhNoRobotComBot, comic bar http://xkcd.com/328/ http://xkcd.com/359/ http://xkcd.com/361/ =head1 DESCRIPTION This module is a L plugin which uses L for its base. It provides interface to search L website from IRC. It accepts input from public channel events, C messages as well as C (private messages); although that can be configured at will. =head1 CONSTRUCTOR =head2 new # plain and simple $irc->plugin_add( 'OhNoRobotCom' => POE::Component::IRC::Plugin::WWW::OhNoRobotCom::Search->new ); # juicy flavor $irc->plugin_add( 'OhNoRobotCom' => POE::Component::IRC::Plugin::WWW::OhNoRobotCom::Search->new( auto => 1, response_event => 'irc_ohnorobot_results', banned => [ qr/aol\.com$/i ], addressed => 1, root => [ qr/mah.net$/i ], trigger => qr/^comics?\s+(?=\S)/i, obj_args => { debug => 1 }, max_results => 3, include => [ qw(all_text meta) ], comic_id => 56, # XKCD comics lucky => 1, 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. B most of these arguments can be changed on the fly by changing them as keys in your plugin's object, i.e. C<< $plug_obj->{banned} = [qr/.*/ ]; >>. The possible arguments/values are as follows: =head3 auto ->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 response_event ->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 banned ->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 root ->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 trigger ->new( trigger => qr/^comics?\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 =head3 obj_args ->new( obj_args => { debug => 1 } ); B. Takes a hashref as an argument which will be dereferenced directly into L's constructor. See documentation for L for possible arguments. B C<{}> ( default constructor ) =head3 comic_id ->( comic_id => 56 ) B. The C argument takes a scalar as a value which should be a comic ID number or an empty string which indicates that search should be done on all comics. To obtain the comic ID number go to L, "View Source" and search for the name of the comic, when you'll find an