From 371927123d8dc3716d12ca3a492a18978f536600 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Wed, 9 Dec 2015 22:47:45 +0100
Subject: [PATCH] Add !conf command

---
 PlayBot/commands/conf.pm   | 104 +++++++++++++++++++++++++++++++++++++
 PlayBot/commands/parser.pm |  18 ++++++-
 PlayBot/sessions/irc.pm    |   4 +-
 PlayBot/utils/db/chan.pm   |   2 +-
 4 files changed, 123 insertions(+), 5 deletions(-)
 create mode 100644 PlayBot/commands/conf.pm

diff --git a/PlayBot/commands/conf.pm b/PlayBot/commands/conf.pm
new file mode 100644
index 0000000..b2ab650
--- /dev/null
+++ b/PlayBot/commands/conf.pm
@@ -0,0 +1,104 @@
+package PlayBot::commands::conf;
+
+use strict;
+use warnings;
+
+use Module::Pluggable sub_name => 'sites', search_path => ['PlayBot::sites'], require => 1;
+
+use PlayBot::utils::print;
+use PlayBot::utils::db;
+use PlayBot::utils::db::chan;
+
+our $irc;
+
+sub exec
+{
+	my ($chan, $nick, $cmd, @args) = @_;
+    print "$chan, $nick, $cmd, ".join('+', @args)."\n";
+    my $msg = 'done';
+
+    if (not $irc->is_channel_operator($chan, $nick))
+    {
+        $irc->yield(privmsg => $chan => "C'est non.");
+        return;
+    }
+
+    if ($cmd eq 'list')
+    {
+        my $sites = list($chan);
+        my @list;
+
+        foreach (keys %$sites)
+        {
+            $_ .= '*' if ($sites->{$_});
+
+            push @list, $_;
+        }
+
+        $msg = join(' ', @list);
+    }
+    elsif ($cmd eq 'add')
+    {
+        add($chan, @args);
+    }
+    elsif ($cmd eq 'remove')
+    {
+        remove($chan, @args);
+    }
+    else
+    {
+        die;
+    }
+
+    $irc->yield(privmsg => $chan => $msg);
+}
+
+
+sub list
+{
+    my $chan = shift;
+
+    my $chan_conf = PlayBot::utils::db::chan->new($chan);
+    my $sites = {};
+
+    foreach my $site (__PACKAGE__->sites)
+    {
+        $site = (split(/::/, $site))[-1];
+        if (grep { $site eq $_ } @{ $chan_conf->sites })
+        {
+            $sites->{$site} = 1;
+        }
+        else
+        {
+            $sites->{$site} = 0;
+        }
+    }
+
+    return $sites;
+}
+
+
+sub add
+{
+    my ($chan, @sites) = @_;
+    my $chan_conf = PlayBot::utils::db::chan->new($chan);
+
+    foreach (@sites)
+    {
+        $chan_conf->add_site($_);
+    }
+}
+
+
+sub remove
+{
+    my ($chan, @sites) = @_;
+    my $chan_conf = PlayBot::utils::db::chan->new($chan);
+
+    foreach (@sites)
+    {
+        $chan_conf->remove_site($_);
+    }
+}
+
+1;
diff --git a/PlayBot/commands/parser.pm b/PlayBot/commands/parser.pm
index 35c9760..5793e38 100644
--- a/PlayBot/commands/parser.pm
+++ b/PlayBot/commands/parser.pm
@@ -4,6 +4,8 @@ use strict;
 use warnings;
 use Try::Tiny;
 
+use PlayBot::commands::conf;
+use PlayBot::commands::stats;
 use PlayBot::commands::fav;
 use PlayBot::commands::later;
 use PlayBot::commands::tag;
@@ -34,6 +36,7 @@ sub setConf {
     $PlayBot::commands::later::log = $log;
     $PlayBot::utils::id::log = $log;
 
+    $PlayBot::commands::conf::irc = $ircNew;
     $PlayBot::commands::fav::irc = $ircNew;
     $PlayBot::commands::get::irc = $ircNew;
     $PlayBot::commands::stats::irc = $ircNew;
@@ -108,6 +111,10 @@ sub exec {
             $irc->yield(privmsg => $chan => $insultes[rand @insultes]);
         }
 	}
