]> ruin.nu Git - NDIRC.git/commitdiff
reorganize context
authorMichael Andreen <harv@ruin.nu>
Sun, 10 Feb 2019 21:45:07 +0000 (22:45 +0100)
committerMichael Andreen <harv@ruin.nu>
Sun, 10 Mar 2019 09:15:42 +0000 (10:15 +0100)
Bot.pm
Context.pm
DiscordContext.pm
IrcContext.pm [new file with mode: 0644]

diff --git a/Bot.pm b/Bot.pm
index 9bf39f9ae09c612b88bdca9100558b1a4f505955..1f5ff2102e50db0c18139449920b2bd969f884be 100644 (file)
--- a/Bot.pm
+++ b/Bot.pm
@@ -36,7 +36,7 @@ use Mojo::Discord;
 use Mojo::IOLoop;
 
 use NDIRC::Dispatcher;
-use NDIRC::Context;
+use NDIRC::IrcContext;
 
 use IO::File;
 
@@ -376,7 +376,7 @@ sub parseCommand {
        }
 
        $address =~ s/.*@(.*)/$1/;
-       my $c = NDIRC::Context->new({
+       my $c = NDIRC::IrcContext->new({
                        host => $address,
                        nick => $nick,
                        channel => $channel,
index 5810ca4e97dc9cc369b2d6327e716d50ea67fd02..39300254d1bf741e59122721a7bddc40e523a02a 100644 (file)
@@ -23,21 +23,10 @@ use warnings;
 use feature ':5.10';
 
 use Moose;
+use namespace::autoclean;
 
 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',
@@ -68,18 +57,6 @@ has model => (
        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;
@@ -101,48 +78,9 @@ sub check_user_roles {
 }
 
 sub reply {
-       my ($self,$msg) = @_;
-
-       my @command = split / /, $self->reply_string;
-       $self->message(@command, $msg);
 }
 
 sub message {
-       my ($self,$command, $target, $msg) = @_;
-
-       $msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
-       $msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;
-
-       #Split the message, using the, slightly modified, algorithm from splitlong.pl in the irssi distribution.
-       if ($command eq 'privmsg'){
-               my $lend = ' ...';
-               my $lstart = '... ';
-               my $maxlength = $self->server->{msg_length} - bytes::length("privmsg $target :" . $self->server->nick_name());
-               my $maxlength2 = $maxlength - bytes::length($lend);
-
-               if (bytes::length($msg) > ($maxlength)) {
-                       my @spltarr;
-
-                       while (bytes::length($msg) > ($maxlength)) {
-                               my $pos = rindex($msg, " ", $maxlength2);
-                               push @spltarr, substr($msg, 0, ($pos < ($maxlength/10 + 4)) ? $maxlength2  : $pos)  . $lend;
-                               $msg = $lstart . substr($msg, ($pos < ($maxlength/10 + 4)) ? $maxlength2 : $pos+1);
-                       }
-
-                       push @spltarr, $msg;
-                       for (@spltarr) {
-                               $self->command($command, $target, $_);
-                       }
-                       return;
-               }
-       }
-       $self->command($command, $target, $msg);
-}
-
-sub command {
-       my ($self,@command) = @_;
-
-       $self->server->yield(@command);
 }
 
 sub intel_log {
@@ -169,9 +107,9 @@ sub _build_roles {
        my $query = $self->model->prepare(q{
 SELECT role FROM group_roles
 WHERE gid IN (SELECT gid FROM groupmembers JOIN users USING (uid)
-       WHERE hostmask = $1)
+       WHERE uid = $1)
                });
-       $query->execute($self->host);
+       $query->execute($self->uid);
 
        my @roles;
        while (my $group = $query->fetchrow_hashref){
@@ -183,29 +121,14 @@ WHERE gid IN (SELECT gid FROM groupmembers JOIN users USING (uid)
 sub _build_uid {
        my ($self) = @_;
 
-       my $query = $self->model->prepare(q{
-SELECT uid FROM users
-WHERE hostmask = $1
-               });
-       $query->execute($self->host);
-
-       if (my ($uid) = $query->fetchrow_array){
-               $query->finish;
-               return $uid;
-       }
        return -4;
 }
 
 sub valuecolor {
        shift @_;
        my $s = $_;
-       $s = $_[1] if $#_ >= 1;
-       $s = "" unless defined $s;
-       return "<c05>$s</c>" if $s eq 'Hostile';
-       return "<c03>$s</c>" if $s eq 'Friendly';
-       return "<c12>$s</c>" if $s eq 'Nap' or $s eq 'NAP';
-       return "<b>$s</b>" if $_[0];
        return $s;
 }
 
+__PACKAGE__->meta->make_immutable;
 1;
index 1d7cb58f472b482b6e6af84dc3a89d76311bd78d..4418959c021878dff7b396bfd668ae1b95c52e4f 100644 (file)
@@ -1,5 +1,5 @@
 #**************************************************************************
-#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
+#   Copyright (C) 2019 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  *
@@ -23,8 +23,8 @@ use warnings;
 use feature ':5.10';
 
 use Moose;
-
-use Set::Object ();
+use namespace::autoclean;
+extends "NDIRC::Context";
 
 has discord_id => (
        is => 'ro',
@@ -38,61 +38,12 @@ has channel_id => (
        required => 1
 );
 
-has channel => (
-       is => 'ro',
-       isa => 'Str',
-       required => 1
-);
-
-has roles => (
-       is => 'ro',
-       isa => 'Object',
-       lazy_build => 1
-);
-
-has uid => (
-       is => 'ro',
-       isa => 'Int',
-       lazy_build => 1
-);
-
-has disp => (
-       is => 'ro',
-       isa => 'Object',
-       required => 1
-);
-
-has model => (
-       is => 'ro',
-       isa => 'Object',
-       required => 1
-);
-
 has discord => (
        is => 'ro',
        isa => 'Object',
        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) = @_;
@@ -109,45 +60,6 @@ sub message {
        $self->discord->send_message($target, $msg ); # Send the response.
 }
 
-sub command {
-       my ($self,@command) = @_;
-
-}
-
-sub intel_log {
-       my ($c,$planet, $message) = @_;
-       my $log = $c->model->prepare_cached(q{
-INSERT INTO forum_posts (ftid,uid,message) VALUES(
-       (SELECT ftid FROM planets WHERE pid = $3),$1,$2)
-               });
-       $log->execute($c->uid,$message,$planet);
-}
-
-sub def_log {
-       my ($c,$call, $message) = @_;
-       my $log = $c->model->prepare(q{
-INSERT INTO forum_posts (ftid,uid,message) VALUES(
-       (SELECT ftid FROM calls WHERE call = $3),$1,$2)
-               });
-       $log->execute($c->uid,$message,$call);
-}
-
-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 discord_id = $1)
-               });
-       $query->execute($self->discord_id);
-
-       my @roles;
-       while (my $group = $query->fetchrow_hashref){
-               push @roles,$group->{role};
-       }
-       return Set::Object->new(@roles);
-}
 
 sub _build_uid {
        my ($self) = @_;
@@ -177,4 +89,6 @@ sub valuecolor {
        return $s;
 }
 
+__PACKAGE__->meta->make_immutable;
+
 1;
diff --git a/IrcContext.pm b/IrcContext.pm
new file mode 100644 (file)
index 0000000..209f25e
--- /dev/null
@@ -0,0 +1,129 @@
+#**************************************************************************
+#   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::IrcContext;
+use strict;
+use warnings;
+use feature ':5.10';
+
+use Moose;
+use namespace::autoclean;
+extends "NDIRC::Context";
+
+use Set::Object ();
+
+has host => (
+       is => 'ro',
+       isa => 'Str',
+       required => 1
+);
+
+has nick => (
+       is => 'ro',
+       isa => 'Str',
+       required => 1
+);
+
+has server => (
+       is => 'ro',
+       isa => 'Object',
+       required => 1
+);
+
+has reply_string => (
+       is => 'ro',
+       isa => 'Str',
+       required => 1,
+);
+
+sub reply {
+       my ($self,$msg) = @_;
+
+       my @command = split / /, $self->reply_string;
+       $self->message(@command, $msg);
+}
+
+sub message {
+       my ($self,$command, $target, $msg) = @_;
+
+       $msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
+       $msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;
+
+       #Split the message, using the, slightly modified, algorithm from splitlong.pl in the irssi distribution.
+       if ($command eq 'privmsg'){
+               my $lend = ' ...';
+               my $lstart = '... ';
+               my $maxlength = $self->server->{msg_length} - bytes::length("privmsg $target :" . $self->server->nick_name());
+               my $maxlength2 = $maxlength - bytes::length($lend);
+
+               if (bytes::length($msg) > ($maxlength)) {
+                       my @spltarr;
+
+                       while (bytes::length($msg) > ($maxlength)) {
+                               my $pos = rindex($msg, " ", $maxlength2);
+                               push @spltarr, substr($msg, 0, ($pos < ($maxlength/10 + 4)) ? $maxlength2  : $pos)  . $lend;
+                               $msg = $lstart . substr($msg, ($pos < ($maxlength/10 + 4)) ? $maxlength2 : $pos+1);
+                       }
+
+                       push @spltarr, $msg;
+                       for (@spltarr) {
+                               $self->command($command, $target, $_);
+                       }
+                       return;
+               }
+       }
+       $self->command($command, $target, $msg);
+}
+
+sub command {
+       my ($self,@command) = @_;
+
+       $self->server->yield(@command);
+}
+
+sub _build_uid {
+       my ($self) = @_;
+
+       my $query = $self->model->prepare(q{
+SELECT uid FROM users
+WHERE hostmask = $1
+               });
+       $query->execute($self->host);
+
+       if (my ($uid) = $query->fetchrow_array){
+               $query->finish;
+               return $uid;
+       }
+       return -4;
+}
+
+sub valuecolor {
+       shift @_;
+       my $s = $_;
+       $s = $_[1] if $#_ >= 1;
+       $s = "" unless defined $s;
+       return "<c05>$s</c>" if $s eq 'Hostile';
+       return "<c03>$s</c>" if $s eq 'Friendly';
+       return "<c12>$s</c>" if $s eq 'Nap' or $s eq 'NAP';
+       return "<b>$s</b>" if $_[0];
+       return $s;
+}
+
+__PACKAGE__->meta->make_immutable;
+1;