POE database is GONE

This commit is contained in:
Ryan Voots 2016-09-22 18:05:17 -07:00
parent 4f27002c2c
commit 88a8951539
5 changed files with 262 additions and 344 deletions

1
bot.pl
View file

@ -19,7 +19,6 @@ BEGIN {
};
use IRC;
use PoeDB;
use BotSync;
use MangaParse;
use DB;

View file

@ -14,6 +14,8 @@ use POSIX;
use Epiquote; #woot! spun off, contains the code for updating episode quotes, i only need to call Epiquote::doupdate with an eid
use POE qw(Component::Client::HTTP Component::Client::Keepalive);
use DB;
my $schema = DB->new();
my $pool = POE::Component::Client::Keepalive->new(
keep_alive => 10, # seconds to keep connections alive
@ -273,11 +275,11 @@ sub setepisodes {
next if ($epinum eq "");
die "Can't update episode $epinum if its undef: " . Dumper($episode) if (!defined($epinum));
$kernel->post(DB => updatebotlist => $epinum, $bot) if ($bot !~ /^\s*$/);
$kernel->post(DB => updatecrc => $epinum, $crc) if ($crc !~ /^\s*$/);
$kernel->post(DB => updategroup => $epinum, $group) if ($group !~ /^\s*$/);
$kernel->post(DB => updatesize => $epinum, $size) if ($size !~ /^\s*$/);
$kernel->post(DB => updatemu => $epinum, $mu) if ($mu !~ /^\s*$/);
$schema->updatebotlist($epinum, $bot) if ($bot !~ /^\s*$/);
$schema->updatecrc($epinum, $crc) if ($crc !~ /^\s*$/);
$schema->updategroup($epinum, $group) if ($group !~ /^\s*$/);
$schema->updatesize($epinum, $size) if ($size !~ /^\s*$/);
$schema->updatemu($epinum, $mu) if ($mu !~ /^\s*$/);
#print "Setting episode $epinum :: $bot :: $crc :: $group\n";
push @updated, $epinum;

237
lib/DB.pm
View file

@ -6,17 +6,23 @@ use BotConfig;
use Carp qw/confess cluck/;
my $dbconf = BotConfig->instance->db;
my $schema = DB::Schema->connect("dbi:Pg:dbname=" . $dbconf->database . ";host=" . $dbconf->host,
my $self = DB::Schema->connect("dbi:Pg:dbname=" . $dbconf->database . ";host=" . $dbconf->host,
$dbconf->username, $dbconf->password, { RaiseError => 1 });
has schema => (
is => 'ro',
default => sub {
return $schema;
return $self;
},
isa => 'DB::Schema',
);
sub _quote_to_ret {
my $row = shift;
return map { $row->$_ } qw/quote who qid desc sid/;
}
sub source {
return $_[0]->schema->resultset('Source');
}
@ -49,4 +55,231 @@ sub episodeinfo {
return $_[0]->schema->resultset('Episodeinfo');
}
sub addquote {
my ($self, $where, $who, $source, $quote) = @_;
my $source_row = $self->source->search({ name => uc $source })->first;
if ($source_row) {
my $counter_row = $self->quote->search({ sid => $source_row->sid },
{ 'select' => [ { 'AVG' => 'counter' } ], 'as' => ['newcount'], 'group_by' => ['me.sid'] })->first;
my $counter = $counter_row ? int($counter_row->get_column('newcount')) || 0 : 0;
my $quote_row = $self->quote->find_or_create({ quote => $quote, sid => $source_row->sid, counter => $counter, who => $who });
}
else {
return "No such source: $source";
}
}
sub getstrip {
my ($self, $channel) = @_;
my $row = $self->channelsetup->search({ channel => lc $channel })->first;
return !$row
? 0
: $row->stripcolors();
}
sub getbyqid {
my ($self, $where, $who, $qid) = @_;
my $row = $self->quotelist->search({ qid => $qid })->first;
if (!$row) {
return "Unable to find quote with $qid, are you sure it exists?";
}
else {
# TODO swap this to local call, rather than through POE
my $banned = $self->isbannedsource($where->[0], $row->sid);
if ($banned) {
return "Unable to find quote with $qid, are you sure it exists?";
}
else {
# TODO this should be a nicer return value
return _quote_to_ret($row);
}
}
}
sub getcid {
my ($self, $where) = @_;
my $row = $self->channelsetup->search({ channel => lc $where })->first;
return $row ? $row->cid : undef;
}
sub isbannedsource {
my ($self, $where, $sid) = @_;
my $row = $self->bansource->search({ 'me.sid' => $sid, 'channelsetup.channel' => lc $where }, { join => 'channelsetup' })->first;
return $row ? 1 : 0;
}
sub deletequote {
my ($self, $where, $who, $qid) = @_;
$self->quote->find({ qid => $qid })->update({ deleted => 1 });
}
sub addsource {
my ($self, $where, $who, $source, $desc) = @_;
my $row = $self->source->find_or_create({ name => uc $source, desc => $desc });
}
sub addtrigger {
my ($self, $where, $who, $trig) = @_;
my $row = $self->trigger->find_or_create({ trigger => $trig });
}
sub getquote {
my ($self, $what, $where) = @_;
$self->trigger->search({ trigger => uc $what })->update({ counter => \'counter + 1' });
# grab the bottom 15 rows by counter+random(), do the final selection in perl
my @rows =
$self->quotelist->search({ trigger => uc $what }, { order_by => { '-asc' => [ 'me.counter', \'RANDOM()' ] }, rows => 15 })->all;
my $final_row = $rows[ rand @rows ];
return "" unless $final_row;
my $qid = $final_row->qid;
my $quote_row = $self->quote->find({ qid => $qid });
$quote_row->update({ counter => \'counter + 1' });
my $banned = $self->isbannedsource($where->[0], $final_row->sid);
if ($banned) {
print "GETQUOTE: failed check\n";
return ("", "", 0, "");
}
else {
return _quote_to_ret($final_row);
}
}
sub getlastquote {
my ($self) = @_;
my $row = $self->quotelist->search({}, { order_by => { '-desc' => 'me.qid' } })->first;
return _quote_to_ret($row) if $row;
return ("", "", 0, "");
}
sub listtriggers {
my ($self, $what) = @_;
my @triggers = map { uc $_->trigger } $self->trigger->search({ tid => { '>' => 0 } })->all;
my $string = join ', ', @triggers;
return $string;
}
sub listsources {
my ($self, $what) = @_;
my @sources = map { uc $_->name } $self->source->search({ sid => { '>' => 0 } })->all;
my $string = join ', ', @sources;
return $string;
}
sub whatistrigger {
my ($self, $where, $who, $what) = @_;
my $trig = $self->trigger->search({ trigger => uc $what })->first;
unless ($trig) { #no rows found
return "Unable to find trigger, $what";
}
my @sources = map { $_->source } $trig->tidsources->all;
my $string = join ', ', map { $_->name } @sources;
return $string;
}
sub appendtotrigger {
my ($self, $where, $who, $source, $trigger) = @_;
my $source_row = $self->source->find({ name => uc $source });
my $trigger_row = $self->trigger->find({ trigger => uc $trigger });
unless ($source_row && $source_row->sid > 0) {
return "Unable to find source, $source";
}
unless ($trigger_row && $trigger_row->tid > 0) {
return "Unable to find trigger, $trigger";
}
# add the source to the trigger
$trigger_row->tidsources->create({ sid => $source_row->sid });
}
sub removefromtrigger {
my ($self, $where, $who, $source, $trigger) = @_;
my $source_row = $self->source->find({ name => uc $source });
my $trigger_row = $self->trigger->find({ trigger => uc $trigger });
unless ($source_row && $source_row->sid > 0) {
return "Unable to find source, $source";
}
unless ($trigger_row && $trigger_row->tid > 0) {
return "Unable to find trigger, $trigger";
}
$trigger_row->tidsources->search({ sid => $source_row->sid })->delete();
}
sub updatebotlist {
my ($self, $eid, $botlist) = @_;
$self->episodeinfo->search({ eid => $eid })->update({ botlisting => $botlist });
}
sub updatecrc {
my ($self, $eid, $crc) = @_;
$self->episodeinfo->search({ eid => $eid })->update({ fcrc => $crc });
}
sub updategroup {
my ($self, $eid, $group) = @_;
$self->episodeinfo->search({ eid => $eid })->update({ group => $group });
}
sub updatesize {
my ($self, $eid, $size) = @_;
$self->episodeinfo->search({ eid => $eid })->update({ fsize => $size });
}
sub updatemu {
my ($self, $eid, $mu) = @_;
$self->episodeinfo->search({ eid => $eid })->update({ filelink => $mu });
}
sub getignoredmsg {
my ($self, $nick) = @_;
my $row = $self->ignore->search({ nick => lc $nick })->first;
return $row ? $row->message : undef;
}
sub isignored {
my ($self, $nick) = @_;
my $row = $self->ignore->search({ nick => lc $nick })->first;
return !!$row;
}
1;

View file

@ -19,6 +19,9 @@ use POE qw(Component::IRC::State
use Data::Dumper;
use POE::Component::IRC::Common qw(:ALL);
use DB;
my $schema = DB->new(); # connect to DB
my $password;
$password = $ARGV[0] if @ARGV;
@ -112,7 +115,7 @@ sub tock {
my $channel = $payload->[0];
my $msg = $payload->[1];
my $strip = $kernel->call("DB", "getstrip", $channel);
my $strip = $schema->getstrip($channel);
#print Dumper($strip);
@ -138,13 +141,13 @@ sub _userignore {
sub _ignore {
my $who = shift;
my $kernel = shift;
return $kernel->call("DB", isignored => $who);
return $schema->isignored($who);
}
sub _ignoremsg {
my $who = shift;
my $kernel = shift;
return $kernel->call("DB", getignoredmsg => $who);
return $schema->getignoredmsg($who);
}
sub irc_public {
@ -179,11 +182,11 @@ sub irc_public {
print STDERR "GETQUOTE: $what ", $where->[0], "\n";
my @quote = $kernel->call("DB", "getquote", $what . $where->[0], $where);
my @quote = $schema->getquote($what . $where->[0], $where);
print STDERR "\t", @quote, "\n";
@quote = $kernel->call("DB", "getquote", $what, $where) unless $quote[0];
@quote = $schema->getquote($what, $where) unless $quote[0];
print STDERR "\t", @quote, "\n";
return "" unless $quote[0]; #don't do anything if we don't get anything back!
$response = doformat(@quote[ 0 .. 3 ], $nick);
@ -211,45 +214,45 @@ sub doadmin {
#I suppose this COULD be more modular
if ($what =~ /^!addquote\s+(\S+)\s+(.*)$/) {
$kernel->post("DB", addquote => $where, $who, $1, $2);
$schema->addquote($where, $who, $1, $2);
return 1;
}
elsif ($what =~ /^!deletequote\s+(?:#\s*)?(\d+)$/) {
$kernel->post("DB", deletequote => $where, $who, $1);
$schema->deletequote($where, $who, $1);
return 1;
}
elsif ($what =~ /^!getquote\s+(?:#\s*)?(\d+)$/) {
my @q = $kernel->call("DB", getbyqid => $where, $who, $1);
my @q = $schema->getbyqid($where, $who, $1);
return doformat(@q[ 0 .. 3 ], $who);
}
elsif ($what =~ /^!getlastquote\s*$/) {
my @q = $kernel->call("DB", "getlastquote");
my @q = $schema->getlastquote();
return doformat(@q[ 0 .. 3 ], $who);
}
elsif ($what =~ /^!addsource\s+(\S+)\s*(\S.*)?$/) {
$kernel->post("DB", addsource => $where, $who, $1, $2);
$schema->addsource($where, $who, $1, $2);
return 1;
}
elsif ($what =~ /^!addtrigger\s+(\S+)$/) {
$kernel->post("DB", addtrigger => $where, $who, $1);
$schema->addtrigger($where, $who, $1);
return 1;
}
elsif ($what =~ /^!addsourcetotrigger\s+(\S+)\s+(\S+)$/) {
$kernel->post("DB", appendtotrigger => $where, $who, $2, $1);
$schema->appendtotrigger($where, $who, $2, $1);
return 1;
}
elsif ($what =~ /^!removesourcefromtrigger\s+(\S+)\s+(\S+)$/) {
$kernel->post("DB", removefromtrigger => $where, $who, $1, $2);
$schema->removefromtrigger($where, $who, $1, $2);
return 1;
}
elsif ($what =~ /^!showsources$/) {
return "/notice $who " . $kernel->call("DB", listsources => $where, $who);
return "/notice $who " . $schema->listsources($where, $who);
}
elsif ($what =~ /^!showtriggers$/) {
return "/notice $who " . $kernel->call("DB", listtriggers => $where, $who);
return "/notice $who " . $schema->listtriggers($where, $who);
}
elsif ($what =~ /^!whatistrigger\s+(\S+)$/) {
return "/notice $who " . $kernel->call("DB", whatistrigger => $where, $who, $1);
return "/notice $who " . $schema->whatistrigger($where, $who, $1);
}
elsif ($what =~ /^!xdccupdate/) {
$kernel->post("BotSync", "startupdate", $who, $where);
@ -363,7 +366,7 @@ If you have further questions PM simcop2387, he'll answer them eventually, also
@lines = messagebreak($response);
}
else {
my @quote = $kernel->call("DB", "getquote", $what, [$nick]);
my @quote = $schema->getquote($what, [$nick]);
return "" unless $quote[0]; #don't do anything if we don't get anything back!
$response = doformat(@quote[ 0 .. 3 ], $nick);

View file

@ -1,319 +0,0 @@
package PoeDB;
use strict;
use warnings;
use Data::Dumper;
use DBI;
use POE qw(Session);
use POE::Component::IRC::Common qw(:ALL);
use BotConfig;
use DB;
my $dbconf = BotConfig->instance->db;
my $schema = DB->new();
my $ses = POE::Session->create(
package_states => [
PoeDB => [
qw(_start addquote addtrigger addsource appendtotrigger getquote removefromtrigger listsources listtriggers whatistrigger deletequote getbyqid getstrip getlastquote ping updatebotlist updatecrc updategroup updatesize isignored getignoredmsg updatemu getcid isbannedsource)
],
],
);
my %sth;
sub _start {
my ($kernel, $heap) = @_[ KERNEL, HEAP ];
$kernel->alias_set("DB");
$kernel->delay_add("ping", 1);
}
sub _quote_to_ret {
my $row = shift;
return map { $row->$_ } qw/quote who qid desc sid/;
}
sub ping {
my $kernel = $_[KERNEL];
$kernel->delay_add("ping", 10);
# print "DB PING\n";
}
sub addquote {
my ($kernel, $heap, $where, $who, $source, $quote) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2, ARG3 ];
my $source_row = $schema->source->search({ name => uc $source })->first;
if ($source_row) {
my $counter_row = $schema->quote->search({ sid => $source_row->sid },
{ 'select' => [ { 'AVG' => 'counter' } ], 'as' => ['newcount'], 'group_by' => ['me.sid'] })->first;
my $counter = $counter_row ? int($counter_row->get_column('newcount')) || 0 : 0;
my $quote_row = $schema->quote->find_or_create({ quote => $quote, sid => $source_row->sid, counter => $counter, who => $who });
}
else {
$kernel->post("IRCHANDLER", msgfromdb => $who, $where, "No such source: $source");
}
}
sub getstrip {
my ($kernel, $heap, $channel) = @_[ KERNEL, HEAP, ARG0 ];
my $row = $schema->channelsetup->search({ channel => lc $channel })->first;
return !$row
? 0
: $row->stripcolors();
}
sub getbyqid {
my ($kernel, $heap, $where, $who, $qid) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2 ];
my $row = $schema->quotelist->search({ qid => $qid })->first;
if (!$row) {
$kernel->post("IRCHANDLER", msgfromdb => $who, $where, "Unable to find quote with $qid, are you sure it exists?");
return ("", "", 0, "");
}
else {
# TODO swap this to local call, rather than through POE
my $banned = $kernel->call(DB => isbannedsource => $where->[0], $row->sid);
if ($banned) {
$kernel->post("IRCHANDLER", msgfromdb => $who, $where, "Unable to find quote with $qid, are you sure it exists?");
return ("", "", 0, "");
}
else {
# TODO this should be a nicer return value
return _quote_to_ret($row);
}
}
}
sub getcid {
my ($kernel, $heap, $where) = @_[ KERNEL, HEAP, ARG0 ];
my $row = $schema->channelsetup->search({ channel => lc $where })->first;
return $row ? $row->cid : undef;
}
sub isbannedsource {
my ($kernel, $heap, $where, $sid) = @_[ KERNEL, HEAP, ARG0, ARG1 ];
my $row = $schema->bansource->search({ 'me.sid' => $sid, 'channelsetup.channel' => lc $where }, { join => 'channelsetup' })->first;
return $row ? 1 : 0;
}
sub deletequote {
my ($kernel, $heap, $where, $who, $qid) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2 ];
$schema->quote->find({ qid => $qid })->update({ deleted => 1 });
}
sub addsource {
my ($kernel, $heap, $where, $who, $source, $desc) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2, ARG3 ];
my $row = $schema->source->find_or_create({ name => uc $source, desc => $desc });
}
sub addtrigger {
my ($kernel, $heap, $where, $who, $trig) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2 ];
my $row = $schema->trigger->find_or_create({ trigger => $trig });
}
sub getquote {
my ($kernel, $heap, $what, $where) = @_[ KERNEL, HEAP, ARG0, ARG1 ];
$schema->trigger->search({ trigger => uc $what })->update({ counter => \'counter + 1' });
# grab the bottom 15 rows by counter+random(), do the final selection in perl
my @rows =
$schema->quotelist->search({ trigger => uc $what }, { order_by => { '-asc' => [ 'me.counter', \'RANDOM()' ] }, rows => 15 })->all;
my $final_row = $rows[ rand @rows ];
return "" unless $final_row;
my $qid = $final_row->qid;
my $quote_row = $schema->quote->find({ qid => $qid });
$quote_row->update({ counter => \'counter + 1' });
my $banned = $kernel->call(DB => isbannedsource => $where->[0], $final_row->sid);
if ($banned) {
print "GETQUOTE: failed check\n";
return ("", "", 0, "");
}
else {
return _quote_to_ret($final_row);
}
}
sub getlastquote {
my ($kernel, $heap) = @_[ KERNEL, HEAP ];
my $row = $schema->quotelist->search({}, { order_by => { '-desc' => 'me.qid' } })->first;
return _quote_to_ret($row) if $row;
return ("", "", 0, "");
}
sub listtriggers {
my ($kernel, $heap, $what) = @_[ KERNEL, HEAP, ARG0 ];
my @triggers = map { uc $_->trigger } $schema->trigger->search({ tid => { '>' => 0 } })->all;
my $string = join ', ', @triggers;
return $string;
}
sub listsources {
my ($kernel, $heap, $what) = @_[ KERNEL, HEAP, ARG0 ];
my @sources = map { uc $_->name } $schema->source->search({ sid => { '>' => 0 } })->all;
my $string = join ', ', @sources;
return $string;
}
sub whatistrigger {
my ($kernel, $heap, $where, $who, $what) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2 ];
my $trig = $schema->trigger->search({ trigger => uc $what })->first;
unless ($trig) { #no rows found
$kernel->post("IRCHANDLER", msgfromdb => $who, $where, "Unable to find trigger, $what");
return;
}
my @sources = map { $_->source } $trig->tidsources->all;
my $string = join ', ', map { $_->name } @sources;
return $string;
}
sub appendtotrigger {
my ($kernel, $heap, $where, $who, $source, $trigger) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2, ARG3 ];
my $source_row = $schema->source->find({ name => uc $source });
my $trigger_row = $schema->trigger->find({ trigger => uc $trigger });
unless ($source_row && $source_row->sid > 0) {
$kernel->post("IRCHANDLER", msgfromdb => $who, $where, "Unable to find source, $source");
return "";
}
unless ($trigger_row && $trigger_row->tid > 0) {
$kernel->post("IRCHANDLER", msgfromdb => $who, $where, "Unable to find trigger, $trigger");
return "";
}
# add the source to the trigger
$trigger_row->tidsources->create({ sid => $source_row->sid });
}
sub removefromtrigger {
my ($kernel, $heap, $where, $who, $source, $trigger) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2, ARG3 ];
my $source_row = $schema->source->find({ name => uc $source });
my $trigger_row = $schema->trigger->find({ trigger => uc $trigger });
unless ($source_row && $source_row->sid > 0) {
$kernel->post("IRCHANDLER", msgfromdb => $who, $where, "Unable to find source, $source");
return "";
}
unless ($trigger_row && $trigger_row->tid > 0) {
$kernel->post("IRCHANDLER", msgfromdb => $who, $where, "Unable to find trigger, $trigger");
return "";
}
$trigger_row->tidsources->search({ sid => $source_row->sid })->delete();
}
sub updatebotlist {
my ($kernel, $heap, $eid, $botlist) = @_[ KERNEL, HEAP, ARG0, ARG1 ];
$schema->episodeinfo->search({ eid => $eid })->update({ botlisting => $botlist });
}
sub updatecrc {
my ($kernel, $heap, $eid, $crc) = @_[ KERNEL, HEAP, ARG0, ARG1 ];
$schema->episodeinfo->search({ eid => $eid })->update({ fcrc => $crc });
}
sub updategroup {
my ($kernel, $heap, $eid, $group) = @_[ KERNEL, HEAP, ARG0, ARG1 ];
$schema->episodeinfo->search({ eid => $eid })->update({ group => $group });
}
sub updatesize {
my ($kernel, $heap, $eid, $size) = @_[ KERNEL, HEAP, ARG0, ARG1 ];
$schema->episodeinfo->search({ eid => $eid })->update({ fsize => $size });
}
sub updatemu {
my ($kernel, $heap, $eid, $mu) = @_[ KERNEL, HEAP, ARG0, ARG1 ];
$schema->episodeinfo->search({ eid => $eid })->update({ filelink => $mu });
}
sub getignoredmsg {
my ($kernel, $heap, $nick) = @_[ KERNEL, HEAP, ARG0 ];
my $row = $schema->ignore->search({ nick => lc $nick })->first;
return $row ? $row->message : undef;
}
sub isignored {
my ($kernel, $heap, $nick) = @_[ KERNEL, HEAP, ARG0 ];
my $row = $schema->ignore->search({ nick => lc $nick })->first;
return !!$row;
}
=begin comment
TRIGGERS
-------------- 1
TID | TRIGGER
TIDSOURCES
---------- many -> many
TID | SID
SOURCES
---------- 1
SID | NAME
QUOTES
----------------- many -> 1
QID | SID | QUOTE
=end comment
=cut
##getquote
####Assume we have a TID already
#
#SELECT quotes.quote, sources.name FROM triggers, tidsources, sources, quotes WHERE triggers.trigger = ? AND tidsources.tid = triggers.tid AND tidsources.sid = sources.sid AND tidsources.sid = quotes.sid ORDER BY quotes.qid;
#SELECT quote FROM list WHERE trigger = ?; #VIEWS ARE AWSOME
##addsourcetotrigger
#AB: INSERT INTO tidsources (tid, sid) VALUES (TID, SID)
#
##addtrigger
##AB: INSERT INTO triggers (trigger) VALUES (TEXT)
# INSERT INTO triggers (trigger) VALUES (?)
##addsource
#AB: INSERT INTO sources (name) VALUES (NAME)
#INSERT INTO sources (name) VALUES (?)
##addquote
#AB: INSERT INTO quotes (source, text) VALUES (SID, TEXT)
#INSERT INTO quotes (source, text) VALUES (?, ?)
#
1;