App-RoboBot

 view release on metacpan or  search on metacpan

lib/App/RoboBot/Plugin/Core/Variables.pm  view on Meta::CPAN


has '+commands' => (
    default => sub {{
        'defined' => { method          => 'is_defined',
                       preprocess_args => 0,
                       description     => 'Returns true if all of the named variables are defined, otherwise false. Must pass variable names as a list.',
                       usage           => '(<varname1> [... <varnameN>])', },

        'setvar' => { method          => 'set_var',
                      preprocess_args => 0,
                      description     => 'Sets the value of a variable.',
                      usage           => '<variable name> <value or expression>',
                      example         => 'foo 10',
                      result          => '10' },

        'unsetvar' => { method          => 'unset_var',
                        preprocess_args => 0,
                        description     => 'Unsets a variable and removes it from the symbol table.',
                        usage           => '<variable name>',
                        example         => 'foo',
                        result          => '' },

        'incr' => { method          => 'increment_var',
                    preprocess_args => 0,
                    description     => 'Increments a numeric variable by the given amount. If no increment amount is provided, 1 is assumed. Negative amounts are permissible.',
                    usage           => '<variable name> [<amount>]' },

        'set-global' => { method      => 'set_global',
                          description => 'Sets a global variable (accessible from any channel on the current network).',
                          usage       => '<name> <value>' },

        'unset-global' => { method      => 'unset_global',
                            description => 'Unsets a global variable on the current network.',
                            usage       => '<name>' },

        'var' => { method      => 'get_global',
                   description => 'Retrieves the value(s) of a global variable, if it exists on the current network. If the variable does not exist, an empty list is returned, unless <default> is specified in which case that is used instead.',
                   usage       => '<name> [<default>]' },
    }},
);

sub set_global {
    my ($self, $message, $command, $rpl, $var_name, @values) = @_;

    unless (defined $var_name && $var_name =~ m{\w+}) {
        $message->response->raise('Must provide a variable name and at least one value.');
        return;
    }

    unless (@values) {
        return $self->unset_global($message, $command, $var_name);
    }

    my $res = $self->bot->config->db->do(q{
        update global_vars
        set ???
        where network_id = ?
            and lower(var_name) = lower(?)
    }, {
        var_values => \@values,
        updated_at => 'now',
    }, $message->network->id, $var_name);

    if ($res && $res->count > 0) {
        $message->response->push(sprintf('Global variable %s has been updated.', $var_name));
        return;
    }

    $res = $self->bot->config->db->do(q{
        insert into global_vars ??? returning *
    }, {
        network_id => $message->network->id,
        var_name   => $var_name,
        var_values => \@values,
        created_by => $message->sender->id,
    });

    if ($res && $res->next && $res->{'id'} =~ m{\d+}) {
        $message->response->push(sprintf('Global variable %s has been set.', $var_name));
        return;
    }

    $message->response->raise('Could not set global variable %s. Please check your input and try again.', $var_name);
    return;
}

sub unset_global {
    my ($self, $message, $command, $rpl, $var_name) = @_;

    unless (defined $var_name && $var_name =~ m{\w+}) {
        $message->response->raise('Must provide a variable name to unset it.');
        return;
    }

    my $res = $self->bot->config->db->do(q{
        delete from global_vars where network_id = ? and lower(var_name) = lower(?)
    }, $message->network->id, $var_name);

    $message->response->push(sprintf('Global variable %s has been unset.', $var_name))
        if $res && $res->count > 0;
    return;
}

sub get_global {
    my ($self, $message, $command, $rpl, $var_name, $default) = @_;

    unless (defined $var_name && $var_name =~ m{\w+}) {
        $message->response->raise('Must provide a variable name to retrieve a value.');
        return;
    }

    my $res = $self->bot->config->db->do(q{
        select var_values
        from global_vars
        where network_id = ? and lower(var_name) = lower(?)
    }, $message->network->id, $var_name);

    if ($res && $res->next && defined $res->{'var_values'}) {
        return @{$res->{'var_values'}};
    } elsif (defined $default) {
        return $default;
    } else {
        return;
    }
}



( run in 0.968 second using v1.01-cache-2.11-cpan-39bf76dae61 )