1 #**************************************************************************
2 # Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu> *
4 # This program is free software; you can redistribute it and/or modify *
5 # it under the terms of the GNU General Public License as published by *
6 # the Free Software Foundation; either version 2 of the License, or *
7 # (at your option) any later version. *
9 # This program is distributed in the hope that it will be useful, *
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 # GNU General Public License for more details. *
14 # You should have received a copy of the GNU General Public License *
15 # along with this program; if not, write to the *
16 # Free Software Foundation, Inc., *
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 #**************************************************************************/
20 package NDIRC::Dispatcher;
31 Moose::Exporter->setup_import_methods(
32 as_is => [ 'command' ]
39 isa => 'HashRef[Object]',
45 isa => 'HashRef[ArrayRef[Str]]',
58 my $file = "NDIRC/Commands/$_.pm";
60 unless (my $return = do $file){
61 warn "couldn't parse $file: $@" if $@;
62 warn "couldn't do $file: $!" if $!;
63 warn "couldn't run $file" unless $return;
70 my ($c,$a,$class) = @_;
73 if (exists $c{alias}){
74 @aliases = @{$c{alias}} if ref $c{alias} eq 'ARRAY';
75 push @aliases, $c{alias} if ref $c{alias} eq '';
78 say "Command: (@aliases)";
81 my $co = $class->new_object(\%c);
82 $DISP->commands->{$a} = $co;
87 my ($self,$channel,$types) = @_;
89 $types = Set::Object->new(@{$types});
90 $types->insert('universal');
91 $self->channels->{lc $channel} = $types;
95 my ($self,$command,$channel) = @_;
96 $channel = lc $channel;
98 return 0 unless defined $command && defined $channel;
99 return 0 unless exists $self->commands->{$command};
100 $command = $self->commands->{$command};
101 return 1 if $command->type eq 'universal';
102 return 0 unless exists $self->channels->{$channel};
104 return $self->channels->{$channel}->has($command->type);
108 my ($self,$c,$command,$args) = @_;
110 return 0 unless exists $self->commands->{$command};
111 my $acl = $self->commands->{$command}->acl;
112 return 0 if $acl && !$c->check_user_roles($acl);
117 $self->commands->{$command}->execute($c,$args)
122 $c->reply($c->disp->commands->{$command}->help);
125 $c->reply("Something went wrong. If it is not temporary, please report it. '$_'");