Skip to content

Commit

Permalink
Merge branch 'roles'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeusz Sośnierz committed May 31, 2010
2 parents f0a9e8b + cf6c73c commit 9c294cc
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 66 deletions.
13 changes: 0 additions & 13 deletions plugins/Echo.pm

This file was deleted.

26 changes: 0 additions & 26 deletions plugins/Help.pm

This file was deleted.

12 changes: 0 additions & 12 deletions plugins/Ping.pm

This file was deleted.

22 changes: 7 additions & 15 deletions xmpbot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ has 'passwd' => (

has 'plugins' => (
is => 'ro',
isa => 'HashRef[HashRef[Str]]',
isa => 'HashRef',
default => sub { {} },
traits => ['Hash'],
handles => {
Expand Down Expand Up @@ -59,7 +59,7 @@ sub BUILD {
my $repl = undef;
my $plugin = $self->get_plugin($comm);
if ($plugin) {
my $ret = $plugin->{plugin}->msg_cb($args, $self);
my $ret = $plugin->msg_cb($args, $self);
if ($ret) {
$repl = $msg->make_reply;
$repl->add_body($ret);
Expand Down Expand Up @@ -88,25 +88,17 @@ sub BUILD {
sub load_plugin {
my ($self, $plugin) = @_;
load $plugin;
my $ret = $plugin->init;
next unless $ret;
if ($self->get_plugin(@$ret[0])) {
my $obj = $plugin->new;
if ($self->get_plugin($obj->command)) {
$self->log("Plugin $plugin tried to register a ",
"keyword @$ret[0], which is alredy registered\n");
"keyword ".$obj->command.", which is alredy registered\n");
} else {
# TODO: Support for passive plugins maybe?
# So they return undef instead of a keyword
# and always handle every message.
# Usecase? Logs, or something
$self->set_plugin(
@$ret[0] => {
plugin => $plugin,
info => @$ret[1],
help => @$ret[2],
},
);
$self->log("Registered plugin $plugin ",
"with keyword @$ret[0]\n");
$self->set_plugin($obj->command, $obj);
$self->log("Registered plugin $plugin\n");
}
}

Expand Down
31 changes: 31 additions & 0 deletions xmpbot/Plugin.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package xmpbot::Plugin;
use Moose::Role;
requires 'msg_cb';
use Carp;

has 'command' => (
is => 'rw',
isa => 'Str',
predicate => 'has_command',
);

has 'description' => (
is => 'rw',
isa => 'Str',
predicate => 'has_description',
);

has 'help' => (
is => 'rw',
isa => 'Str',
predicate => 'has_help',
);

after 'BUILD' => sub {
my $self = shift;
croak "Command not specified!" unless $self->has_command;
carp "Warning: description not set" unless $self->has_description;
carp "Warning: help message not set" unless $self->has_help;
};

1;
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions xmpbot/Plugin/Echo.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xmpbot::Plugin::Echo;
use Moose;
with 'xmpbot::Plugin';

sub BUILD {
my $self = shift;
$self->command('echo');
$self->description('echoes what was said');
$self->help('This plugin simply retypes what the user said');
}

sub msg_cb {
my ($self, $msg) = @_;
return $msg;
}

1;
30 changes: 30 additions & 0 deletions xmpbot/Plugin/Help.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package xmpbot::Plugin::Help;
use Moose;
with 'xmpbot::Plugin';

sub BUILD {
my $self = shift;
$self->command('help');
$self->description('list all available commands');
$self->help('This plugin shows a list of available commands with short descriptions, or a longer help for a specified command');
}

sub msg_cb {
my (undef, $args, $bot) = @_;
my $resp;
if (not defined $args) {
for my $pair ($bot->plugins_pairs) {
$resp .= "$pair->[0]\t".$pair->[1]->description."\n";
}
} else {
my $comm = $bot->get_plugin($args);
if ($comm) {
$resp = $comm->help;
} else {
$resp = "No help available for '$args'";
}
}
return $resp;
}

1;
16 changes: 16 additions & 0 deletions xmpbot/Plugin/Ping.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package xmpbot::Plugin::Ping;
use Moose;
with 'xmpbot::Plugin';

sub BUILD {
my $self = shift;
$self->command('ping');
$self->description('respond to "ping"');
$self->help('This plugin responds to "ping" with "pong". Yep, that\'s it');
}

sub msg_cb {
return "pong";
}

1;
File renamed without changes.
File renamed without changes.

0 comments on commit 9c294cc

Please sign in to comment.