2019-08-25 04:03:08 +02:00
|
|
|
|
|
|
|
# File: FuncCommand.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Special `func` command that executes built-in functions with
|
|
|
|
# optional arguments. Usage: func <identifier> [arguments].
|
|
|
|
#
|
2019-08-25 08:58:07 +02:00
|
|
|
# Intended usage is with command-substitution (&{}) or pipes (|{}).
|
2019-08-25 04:03:08 +02:00
|
|
|
#
|
2019-08-25 08:58:07 +02:00
|
|
|
# For example:
|
|
|
|
#
|
2019-08-25 09:05:51 +02:00
|
|
|
# factadd img /call echo https://google.com/search?q=&{func uri_escape $args}&tbm=isch
|
2019-08-25 04:03:08 +02:00
|
|
|
#
|
2019-08-25 09:05:51 +02:00
|
|
|
# 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
|
2019-08-25 08:58:07 +02:00
|
|
|
# used in the URL of this simple Google Image Search factoid command.
|
2019-08-25 04:03:08 +02:00
|
|
|
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package PBot::FuncCommand;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
if (ref($_[1]) eq 'HASH') {
|
|
|
|
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to FactoidCommands");
|
|
|
|
$self->{pbot}->{commands}->register(sub { return $self->do_func(@_) }, 'func', 0);
|
|
|
|
$self->init_funcs;
|
|
|
|
}
|
|
|
|
|
2019-08-25 09:10:45 +02:00
|
|
|
# this is a subroutine so PBot::BotAdminCommands::reload() can reload
|
|
|
|
# the funcs without requiring a bot restart.
|
2019-08-25 04:03:08 +02:00
|
|
|
sub init_funcs {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
$self->{funcs} = {
|
|
|
|
help => {
|
|
|
|
desc => 'provides help about a func',
|
|
|
|
usage => 'help [func]',
|
|
|
|
subref => sub { $self->func_help(@_) }
|
|
|
|
},
|
|
|
|
list => {
|
|
|
|
desc => 'lists available funcs',
|
|
|
|
usage => 'list [regex]',
|
|
|
|
subref => sub { $self->func_list(@_) }
|
|
|
|
},
|
|
|
|
uri_escape => {
|
|
|
|
desc => 'percent-encode unsafe URI characters',
|
|
|
|
usage => 'uri_escape <text>',
|
|
|
|
subref => sub { $self->func_uri_escape(@_) }
|
|
|
|
},
|
2019-08-25 04:25:21 +02:00
|
|
|
sed => {
|
|
|
|
desc => 'a sed-like stream editor',
|
|
|
|
usage => 'sed s/<regex>/<replacement>/[Pig]; P preserve case; i ignore case; g replace all',
|
|
|
|
subref => sub { $self->func_sed(@_) }
|
|
|
|
},
|
2019-08-25 05:35:24 +02:00
|
|
|
unquote => {
|
|
|
|
desc => 'removes unescaped surrounding quotes and strips escapes from escaped quotes',
|
|
|
|
usage => 'unquote <text>',
|
|
|
|
subref => sub { $self->func_unquote(@_) }
|
|
|
|
},
|
2019-08-25 04:03:08 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-25 09:10:45 +02:00
|
|
|
sub do_func {
|
|
|
|
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
|
|
|
|
|
|
|
|
my $func = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist});
|
|
|
|
|
|
|
|
if (not defined $func) {
|
2019-09-02 19:36:34 +02:00
|
|
|
return "Usage: func <keyword> [arguments]; see also: func help";
|
2019-08-25 09:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (not exists $self->{funcs}->{$func}) {
|
|
|
|
return "[No such func '$func']";
|
|
|
|
}
|
|
|
|
|
|
|
|
my @params;
|
|
|
|
while (my $param = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist})) {
|
|
|
|
push @params, $param;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $result = $self->{funcs}->{$func}->{subref}->(@params);
|
|
|
|
$result =~ s/\x1/1/g;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-08-25 04:03:08 +02:00
|
|
|
sub func_help {
|
|
|
|
my ($self, $func) = @_;
|
|
|
|
|
2019-09-02 19:36:34 +02:00
|
|
|
if (not length $func) {
|
|
|
|
return "func: invoke built-in functions; usage: func <keyword> [arguments]; to list available functions: func list";
|
|
|
|
}
|
|
|
|
|
2019-08-25 04:03:08 +02:00
|
|
|
if (not exists $self->{funcs}->{$func}) {
|
|
|
|
return "No such func '$func'.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "$func: $self->{funcs}->{$func}->{desc}; usage: $self->{funcs}->{$func}->{usage}";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func_list {
|
|
|
|
my ($self, $regex) = @_;
|
|
|
|
|
|
|
|
$regex = '.*' if not defined $regex;
|
|
|
|
|
|
|
|
my $result = eval {
|
|
|
|
my $text = '';
|
|
|
|
|
|
|
|
foreach my $func (sort keys %{$self->{funcs}}) {
|
2019-08-25 04:35:56 +02:00
|
|
|
if ($func =~ m/$regex/i or $self->{funcs}->{$func}->{desc} =~ m/$regex/i) {
|
2019-08-25 04:03:08 +02:00
|
|
|
$text .= "$func: $self->{funcs}->{$func}->{desc}.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not length $text) {
|
|
|
|
if ($regex eq '.*') {
|
|
|
|
$text = "No funcs yet.";
|
|
|
|
} else {
|
|
|
|
$text = "No matching func.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
my $error = $@;
|
|
|
|
$error =~ s/at PBot.FuncCommand.*$//;
|
|
|
|
return "Error: $error\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-08-25 05:35:24 +02:00
|
|
|
sub func_unquote {
|
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
$text =~ s/^"(.*?)(?<!\\)"$/$1/ || $text =~ s/^'(.*?)(?<!\\)'$/$1/;
|
|
|
|
$text =~ s/(?<!\\)\\'/'/g;
|
|
|
|
$text =~ s/(?<!\\)\\"/"/g;
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2019-08-25 23:45:21 +02:00
|
|
|
use URI::Escape qw/uri_escape_utf8/;
|
2019-08-25 05:35:24 +02:00
|
|
|
|
|
|
|
sub func_uri_escape {
|
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
2019-08-25 23:45:21 +02:00
|
|
|
return uri_escape_utf8($text);
|
2019-08-25 05:35:24 +02:00
|
|
|
}
|
|
|
|
|
2019-08-25 04:25:21 +02:00
|
|
|
# near-verbatim insertion of krok's `sed` factoid
|
|
|
|
no warnings;
|
|
|
|
sub func_sed {
|
|
|
|
my $self = shift;
|
|
|
|
my $text = "@_";
|
|
|
|
|
2019-08-25 04:29:14 +02:00
|
|
|
if ($text =~ /^s(.)(.*?)(?<!\\)\1(.*?)(?<!\\)\1(\S*)\s+(.*)/p) {
|
|
|
|
my ($a, $r, $g, $m, $t) = ($5,"'\"$3\"'", index($4,"g") != -1, $4, $2);
|
|
|
|
|
|
|
|
if ($m=~/P/) {
|
|
|
|
$r =~ s/^'"(.*)"'$/$1/;
|
|
|
|
$m=~s/P//g;
|
|
|
|
|
|
|
|
if($g) {
|
|
|
|
$a =~ s|(?$m)($t)|$1=~/^[A-Z][^A-Z]/?ucfirst$r:($1=~/^[A-Z]+$/?uc$r:$r)|gie;
|
|
|
|
} else {
|
|
|
|
$a =~ s|(?$m)($t)|$1=~/^[A-Z][^A-Z]/?ucfirst$r:($1=~/^[A-Z]+$/?uc$r:$r)|ie;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($g) {
|
|
|
|
$a =~ s/(?$m)$t/$r/geee;
|
|
|
|
} else {
|
|
|
|
$a=~s/(?$m)$t/$r/eee;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $a;
|
|
|
|
} else {
|
|
|
|
return "sed: syntax error";
|
|
|
|
}
|
2019-08-25 04:25:21 +02:00
|
|
|
}
|
|
|
|
use warnings;
|
|
|
|
|
2019-08-25 04:03:08 +02:00
|
|
|
1;
|