X-Git-Url: https://ruin.nu/git/?p=NDIRC.git;a=blobdiff_plain;f=Dispatcher.pm;h=76beef574c6687127d2fe3cb1bcc05501d4c40dd;hp=8d7ffe9843893ba2768cced2fd149a8becec64c5;hb=8d103256e7f29d892d040ec25a38b2cb56e7f942;hpb=cd394b6907c15c9a6a9161022a9e58f403f9618a diff --git a/Dispatcher.pm b/Dispatcher.pm index 8d7ffe9..76beef5 100644 --- a/Dispatcher.pm +++ b/Dispatcher.pm @@ -23,86 +23,70 @@ use warnings; use feature ':5.10'; use Moose; -use Symbol; +use Moose::Exporter; use Set::Object (); use NDIRC::Command; +Moose::Exporter->setup_import_methods( + as_is => [ 'command' ] +); + + has commands => ( - is => 'rw', + is => 'ro', isa => 'HashRef[Object]', default => sub{ {} }, ); has channels => ( - is => 'rw', + is => 'ro', isa => 'HashRef[ArrayRef[Str]]', default => sub{ {} }, ); + +my $DISP; + sub load { my $self = shift; + $DISP = $self; + for (@_){ - my $class = "NDIRC::Commands::$_"; my $file = "NDIRC/Commands/$_.pm"; - Symbol::delete_package($class); unless (my $return = do $file){ warn "couldn't parse $file: $@" if $@; warn "couldn't do $file: $!" if $!; warn "couldn't run $file" unless $return; - }else { - print "Loading $class\n"; - $self->load_class($class); - } } + $DISP = undef; } -sub load_class { - my ($self,$class) = @_; - - my @subs = eval "$class->meta->get_method_list"; - for my $c (@subs){ - my $attr = eval "$class->meta->get_method('$c')->attributes"; - if (@$attr){ - print "Command: $c"; - my %c = (channels => ['all']); - my @aliases; - for (@{$attr}){ - #Parse attributes, from Attribute::Handlers - my ($attr, $data) = /^([a-z_]\w*)(?:[(](.*)[)])?$/is or next; - my $evaled = eval("package Temp; no warnings; - local \$SIG{__WARN__}=sub{die}; [$data]"); - $evaled = ($evaled && $data =~ /^\s*\[/) ? [$evaled] - : ($evaled) ? $evaled : [$data]; - - given ($attr){ - when ('Alias'){ - @aliases = @$evaled; - } - default { - $c{lc $attr} = $evaled; - } - } - } - push @aliases, $c; - my $f = eval "\\&${class}::$c"; - $c{func} = $f; - for my $a (@aliases){ - $c{name} = $a; - my $co = NDIRC::Command->new(\%c); - $self->commands->{$a} = $co; - } - } +sub command ($$$) { + my ($c,$a,$class) = @_; + my %c = %{$a}; + my @aliases; + if (exists $c{alias}){ + @aliases = @{$c{alias}} if ref $c{alias} eq 'ARRAY'; + push @aliases, $c{alias} if ref $c{alias} eq ''; + } + push @aliases, $c; + say "Command: (@aliases)"; + for my $a (@aliases){ + $c{name} = $a; + my $co = $class->new_object(\%c); + $DISP->commands->{$a} = $co; } } sub add_channel { my ($self,$channel,$types) = @_; + $types = Set::Object->new(@{$types}); $self->channels->{lc $channel} = $types; } @@ -110,16 +94,12 @@ sub has_command { my ($self,$command,$channel) = @_; $channel = lc $channel; - return 0 unless defined $command && exists $self->commands->{$command}; - - my $types = Set::Object->new(@{$self->commands->{$command}->type}); - my @types = qw/pub/; - @types = @{$self->channels->{$channel}} if exists $self->channels->{$channel}; + return 0 unless defined $command && defined $channel; + return 0 unless exists $self->commands->{$command}; + return 0 unless exists $self->channels->{$channel}; - for (@types){ - return 1 if $types->element($_); - } - return 0; + $command = $self->commands->{$command}; + return $self->channels->{$channel}->has($command->type); } sub run_command { @@ -127,7 +107,7 @@ sub run_command { return 0 unless exists $self->commands->{$command}; my $acl = $self->commands->{$command}->acl; - return 0 if $acl && !$c->check_user_roles(@$acl); + return 0 if $acl && !$c->check_user_roles($acl); $args //= ''; @@ -137,9 +117,7 @@ sub run_command { if ($@){ given ($@){ when(/^ARGS/){ - for (@{$c->disp->commands->{$command}->help}){ - $c->reply($_); - } + $c->reply($c->disp->commands->{$command}->help); } default { $c->reply("Something went wrong. If it is not temporary, please report it. '$_'");