]> ruin.nu Git - NDIRC.git/blob - Context.pm
8a0a911a3b2abbaca49a0e8995dc853540c8b19d
[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
27 use Set::Object ();
28
29 has host => (
30         is => 'ro',
31         isa => 'Str',
32         required => 1
33 );
34
35 has nick => (
36         is => 'ro',
37         isa => 'Str',
38         required => 1
39 );
40
41 has channel => (
42         is => 'ro',
43         isa => 'Str',
44         required => 1
45 );
46
47 has roles => (
48         is => 'ro',
49         isa => 'Object',
50         lazy_build => 1
51 );
52
53 has disp => (
54         is => 'ro',
55         isa => 'Object',
56         required => 1
57 );
58
59 has model => (
60         is => 'ro',
61         isa => 'Object',
62         required => 1
63 );
64
65 has server => (
66         is => 'ro',
67         isa => 'Object',
68         required => 1
69 );
70
71 has reply_string => (
72         is => 'ro',
73         isa => 'Str',
74         required => 1,
75 );
76
77 sub assert_user_roles {
78         my ($self,@roles) = @_;
79         return 1 unless @roles;
80
81         my $need = Set::Object->new(@roles);
82
83         if ($self->roles->superset($need)){
84                 return 1;
85         }
86
87         die "Access denied";
88 }
89
90 sub check_user_roles {
91         my ($self,@roles) = @_;
92
93         local $@;
94         eval { $self->assert_user_roles(@roles) };
95 }
96
97 sub reply {
98         my ($self,$msg) = @_;
99
100         my @command = split / /, $self->reply_string;
101         $self->message(@command, $msg);
102 }
103
104 sub message {
105         my ($self,$command, $target, $msg) = @_;
106
107         $msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
108         $msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;
109
110         #Split the message, using the, slightly modified, algorithm from splitlong.pl in the irssi distribution.
111         if ($command eq 'privmsg'){
112                 my $lend = ' ...';
113                 my $lstart = '... ';
114                 my $maxlength = $self->server->{msg_length} - bytes::length("privmsg $target :" . $self->server->nick_name());
115                 my $maxlength2 = $maxlength - bytes::length($lend);
116
117                 if (bytes::length($msg) > ($maxlength)) {
118                         my @spltarr;
119
120                         while (bytes::length($msg) > ($maxlength)) {
121                                 my $pos = rindex($msg, " ", $maxlength2);
122                                 push @spltarr, substr($msg, 0, ($pos < ($maxlength/10 + 4)) ? $maxlength2  : $pos)  . $lend;
123                                 $msg = $lstart . substr($msg, ($pos < ($maxlength/10 + 4)) ? $maxlength2 : $pos+1);
124                         }
125
126                         push @spltarr, $msg;
127                         for (@spltarr) {
128                                 $self->command($command, $target, $_);
129                         }
130                         return;
131                 }
132         }
133         $self->command($command, $target, $msg);
134 }
135
136 sub command {
137         my ($self,@command) = @_;
138
139         $self->server->yield(@command);
140 }
141
142 sub intel_log {
143         my ($c,$planet, $message) = @_;
144         my $log = $c->model->prepare_cached(q{
145 INSERT INTO forum_posts (ftid,uid,message) VALUES(
146         (SELECT ftid FROM planets WHERE pid = $3)
147         ,(SELECT uid FROM users WHERE hostmask ILIKE $1)
148         ,$2)
149                 });
150         $log->execute($c->host,$message,$planet);
151 }
152
153 sub def_log {
154         my ($c,$call, $message) = @_;
155         my $log = $c->model->prepare(q{
156 INSERT INTO forum_posts (ftid,uid,message) VALUES(
157         (SELECT ftid FROM calls WHERE call = $3)
158         ,(SELECT uid FROM users WHERE hostmask ILIKE $1),$2)
159                 });
160         $log->execute($c->host,$message,$call);
161 }
162
163 sub _build_roles {
164         my ($self) = @_;
165
166         my $query = $self->model->prepare(q{
167 SELECT role FROM group_roles
168 WHERE gid IN (SELECT gid FROM groupmembers JOIN users USING (uid)
169         WHERE hostmask ILIKE $1)
170                 });
171         $query->execute($self->host);
172
173         my @roles;
174         while (my $group = $query->fetchrow_hashref){
175                 push @roles,$group->{role};
176         }
177         return Set::Object->new(@roles);
178 }
179
180
181 1;