Permalink
Browse files

Merge branch roles

  • Loading branch information...
2 parents cc8989c + b81b27e commit e2ebef0efc43b243c535bb79c4bb1a46b54ce1b8 @tadzik committed May 23, 2010
Showing with 234 additions and 218 deletions.
  1. +2 −0 TODO
  2. +7 −8 a8bot.pm
  3. +53 −75 a8bot/Plugin.pm
  4. +23 −29 {plugins → a8bot/Plugin}/Odzywki.pm
  5. +18 −0 a8bot/Plugin/Ping.pm
  6. +54 −0 a8bot/Plugin/Powiedz.pm
  7. +47 −0 a8bot/Plugin/Seen.pm
  8. +30 −0 a8bot/Plugin/WebTitle.pm
  9. +0 −19 plugins/Ping.pm
  10. +0 −36 plugins/Powiedz.pm
  11. +0 −24 plugins/Seen.pm
  12. +0 −27 plugins/WebTitle.pm
View
2 TODO
@@ -0,0 +1,2 @@
+Adapt rest of the plugins
+In plugins: methods register_keyword and register_passive. Sounds like fun, eh?
View
@@ -1,6 +1,7 @@
package a8bot;
use feature ':5.10';
use a8bot::Plugin;
+use Module::Load;
use Moose;
use MooseX::NonMoose;
use AnyEvent;
@@ -37,7 +38,7 @@ has 'passwd' => (
has 'plugins' => (
is => 'rw',
- isa => 'ArrayRef[a8bot::Plugin]',
+ isa => 'ArrayRef',
default => sub { [] },
traits => [ 'Array' ],
handles => {
@@ -87,9 +88,9 @@ sub BUILD {
$self->log("Error $code: $message");
},
publicmsg => sub {
- my ($client, $channel, $params) = @_;
+ my $params = $_[2];
foreach my $plugin ($self->list_plugins) {
- $plugin->publicmsg($channel, $params);
+ $plugin->pubmsg_cb($params);
}
},
registered => sub {
@@ -99,7 +100,7 @@ sub BUILD {
# logging purposes
$client->enable_ping(60);
foreach my $plugin ($self->list_plugins) {
- $plugin->registered;
+ $plugin->registered_cb;
}
},
);
@@ -116,10 +117,8 @@ sub BUILD {
sub load_plugin {
my ($self, $plugin) = @_;
- my $plug = a8bot::Plugin->new(
- bot => $self,
- plugin => $plugin,
- );
+ load $plugin;
+ my $plug = $plugin->new(bot => $self);
$self->add_plugin($plug);
}
View
@@ -1,8 +1,6 @@
package a8bot::Plugin;
use feature ':5.10';
-use lib 'plugins';
-use Module::Load;
-use Moose;
+use Moose::Role;
has 'bot' => (
is => 'ro',
@@ -11,97 +9,77 @@ has 'bot' => (
documentation => 'The a8bot object owning the plugins',
);
-# The actual module being handled
-has 'plugin' => (
- is => 'ro', # why rw?
- required => 1,
- documentation => 'The module which is being handled',
+has 'keyword' => (
+ is => 'rw',
+ isa => 'Str',
+ documentation => 'A specific keyword plugin reacts on',
);
-# this keeps whether the plugin reacts on certain types
-# of AE::IRC::Client events
-has '_disconnect' => (
- is => 'rw',
- isa => 'CodeRef',
- traits => [ 'Code' ],
- handles => {
- call_disconnect => 'execute',
+has 'keyword_cb' => (
+ is => 'rw',
+ isa => 'CodeRef',
+ traits => [ 'Code' ],
+ handles => {
+ handle_direct => 'execute',
},
+ default => sub { sub {} },
);
-has '_publicmsg' => (
- is => 'rw',
- isa => 'CodeRef',
- traits => [ 'Code' ],
- handles => {
- call_publicmsg => 'execute',
+has 'passive_cb' => (
+ is => 'rw',
+ isa => 'CodeRef',
+ traits => [ 'Code' ],
+ handles => {
+ handle_passive => 'execute',
},
+ default => sub { sub {} },
);
-has '_registered' => (
- is => 'rw',
- isa => 'CodeRef',
- traits => [ 'Code' ],
- handles => {
- call_registered => 'execute',
- },
-);
-
-sub BUILD {
- my $self = shift;
- load $self->plugin;
- my $events = $self->plugin->init;
- $self->_disconnect($events->{disconnect} // sub {});
- $self->_publicmsg($events->{publicmsg} // sub {});
- $self->_registered($events->{registered} // sub {});
-}
-
-sub publicmsg {
- my ($self, $channel, $params) = @_;
- my ($nick, $host, $msg);
- ($nick, $host) = split(/!/, $params->{prefix});
- (undef, $msg) = @{$params->{params}};
- # TODO: Maybe just params itself,
- # as an additional arg, just in case
- my $resp = $self->call_publicmsg(
- {
- nick => $self->bot->nick,
- },
- {
- nick => $nick,
- host => $host,
- command => $params->{command},
- channel => $channel,
- msg => $msg,
- },
- );
+sub handle_resp {
+ my ($self, $channel, $resp) = @_;
if (ref $resp eq 'ARRAY') {
$self->bot->send_srv(@$resp);
} elsif (defined $resp and $resp ne '') {
$self->bot->send_srv(PRIVMSG => $channel, $resp);
}
}
-sub registered {
+sub pubmsg_cb {
+ my ($self, $params) = @_;
+ my ($channel, $nick, $host, $msg);
+ ($nick, $host) = split(/!/, $params->{prefix});
+ ($channel, $msg) = @{$params->{params}};
+ my %args = (
+ nick => $nick,
+ host => $host,
+ command => $params->{command},
+ channel => $channel,
+ msg => $msg,
+ );
+ $self->handle_resp($channel, $self->handle_passive(%args));
+ if ($self->keyword) {
+ my $nick = $self->bot->nick;
+ my $comm = $self->keyword;
+ if ($msg =~ /^$nick:?,? ($comm) ?(.+)?$/) {
+ if ($2) {
+ my @foo = split / /, $2;
+ $args{args} = \@foo;
+ }
+ $self->handle_resp(
+ $channel,
+ $self->handle_direct(%args),
+ );
+ }
+ }
+}
+
+sub registered_cb {
my $self = shift;
- my $resp = $self->call_registered({ nick => $self->bot->{nick} });
+ return unless $self->can('registered');
+ my $resp = $self->registered;
if (ref $resp eq 'ARRAY') {
$self->bot->send_srv(@$resp);
}
}
-__PACKAGE__->meta->make_immutable;
-
1;
-
-__END__
-sub _mode {
- my ($self, @args) = @_;
- $self->send_srv(MODE => @args);
-}
-
-sub _privmsg {
- my ($self, $to, $msg) = @_;
- say "_privmsg: sending a message to $to";
- $self->send_srv(PRIVMSG => $to, $msg);
-}
@@ -1,31 +1,30 @@
-package Odzywki;
-use Tie::RegexpHash;
+package a8bot::Plugin::Odzywki;
+use feature ':5.10';
+use Moose;
+with 'a8bot::Plugin';
-tie my %db, 'Tie::RegexpHash';
-$lastresponse = 0;
+has 'lastresponse' => (is => 'rw', isa => 'Int', default => sub { 0 });
-%db = (
- qr/.*kurwa.*/i => \&kurwa,
- qr/.*ty chuju.*/i => \&ty_chuju,
- qr/.*chuj.*/i => \&chuj,
- qr/.*dupa.*/i => \&dupa,
- qr/.*spierdalaj.*/i => \&spierdalaj,
- qr/.*jeb[ię||ie|e].*/ => \&jebac
-);
-
-
-sub init {
- return { publicmsg => \&pubmsg };
+sub BUILD {
+ my $self = shift;
+ $self->passive_cb(sub { pubmsg($self, @_) });
}
sub pubmsg {
- my ($bot, $data) = @_;
- if (time - $lastresponse > 120) {
- if (my $resp = $db{$data->{msg}}) {
- $lastresponse = time;
- return "$data->{nick}: " . &$resp;
- }
+ my ($self, %data) = @_;
+ my $resp;
+ return if time - $self->lastresponse < 120;
+ given ($data{msg}) {
+ when (/kurwa/i) { $resp = $self->kurwa };
+ when (/ty chuju/i) { $resp = $self->ty_chuju };
+ when (/chuj/i) { $resp = $self->chuj };
+ when (/dupa/i) { $resp = $self->dupa };
+ when (/spierdalaj/i) { $resp = $self->spierdalaj };
+ when (/jeb[ię||ie|e]/i) { $resp = $self->jebac };
+ default { return undef };
}
+ $self->lastresponse(time);
+ return "$data{nick}: $resp";
}
sub chuj {
@@ -37,15 +36,13 @@ sub chuj {
'Grozisz mi!?',
'A na chuj mnie ten kaktus?!',
'Repeat, please: „My pen is...”.',
- 'Kurwa, Bomba, jesteś głuchy, czy pierdolnięty?',
'Jako i Ty szlachetny Panie.'
);
return $resp[rand($#resp)];
}
sub dupa {
my @resp = (
- 'Wypiąć bardziej, niech nie kuli jak pies przy kupci.',
'Poglądy są jak dupa, każdy jakieś ma, ale po co od razu pokazywać...'
);
return $resp[rand($#resp)];
@@ -63,11 +60,6 @@ sub kurwa {
'Też kobieta. Tylko pizda nie ta.',
'O! Ty też?',
'Też dziewczyna. Tylko krocze ma robocze.',
- 'To be, kurwa! Or nor to be!',
- 'Dżizus, kurwa, ja pierdolę!',
- 'Dawno temu ja też zaufałem pewnej kobiecie, wtedy dałbym sobie za nią rękę uciąć. I wiesz, co... I bym teraz, kurwa, nie miał ręki!',
- 'Trzeba płacić. W paszczu pięćdziesiąt, a za seks...',
- 'Lubisz to suko!'
);
return $resp[rand($#resp)];
}
@@ -88,4 +80,6 @@ sub ty_chuju {
return $resp[rand($#resp)];
}
+__PACKAGE__->meta->make_immutable;
+
1;
View
@@ -0,0 +1,18 @@
+package a8bot::Plugin::Ping;
+use Moose;
+with 'a8bot::Plugin';
+
+sub BUILD {
+ my $self = shift;
+ $self->keyword('ping');
+ $self->keyword_cb(sub { pubmsg(@_) });
+}
+
+sub pubmsg {
+ my (%data) = @_;
+ return "$data{nick}: pong";
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
View
@@ -0,0 +1,54 @@
+package a8bot::Plugin::Powiedz;
+use Moose;
+use Hash::MultiValue;
+with 'a8bot::Plugin';
+
+has 'db' => (
+ is => 'rw',
+ isa => 'Hash::MultiValue',
+ default => sub { Hash::MultiValue->new },
+);
+
+sub BUILD {
+ my $self = shift;
+ $self->keyword('powiedz');
+ $self->keyword_cb( sub { active($self, @_) } );
+ $self->passive_cb( sub { passive($self, @_) } );
+}
+
+sub active {
+ my ($self, %data) = @_;
+ #number of elements in $data{args} arrayref
+ my @args = @{$data{args} // []};
+ if ($#args < 1) {
+ return "$data{nick}: Usage: powiedz <nick> <wiadomosc>";
+ } else {
+ $self->db->add($args[0] => [
+ $data{nick},
+ join(' ', @args[1..$#args]),
+ ]
+ );
+ return "$data{nick}: ok, powiem mu jak się pojawi.";
+ }
+}
+
+sub passive {
+ my ($self, %data) = @_;
+ if ($self->db->get($data{nick})) {
+ my @msgs = $self->db->get_all($data{nick});
+ my $resp = '';
+ for my $msg (@msgs) {
+ if ($resp ne '') {
+ $resp .= ' Poza tym, ';
+ }
+ $resp .= "$msg->[0] kazał Ci powiedzieć: $msg->[1].";
+ }
+ $self->db->remove($data{nick});
+ return "$data{nick}: $resp";
+ }
+ return undef;
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
Oops, something went wrong.

0 comments on commit e2ebef0

Please sign in to comment.