From ac607520e38835ccdbed15488b6cbd3eddcc6a20 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sun, 17 Aug 2014 00:34:16 +0300 Subject: [PATCH] Reindent code, tests, Makefile.PL --- Makefile.PL | 34 +++--- lib/POE/Component/IRC/Plugin/Infobot.pm | 152 ++++++++++++------------ t/POE-Component-IRC-Plugin-Infobot.t | 20 ++-- 3 files changed, 103 insertions(+), 103 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 372de9c..4645acd 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -1,21 +1,21 @@ use ExtUtils::MakeMaker; WriteMakefile( - NAME => 'POE::Component::IRC::Plugin::Infobot', - VERSION_FROM => 'lib/POE/Component/IRC/Plugin/Infobot.pm', - ABSTRACT_FROM => 'lib/POE/Component/IRC/Plugin/Infobot.pm', - AUTHOR => 'Marius Gavrilescu ', - LICENSE => 'perl', - MIN_PERL_VERSION => '5.14.0', - META_ADD => { - dynamic_config => 0, - }, - PREREQ_PM => { - POE::Component::IRC::Plugin => 0, - IRC::Utils => 0.12, - DB_File => 0. - }, - BUILD_REQUIRES => { - Test::MockObject => 0, - } + NAME => 'POE::Component::IRC::Plugin::Infobot', + VERSION_FROM => 'lib/POE/Component/IRC/Plugin/Infobot.pm', + ABSTRACT_FROM => 'lib/POE/Component/IRC/Plugin/Infobot.pm', + AUTHOR => 'Marius Gavrilescu ', + LICENSE => 'perl', + MIN_PERL_VERSION => '5.14.0', + META_ADD => { + dynamic_config => 0, + }, + PREREQ_PM => { + POE::Component::IRC::Plugin => 0, + IRC::Utils => 0.12, + DB_File => 0. + }, + BUILD_REQUIRES => { + Test::MockObject => 0, + } ); diff --git a/lib/POE/Component/IRC/Plugin/Infobot.pm b/lib/POE/Component/IRC/Plugin/Infobot.pm index 4e3426a..1527d86 100644 --- a/lib/POE/Component/IRC/Plugin/Infobot.pm +++ b/lib/POE/Component/IRC/Plugin/Infobot.pm @@ -12,120 +12,120 @@ use IRC::Utils qw/parse_user/; use POE::Component::IRC::Plugin qw/PCI_EAT_NONE/; use constant +{ - OK => [ 'sure, %s', 'ok, %s', 'gotcha, %s'], - A_IS_B => [ '%s is %s', 'I think %s is %s', 'hmmm... %s is %s', 'it has been said that %s is %s', '%s is probably %s', 'rumour has it %s is %s', 'i heard %s was %s', 'somebody said %s is %s', 'i guess %s is %s', 'well, %s is %s', '%s is, like, %s', 'methinks %s is %s'], - I_DONT_KNOW => [ 'I don\'t know, %s', 'Dunno, %s', 'No idea, %s', '%s: huh?', 'nem tudom, %s', 'anlamıyorum, %s', 'bilmiyorum, %s', 'nu ştiu d\'astea, %s', 'Je ne sais pas, %s', 'Я не знаю, %s'], + OK => [ 'sure, %s', 'ok, %s', 'gotcha, %s'], + A_IS_B => [ '%s is %s', 'I think %s is %s', 'hmmm... %s is %s', 'it has been said that %s is %s', '%s is probably %s', 'rumour has it %s is %s', 'i heard %s was %s', 'somebody said %s is %s', 'i guess %s is %s', 'well, %s is %s', '%s is, like, %s', 'methinks %s is %s'], + I_DONT_KNOW => [ 'I don\'t know, %s', 'Dunno, %s', 'No idea, %s', '%s: huh?', 'nem tudom, %s', 'anlamıyorum, %s', 'bilmiyorum, %s', 'nu ştiu d\'astea, %s', 'Je ne sais pas, %s', 'Я не знаю, %s'], }; sub new { - my $class = shift; - my $self = { - filename => 'factoids.db', - @_ - }; - - my %db; - $self->{dbobj} = tie %db, DB_File => $self->{filename} if defined $self->{filename}; - $self->{db} = \%db; - bless $self, $class + my $class = shift; + my $self = { + filename => 'factoids.db', + @_ + }; + + my %db; + $self->{dbobj} = tie %db, DB_File => $self->{filename} if defined $self->{filename}; + $self->{db} = \%db; + bless $self, $class } sub getstr { - my $rstrings = shift; - my @strings = @$rstrings; - sprintf $strings[int rand $#strings], @_ + my $rstrings = shift; + my @strings = @$rstrings; + sprintf $strings[int rand $#strings], @_ } sub infobot_add { - my ($self, $irc, $key, $value, $to, $nick) = @_; - if (exists $self->{db}->{$key}) { - $irc->yield(privmsg => $to => "I already had it that way, $nick") if $value eq $self->{db}->{$key}; - $irc->yield(privmsg => $to => "... but $key is $self->{db}->{$key}!") unless $value eq $self->{db}->{$key}; - } else { - $self->{db}->{$key} = $value; - $self->{dbobj}->sync if exists $self->{dbobj}; - $irc->yield(privmsg => $to => getstr OK, $nick); - } + my ($self, $irc, $key, $value, $to, $nick) = @_; + if (exists $self->{db}->{$key}) { + $irc->yield(privmsg => $to => "I already had it that way, $nick") if $value eq $self->{db}->{$key}; + $irc->yield(privmsg => $to => "... but $key is $self->{db}->{$key}!") unless $value eq $self->{db}->{$key}; + } else { + $self->{db}->{$key} = $value; + $self->{dbobj}->sync if exists $self->{dbobj}; + $irc->yield(privmsg => $to => getstr OK, $nick); + } } sub infobot_query { - my ($self, $irc, $key, $to, $nick, $addressed) = @_; - if (exists $self->{db}->{$key}) { - my @answers = split /\s+\|\s+/, $self->{db}->{$key}; - local $_ = $answers[int rand $#answers]; - - if (/^ (.+)$/i) { - $irc->yield(ctcp => $to => "ACTION $1") - } elsif (/^ (.*)$/i){ - $irc->yield(privmsg => $to => $1) - } else { - $irc->yield(privmsg => $to => getstr A_IS_B, $key, $_) + my ($self, $irc, $key, $to, $nick, $addressed) = @_; + if (exists $self->{db}->{$key}) { + my @answers = split /\s+\|\s+/, $self->{db}->{$key}; + local $_ = $answers[int rand $#answers]; + + if (/^ (.+)$/i) { + $irc->yield(ctcp => $to => "ACTION $1") + } elsif (/^ (.*)$/i) { + $irc->yield(privmsg => $to => $1) + } else { + $irc->yield(privmsg => $to => getstr A_IS_B, $key, $_) + } + } elsif ($addressed) { + $irc->yield(privmsg => $to => getstr I_DONT_KNOW, $nick) } - } elsif ($addressed) { - $irc->yield(privmsg => $to => getstr I_DONT_KNOW, $nick) - } } sub infobot_forget { - my ($self, $irc, $key, $to, $nick) = @_; - if (exists $self->{db}->{$key}) { - delete $self->{db}->{$key}; - $self->{dbobj}->sync if exists $self->{dbobj}; - $irc->yield(privmsg => $to => "$nick: I forgot $key") - } else { - $irc->yield(privmsg => $to => "I didn't have anything matching $key, $nick") - } + my ($self, $irc, $key, $to, $nick) = @_; + if (exists $self->{db}->{$key}) { + delete $self->{db}->{$key}; + $self->{dbobj}->sync if exists $self->{dbobj}; + $irc->yield(privmsg => $to => "$nick: I forgot $key") + } else { + $irc->yield(privmsg => $to => "I didn't have anything matching $key, $nick") + } } sub runcmd{ - my ($self, $irc, $to, $nick, $message, $addressed) = @_; + my ($self, $irc, $to, $nick, $message, $addressed) = @_; - local $_= $message; + local $_= $message; - if (/^(.+)\s+is\s+(.*[^?])$/) { - infobot_add $self, $irc, $1, $2, $to, $nick if $addressed - } elsif (/^(.+)\?$/) { - infobot_query $self, $irc, $1, $to, $nick, $addressed - } elsif (/^forget\s+(.*)$/) { - infobot_forget $self, $irc, $1, $to, $nick if $addressed - } + if (/^(.+)\s+is\s+(.*[^?])$/) { + infobot_add $self, $irc, $1, $2, $to, $nick if $addressed + } elsif (/^(.+)\?$/) { + infobot_query $self, $irc, $1, $to, $nick, $addressed + } elsif (/^forget\s+(.*)$/) { + infobot_forget $self, $irc, $1, $to, $nick if $addressed + } } sub PCI_register { - my ($self, $irc) = @_; - $irc->plugin_register($self, SERVER => qw/public msg/); - 1 + my ($self, $irc) = @_; + $irc->plugin_register($self, SERVER => qw/public msg/); + 1 } sub PCI_unregister{ 1 } sub S_public { - my ($self, $irc, $rfullname, $rchannels, $rmessage) = @_; - my $nick = parse_user $$rfullname; + my ($self, $irc, $rfullname, $rchannels, $rmessage) = @_; + my $nick = parse_user $$rfullname; - for my $channel (@$$rchannels) { - local $_ = $$rmessage; + for my $channel (@$$rchannels) { + local $_ = $$rmessage; - my $addressed=0; - my $mynick=$irc->nick_name; - if (/^$mynick [,:]\s+/x) { - $addressed=1; - s/^$mynick [,:]\s+//x; - } + my $addressed=0; + my $mynick=$irc->nick_name; + if (/^$mynick [,:]\s+/x) { + $addressed=1; + s/^$mynick [,:]\s+//x; + } - runcmd $self, $irc, $channel, $nick, $_, $addressed - } + runcmd $self, $irc, $channel, $nick, $_, $addressed + } - PCI_EAT_NONE + PCI_EAT_NONE } sub S_msg{ - my ($self, $irc, $rfullname, $rtargets, $rmessage) = @_; - my $nick = parse_user $$rfullname; + my ($self, $irc, $rfullname, $rtargets, $rmessage) = @_; + my $nick = parse_user $$rfullname; - runcmd $self, $irc, $nick, $nick, $$rmessage, 1; + runcmd $self, $irc, $nick, $nick, $$rmessage, 1; - PCI_EAT_NONE + PCI_EAT_NONE } 1; diff --git a/t/POE-Component-IRC-Plugin-Infobot.t b/t/POE-Component-IRC-Plugin-Infobot.t index d2d02d1..cbe422e 100644 --- a/t/POE-Component-IRC-Plugin-Infobot.t +++ b/t/POE-Component-IRC-Plugin-Infobot.t @@ -9,8 +9,8 @@ BEGIN { use_ok('POE::Component::IRC::Plugin::Infobot') }; no warnings 'redefine'; sub POE::Component::IRC::Plugin::Infobot::getstr { - my $rstrings = shift; - sprintf @{$rstrings}[0], @_ + my $rstrings = shift; + sprintf @{$rstrings}[0], @_ } use warnings 'redefine'; @@ -18,8 +18,8 @@ my $last_msg; my $last_ctcp; sub yield { - $last_msg = $_[3] if $_[1] eq 'privmsg'; - $last_ctcp = $_[3] if $_[1] eq 'ctcp'; + $last_msg = $_[3] if $_[1] eq 'privmsg'; + $last_ctcp = $_[3] if $_[1] eq 'ctcp'; } my $mockirc = Test::MockObject->new; @@ -28,12 +28,12 @@ $mockirc->mock(yield => \&yield)->set_always(nick_name => 'bot'); my $self = POE::Component::IRC::Plugin::Infobot->new(filename => undef); sub runtest{ - my ($message, $expect, $comment, $private) = @_; - undef $last_msg; - undef $last_ctcp; - $self->S_public($mockirc, \'mgv!marius@ieval.ro', \([ '#chan' ]), \$message) unless $private; - $self->S_msg($mockirc, \'mgv!marius@ieval.ro', undef, \$message) if $private; - is($last_msg // $last_ctcp, $expect, $comment) + my ($message, $expect, $comment, $private) = @_; + undef $last_msg; + undef $last_ctcp; + $self->S_public($mockirc, \'mgv!marius@ieval.ro', \([ '#chan' ]), \$message) unless $private; + $self->S_msg($mockirc, \'mgv!marius@ieval.ro', undef, \$message) if $private; + is($last_msg // $last_ctcp, $expect, $comment) } runtest 'bot: a is b', 'sure, mgv', 'add'; -- 2.39.2