diff --git a/PlayBot/sites.pm b/PlayBot/sites.pm index 5f7d04ee9f6de2b7e6825c902f6d8f0166b7eed6..3cf9d9a93de25f48cc1d37af1f18e8efe4d5a3bb 100644 --- a/PlayBot/sites.pm +++ b/PlayBot/sites.pm @@ -6,6 +6,7 @@ use warnings; use Module::Pluggable sub_name => 'sites', search_path => ['PlayBot::sites'], require => 1; use PlayBot::utils::db; +use PlayBot::utils::db::chan; use PlayBot::utils::print; use PlayBot::commands::parser; @@ -18,6 +19,8 @@ sub parse { my ($kernel, $user, $chan, $msg) = @_; my ($nick,$mask) = split(/!/,$user); + my $chan_conf = PlayBot::utils::db::chan->new($chan); + my %content; my $id; my $dbh = PlayBot::utils::db::main_session(); @@ -28,6 +31,8 @@ sub parse { # parsing foreach my $site (__PACKAGE__->sites) { + not grep { $site eq "PlayBot::sites::$_" } @{ $chan_conf->sites } and next; + if (my @args = ($msg =~ $site->regex)) { eval { %content = $site->get(@args) }; diff --git a/PlayBot/utils/db/chan.pm b/PlayBot/utils/db/chan.pm new file mode 100644 index 0000000000000000000000000000000000000000..f62c4ab645a44c87307cba968c07799303631cdb --- /dev/null +++ b/PlayBot/utils/db/chan.pm @@ -0,0 +1,103 @@ +package PlayBot::utils::db::chan; + +use strict; +use warnings; + +use JSON; +use Moose; + +use PlayBot::utils::db; + + +has 'name' => (is => 'ro', isa => 'Str'); +has 'sites' => (is => 'ro', isa => 'ArrayRef[Str]', writer => '_set_sites'); + + +around 'BUILDARGS' => sub +{ + my ($orig, $class, $name) = @_; + + not $name and die "missing arg 'name'"; + + my $dbh = PlayBot::utils::db::main_session; + my $sth = $dbh->prepare(' + select sites + from playbot_config + where name = ? + '); + $sth->execute($name); + + my $sites; + my $row = $sth->fetch; + + if (not $row) + { + $sites = ['dailymotion', 'mixcloud', 'soundcloud', 'youtube']; + + $sth = $dbh->prepare(' + insert into playbot_config (name, sites) + values (?, ?) + '); + $sth->execute($name, encode_json($sites)); + } + else + { + eval + { + $sites = decode_json($row->[0]); + }; + + $@ and $sites = []; + } + + $dbh->commit; + + return $class->$orig( + name => $name, + sites => $sites, + ); +}; + + +sub add_site +{ + my $self = shift; + my $site = shift; + + my @new_sites = (@{ $self->sites }, $site); + + $self->_update_db(\@new_sites); + $self->_set_sites(\@new_sites); +} + + +sub remove_site +{ + my $self = shift; + my $site = shift; + + my @new_sites = grep { $_ ne $site } @{ $self->sites }; + + $self->_update_db(\@new_sites); + $self->_set_sites(\@new_sites); +} + + +sub _update_db +{ + my $self = shift; + my $sites = shift; + + my $dbh = PlayBot::utils::db::main_session; + my $sth = $dbh->prepare(' + update playbot_config + set site = ? + '); + $sth->execute(encode_json($sites)); + + $dbh->commit; +} + + + +1;