From 9d7bb31f4e5e64b5a49b5ab7866a0a24e82adb9b Mon Sep 17 00:00:00 2001 From: Alexandre Morignot <erdnaxeli@gmail.com> Date: Thu, 8 Aug 2013 23:49:34 +0200 Subject: [PATCH] New module commands::parser. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All the commands' code (!later, !tag, …) is now deported in this module. It will be separated in different modules in the futur. --- PlayBot.pl | 45 ++++++-------------------------- lib/commands/parser.pm | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 lib/commands/parser.pm diff --git a/PlayBot.pl b/PlayBot.pl index e09baad..d2c6129 100755 --- a/PlayBot.pl +++ b/PlayBot.pl @@ -14,6 +14,7 @@ use FindBin; use lib "$FindBin::Bin/lib/"; use Logging; use sites::parser; +use commands::parser; # nom du fichier my $bot = $0; @@ -58,6 +59,8 @@ my $dbh = DBI->connect('DBI:mysql:'.$conf->{'bdd'}.';host='.$conf->{'host'}, $co }) or die("Couldn't connect to database: ".DBI->errstr); +$commands::parser::irc = $irc; +$commands::parser::dbh = $dbh; # Evenements que le bot va gérer POE::Session->create( @@ -77,7 +80,6 @@ irc_notice => \&on_notice, my %commandes_admin = ("cycle" => \&cycle); - ### FONCTIONS sub flux { @@ -259,6 +261,8 @@ sub on_invite sub on_speak { my ($kernel, $user, $chan, $msg) = @_[KERNEL, ARG0, ARG1, ARG2]; + my @args = ($kernel, $user, $chan, $msg); + my ($nick,$mask) = split(/!/,$user); my %content; @@ -315,42 +319,9 @@ sub on_speak $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}) ; } } - elsif ($msg =~ /^!fav( ([0-9]+))?/) { - my $id = ($2) ? $2 : $lastID; - - my $sth = $dbh->prepare_cached('SELECT user FROM playbot_codes WHERE nick = ?'); - $sth->execute($nick) - or $log->error("Couldn't finish transaction: " . $dbh->errstr); - - if (!$sth->rows) { - $irc->yield(privmsg => $nick => "Ce nick n'est associé à aucun login arise. Va sur http://nightiies.iiens.net/links/fav pour obtenir ton code personel."); - } - else { - my $sth2 = $dbh->prepare_cached('INSERT INTO playbot_fav (id, user) VALUES (?, ?)'); - $sth2->execute($id, $sth->fetch->[0]) - or $log->error("Couldn't finish transaction: " . $dbh->errstr); - } - } - elsif ($msg =~ /^!later(?: ([0-9]+))?(?: in ([0-9]*)?(h|m|s)?)?/) { - my ($id, $time, $unit) = ($1, $2, $3); - - $id = $lastID if (!$id); - $time = 6 if (!$time); - $time *= ($unit eq 's') ? 1 : ($unit eq 'm') ? 60 : 3600; - $kernel->delay_set('_later', $time, $nick, $id); - } - elsif ($msg =~ /^!tag( +([0-9]+))?/) { - my $id = ($2) ? $2 : $lastID; - while ($msg =~ /#([a-zA-Z0-9_-]+)/g) { - addTag($id, $1); - } - } - elsif ($msg =~ /^!help/) { - $irc->yield(privmsg => $chan => '!fav [<id>] : enregistre la vidéo dans les favoris'); - $irc->yield(privmsg => $chan => '!tag [<id>] <tag1> <tag2> ... : tag la vidéo'); - $irc->yield(privmsg => $chan => '!later [<id>] [in <x>[s|m|h]] : vidéo rappelée par query (par défaut temps de 6h)'); - $irc->yield(privmsg => $chan => 'Sans id précisée, la dernière vidéo postée est utilisée.'); - } + else { + commands::parser::exec(@args); + } } diff --git a/lib/commands/parser.pm b/lib/commands/parser.pm new file mode 100644 index 0000000..d18fa55 --- /dev/null +++ b/lib/commands/parser.pm @@ -0,0 +1,58 @@ +package commands::parser; + +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT_OK = qw(exec irc); + +use lib "$FindBin::Bin/lib/commands/"; +#use fav; +#use later; +#use tag; +#use help; + +our $irc; +our $dbh; + +sub exec { + my ($kernel, $user, $chan, $msg) = @_; + my ($nick,$mask) = split(/!/,$user); + + if ($msg =~ /^!fav( ([0-9]+))?/) { + my $id = ($2) ? $2 : $lastID; + + my $sth = $dbh->prepare_cached('SELECT user FROM playbot_codes WHERE nick = ?'); + $sth->execute($nick) + or $log->error("Couldn't finish transaction: " . $dbh->errstr); + + if (!$sth->rows) { + $irc->yield(privmsg => $nick => "Ce nick n'est associé à aucun login arise. Va sur http://nightiies.iiens.net/links/fav pour obtenir ton code personel."); + } + else { + my $sth2 = $dbh->prepare_cached('INSERT INTO playbot_fav (id, user) VALUES (?, ?)'); + $sth2->execute($id, $sth->fetch->[0]) + or $log->error("Couldn't finish transaction: " . $dbh->errstr); + } + } + elsif ($msg =~ /^!later(?: ([0-9]+))?(?: in ([0-9]*)?(h|m|s)?)?/) { + my ($id, $time, $unit) = ($1, $2, $3); + + $id = $lastID if (!$id); + $time = 6 if (!$time); + $time *= ($unit eq 's') ? 1 : ($unit eq 'm') ? 60 : 3600; + $kernel->delay_set('_later', $time, $nick, $id); + } + elsif ($msg =~ /^!tag( +([0-9]+))?/) { + my $id = ($2) ? $2 : $lastID; + while ($msg =~ /#([a-zA-Z0-9_-]+)/g) { + addTag($id, $1); + } + } + elsif ($msg =~ /^!help/) { + $irc->yield(privmsg => $chan => '!fav [<id>] : enregistre la vidéo dans les favoris'); + $irc->yield(privmsg => $chan => '!tag [<id>] <tag1> <tag2> ... : tag la vidéo'); + $irc->yield(privmsg => $chan => '!later [<id>] [in <x>[s|m|h]] : vidéo rappelée par query (par défaut temps de 6h)'); + $irc->yield(privmsg => $chan => 'Sans id précisée, la dernière vidéo postée est utilisée.'); + } +} + +1; -- GitLab