Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • 6ae5674b9dff4090a350bbe25e3f4d4f1a23ec6b
  • master par défaut
  • cinch
  • ruby
  • gh-pages
  • v1.0.0
6 résultats

get.pm

Blame
  • get.pm 4,99 Kio
    package commands::get;
    
    use strict;
    use warnings;
    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) = @_;
    
        # if we are in a query or arg -all, we search in all the channels
        my $all = 0;
        $all = 1 if ($chan->[0] !~ /^#/ || $msg =~ s/-a(ll)?//);
    
        my @tags = ($msg =~ /#([a-zA-Z0-9_-]+)/g);
        my $content;
        my $req;
        my $rows;
    
        my @words = ($msg =~ /(?:^| )([^#\s]+)/g);
    
        if (not defined $last_req or $msg ne $last_req) {
            my $dbh = utils::db::get_session;
    
            my @words_param;
            foreach (@words) {
                unshift @words_param, '%'.$_.'%';
            }
    
            my $words_sql;
            foreach (@words) {
                $words_sql .= ' and ' if ($words_sql);
                $words_sql .= "concat(sender, ' ', title) like ?";
            }
    
            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;
    
                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