Skip to content

Instantly share code, notes, and snippets.

@worr
Created July 26, 2011 05:30
Show Gist options
  • Select an option

  • Save worr/1106045 to your computer and use it in GitHub Desktop.

Select an option

Save worr/1106045 to your computer and use it in GitHub Desktop.
A quick POE bot that fixes spelling...poorly
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use Data::Dumper;
use POE qw/Component::IRC/;
use Search::Tools::SpellCheck;
my $nick = 'nilla_waferz';
my $name = 'Newt_Gingrich';
my $server = 'irc.rizon.net';
my $channel = '#csh';
my $dict = '/usr/lib/aspell-0.60/en_US.multi';
my $target_nick = 'teh_spartan';
my $irc = POE::Component::IRC->spawn(
nick => $nick,
ircname => $name,
username => $name,
server => $server,
) or die "Error: $!";
POE::Session->create(
package_states => [
main => [ qw/_start irc_public irc_001/ ],
],
heap => { irc => $irc },
);
my $sc = Search::Tools::SpellCheck->new(
dict => $dict,
max_suggest => 1,
);
POE::Kernel->run();
sub _start {
my $irc = $_[HEAP]->{irc};
$irc->yield(register => 'all');
$irc->yield(connect => {});
return;
}
sub irc_001 {
my $irc = $_[SENDER]->get_heap();
say "Connected to $server";
say "Joining $channel";
$irc->yield(join => $channel);
#$irc->yield(privmsg => $channel => "I'm $target_nick\'s personal spell check bot!");
return;
}
sub irc_public {
my $recv_nick = (split /!/, $_[ARG0])[0];
return unless $recv_nick eq $target_nick;
my $msg = $_[ARG2];
return if $msg =~ /^[\w_\d]+:/;
return if $msg =~ /https?:\/\//;
my $irc = $_[SENDER]->get_heap();
my $count = 0;
my $words = split(/[\s\.,!\?]+/,$msg);
my $fixed = $sc->suggest($msg);
foreach my $word_ref (@$fixed) {
if ($word_ref->{suggestions} == 0 or not defined $word_ref->{suggestions}->[0]) {
$count++;
next;
}
say "Word: " . $word_ref->{word};
say "Suggestion: " . $word_ref->{suggestions}->[0];
$msg =~ s/$word_ref->{word}/$word_ref->{suggestions}->[0]/g if
$word_ref->{suggestions} != 0 and $word_ref->{suggestions}->[0] ne '';
}
$msg = "$target_nick: $msg";
$irc->yield(privmsg => $channel => $msg) if ($count != $words);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment