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]]',
48 my $class = "NDIRC::Commands::$_";
49 my $file = "NDIRC/Commands/$_.pm";
50 Symbol::delete_package($class);
52 unless (my $return = do $file){
53 warn "couldn't parse $file: $@" if $@;
54 warn "couldn't do $file: $!" if $!;
55 warn "couldn't run $file" unless $return;
57 print "Loading $class\n";
58 $self->load_class($class);
65 my ($self,$class) = @_;
67 my @subs = eval "$class->meta->get_method_list";
69 my $attr = eval "$class->meta->get_method('$c')->attributes";
72 my %c = (channels => ['all']);
75 #Parse attributes, from Attribute::Handlers
76 my ($attr, $data) = /^([a-z_]\w*)(?:[(](.*)[)])?$/is or next;
77 my $evaled = eval("package Temp; no warnings;
78 local \$SIG{__WARN__}=sub{die}; [$data]");
79 $evaled = ($evaled && $data =~ /^\s*\[/) ? [$evaled]
80 : ($evaled) ? $evaled : [$data];
87 $c{lc $attr} = $evaled;
92 my $f = eval "\\&${class}::$c";
96 my $co = NDIRC::Command->new(\%c);
97 $self->commands->{$a} = $co;
104 my ($self,$channel,$types) = @_;
106 $self->channels->{lc $channel} = $types;
110 my ($self,$command,$channel) = @_;
111 $channel = lc $channel;
113 return 0 unless defined $command && exists $self->commands->{$command};
115 my $types = Set::Object->new(@{$self->commands->{$command}->type});
117 @types = @{$self->channels->{$channel}} if exists $self->channels->{$channel};
120 return 1 if $types->element($_);
126 my ($self,$c,$command,$args) = @_;
128 return 0 unless exists $self->commands->{$command};
129 my $acl = $self->commands->{$command}->acl;
130 return 0 if $acl && !$c->check_user_roles(@$acl);
135 $self->commands->{$command}->execute($c,$args)
140 for (@{$c->disp->commands->{$command}->help}){
145 $c->reply("Something went wrong. If it is not temporary, please report it. '$_'");