package PlayBot::commands::parser; use strict; use warnings; use utf8; use Try::Tiny; use PlayBot::commands::conf; use PlayBot::commands::fav; use PlayBot::commands::later; use PlayBot::commands::tag; use PlayBot::commands::get; use PlayBot::commands::broken; use PlayBot::commands::stats; use PlayBot::utils::id; my $lastID; my $irc; my $dbh; my @insultes = ("Ahahahah ! 23 à 0 !", "C'est la piquette, Jack !", "Tu sais pas jouer, Jack !", "T'es mauvais, Jack !"); # store a chan last command my %command; sub setConf { my ($ircNew, $dbhNew, $log, $lastIDnew) = @_; $PlayBot::commands::fav::dbh = $dbhNew; $PlayBot::commands::tag::dbh = $dbhNew; $PlayBot::commands::later::dbh = $dbhNew; $PlayBot::utils::id::dbh = $dbhNew; $PlayBot::commands::fav::log = $log; $PlayBot::commands::tag::log = $log; $PlayBot::commands::get::log = $log; $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; $lastID = $lastIDnew; $irc = $ircNew; $dbh = $dbhNew; } sub exec { my ($kernel, $user, $chan, $msg) = @_; my ($nick, $mask) = split(/!/,$user); if ($msg eq '!' and $command{$chan}) { $msg = $command{$chan}; } if ($msg =~ /^ *!fav(?: (\S+))? *$/) { my $index = $1; try { my $id = PlayBot::utils::id::get($chan, $index); PlayBot::commands::fav::exec($nick, $id) } catch { $irc->yield(privmsg => $chan => $insultes[rand @insultes]); } } elsif ($msg =~ /^ *!later(?: (\S+))?(?: in (\d+)(h|m|s)?)? *$/) { my $index = $1; my ($time, $unit) = ($2, $3); try { my $id = PlayBot::utils::id::get($chan, $index); PlayBot::commands::later::exec($kernel, $nick, $id, $chan, $time, $unit); } catch { $irc->yield(privmsg => $chan => $insultes[rand @insultes]); }; } elsif ($msg =~ /^( *!tag)(?:( +)(-?\d+))?/) { my $index = $3; my $id; my $tags; if ($3) { $tags = substr $msg, (length $1) + (length $2) + (length $3); } else { $tags = substr $msg, (length $1) + (length $2); } try { $id = PlayBot::utils::id::get($chan, $index); PlayBot::commands::tag::exec($id, $tags); } catch { $irc->yield(privmsg => $chan => $insultes[rand @insultes]); }; } elsif ($msg =~ /^ *!(get|fts)( +.*)?$/) { my $query = $2; my $fts = $1 eq 'fts'; my @args = ($fts, $chan, $query); my $id = PlayBot::commands::get::exec(@args); if ($id) { $lastID->{$chan} = $id; } } elsif ($msg =~ /^ *!broken(?: (\S+))? *$/) { my $index = $1; try { my $id = PlayBot::utils::id::get($chan, $index); my @msg; try { @msg = PlayBot::commands::broken::exec($chan, $id); } catch { @msg = split(/\n/, $_); }; $irc->yield(privmsg => $chan => $_) foreach (@msg); } catch { $irc->yield(privmsg => $chan => $insultes[rand @insultes]); }; } elsif ($msg =~ /^ *!stats(?: (\S+))? *$/) { my $index = $1; try { my $id = PlayBot::utils::id::get($chan, $index); PlayBot::commands::stats::exec($chan, $id) } catch { $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'); $irc->yield(privmsg => $nick => '!later [<id>] [in <x>[s|m|h]] : vidéo rappelée par query (par défaut temps de 6h)'); $irc->yield(privmsg => $nick => " Sans id précisée, la dernière vidéo postée sur le chan est utilisée."); $irc->yield(privmsg => $nick => ' L\'id peut être négatif, auquel cas -1 correspond à l\'avant dernière vidéo.'); $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 => "!broken [<id>] : marque un contenu comme indisponible"); $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 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]); } else { return 0; } $command{$chan} = $msg; return 1; } sub tag { my ($msg, $chan, $id) = @_; my @tags = ($msg =~ /(?:^| )(#[a-zA-Z0-9_]+)/g); $id = PlayBot::utils::id::get($chan) if (not defined($id)); PlayBot::commands::tag::exec($id, "@tags"); return @tags; } 1;