diff --git a/lib/commands/get.pm b/lib/commands/get.pm index 6b26a0e06206d8971ae5214980e43492dc7cd029..eaccba7506f9d41a65a71ffc6972dd18bb159937 100644 --- a/lib/commands/get.pm +++ b/lib/commands/get.pm @@ -6,11 +6,15 @@ use Scalar::Util qw(looks_like_number); use lib "$FindBin::Bin/lib/"; use utils::print; +use utils::db; our $dbh; our $irc; our $log; +my $last_req; +my $sth; + sub exec { my ($kernel, $nick, $chan, $msg) = @_; @@ -21,106 +25,116 @@ sub exec { my @tags = ($msg =~ /#([a-zA-Z0-9_-]+)/g); my $content; my $req; - my $sth; my $rows; my @words = ($msg =~ /(?:^| )([a-zA-Z0-9_-]+)/g); - my @words_param; - while ($msg =~ /(?:^| )([a-zA-Z0-9_-]+)/g) { - unshift @words_param, '%'.$1.'%'; - } - my $words_sql; - foreach (@words) { - $words_sql .= ' and ' if ($words_sql); - $words_sql .= "concat(sender, ' ', title) like ?"; - } + if (not defined $last_req or $msg ne $last_req) { + my $dbh = utils::db::get_session; - if (@words && looks_like_number($words[0])) { - $sth = $dbh->prepare('select id, sender, title, url, duration - from playbot - where id = ?'); - $sth->execute($words[0]); - } - elsif (@tags) { - my $params = join ', ' => ('?') x @tags; + my @words_param; + while ($msg =~ /(?:^| )([a-zA-Z0-9_-]+)/g) { + unshift @words_param, '%'.$1.'%'; + } - if ($all) { - $req = 'select id, sender, title, url, duration - from playbot - natural join playbot_tags - where tag in ('.$params.')'; - $req .= ' and '.$words_sql if ($words_sql); - $req .= ' group by id - having count(*) >= ? - order by rand()'; - - $sth = $dbh->prepare($req); - $sth->execute(@tags, @words_param, scalar @tags); + my $words_sql; + foreach (@words) { + $words_sql .= ' and ' if ($words_sql); + $words_sql .= "concat(sender, ' ', title) like ?"; } - else { - $req = 'select p.id, p.sender, p.title, p.url, duration - from playbot p - natural join playbot_tags pt - join playbot_chan pc on p.id = pc.content - where pt.tag in ('.$params.')'; - $req .= ' and '.$words_sql if ($words_sql); - $req .= ' and pc.chan = ? - group by p.id - having count(*) >= ? - order by rand()'; - - $sth = $dbh->prepare($req); - $sth->execute(@tags, @words_param, $chan->[0], scalar @tags); + + if (@words && looks_like_number($words[0])) { + $sth = $dbh->prepare('select id, sender, title, url, duration + from playbot + where id = ?'); + $sth->execute($words[0]); } - } - else { - if ($all) { - $req = 'select id, sender, title, url, duration from playbot'; - $req .= ' where '.$words_sql if ($words_sql); - $req .= ' group by id'; - $req .= ' order by rand()'; - - $sth = $dbh->prepare($req); - $sth->execute (@words_param); + elsif (@tags) { + my $params = join ', ' => ('?') x @tags; + + if ($all) { + $req = 'select id, sender, title, url, duration + from playbot + natural join playbot_tags + where tag in ('.$params.')'; + $req .= ' and '.$words_sql if ($words_sql); + $req .= ' group by id + having count(*) >= ? + order by rand()'; + + $sth = $dbh->prepare($req); + $sth->execute(@tags, @words_param, scalar @tags); + } + else { + $req = 'select p.id, p.sender, p.title, p.url, duration + from playbot p + natural join playbot_tags pt + join playbot_chan pc on p.id = pc.content + where pt.tag in ('.$params.')'; + $req .= ' and '.$words_sql if ($words_sql); + $req .= ' and pc.chan = ? + group by p.id + having count(*) >= ? + order by rand()'; + + $sth = $dbh->prepare($req); + $sth->execute(@tags, @words_param, $chan->[0], scalar @tags); + } } else { - $req = 'select p.id, p.sender, p.title, p.url, duration - from playbot p - join playbot_chan pc on p.id = pc.content - where pc.chan = ?'; - $req .= ' and '.$words_sql if ($words_sql); - $req .= ' group by p.id'; - $req .= ' order by rand()'; - - $sth = $dbh->prepare($req); - $sth->execute($chan->[0], @words_param); + if ($all) { + $req = 'select id, sender, title, url, duration from playbot'; + $req .= ' where '.$words_sql if ($words_sql); + $req .= ' group by id'; + $req .= ' order by rand()'; + + $sth = $dbh->prepare($req); + $sth->execute (@words_param); + } + else { + $req = 'select p.id, p.sender, p.title, p.url, duration + from playbot p + join playbot_chan pc on p.id = pc.content + where pc.chan = ?'; + $req .= ' and '.$words_sql if ($words_sql); + $req .= ' group by p.id'; + $req .= ' order by rand()'; + + $sth = $dbh->prepare($req); + $sth->execute($chan->[0], @words_param); + } } } $content = $sth->fetch; if (!$content) { - if (@words or @tags) { + if ($last_req eq $msg) { + # the request was already executed, there is nothing more + $irc->yield(privmsg => $chan => "Tu tournes en rond, Jack !"); + } + elsif (@words or @tags) { $irc->yield(privmsg => $chan => "Je n'ai rien dans ce registre."); } else { $irc->yield(privmsg => $chan => "Poste d'abord du contenu, n00b."); } + + $last_req = undef; return } # this is specific to the mysql driver $rows = $sth->rows; - $sth = $dbh->prepare("select tag + my $sth2 = utils::db::get_session()->prepare("select tag from playbot_tags where id = ? "); - $sth->execute($content->[0]); + $sth2->execute($content->[0]); my @tags; - while (my $data = $sth->fetch) { + while (my $data = $sth2->fetch) { my $tag = $data->[0]; $tag =~ s/([a-zA-Z0-9_-]+)/#$1/; push @tags, $tag; @@ -134,14 +148,14 @@ sub exec { $content_h{'duration'} = $content->[4]; $content_h{'tags'} = \@tags; - my $msg = utils::print::print(\%content_h); - $msg .= ' [' . $rows . ' résultat'; - $msg .= 's' if ($rows > 1); - $msg .= ']'; - $irc->yield(privmsg => $chan => $msg); + my $irc_msg = utils::print::print(\%content_h); + $irc_msg .= ' [' . $rows . ' résultat'; + $irc_msg .= 's' if ($rows > 1); + $irc_msg .= ']'; + $irc->yield(privmsg => $chan => $irc_msg); # we save the get like a post - my $sth2 = $dbh->prepare_cached(' + $sth2 = utils::db::get_session()->prepare_cached(' INSERT INTO playbot_chan (content, chan, sender_irc) VALUES (?,?,?)'); $log->error("Couldn't prepare querie; aborting") unless (defined $sth2); @@ -149,6 +163,9 @@ sub exec { $sth2->execute($content->[0], $chan->[0], "PlayBot") or $log->error("Couldn't finish transaction: " . $dbh->errstr); + # we save the request + $last_req = $msg; + return $content->[0]; }