]> ruin.nu Git - NDIRC.git/blob - Context.pm
Updated -user to account for discord id
[NDIRC.git] / Context.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
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.                                   *
8 #                                                                         *
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.                          *
13 #                                                                         *
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 #**************************************************************************/
19
20 package NDIRC::Context;
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use Moose;
26 use namespace::autoclean;
27
28 use Set::Object ();
29
30 has channel => (
31         is => 'ro',
32         isa => 'Str',
33         required => 1
34 );
35
36 has roles => (
37         is => 'ro',
38         isa => 'Object',
39         lazy_build => 1
40 );
41
42 has uid => (
43         is => 'ro',
44         isa => 'Int',
45         lazy_build => 1
46 );
47
48 has disp => (
49         is => 'ro',
50         isa => 'Object',
51         required => 1
52 );
53
54 has model => (
55         is => 'ro',
56         isa => 'Object',
57         required => 1
58 );
59
60 sub assert_user_roles {
61         my ($self,@roles) = @_;
62         return 1 unless @roles;
63
64         my $need = Set::Object->new(@roles);
65
66         if ($self->roles->superset($need)){
67                 return 1;
68         }
69
70         die "Access denied";
71 }
72
73 sub check_user_roles {
74         my ($self,@roles) = @_;
75
76         local $@;
77         eval { $self->assert_user_roles(@roles) };
78 }
79
80 sub reply {
81 }
82
83 sub message {
84 }
85
86 sub intel_log {
87         my ($c,$planet, $message) = @_;
88         my $log = $c->model->prepare_cached(q{
89 INSERT INTO forum_posts (ftid,uid,message) VALUES(
90         (SELECT ftid FROM planets WHERE pid = $3),$1,$2)
91                 });
92         $log->execute($c->uid,$message,$planet);
93 }
94
95 sub def_log {
96         my ($c,$call, $message) = @_;
97         my $log = $c->model->prepare(q{
98 INSERT INTO forum_posts (ftid,uid,message) VALUES(
99         (SELECT ftid FROM calls WHERE call = $3),$1,$2)
100                 });
101         $log->execute($c->uid,$message,$call);
102 }
103
104 sub _build_roles {
105         my ($self) = @_;
106
107         my $query = $self->model->prepare(q{
108 SELECT role FROM group_roles
109 WHERE gid IN (SELECT gid FROM groupmembers JOIN users USING (uid)
110         WHERE uid = $1)
111                 });
112         $query->execute($self->uid);
113
114         my @roles;
115         while (my $group = $query->fetchrow_hashref){
116                 push @roles,$group->{role};
117         }
118         return Set::Object->new(@roles);
119 }
120
121 sub _build_uid {
122         my ($self) = @_;
123
124         return -4;
125 }
126
127 sub valuecolor {
128         shift @_;
129         my $s = $_;
130         return $s;
131 }
132
133 __PACKAGE__->meta->make_immutable;
134 1;