From 0c1f9a4f85cfdfd09196f34965f95a7912930d06 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Sun, 26 Jun 2016 19:11:02 -0400 Subject: [PATCH] Add in the allowpaste plugin to control the pastebin --- etc/plugins.conf | 1 + lib/Bot/BB3/Roles/Evalpastebin.pm | 41 +++++++++++--- lib/Bot/BB3/Roles/IRC.pm | 10 ++++ plugins/allowpaste.pm | 90 +++++++++++++++++++++++++++++++ plugins/translate.pm | 2 +- 5 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 plugins/allowpaste.pm diff --git a/etc/plugins.conf b/etc/plugins.conf index d6857b6..174c6b2 100644 --- a/etc/plugins.conf +++ b/etc/plugins.conf @@ -2,6 +2,7 @@ server "*" { channel "*" { plugin "*" { addressed: true } plugin "join" { access: op; addressed: true } + plugin "allowpaste" { access: op; addressed: true } plugin "part" { access: op } plugin "reload_plugins" { access: root } plugin "restart" { access: root } diff --git a/lib/Bot/BB3/Roles/Evalpastebin.pm b/lib/Bot/BB3/Roles/Evalpastebin.pm index b08d466..4d91327 100644 --- a/lib/Bot/BB3/Roles/Evalpastebin.pm +++ b/lib/Bot/BB3/Roles/Evalpastebin.pm @@ -20,6 +20,27 @@ sub new { return $self; } +{ + my $dbh; + sub dbh { + if( $dbh and $dbh->ping ) { + return $dbh; + } + + $dbh = DBI->connect( "dbi:SQLite:dbname=var/allowpaste.db", "", "", { PrintError => 0, RaiseError => 1 } ); + + return $dbh; + } +} + +sub get_status { + my ($chancon) = @_; + + my $status = dbh()->selectrow_hashref('SELECT value FROM allowpaste WHERE channel = ?', {}, $chancon); + + return ($status // {})->{value}; +} + sub _start { my( $self, $kernel ) = @_[OBJECT,KERNEL]; my $conf = $self->{conf}; @@ -50,13 +71,19 @@ sub receive_paste { if( $alert_channel !~ /^\s*---/ ) { # Ignore things like "---irc.freenode, skip server names my($server,$nick,$channel) = split /:/,$alert_channel,3; - $_[KERNEL]->post( "Bot::BB3::Roles::IRC", - external_message => - $server, - $nick, - $channel, - "$who pasted a new file at $link - $summary" - ); + my $setting = get_status($alert_channel) // 1; + + if ($setting) { + $_[KERNEL]->post( "Bot::BB3::Roles::IRC", + external_message => + $server, + $nick, + $channel, + "$who pasted a new file at $link - $summary" + ); + } else { + return; # we've been disallowed + } } } } diff --git a/lib/Bot/BB3/Roles/IRC.pm b/lib/Bot/BB3/Roles/IRC.pm index ce1b059..23b3607 100644 --- a/lib/Bot/BB3/Roles/IRC.pm +++ b/lib/Bot/BB3/Roles/IRC.pm @@ -500,6 +500,16 @@ sub external_message { } } +sub get_server_conf { + my( $self, $input_pci_id) = @_[OBJECT,ARG0]; + + for my $pci_id ( keys %{ $self->{bot_confs} } ) { + my $conf = $self->{bot_confs}->{$pci_id}; + my $poco_irc = $self->get_component($pci_id); + + } +} + sub channel_list { my( $self, $kernel, $sender ) = @_[OBJECT,KERNEL,SENDER]; diff --git a/plugins/allowpaste.pm b/plugins/allowpaste.pm new file mode 100644 index 0000000..f55fb6c --- /dev/null +++ b/plugins/allowpaste.pm @@ -0,0 +1,90 @@ +package Bot::BB3::Plugin::Allowpaste; +use POE::Component::IRC::Common qw/l_irc/; +use DBD::SQLite; +use strict; + +sub new { + my( $class ) = @_; + my $self = bless {}, $class; + $self->{name} = "allowpaste"; + $self->{opts} = { + command => 1, + }; + + return $self; +} + +sub dbh { + my( $self ) = @_; + + if( $self->{dbh} and $self->{dbh}->ping ) { + return $self->{dbh}; + } + + my $dbh = $self->{dbh} = DBI->connect( "dbi:SQLite:dbname=var/allowpaste.db", "", "", { PrintError => 0, RaiseError => 1 } ); + + return $dbh; +} +sub postload { + my( $self, $pm ) = @_; + + + my $sql = "CREATE TABLE allowpaste ( + channel VARCHAR(255) NOT NULL UNIQUE, + value INTEGER NOT NULL, + setby VARCHAR(255) NOT NULL, + set_date INTEGER NOT NULL + ); + + "; + + $pm->create_table( $self->dbh, "allowpaste", $sql ); + + delete $self->{dbh}; # UGLY HAX GO. + # Basically we delete the dbh we cached so we don't fork + # with one active +} + +sub get_status { + my ($self, $chancon) = @_; + + my $status = $self->dbh->selectrow_hashref('SELECT value FROM allowpaste WHERE channel = ?', {}, $chancon); + + return ($status // {})->{value}; +} + +sub set_status { + my ($self, $chancon, $setting, $who) = @_; + + if (defined $self->get_status($chancon)) { + $self->dbh->do('UPDATE allowpaste SET value = ?, setby = ?, set_date = ? WHERE channel = ?', {}, $setting eq 'on' ? 1 : 0, $who, time(), $chancon); + } else { + $self->dbh->do('INSERT INTO allowpaste (channel, value, setby, set_date) VALUES (?, ?, ?, ?)', {}, $chancon, $setting eq 'on' ? 1 : 0, $who, time()); + } +} + +sub command { + my( $self, $said, $pm ) = @_; + my( $set_to ) = @{ $said->{recommended_args} }; + + my $server_conf = $pm->{bb3}{'Bot::BB3::Roles::IRC'}{bot_confs}{$said->{pci_id}}; + my ($botname, $servername) = @{$server_conf}{qw/botname server/}; + my $channel = $said->{channel}; + + my $chanconstruct = "$servername:$botname:$channel"; + + if ($set_to && (lc($set_to) eq 'on' || lc($set_to) eq 'off')) { + $self->set_status($chanconstruct, $set_to, $said->{name}); + return('handled', "This channel has pastebin set [$set_to]"); + } else { + my $status = $self->get_status($chanconstruct)//1 ? 'on' : 'off'; + return('handled', "This channel has pastebin set to [$status] :: $chanconstruct"); + } +} + +no warnings 'void'; +"Bot::BB3::Plugin::Allowpaste"; + +__DATA__ +The allowpaste plugin. Lets operators disable pastes being announced in the channel. allowpaste [on|off] => Tell you the state, or turn it on or off. + diff --git a/plugins/translate.pm b/plugins/translate.pm index cf9a8f7..62760f1 100644 --- a/plugins/translate.pm +++ b/plugins/translate.pm @@ -1,6 +1,6 @@ use strict; use warnings; -use Bing::Translate; +#use Bing::Translate;