2020-02-14 07:36:05 +01:00
|
|
|
# File: Functions.pm
|
|
|
|
#
|
|
|
|
# Purpose: Special `func` command that executes built-in functions with
|
|
|
|
# optional arguments. Usage: func <identifier> [arguments].
|
|
|
|
#
|
|
|
|
# Intended usage is with command-substitution (&{}) or pipes (|{}).
|
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# factadd img /call echo https://google.com/search?q=&{func uri_escape $args}&tbm=isch
|
|
|
|
#
|
|
|
|
# The above would invoke the function 'uri_escape' on $args and then replace
|
|
|
|
# the command-substitution with the result, thus escaping $args to be safely
|
|
|
|
# used in the URL of this simple Google Image Search factoid command.
|
2021-07-24 01:41:32 +02:00
|
|
|
#
|
|
|
|
# See also: Plugin/FuncBuiltins.pm, Plugin/FuncGrep.pm and Plugin/FuncSed.pm
|
2020-02-14 07:36:05 +01:00
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2020-02-14 07:36:05 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::Functions;
|
|
|
|
use parent 'PBot::Core::Class';
|
2020-02-14 07:36:05 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2020-02-14 07:36:05 +01:00
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2021-07-24 01:41:32 +02:00
|
|
|
# register `list` and `help` functions used to list
|
|
|
|
# functions and obtain help about them
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
$self->register(
|
|
|
|
'list',
|
|
|
|
{
|
|
|
|
desc => 'lists available funcs',
|
|
|
|
usage => 'list [regex]',
|
|
|
|
subref => sub { $self->func_list(@_) }
|
|
|
|
}
|
|
|
|
);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2021-07-24 01:41:32 +02:00
|
|
|
$self->register(
|
|
|
|
'help',
|
|
|
|
{
|
|
|
|
desc => 'provides help about a func',
|
|
|
|
usage => 'help [func]',
|
|
|
|
subref => sub { $self->func_help(@_) }
|
|
|
|
}
|
|
|
|
);
|
2020-02-14 07:36:05 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub register {
|
|
|
|
my ($self, $func, $data) = @_;
|
|
|
|
$self->{funcs}->{$func} = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub unregister {
|
|
|
|
my ($self, $func) = @_;
|
|
|
|
delete $self->{funcs}->{$func};
|
|
|
|
}
|
|
|
|
|
2020-02-14 07:36:05 +01:00
|
|
|
sub func_list {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $regex) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
$regex //= '.*';
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $result = eval {
|
2021-06-05 22:20:03 +02:00
|
|
|
my @funcs;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $func (sort keys %{$self->{funcs}}) {
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($func =~ m/$regex/i or $self->{funcs}->{$func}->{desc} =~ m/$regex/i) {
|
|
|
|
push @funcs, $func;
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
my $result = join ', ', @funcs;
|
|
|
|
|
|
|
|
if (not length $result) {
|
|
|
|
if ($regex eq '.*') {
|
|
|
|
$result = "No funcs yet.";
|
|
|
|
} else {
|
|
|
|
$result = "No matching func.";
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
return "Available funcs: $result; see also: func help <keyword>";
|
2020-02-15 23:38:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
my $error = $@;
|
|
|
|
$error =~ s/at PBot.Functions.*$//;
|
|
|
|
return "Error: $error\n";
|
2020-02-14 07:36:05 +01:00
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $result;
|
2020-02-14 07:36:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-24 01:41:32 +02:00
|
|
|
sub func_help {
|
|
|
|
my ($self, $func) = @_;
|
|
|
|
|
|
|
|
if (not length $func) {
|
|
|
|
return "func: invoke built-in functions; usage: func <keyword> [arguments]; to list available functions: func list [regex]";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not exists $self->{funcs}->{$func}) {
|
|
|
|
return "No such func '$func'.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "$func: $self->{funcs}->{$func}->{desc}; usage: $self->{funcs}->{$func}->{usage}";
|
|
|
|
}
|
|
|
|
|
2020-02-14 07:36:05 +01:00
|
|
|
1;
|