Created
January 5, 2012 18:20
-
-
Save hinrik/1566480 to your computer and use it in GitHub Desktop.
A simple IRC bot plugin which answers grok queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You can run this like so (see https://metacpan.org/release/App-Pocoirc): | |
# $ pocoirc -itv -I /source/dir -n grokbot -s irc.freenode.net -j '#perl6' -a Connector -a 'Grok{"Channels": ["#perl6"]} | |
package Grok; | |
use strict; | |
use warnings FATAL => 'all'; | |
use Carp qw(croak); | |
use App::Grok; | |
use App::Grok::Resource::Functions qw<func_fetch>; | |
use App::Grok::Resource::Tablet qw<tablet_fetch>; | |
use App::Grok::Parser::Pod5; | |
use POE::Component::IRC::Plugin qw(PCI_EAT_NONE); | |
use POE::Component::IRC::Plugin::BotAddressed; | |
sub new { | |
my ($package, %args) = @_; | |
my $self = bless \%args, $package; | |
if (ref $self->{Channels} ne 'ARRAY' || !$self->{Channels}) { | |
croak __PACKAGE__ . ': No channels defined'; | |
} | |
$self->{Method} = 'privmsg' if !defined $self->{Method}; | |
$self->{parser} = App::Grok::Parser::Pod5->new; | |
return $self; | |
} | |
sub PCI_register { | |
my ($self, $irc) = @_; | |
if (!grep { $_->isa('POE::Component::IRC::Plugin::BotAddressed') } values %{ $irc->plugin_list() }) { | |
$irc->plugin_add('BotAddressed', POE::Component::IRC::Plugin::BotAddressed->new()); | |
} | |
$irc->plugin_register($self, 'SERVER', qw(bot_addressed)); | |
return 1; | |
} | |
sub PCI_unregister { | |
my ($self, $irc) = @_; | |
return 1; | |
} | |
sub S_bot_addressed { | |
my ($self, $irc) = splice @_, 0, 2; | |
my $chan = ${ $_[1] }->[0]; | |
my $syntax = ${ $_[2] }; | |
my $found = func_fetch($syntax); | |
$found = tablet_fetch($syntax) if !defined $found; | |
if (!defined $found) { | |
$irc->yield($self->{Method}, $chan, "Syntax '$syntax' not recognized"); | |
return PCI_EAT_NONE; | |
} | |
my $result = $self->{parser}->render_string($found, 'text'); | |
$result =~ s/^ .+?$//gm; | |
$result =~ s/^ +| +$//gm; | |
$result =~ s/\n+/ /m; | |
$result =~ s/\n+/ /gm; | |
if (length $result > 400) { | |
$result = substr $result, 0, 399; | |
$result .= '…'; | |
} | |
$irc->yield($self->{Method}, $chan, $result); | |
return PCI_EAT_NONE; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment