]> ruin.nu Git - NDIRC.git/blobdiff - Context.pm
Basic infrastructure for new irc command dispatcher
[NDIRC.git] / Context.pm
diff --git a/Context.pm b/Context.pm
new file mode 100644 (file)
index 0000000..5d0fb1c
--- /dev/null
@@ -0,0 +1,121 @@
+#**************************************************************************
+#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
+#                                                                         *
+#   This program is free software; you can redistribute it and/or modify  *
+#   it under the terms of the GNU General Public License as published by  *
+#   the Free Software Foundation; either version 2 of the License, or     *
+#   (at your option) any later version.                                   *
+#                                                                         *
+#   This program is distributed in the hope that it will be useful,       *
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+#   GNU General Public License for more details.                          *
+#                                                                         *
+#   You should have received a copy of the GNU General Public License     *
+#   along with this program; if not, write to the                         *
+#   Free Software Foundation, Inc.,                                       *
+#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+#**************************************************************************/
+
+package NDIRC::Context;
+use strict;
+use warnings;
+use feature ':5.10';
+
+use Moose;
+
+use Set::Object ();
+
+has host => (
+       is => 'ro',
+       isa => 'Str',
+       required => 1
+);
+
+has nick => (
+       is => 'ro',
+       isa => 'Str',
+       required => 1
+);
+
+has channel => (
+       is => 'ro',
+       isa => 'Str',
+       required => 1
+);
+
+has roles => (
+       is => 'ro',
+       isa => 'Object',
+       lazy_build => 1
+);
+
+has disp => (
+       is => 'ro',
+       isa => 'Object',
+       required => 1
+);
+
+has model => (
+       is => 'ro',
+       isa => 'Object',
+       required => 1
+);
+
+has server => (
+       is => 'ro',
+       isa => 'Object',
+       required => 1
+);
+
+has reply_string => (
+       is => 'ro',
+       isa => 'Str',
+       required => 1,
+);
+
+sub assert_user_roles {
+       my ($self,@roles) = @_;
+       return 1 unless @roles;
+
+       my $need = Set::Object->new(@roles);
+
+       if ($self->roles->superset($need)){
+               return 1;
+       }
+
+       die "Access denied";
+}
+
+sub check_user_roles {
+       my ($self,@roles) = @_;
+
+       local $@;
+       eval { $self->assert_user_roles(@roles) };
+}
+
+sub reply {
+       my ($self,$msg) = @_;
+
+       $self->server->command($self->reply_string . $msg);
+}
+
+sub _build_roles {
+       my ($self) = @_;
+
+       my $query = $self->model->prepare(q{
+SELECT role FROM group_roles
+WHERE gid IN (SELECT gid FROM groupmembers JOIN users USING (uid)
+       WHERE hostmask ILIKE $1)
+               });
+       $query->execute($self->host);
+
+       my @roles;
+       while (my $group = $query->fetchrow_hashref){
+               push @roles,$group->{role};
+       }
+       return Set::Object->new(@roles);
+}
+
+
+1;