+    elsif ($msg =~ /^ *!conf +(.*)/) {
+            my @args = split(/ +/, $1);
+            PlayBot::commands::conf::exec($chan, $nick, @args);
+    }
     elsif ($msg =~ /^ *!help/) {
 		$irc->yield(privmsg => $nick => '!fav [<id>] : enregistre la vidéo dans les favoris');
 		$irc->yield(privmsg => $nick => '!tag [<id>] <tag1> <tag2> ... : tag la vidéo');
@@ -117,9 +124,16 @@ sub exec {
 		$irc->yield(privmsg => $nick => '!get [<id>|<query>] : sort aléatoirement une vidéo');
 		$irc->yield(privmsg => $nick => '    Si un id est précisé, sort ce contenu (s\'il existe).');
 		$irc->yield(privmsg => $nick => '    <query> : composée de tags commençant par un \'#\' ou de mots. Les mots sont recherché dans le titre ainsi que le nom de l\'auteur du contenu.');
+        $irc->yield(privmsg => $nick => "!stats [<id>] : informations sur un contenu");
+        $irc->yield(privmsg => $nick => "---");
 		$irc->yield(privmsg => $nick => "Un tag est de la forme « #[a-zA-Z0-9_]+ ». Par exemple « #loLILol_mdr42 » est un tag valide, tandis que « #céducaca » et « #je-suis-nul » n'en sont pas et seront considéré respectivement comme « #c » et « #je ».");
-        $irc->yield(privmsg => $nick => "Toutes les commandes fonctionnent en query.");
-        $irc->yield(privmsg => $nick => 'Niveau vie privée, potentiellement toute commande (excepté !help) entraine un enregistrement dans la base de données avec au minimum la date et l\'heure et le nick de la personne ayant exécuté la commande.');
+        $irc->yield(privmsg => $nick => "Toutes ces commandes fonctionnent en query.");
+        $irc->yield(privmsg => $nick => "---");
+        $irc->yield(privmsg => $nick => "Configuration d'un channel, utilisable uniquement par un op :");
+        $irc->yield(privmsg => $nick => "   !conf list : liste les sites supportés, ceux indiqués avec une astérisque étant activés");
+        $irc->yield(privmsg => $nick => "   !conf add|remove <site> : active ou désactive le support d'un site");
+        $irc->yield(privmsg => $nick => "---");
+        $irc->yield(privmsg => $nick => "Niveau vie privée, !fav, !later, et bien sûr poster un lien supporté entrainent un enregistrement dans la base de données avec au minimum la date, l'heure et le nick de la personne ayant exécuté la commande.");
     }
     elsif ($msg =~/^ *!(fav|lat|tag|stats)/) {
         $irc->yield(privmsg => $chan => $insultes[rand @insultes]);
diff --git a/PlayBot/sessions/irc.pm b/PlayBot/sessions/irc.pm
index dcadc25..b5147d4 100644
--- a/PlayBot/sessions/irc.pm
+++ b/PlayBot/sessions/irc.pm
@@ -5,7 +5,7 @@ use warnings;
 
 use DBI;
 use POE;
-use POE::Component::IRC;
+use POE::Component::IRC::State;
 use POSIX 'strftime';
 use Tie::File;
 use JSON;
@@ -24,7 +24,7 @@ my $json = <CONF>;
 my $conf = decode_json($json);
 
 ## CONNEXION 
-my ($irc) = POE::Component::IRC->spawn();
+my ($irc) = POE::Component::IRC::State->spawn();
 my $dbh = DBI->connect('DBI:mysql:'.$conf->{'bdd'}.';host='.$conf->{'host'}, $conf->{'user'}, $conf->{'passwd'}, {
 	    PrintError => 0,
 	    AutoCommit => 1,
diff --git a/PlayBot/utils/db/chan.pm b/PlayBot/utils/db/chan.pm
index f62c4ab..b37c777 100644
--- a/PlayBot/utils/db/chan.pm
+++ b/PlayBot/utils/db/chan.pm
@@ -91,7 +91,7 @@ sub _update_db
     my $dbh = PlayBot::utils::db::main_session;
     my $sth = $dbh->prepare('
         update playbot_config
-        set site = ?
+        set sites = ?
     ');
     $sth->execute(encode_json($sites));
 
-- 
GitLab