diff --git a/PlayBot.pl b/PlayBot.pl index 80b509ad6c1bfba04740a41ac5a564cbae2b1825..9f6222b5819032e860c32efac9a3b29b0a4210c6 100755 --- a/PlayBot.pl +++ b/PlayBot.pl @@ -174,9 +174,14 @@ sub on_connect # Discussion privée sub on_query { - my ($user,$msg) = @_[ARG0, ARG2]; + my ($kernel, $user, $msg) = @_[KERNEL, ARG0, ARG2]; my ($nick) = split (/!/,$user); - print $msg."\n"; + + my @fake_chan = ($nick); + my @args = ($kernel, $user, \@fake_chan, $msg); + + my $fake_chan = \@fake_chan; + return if (commands::parser::exec(@args)); if ($msg =~ m/^!/ && $nick eq $admin) { my $commande = ( $msg =~ m/^!([^ ]*)/ )[0]; diff --git a/lib/commands/get.pm b/lib/commands/get.pm index 76fb132eec1e6fa71b30ad04c0801b4093078c63..2bc4e54d0c68916f7a4d43cd3d8c9ccc9d635c61 100644 --- a/lib/commands/get.pm +++ b/lib/commands/get.pm @@ -17,15 +17,30 @@ sub exec { my @tags = ($msg =~ /#?([a-zA-Z0-9_-]+)/g); my $content; + # if we are in a query, we search in all the channels + my $all = ($chan->[0] !~ /^#/) ? 1 : 0; + if (@tags) { my $params = join ', ' => ('?') x @tags; - my $sth = $dbh->prepare('select id, sender, title, url from playbot - natural join playbot_tags - where tag in ('.$params.') - and chan = ? - group by id - having count(*) >= ?'); - $sth->execute(@tags, $chan->[0], scalar @tags); + my $sth; + + if ($all) { + $sth = $dbh->prepare('select id, sender, title, url from playbot + natural join playbot_tags + where tag in ('.$params.') + group by id + having count(*) >= ?'); + $sth->execute(@tags, scalar @tags); + } + else { + $sth = $dbh->prepare('select id, sender, title, url from playbot + natural join playbot_tags + where tag in ('.$params.') + and chan = ? + group by id + having count(*) >= ?'); + $sth->execute(@tags, $chan->[0], scalar @tags); + } $content = $sth->fetchall_arrayref; @@ -38,11 +53,22 @@ sub exec { } } else { - my $sth = $dbh->prepare('select id, sender, title, url from playbot - where chan = ? - order by rand() - limit 1'); - $sth->execute($chan->[0]); + my $sth; + + if ($all) { + $sth = $dbh->prepare('select id, sender, title, url from playbot + order by rand() + limit 1'); + $sth->execute; + } + else { + $sth = $dbh->prepare('select id, sender, title, url from playbot + where chan = ? + order by rand() + limit 1'); + $sth->execute($chan->[0]); + } + $content = $sth->fetch; if (!$content) { diff --git a/lib/commands/parser.pm b/lib/commands/parser.pm index 4c599a891f9034304c7ebf52b1fd70d07a002546..2051e9284cb251dfeddfcea71aa7d9f150d8ca71 100644 --- a/lib/commands/parser.pm +++ b/lib/commands/parser.pm @@ -35,7 +35,7 @@ sub setConf { sub exec { my @args = @_; my ($kernel, $user, $chan, $msg) = @args; - my ($nick,$mask) = split(/!/,$user); + my ($nick, $mask) = split(/!/,$user); if ($msg =~ /^!fav(?: ([0-9]+))?/) { my $id = ($1) ? $1 : $lastID{$chan->[0]};