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;
34 isa => 'HashRef[Object]',
40 isa => 'HashRef[ArrayRef[Str]]',
46 isa => 'HashRef[Str]',
54 my $class = "NDIRC::Commands::$_";
55 my $file = "NDIRC/Commands/$_.pm";
56 Symbol::delete_package($class);
58 unless (my $return = do $file){
59 warn "couldn't parse $file: $@" if $@;
60 warn "couldn't do $file: $!" if $!;
61 warn "couldn't run $file" unless $return;
63 say "\nLoading $class";
64 $self->load_class($class);
71 my ($self,$class) = @_;
73 my @subs = eval "$class->meta->get_method_list";
75 my $attr = eval "$class->meta->get_method('$c')->attributes";
78 my %c = (channels => ['all']);
81 #Parse attributes, from Attribute::Handlers
82 my ($attr, $data) = /^([a-z_]\w*)(?:[(](.*)[)])?$/is or next;
83 my $evaled = eval("package Temp; no warnings;
84 local \$SIG{__WARN__}=sub{die}; [$data]");
85 $evaled = ($evaled && $data =~ /^\s*\[/) ? [$evaled]
86 : ($evaled) ? $evaled : [$data];
93 $c{lc $attr} = $evaled;
98 my $f = eval "\\&${class}::$c";
100 for my $a (@aliases){
102 my $co = NDIRC::Command->new(\%c);
103 $self->commands->{$a} = $co;
110 my ($self,$channel,$types) = @_;
112 $self->channels->{lc $channel} = $types;
116 my ($self,$command,$channel) = @_;
117 $channel = lc $channel;
119 return 0 unless defined $command && exists $self->commands->{$command};
121 my $types = Set::Object->new(@{$self->commands->{$command}->type});
123 @types = @{$self->channels->{$channel}} if exists $self->channels->{$channel};
126 return 1 if $types->element($_);
132 my ($self,$label,$target) = @_;
133 $self->targets->{$label} = $target;
137 my ($self,$c,$command,$args) = @_;
139 return 0 unless exists $self->commands->{$command};
140 my $acl = $self->commands->{$command}->acl;
141 return 0 if $acl && !$c->check_user_roles(@$acl);
146 $self->commands->{$command}->execute($c,$args)
151 for (@{$c->disp->commands->{$command}->help}){
156 $c->reply("Something went wrong. If it is not temporary, please report it. '$_'");